]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_mount.c
devd.conf(5): Fix a mandoc related issue
[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/smp.h>
45 #include <sys/devctl.h>
46 #include <sys/eventhandler.h>
47 #include <sys/fcntl.h>
48 #include <sys/jail.h>
49 #include <sys/kernel.h>
50 #include <sys/ktr.h>
51 #include <sys/libkern.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/namei.h>
56 #include <sys/priv.h>
57 #include <sys/proc.h>
58 #include <sys/filedesc.h>
59 #include <sys/reboot.h>
60 #include <sys/sbuf.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/sysproto.h>
63 #include <sys/sx.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysent.h>
66 #include <sys/systm.h>
67 #include <sys/vnode.h>
68 #include <vm/uma.h>
69
70 #include <geom/geom.h>
71
72 #include <machine/stdarg.h>
73
74 #include <security/audit/audit.h>
75 #include <security/mac/mac_framework.h>
76
77 #define VFS_MOUNTARG_SIZE_MAX   (1024 * 64)
78
79 static int      vfs_domount(struct thread *td, const char *fstype, char *fspath,
80                     uint64_t fsflags, struct vfsoptlist **optlist);
81 static void     free_mntarg(struct mntarg *ma);
82
83 static int      usermount = 0;
84 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
85     "Unprivileged users may mount and unmount file systems");
86
87 static bool     default_autoro = false;
88 SYSCTL_BOOL(_vfs, OID_AUTO, default_autoro, CTLFLAG_RW, &default_autoro, 0,
89     "Retry failed r/w mount as r/o if no explicit ro/rw option is specified");
90
91 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
92 MALLOC_DEFINE(M_STATFS, "statfs", "statfs structure");
93 static uma_zone_t mount_zone;
94
95 /* List of mounted filesystems. */
96 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist);
97
98 /* For any iteration/modification of mountlist */
99 struct mtx_padalign __exclusive_cache_line mountlist_mtx;
100 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF);
101
102 EVENTHANDLER_LIST_DEFINE(vfs_mounted);
103 EVENTHANDLER_LIST_DEFINE(vfs_unmounted);
104
105 static void mount_devctl_event(const char *type, struct mount *mp, bool donew);
106
107 /*
108  * Global opts, taken by all filesystems
109  */
110 static const char *global_opts[] = {
111         "errmsg",
112         "fstype",
113         "fspath",
114         "ro",
115         "rw",
116         "nosuid",
117         "noexec",
118         NULL
119 };
120
121 static int
122 mount_init(void *mem, int size, int flags)
123 {
124         struct mount *mp;
125
126         mp = (struct mount *)mem;
127         mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF);
128         mtx_init(&mp->mnt_listmtx, "struct mount vlist mtx", NULL, MTX_DEF);
129         lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0);
130         mp->mnt_pcpu = uma_zalloc_pcpu(pcpu_zone_16, M_WAITOK | M_ZERO);
131         mp->mnt_ref = 0;
132         mp->mnt_vfs_ops = 1;
133         mp->mnt_rootvnode = NULL;
134         return (0);
135 }
136
137 static void
138 mount_fini(void *mem, int size)
139 {
140         struct mount *mp;
141
142         mp = (struct mount *)mem;
143         uma_zfree_pcpu(pcpu_zone_16, mp->mnt_pcpu);
144         lockdestroy(&mp->mnt_explock);
145         mtx_destroy(&mp->mnt_listmtx);
146         mtx_destroy(&mp->mnt_mtx);
147 }
148
149 static void
150 vfs_mount_init(void *dummy __unused)
151 {
152
153         mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL,
154             NULL, mount_init, mount_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
155 }
156 SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL);
157
158 /*
159  * ---------------------------------------------------------------------
160  * Functions for building and sanitizing the mount options
161  */
162
163 /* Remove one mount option. */
164 static void
165 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt)
166 {
167
168         TAILQ_REMOVE(opts, opt, link);
169         free(opt->name, M_MOUNT);
170         if (opt->value != NULL)
171                 free(opt->value, M_MOUNT);
172         free(opt, M_MOUNT);
173 }
174
175 /* Release all resources related to the mount options. */
176 void
177 vfs_freeopts(struct vfsoptlist *opts)
178 {
179         struct vfsopt *opt;
180
181         while (!TAILQ_EMPTY(opts)) {
182                 opt = TAILQ_FIRST(opts);
183                 vfs_freeopt(opts, opt);
184         }
185         free(opts, M_MOUNT);
186 }
187
188 void
189 vfs_deleteopt(struct vfsoptlist *opts, const char *name)
190 {
191         struct vfsopt *opt, *temp;
192
193         if (opts == NULL)
194                 return;
195         TAILQ_FOREACH_SAFE(opt, opts, link, temp)  {
196                 if (strcmp(opt->name, name) == 0)
197                         vfs_freeopt(opts, opt);
198         }
199 }
200
201 static int
202 vfs_isopt_ro(const char *opt)
203 {
204
205         if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 ||
206             strcmp(opt, "norw") == 0)
207                 return (1);
208         return (0);
209 }
210
211 static int
212 vfs_isopt_rw(const char *opt)
213 {
214
215         if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0)
216                 return (1);
217         return (0);
218 }
219
220 /*
221  * Check if options are equal (with or without the "no" prefix).
222  */
223 static int
224 vfs_equalopts(const char *opt1, const char *opt2)
225 {
226         char *p;
227
228         /* "opt" vs. "opt" or "noopt" vs. "noopt" */
229         if (strcmp(opt1, opt2) == 0)
230                 return (1);
231         /* "noopt" vs. "opt" */
232         if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
233                 return (1);
234         /* "opt" vs. "noopt" */
235         if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
236                 return (1);
237         while ((p = strchr(opt1, '.')) != NULL &&
238             !strncmp(opt1, opt2, ++p - opt1)) {
239                 opt2 += p - opt1;
240                 opt1 = p;
241                 /* "foo.noopt" vs. "foo.opt" */
242                 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
243                         return (1);
244                 /* "foo.opt" vs. "foo.noopt" */
245                 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
246                         return (1);
247         }
248         /* "ro" / "rdonly" / "norw" / "rw" / "noro" */
249         if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) &&
250             (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2)))
251                 return (1);
252         return (0);
253 }
254
255 /*
256  * If a mount option is specified several times,
257  * (with or without the "no" prefix) only keep
258  * the last occurrence of it.
259  */
260 static void
261 vfs_sanitizeopts(struct vfsoptlist *opts)
262 {
263         struct vfsopt *opt, *opt2, *tmp;
264
265         TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) {
266                 opt2 = TAILQ_PREV(opt, vfsoptlist, link);
267                 while (opt2 != NULL) {
268                         if (vfs_equalopts(opt->name, opt2->name)) {
269                                 tmp = TAILQ_PREV(opt2, vfsoptlist, link);
270                                 vfs_freeopt(opts, opt2);
271                                 opt2 = tmp;
272                         } else {
273                                 opt2 = TAILQ_PREV(opt2, vfsoptlist, link);
274                         }
275                 }
276         }
277 }
278
279 /*
280  * Build a linked list of mount options from a struct uio.
281  */
282 int
283 vfs_buildopts(struct uio *auio, struct vfsoptlist **options)
284 {
285         struct vfsoptlist *opts;
286         struct vfsopt *opt;
287         size_t memused, namelen, optlen;
288         unsigned int i, iovcnt;
289         int error;
290
291         opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
292         TAILQ_INIT(opts);
293         memused = 0;
294         iovcnt = auio->uio_iovcnt;
295         for (i = 0; i < iovcnt; i += 2) {
296                 namelen = auio->uio_iov[i].iov_len;
297                 optlen = auio->uio_iov[i + 1].iov_len;
298                 memused += sizeof(struct vfsopt) + optlen + namelen;
299                 /*
300                  * Avoid consuming too much memory, and attempts to overflow
301                  * memused.
302                  */
303                 if (memused > VFS_MOUNTARG_SIZE_MAX ||
304                     optlen > VFS_MOUNTARG_SIZE_MAX ||
305                     namelen > VFS_MOUNTARG_SIZE_MAX) {
306                         error = EINVAL;
307                         goto bad;
308                 }
309
310                 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
311                 opt->name = malloc(namelen, M_MOUNT, M_WAITOK);
312                 opt->value = NULL;
313                 opt->len = 0;
314                 opt->pos = i / 2;
315                 opt->seen = 0;
316
317                 /*
318                  * Do this early, so jumps to "bad" will free the current
319                  * option.
320                  */
321                 TAILQ_INSERT_TAIL(opts, opt, link);
322
323                 if (auio->uio_segflg == UIO_SYSSPACE) {
324                         bcopy(auio->uio_iov[i].iov_base, opt->name, namelen);
325                 } else {
326                         error = copyin(auio->uio_iov[i].iov_base, opt->name,
327                             namelen);
328                         if (error)
329                                 goto bad;
330                 }
331                 /* Ensure names are null-terminated strings. */
332                 if (namelen == 0 || opt->name[namelen - 1] != '\0') {
333                         error = EINVAL;
334                         goto bad;
335                 }
336                 if (optlen != 0) {
337                         opt->len = optlen;
338                         opt->value = malloc(optlen, M_MOUNT, M_WAITOK);
339                         if (auio->uio_segflg == UIO_SYSSPACE) {
340                                 bcopy(auio->uio_iov[i + 1].iov_base, opt->value,
341                                     optlen);
342                         } else {
343                                 error = copyin(auio->uio_iov[i + 1].iov_base,
344                                     opt->value, optlen);
345                                 if (error)
346                                         goto bad;
347                         }
348                 }
349         }
350         vfs_sanitizeopts(opts);
351         *options = opts;
352         return (0);
353 bad:
354         vfs_freeopts(opts);
355         return (error);
356 }
357
358 /*
359  * Merge the old mount options with the new ones passed
360  * in the MNT_UPDATE case.
361  *
362  * XXX: This function will keep a "nofoo" option in the new
363  * options.  E.g, if the option's canonical name is "foo",
364  * "nofoo" ends up in the mount point's active options.
365  */
366 static void
367 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts)
368 {
369         struct vfsopt *opt, *new;
370
371         TAILQ_FOREACH(opt, oldopts, link) {
372                 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK);
373                 new->name = strdup(opt->name, M_MOUNT);
374                 if (opt->len != 0) {
375                         new->value = malloc(opt->len, M_MOUNT, M_WAITOK);
376                         bcopy(opt->value, new->value, opt->len);
377                 } else
378                         new->value = NULL;
379                 new->len = opt->len;
380                 new->seen = opt->seen;
381                 TAILQ_INSERT_HEAD(toopts, new, link);
382         }
383         vfs_sanitizeopts(toopts);
384 }
385
386 /*
387  * Mount a filesystem.
388  */
389 #ifndef _SYS_SYSPROTO_H_
390 struct nmount_args {
391         struct iovec *iovp;
392         unsigned int iovcnt;
393         int flags;
394 };
395 #endif
396 int
397 sys_nmount(struct thread *td, struct nmount_args *uap)
398 {
399         struct uio *auio;
400         int error;
401         u_int iovcnt;
402         uint64_t flags;
403
404         /*
405          * Mount flags are now 64-bits. On 32-bit archtectures only
406          * 32-bits are passed in, but from here on everything handles
407          * 64-bit flags correctly.
408          */
409         flags = uap->flags;
410
411         AUDIT_ARG_FFLAGS(flags);
412         CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__,
413             uap->iovp, uap->iovcnt, flags);
414
415         /*
416          * Filter out MNT_ROOTFS.  We do not want clients of nmount() in
417          * userspace to set this flag, but we must filter it out if we want
418          * MNT_UPDATE on the root file system to work.
419          * MNT_ROOTFS should only be set by the kernel when mounting its
420          * root file system.
421          */
422         flags &= ~MNT_ROOTFS;
423
424         iovcnt = uap->iovcnt;
425         /*
426          * Check that we have an even number of iovec's
427          * and that we have at least two options.
428          */
429         if ((iovcnt & 1) || (iovcnt < 4)) {
430                 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__,
431                     uap->iovcnt);
432                 return (EINVAL);
433         }
434
435         error = copyinuio(uap->iovp, iovcnt, &auio);
436         if (error) {
437                 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno",
438                     __func__, error);
439                 return (error);
440         }
441         error = vfs_donmount(td, flags, auio);
442
443         free(auio, M_IOV);
444         return (error);
445 }
446
447 /*
448  * ---------------------------------------------------------------------
449  * Various utility functions
450  */
451
452 void
453 vfs_ref(struct mount *mp)
454 {
455         struct mount_pcpu *mpcpu;
456
457         CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
458         if (vfs_op_thread_enter(mp, mpcpu)) {
459                 vfs_mp_count_add_pcpu(mpcpu, ref, 1);
460                 vfs_op_thread_exit(mp, mpcpu);
461                 return;
462         }
463
464         MNT_ILOCK(mp);
465         MNT_REF(mp);
466         MNT_IUNLOCK(mp);
467 }
468
469 void
470 vfs_rel(struct mount *mp)
471 {
472         struct mount_pcpu *mpcpu;
473
474         CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
475         if (vfs_op_thread_enter(mp, mpcpu)) {
476                 vfs_mp_count_sub_pcpu(mpcpu, ref, 1);
477                 vfs_op_thread_exit(mp, mpcpu);
478                 return;
479         }
480
481         MNT_ILOCK(mp);
482         MNT_REL(mp);
483         MNT_IUNLOCK(mp);
484 }
485
486 /*
487  * Allocate and initialize the mount point struct.
488  */
489 struct mount *
490 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath,
491     struct ucred *cred)
492 {
493         struct mount *mp;
494
495         mp = uma_zalloc(mount_zone, M_WAITOK);
496         bzero(&mp->mnt_startzero,
497             __rangeof(struct mount, mnt_startzero, mnt_endzero));
498         mp->mnt_kern_flag = 0;
499         mp->mnt_flag = 0;
500         mp->mnt_rootvnode = NULL;
501         mp->mnt_vnodecovered = NULL;
502         mp->mnt_op = NULL;
503         mp->mnt_vfc = NULL;
504         TAILQ_INIT(&mp->mnt_nvnodelist);
505         mp->mnt_nvnodelistsize = 0;
506         TAILQ_INIT(&mp->mnt_lazyvnodelist);
507         mp->mnt_lazyvnodelistsize = 0;
508         if (mp->mnt_ref != 0 || mp->mnt_lockref != 0 ||
509             mp->mnt_writeopcount != 0)
510                 panic("%s: non-zero counters on new mp %p\n", __func__, mp);
511         if (mp->mnt_vfs_ops != 1)
512                 panic("%s: vfs_ops should be 1 but %d found\n", __func__,
513                     mp->mnt_vfs_ops);
514         (void) vfs_busy(mp, MBF_NOWAIT);
515         atomic_add_acq_int(&vfsp->vfc_refcount, 1);
516         mp->mnt_op = vfsp->vfc_vfsops;
517         mp->mnt_vfc = vfsp;
518         mp->mnt_stat.f_type = vfsp->vfc_typenum;
519         mp->mnt_gen++;
520         strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
521         mp->mnt_vnodecovered = vp;
522         mp->mnt_cred = crdup(cred);
523         mp->mnt_stat.f_owner = cred->cr_uid;
524         strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN);
525         mp->mnt_iosize_max = DFLTPHYS;
526 #ifdef MAC
527         mac_mount_init(mp);
528         mac_mount_create(cred, mp);
529 #endif
530         arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0);
531         TAILQ_INIT(&mp->mnt_uppers);
532         return (mp);
533 }
534
535 /*
536  * Destroy the mount struct previously allocated by vfs_mount_alloc().
537  */
538 void
539 vfs_mount_destroy(struct mount *mp)
540 {
541
542         if (mp->mnt_vfs_ops == 0)
543                 panic("%s: entered with zero vfs_ops\n", __func__);
544
545         vfs_assert_mount_counters(mp);
546
547         MNT_ILOCK(mp);
548         mp->mnt_kern_flag |= MNTK_REFEXPIRE;
549         if (mp->mnt_kern_flag & MNTK_MWAIT) {
550                 mp->mnt_kern_flag &= ~MNTK_MWAIT;
551                 wakeup(mp);
552         }
553         while (mp->mnt_ref)
554                 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0);
555         KASSERT(mp->mnt_ref == 0,
556             ("%s: invalid refcount in the drain path @ %s:%d", __func__,
557             __FILE__, __LINE__));
558         if (mp->mnt_writeopcount != 0)
559                 panic("vfs_mount_destroy: nonzero writeopcount");
560         if (mp->mnt_secondary_writes != 0)
561                 panic("vfs_mount_destroy: nonzero secondary_writes");
562         atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1);
563         if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) {
564                 struct vnode *vp;
565
566                 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes)
567                         vn_printf(vp, "dangling vnode ");
568                 panic("unmount: dangling vnode");
569         }
570         KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers"));
571         if (mp->mnt_nvnodelistsize != 0)
572                 panic("vfs_mount_destroy: nonzero nvnodelistsize");
573         if (mp->mnt_lazyvnodelistsize != 0)
574                 panic("vfs_mount_destroy: nonzero lazyvnodelistsize");
575         if (mp->mnt_lockref != 0)
576                 panic("vfs_mount_destroy: nonzero lock refcount");
577         MNT_IUNLOCK(mp);
578
579         if (mp->mnt_vfs_ops != 1)
580                 panic("%s: vfs_ops should be 1 but %d found\n", __func__,
581                     mp->mnt_vfs_ops);
582
583         if (mp->mnt_rootvnode != NULL)
584                 panic("%s: mount point still has a root vnode %p\n", __func__,
585                     mp->mnt_rootvnode);
586
587         if (mp->mnt_vnodecovered != NULL)
588                 vrele(mp->mnt_vnodecovered);
589 #ifdef MAC
590         mac_mount_destroy(mp);
591 #endif
592         if (mp->mnt_opt != NULL)
593                 vfs_freeopts(mp->mnt_opt);
594         crfree(mp->mnt_cred);
595         uma_zfree(mount_zone, mp);
596 }
597
598 static bool
599 vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error)
600 {
601         /* This is an upgrade of an exisiting mount. */
602         if ((fsflags & MNT_UPDATE) != 0)
603                 return (false);
604         /* This is already an R/O mount. */
605         if ((fsflags & MNT_RDONLY) != 0)
606                 return (false);
607
608         switch (error) {
609         case ENODEV:    /* generic, geom, ... */
610         case EACCES:    /* cam/scsi, ... */
611         case EROFS:     /* md, mmcsd, ... */
612                 /*
613                  * These errors can be returned by the storage layer to signal
614                  * that the media is read-only.  No harm in the R/O mount
615                  * attempt if the error was returned for some other reason.
616                  */
617                 return (true);
618         default:
619                 return (false);
620         }
621 }
622
623 int
624 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions)
625 {
626         struct vfsoptlist *optlist;
627         struct vfsopt *opt, *tmp_opt;
628         char *fstype, *fspath, *errmsg;
629         int error, fstypelen, fspathlen, errmsg_len, errmsg_pos;
630         bool autoro;
631
632         errmsg = fspath = NULL;
633         errmsg_len = fspathlen = 0;
634         errmsg_pos = -1;
635         autoro = default_autoro;
636
637         error = vfs_buildopts(fsoptions, &optlist);
638         if (error)
639                 return (error);
640
641         if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0)
642                 errmsg_pos = vfs_getopt_pos(optlist, "errmsg");
643
644         /*
645          * We need these two options before the others,
646          * and they are mandatory for any filesystem.
647          * Ensure they are NUL terminated as well.
648          */
649         fstypelen = 0;
650         error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen);
651         if (error || fstypelen <= 0 || fstype[fstypelen - 1] != '\0') {
652                 error = EINVAL;
653                 if (errmsg != NULL)
654                         strncpy(errmsg, "Invalid fstype", errmsg_len);
655                 goto bail;
656         }
657         fspathlen = 0;
658         error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen);
659         if (error || fspathlen <= 0 || fspath[fspathlen - 1] != '\0') {
660                 error = EINVAL;
661                 if (errmsg != NULL)
662                         strncpy(errmsg, "Invalid fspath", errmsg_len);
663                 goto bail;
664         }
665
666         /*
667          * We need to see if we have the "update" option
668          * before we call vfs_domount(), since vfs_domount() has special
669          * logic based on MNT_UPDATE.  This is very important
670          * when we want to update the root filesystem.
671          */
672         TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) {
673                 int do_freeopt = 0;
674
675                 if (strcmp(opt->name, "update") == 0) {
676                         fsflags |= MNT_UPDATE;
677                         do_freeopt = 1;
678                 }
679                 else if (strcmp(opt->name, "async") == 0)
680                         fsflags |= MNT_ASYNC;
681                 else if (strcmp(opt->name, "force") == 0) {
682                         fsflags |= MNT_FORCE;
683                         do_freeopt = 1;
684                 }
685                 else if (strcmp(opt->name, "reload") == 0) {
686                         fsflags |= MNT_RELOAD;
687                         do_freeopt = 1;
688                 }
689                 else if (strcmp(opt->name, "multilabel") == 0)
690                         fsflags |= MNT_MULTILABEL;
691                 else if (strcmp(opt->name, "noasync") == 0)
692                         fsflags &= ~MNT_ASYNC;
693                 else if (strcmp(opt->name, "noatime") == 0)
694                         fsflags |= MNT_NOATIME;
695                 else if (strcmp(opt->name, "atime") == 0) {
696                         free(opt->name, M_MOUNT);
697                         opt->name = strdup("nonoatime", M_MOUNT);
698                 }
699                 else if (strcmp(opt->name, "noclusterr") == 0)
700                         fsflags |= MNT_NOCLUSTERR;
701                 else if (strcmp(opt->name, "clusterr") == 0) {
702                         free(opt->name, M_MOUNT);
703                         opt->name = strdup("nonoclusterr", M_MOUNT);
704                 }
705                 else if (strcmp(opt->name, "noclusterw") == 0)
706                         fsflags |= MNT_NOCLUSTERW;
707                 else if (strcmp(opt->name, "clusterw") == 0) {
708                         free(opt->name, M_MOUNT);
709                         opt->name = strdup("nonoclusterw", M_MOUNT);
710                 }
711                 else if (strcmp(opt->name, "noexec") == 0)
712                         fsflags |= MNT_NOEXEC;
713                 else if (strcmp(opt->name, "exec") == 0) {
714                         free(opt->name, M_MOUNT);
715                         opt->name = strdup("nonoexec", M_MOUNT);
716                 }
717                 else if (strcmp(opt->name, "nosuid") == 0)
718                         fsflags |= MNT_NOSUID;
719                 else if (strcmp(opt->name, "suid") == 0) {
720                         free(opt->name, M_MOUNT);
721                         opt->name = strdup("nonosuid", M_MOUNT);
722                 }
723                 else if (strcmp(opt->name, "nosymfollow") == 0)
724                         fsflags |= MNT_NOSYMFOLLOW;
725                 else if (strcmp(opt->name, "symfollow") == 0) {
726                         free(opt->name, M_MOUNT);
727                         opt->name = strdup("nonosymfollow", M_MOUNT);
728                 }
729                 else if (strcmp(opt->name, "noro") == 0) {
730                         fsflags &= ~MNT_RDONLY;
731                         autoro = false;
732                 }
733                 else if (strcmp(opt->name, "rw") == 0) {
734                         fsflags &= ~MNT_RDONLY;
735                         autoro = false;
736                 }
737                 else if (strcmp(opt->name, "ro") == 0) {
738                         fsflags |= MNT_RDONLY;
739                         autoro = false;
740                 }
741                 else if (strcmp(opt->name, "rdonly") == 0) {
742                         free(opt->name, M_MOUNT);
743                         opt->name = strdup("ro", M_MOUNT);
744                         fsflags |= MNT_RDONLY;
745                         autoro = false;
746                 }
747                 else if (strcmp(opt->name, "autoro") == 0) {
748                         do_freeopt = 1;
749                         autoro = true;
750                 }
751                 else if (strcmp(opt->name, "suiddir") == 0)
752                         fsflags |= MNT_SUIDDIR;
753                 else if (strcmp(opt->name, "sync") == 0)
754                         fsflags |= MNT_SYNCHRONOUS;
755                 else if (strcmp(opt->name, "union") == 0)
756                         fsflags |= MNT_UNION;
757                 else if (strcmp(opt->name, "automounted") == 0) {
758                         fsflags |= MNT_AUTOMOUNTED;
759                         do_freeopt = 1;
760                 } else if (strcmp(opt->name, "nocover") == 0) {
761                         fsflags |= MNT_NOCOVER;
762                         do_freeopt = 1;
763                 } else if (strcmp(opt->name, "cover") == 0) {
764                         fsflags &= ~MNT_NOCOVER;
765                         do_freeopt = 1;
766                 } else if (strcmp(opt->name, "emptydir") == 0) {
767                         fsflags |= MNT_EMPTYDIR;
768                         do_freeopt = 1;
769                 } else if (strcmp(opt->name, "noemptydir") == 0) {
770                         fsflags &= ~MNT_EMPTYDIR;
771                         do_freeopt = 1;
772                 }
773                 if (do_freeopt)
774                         vfs_freeopt(optlist, opt);
775         }
776
777         /*
778          * Be ultra-paranoid about making sure the type and fspath
779          * variables will fit in our mp buffers, including the
780          * terminating NUL.
781          */
782         if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) {
783                 error = ENAMETOOLONG;
784                 goto bail;
785         }
786
787         error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
788
789         /*
790          * See if we can mount in the read-only mode if the error code suggests
791          * that it could be possible and the mount options allow for that.
792          * Never try it if "[no]{ro|rw}" has been explicitly requested and not
793          * overridden by "autoro".
794          */
795         if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) {
796                 printf("%s: R/W mount failed, possibly R/O media,"
797                     " trying R/O mount\n", __func__);
798                 fsflags |= MNT_RDONLY;
799                 error = vfs_domount(td, fstype, fspath, fsflags, &optlist);
800         }
801 bail:
802         /* copyout the errmsg */
803         if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt)
804             && errmsg_len > 0 && errmsg != NULL) {
805                 if (fsoptions->uio_segflg == UIO_SYSSPACE) {
806                         bcopy(errmsg,
807                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
808                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
809                 } else {
810                         copyout(errmsg,
811                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base,
812                             fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len);
813                 }
814         }
815
816         if (optlist != NULL)
817                 vfs_freeopts(optlist);
818         return (error);
819 }
820
821 /*
822  * Old mount API.
823  */
824 #ifndef _SYS_SYSPROTO_H_
825 struct mount_args {
826         char    *type;
827         char    *path;
828         int     flags;
829         caddr_t data;
830 };
831 #endif
832 /* ARGSUSED */
833 int
834 sys_mount(struct thread *td, struct mount_args *uap)
835 {
836         char *fstype;
837         struct vfsconf *vfsp = NULL;
838         struct mntarg *ma = NULL;
839         uint64_t flags;
840         int error;
841
842         /*
843          * Mount flags are now 64-bits. On 32-bit architectures only
844          * 32-bits are passed in, but from here on everything handles
845          * 64-bit flags correctly.
846          */
847         flags = uap->flags;
848
849         AUDIT_ARG_FFLAGS(flags);
850
851         /*
852          * Filter out MNT_ROOTFS.  We do not want clients of mount() in
853          * userspace to set this flag, but we must filter it out if we want
854          * MNT_UPDATE on the root file system to work.
855          * MNT_ROOTFS should only be set by the kernel when mounting its
856          * root file system.
857          */
858         flags &= ~MNT_ROOTFS;
859
860         fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK);
861         error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL);
862         if (error) {
863                 free(fstype, M_TEMP);
864                 return (error);
865         }
866
867         AUDIT_ARG_TEXT(fstype);
868         vfsp = vfs_byname_kld(fstype, td, &error);
869         free(fstype, M_TEMP);
870         if (vfsp == NULL)
871                 return (ENOENT);
872         if (((vfsp->vfc_flags & VFCF_SBDRY) != 0 &&
873             vfsp->vfc_vfsops_sd->vfs_cmount == NULL) ||
874             ((vfsp->vfc_flags & VFCF_SBDRY) == 0 &&
875             vfsp->vfc_vfsops->vfs_cmount == NULL))
876                 return (EOPNOTSUPP);
877
878         ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN);
879         ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN);
880         ma = mount_argb(ma, flags & MNT_RDONLY, "noro");
881         ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid");
882         ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec");
883
884         if ((vfsp->vfc_flags & VFCF_SBDRY) != 0)
885                 return (vfsp->vfc_vfsops_sd->vfs_cmount(ma, uap->data, flags));
886         return (vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags));
887 }
888
889 /*
890  * vfs_domount_first(): first file system mount (not update)
891  */
892 static int
893 vfs_domount_first(
894         struct thread *td,              /* Calling thread. */
895         struct vfsconf *vfsp,           /* File system type. */
896         char *fspath,                   /* Mount path. */
897         struct vnode *vp,               /* Vnode to be covered. */
898         uint64_t fsflags,               /* Flags common to all filesystems. */
899         struct vfsoptlist **optlist     /* Options local to the filesystem. */
900         )
901 {
902         struct vattr va;
903         struct mount *mp;
904         struct vnode *newdp, *rootvp;
905         int error, error1;
906         bool unmounted;
907
908         ASSERT_VOP_ELOCKED(vp, __func__);
909         KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here"));
910
911         if ((fsflags & MNT_EMPTYDIR) != 0) {
912                 error = vfs_emptydir(vp);
913                 if (error != 0) {
914                         vput(vp);
915                         return (error);
916                 }
917         }
918
919         /*
920          * If the jail of the calling thread lacks permission for this type of
921          * file system, deny immediately.
922          */
923         if (jailed(td->td_ucred) && !prison_allow(td->td_ucred,
924             vfsp->vfc_prison_flag)) {
925                 vput(vp);
926                 return (EPERM);
927         }
928
929         /*
930          * If the user is not root, ensure that they own the directory
931          * onto which we are attempting to mount.
932          */
933         error = VOP_GETATTR(vp, &va, td->td_ucred);
934         if (error == 0 && va.va_uid != td->td_ucred->cr_uid)
935                 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN);
936         if (error == 0)
937                 error = vinvalbuf(vp, V_SAVE, 0, 0);
938         if (error == 0 && vp->v_type != VDIR)
939                 error = ENOTDIR;
940         if (error == 0) {
941                 VI_LOCK(vp);
942                 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL)
943                         vp->v_iflag |= VI_MOUNT;
944                 else
945                         error = EBUSY;
946                 VI_UNLOCK(vp);
947         }
948         if (error != 0) {
949                 vput(vp);
950                 return (error);
951         }
952         vn_seqc_write_begin(vp);
953         VOP_UNLOCK(vp);
954
955         /* Allocate and initialize the filesystem. */
956         mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred);
957         /* XXXMAC: pass to vfs_mount_alloc? */
958         mp->mnt_optnew = *optlist;
959         /* Set the mount level flags. */
960         mp->mnt_flag = (fsflags & (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY));
961
962         /*
963          * Mount the filesystem.
964          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
965          * get.  No freeing of cn_pnbuf.
966          */
967         error1 = 0;
968         unmounted = true;
969         if ((error = VFS_MOUNT(mp)) != 0 ||
970             (error1 = VFS_STATFS(mp, &mp->mnt_stat)) != 0 ||
971             (error1 = VFS_ROOT(mp, LK_EXCLUSIVE, &newdp)) != 0) {
972                 rootvp = NULL;
973                 if (error1 != 0) {
974                         MPASS(error == 0);
975                         rootvp = vfs_cache_root_clear(mp);
976                         if (rootvp != NULL) {
977                                 vhold(rootvp);
978                                 vrele(rootvp);
979                         }
980                         (void)vn_start_write(NULL, &mp, V_WAIT);
981                         MNT_ILOCK(mp);
982                         mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_UNMOUNTF;
983                         MNT_IUNLOCK(mp);
984                         VFS_PURGE(mp);
985                         error = VFS_UNMOUNT(mp, 0);
986                         vn_finished_write(mp);
987                         if (error != 0) {
988                                 printf(
989                     "failed post-mount (%d): rollback unmount returned %d\n",
990                                     error1, error);
991                                 unmounted = false;
992                         }
993                         error = error1;
994                 }
995                 vfs_unbusy(mp);
996                 mp->mnt_vnodecovered = NULL;
997                 if (unmounted) {
998                         /* XXXKIB wait for mnt_lockref drain? */
999                         vfs_mount_destroy(mp);
1000                 }
1001                 VI_LOCK(vp);
1002                 vp->v_iflag &= ~VI_MOUNT;
1003                 VI_UNLOCK(vp);
1004                 if (rootvp != NULL) {
1005                         vn_seqc_write_end(rootvp);
1006                         vdrop(rootvp);
1007                 }
1008                 vn_seqc_write_end(vp);
1009                 vrele(vp);
1010                 return (error);
1011         }
1012         vn_seqc_write_begin(newdp);
1013         VOP_UNLOCK(newdp);
1014
1015         if (mp->mnt_opt != NULL)
1016                 vfs_freeopts(mp->mnt_opt);
1017         mp->mnt_opt = mp->mnt_optnew;
1018         *optlist = NULL;
1019
1020         /*
1021          * Prevent external consumers of mount options from reading mnt_optnew.
1022          */
1023         mp->mnt_optnew = NULL;
1024
1025         MNT_ILOCK(mp);
1026         if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1027             (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1028                 mp->mnt_kern_flag |= MNTK_ASYNC;
1029         else
1030                 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1031         MNT_IUNLOCK(mp);
1032
1033         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1034         cache_purge(vp);
1035         VI_LOCK(vp);
1036         vp->v_iflag &= ~VI_MOUNT;
1037         VI_UNLOCK(vp);
1038         vp->v_mountedhere = mp;
1039         /* Place the new filesystem at the end of the mount list. */
1040         mtx_lock(&mountlist_mtx);
1041         TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1042         mtx_unlock(&mountlist_mtx);
1043         vfs_event_signal(NULL, VQ_MOUNT, 0);
1044         vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
1045         VOP_UNLOCK(vp);
1046         EVENTHANDLER_DIRECT_INVOKE(vfs_mounted, mp, newdp, td);
1047         VOP_UNLOCK(newdp);
1048         mount_devctl_event("MOUNT", mp, false);
1049         mountcheckdirs(vp, newdp);
1050         vn_seqc_write_end(vp);
1051         vn_seqc_write_end(newdp);
1052         vrele(newdp);
1053         if ((mp->mnt_flag & MNT_RDONLY) == 0)
1054                 vfs_allocate_syncvnode(mp);
1055         vfs_op_exit(mp);
1056         vfs_unbusy(mp);
1057         return (0);
1058 }
1059
1060 /*
1061  * vfs_domount_update(): update of mounted file system
1062  */
1063 static int
1064 vfs_domount_update(
1065         struct thread *td,              /* Calling thread. */
1066         struct vnode *vp,               /* Mount point vnode. */
1067         uint64_t fsflags,               /* Flags common to all filesystems. */
1068         struct vfsoptlist **optlist     /* Options local to the filesystem. */
1069         )
1070 {
1071         struct export_args export;
1072         struct o2export_args o2export;
1073         struct vnode *rootvp;
1074         void *bufp;
1075         struct mount *mp;
1076         int error, export_error, i, len;
1077         uint64_t flag;
1078         gid_t *grps;
1079
1080         ASSERT_VOP_ELOCKED(vp, __func__);
1081         KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here"));
1082         mp = vp->v_mount;
1083
1084         if ((vp->v_vflag & VV_ROOT) == 0) {
1085                 if (vfs_copyopt(*optlist, "export", &export, sizeof(export))
1086                     == 0)
1087                         error = EXDEV;
1088                 else
1089                         error = EINVAL;
1090                 vput(vp);
1091                 return (error);
1092         }
1093
1094         /*
1095          * We only allow the filesystem to be reloaded if it
1096          * is currently mounted read-only.
1097          */
1098         flag = mp->mnt_flag;
1099         if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) {
1100                 vput(vp);
1101                 return (EOPNOTSUPP);    /* Needs translation */
1102         }
1103         /*
1104          * Only privileged root, or (if MNT_USER is set) the user that
1105          * did the original mount is permitted to update it.
1106          */
1107         error = vfs_suser(mp, td);
1108         if (error != 0) {
1109                 vput(vp);
1110                 return (error);
1111         }
1112         if (vfs_busy(mp, MBF_NOWAIT)) {
1113                 vput(vp);
1114                 return (EBUSY);
1115         }
1116         VI_LOCK(vp);
1117         if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) {
1118                 VI_UNLOCK(vp);
1119                 vfs_unbusy(mp);
1120                 vput(vp);
1121                 return (EBUSY);
1122         }
1123         vp->v_iflag |= VI_MOUNT;
1124         VI_UNLOCK(vp);
1125         VOP_UNLOCK(vp);
1126
1127         vfs_op_enter(mp);
1128         vn_seqc_write_begin(vp);
1129
1130         rootvp = NULL;
1131         MNT_ILOCK(mp);
1132         if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) {
1133                 MNT_IUNLOCK(mp);
1134                 error = EBUSY;
1135                 goto end;
1136         }
1137         mp->mnt_flag &= ~MNT_UPDATEMASK;
1138         mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE |
1139             MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY);
1140         if ((mp->mnt_flag & MNT_ASYNC) == 0)
1141                 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1142         rootvp = vfs_cache_root_clear(mp);
1143         MNT_IUNLOCK(mp);
1144         mp->mnt_optnew = *optlist;
1145         vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt);
1146
1147         /*
1148          * Mount the filesystem.
1149          * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
1150          * get.  No freeing of cn_pnbuf.
1151          */
1152         error = VFS_MOUNT(mp);
1153
1154         export_error = 0;
1155         /* Process the export option. */
1156         if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp,
1157             &len) == 0) {
1158                 /* Assume that there is only 1 ABI for each length. */
1159                 switch (len) {
1160                 case (sizeof(struct oexport_args)):
1161                         bzero(&o2export, sizeof(o2export));
1162                         /* FALLTHROUGH */
1163                 case (sizeof(o2export)):
1164                         bcopy(bufp, &o2export, len);
1165                         export.ex_flags = (uint64_t)o2export.ex_flags;
1166                         export.ex_root = o2export.ex_root;
1167                         export.ex_uid = o2export.ex_anon.cr_uid;
1168                         export.ex_groups = NULL;
1169                         export.ex_ngroups = o2export.ex_anon.cr_ngroups;
1170                         if (export.ex_ngroups > 0) {
1171                                 if (export.ex_ngroups <= XU_NGROUPS) {
1172                                         export.ex_groups = malloc(
1173                                             export.ex_ngroups * sizeof(gid_t),
1174                                             M_TEMP, M_WAITOK);
1175                                         for (i = 0; i < export.ex_ngroups; i++)
1176                                                 export.ex_groups[i] =
1177                                                   o2export.ex_anon.cr_groups[i];
1178                                 } else
1179                                         export_error = EINVAL;
1180                         } else if (export.ex_ngroups < 0)
1181                                 export_error = EINVAL;
1182                         export.ex_addr = o2export.ex_addr;
1183                         export.ex_addrlen = o2export.ex_addrlen;
1184                         export.ex_mask = o2export.ex_mask;
1185                         export.ex_masklen = o2export.ex_masklen;
1186                         export.ex_indexfile = o2export.ex_indexfile;
1187                         export.ex_numsecflavors = o2export.ex_numsecflavors;
1188                         if (export.ex_numsecflavors < MAXSECFLAVORS) {
1189                                 for (i = 0; i < export.ex_numsecflavors; i++)
1190                                         export.ex_secflavors[i] =
1191                                             o2export.ex_secflavors[i];
1192                         } else
1193                                 export_error = EINVAL;
1194                         if (export_error == 0)
1195                                 export_error = vfs_export(mp, &export);
1196                         free(export.ex_groups, M_TEMP);
1197                         break;
1198                 case (sizeof(export)):
1199                         bcopy(bufp, &export, len);
1200                         grps = NULL;
1201                         if (export.ex_ngroups > 0) {
1202                                 if (export.ex_ngroups <= NGROUPS_MAX) {
1203                                         grps = malloc(export.ex_ngroups *
1204                                             sizeof(gid_t), M_TEMP, M_WAITOK);
1205                                         export_error = copyin(export.ex_groups,
1206                                             grps, export.ex_ngroups *
1207                                             sizeof(gid_t));
1208                                         if (export_error == 0)
1209                                                 export.ex_groups = grps;
1210                                 } else
1211                                         export_error = EINVAL;
1212                         } else if (export.ex_ngroups == 0)
1213                                 export.ex_groups = NULL;
1214                         else
1215                                 export_error = EINVAL;
1216                         if (export_error == 0)
1217                                 export_error = vfs_export(mp, &export);
1218                         free(grps, M_TEMP);
1219                         break;
1220                 default:
1221                         export_error = EINVAL;
1222                         break;
1223                 }
1224         }
1225
1226         MNT_ILOCK(mp);
1227         if (error == 0) {
1228                 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE |
1229                     MNT_SNAPSHOT);
1230         } else {
1231                 /*
1232                  * If we fail, restore old mount flags. MNT_QUOTA is special,
1233                  * because it is not part of MNT_UPDATEMASK, but it could have
1234                  * changed in the meantime if quotactl(2) was called.
1235                  * All in all we want current value of MNT_QUOTA, not the old
1236                  * one.
1237                  */
1238                 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
1239         }
1240         if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1241             (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1242                 mp->mnt_kern_flag |= MNTK_ASYNC;
1243         else
1244                 mp->mnt_kern_flag &= ~MNTK_ASYNC;
1245         MNT_IUNLOCK(mp);
1246
1247         if (error != 0)
1248                 goto end;
1249
1250         mount_devctl_event("REMOUNT", mp, true);
1251         if (mp->mnt_opt != NULL)
1252                 vfs_freeopts(mp->mnt_opt);
1253         mp->mnt_opt = mp->mnt_optnew;
1254         *optlist = NULL;
1255         (void)VFS_STATFS(mp, &mp->mnt_stat);
1256         /*
1257          * Prevent external consumers of mount options from reading
1258          * mnt_optnew.
1259          */
1260         mp->mnt_optnew = NULL;
1261
1262         if ((mp->mnt_flag & MNT_RDONLY) == 0)
1263                 vfs_allocate_syncvnode(mp);
1264         else
1265                 vfs_deallocate_syncvnode(mp);
1266 end:
1267         vfs_op_exit(mp);
1268         if (rootvp != NULL) {
1269                 vn_seqc_write_end(rootvp);
1270                 vrele(rootvp);
1271         }
1272         vn_seqc_write_end(vp);
1273         vfs_unbusy(mp);
1274         VI_LOCK(vp);
1275         vp->v_iflag &= ~VI_MOUNT;
1276         VI_UNLOCK(vp);
1277         vrele(vp);
1278         return (error != 0 ? error : export_error);
1279 }
1280
1281 /*
1282  * vfs_domount(): actually attempt a filesystem mount.
1283  */
1284 static int
1285 vfs_domount(
1286         struct thread *td,              /* Calling thread. */
1287         const char *fstype,             /* Filesystem type. */
1288         char *fspath,                   /* Mount path. */
1289         uint64_t fsflags,               /* Flags common to all filesystems. */
1290         struct vfsoptlist **optlist     /* Options local to the filesystem. */
1291         )
1292 {
1293         struct vfsconf *vfsp;
1294         struct nameidata nd;
1295         struct vnode *vp;
1296         char *pathbuf;
1297         int error;
1298
1299         /*
1300          * Be ultra-paranoid about making sure the type and fspath
1301          * variables will fit in our mp buffers, including the
1302          * terminating NUL.
1303          */
1304         if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN)
1305                 return (ENAMETOOLONG);
1306
1307         if (jailed(td->td_ucred) || usermount == 0) {
1308                 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0)
1309                         return (error);
1310         }
1311
1312         /*
1313          * Do not allow NFS export or MNT_SUIDDIR by unprivileged users.
1314          */
1315         if (fsflags & MNT_EXPORTED) {
1316                 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED);
1317                 if (error)
1318                         return (error);
1319         }
1320         if (fsflags & MNT_SUIDDIR) {
1321                 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR);
1322                 if (error)
1323                         return (error);
1324         }
1325         /*
1326          * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users.
1327          */
1328         if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) {
1329                 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0)
1330                         fsflags |= MNT_NOSUID | MNT_USER;
1331         }
1332
1333         /* Load KLDs before we lock the covered vnode to avoid reversals. */
1334         vfsp = NULL;
1335         if ((fsflags & MNT_UPDATE) == 0) {
1336                 /* Don't try to load KLDs if we're mounting the root. */
1337                 if (fsflags & MNT_ROOTFS)
1338                         vfsp = vfs_byname(fstype);
1339                 else
1340                         vfsp = vfs_byname_kld(fstype, td, &error);
1341                 if (vfsp == NULL)
1342                         return (ENODEV);
1343         }
1344
1345         /*
1346          * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE.
1347          */
1348         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1349             UIO_SYSSPACE, fspath, td);
1350         error = namei(&nd);
1351         if (error != 0)
1352                 return (error);
1353         NDFREE(&nd, NDF_ONLY_PNBUF);
1354         vp = nd.ni_vp;
1355         if ((fsflags & MNT_UPDATE) == 0) {
1356                 if ((vp->v_vflag & VV_ROOT) != 0 &&
1357                     (fsflags & MNT_NOCOVER) != 0) {
1358                         vput(vp);
1359                         return (EBUSY);
1360                 }
1361                 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1362                 strcpy(pathbuf, fspath);
1363                 error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN);
1364                 if (error == 0) {
1365                         error = vfs_domount_first(td, vfsp, pathbuf, vp,
1366                             fsflags, optlist);
1367                 }
1368                 free(pathbuf, M_TEMP);
1369         } else
1370                 error = vfs_domount_update(td, vp, fsflags, optlist);
1371
1372         return (error);
1373 }
1374
1375 /*
1376  * Unmount a filesystem.
1377  *
1378  * Note: unmount takes a path to the vnode mounted on as argument, not
1379  * special file (as before).
1380  */
1381 #ifndef _SYS_SYSPROTO_H_
1382 struct unmount_args {
1383         char    *path;
1384         int     flags;
1385 };
1386 #endif
1387 /* ARGSUSED */
1388 int
1389 sys_unmount(struct thread *td, struct unmount_args *uap)
1390 {
1391
1392         return (kern_unmount(td, uap->path, uap->flags));
1393 }
1394
1395 int
1396 kern_unmount(struct thread *td, const char *path, int flags)
1397 {
1398         struct nameidata nd;
1399         struct mount *mp;
1400         char *pathbuf;
1401         int error, id0, id1;
1402
1403         AUDIT_ARG_VALUE(flags);
1404         if (jailed(td->td_ucred) || usermount == 0) {
1405                 error = priv_check(td, PRIV_VFS_UNMOUNT);
1406                 if (error)
1407                         return (error);
1408         }
1409
1410         pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1411         error = copyinstr(path, pathbuf, MNAMELEN, NULL);
1412         if (error) {
1413                 free(pathbuf, M_TEMP);
1414                 return (error);
1415         }
1416         if (flags & MNT_BYFSID) {
1417                 AUDIT_ARG_TEXT(pathbuf);
1418                 /* Decode the filesystem ID. */
1419                 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) {
1420                         free(pathbuf, M_TEMP);
1421                         return (EINVAL);
1422                 }
1423
1424                 mtx_lock(&mountlist_mtx);
1425                 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1426                         if (mp->mnt_stat.f_fsid.val[0] == id0 &&
1427                             mp->mnt_stat.f_fsid.val[1] == id1) {
1428                                 vfs_ref(mp);
1429                                 break;
1430                         }
1431                 }
1432                 mtx_unlock(&mountlist_mtx);
1433         } else {
1434                 /*
1435                  * Try to find global path for path argument.
1436                  */
1437                 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1,
1438                     UIO_SYSSPACE, pathbuf, td);
1439                 if (namei(&nd) == 0) {
1440                         NDFREE(&nd, NDF_ONLY_PNBUF);
1441                         error = vn_path_to_global_path(td, nd.ni_vp, pathbuf,
1442                             MNAMELEN);
1443                         if (error == 0)
1444                                 vput(nd.ni_vp);
1445                 }
1446                 mtx_lock(&mountlist_mtx);
1447                 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
1448                         if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) {
1449                                 vfs_ref(mp);
1450                                 break;
1451                         }
1452                 }
1453                 mtx_unlock(&mountlist_mtx);
1454         }
1455         free(pathbuf, M_TEMP);
1456         if (mp == NULL) {
1457                 /*
1458                  * Previously we returned ENOENT for a nonexistent path and
1459                  * EINVAL for a non-mountpoint.  We cannot tell these apart
1460                  * now, so in the !MNT_BYFSID case return the more likely
1461                  * EINVAL for compatibility.
1462                  */
1463                 return ((flags & MNT_BYFSID) ? ENOENT : EINVAL);
1464         }
1465
1466         /*
1467          * Don't allow unmounting the root filesystem.
1468          */
1469         if (mp->mnt_flag & MNT_ROOTFS) {
1470                 vfs_rel(mp);
1471                 return (EINVAL);
1472         }
1473         error = dounmount(mp, flags, td);
1474         return (error);
1475 }
1476
1477 /*
1478  * Return error if any of the vnodes, ignoring the root vnode
1479  * and the syncer vnode, have non-zero usecount.
1480  *
1481  * This function is purely advisory - it can return false positives
1482  * and negatives.
1483  */
1484 static int
1485 vfs_check_usecounts(struct mount *mp)
1486 {
1487         struct vnode *vp, *mvp;
1488
1489         MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
1490                 if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON &&
1491                     vp->v_usecount != 0) {
1492                         VI_UNLOCK(vp);
1493                         MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
1494                         return (EBUSY);
1495                 }
1496                 VI_UNLOCK(vp);
1497         }
1498
1499         return (0);
1500 }
1501
1502 static void
1503 dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags)
1504 {
1505
1506         mtx_assert(MNT_MTX(mp), MA_OWNED);
1507         mp->mnt_kern_flag &= ~mntkflags;
1508         if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) {
1509                 mp->mnt_kern_flag &= ~MNTK_MWAIT;
1510                 wakeup(mp);
1511         }
1512         vfs_op_exit_locked(mp);
1513         MNT_IUNLOCK(mp);
1514         if (coveredvp != NULL) {
1515                 VOP_UNLOCK(coveredvp);
1516                 vdrop(coveredvp);
1517         }
1518         vn_finished_write(mp);
1519 }
1520
1521 /*
1522  * There are various reference counters associated with the mount point.
1523  * Normally it is permitted to modify them without taking the mnt ilock,
1524  * but this behavior can be temporarily disabled if stable value is needed
1525  * or callers are expected to block (e.g. to not allow new users during
1526  * forced unmount).
1527  */
1528 void
1529 vfs_op_enter(struct mount *mp)
1530 {
1531         struct mount_pcpu *mpcpu;
1532         int cpu;
1533
1534         MNT_ILOCK(mp);
1535         mp->mnt_vfs_ops++;
1536         if (mp->mnt_vfs_ops > 1) {
1537                 MNT_IUNLOCK(mp);
1538                 return;
1539         }
1540         vfs_op_barrier_wait(mp);
1541         CPU_FOREACH(cpu) {
1542                 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1543
1544                 mp->mnt_ref += mpcpu->mntp_ref;
1545                 mpcpu->mntp_ref = 0;
1546
1547                 mp->mnt_lockref += mpcpu->mntp_lockref;
1548                 mpcpu->mntp_lockref = 0;
1549
1550                 mp->mnt_writeopcount += mpcpu->mntp_writeopcount;
1551                 mpcpu->mntp_writeopcount = 0;
1552         }
1553         if (mp->mnt_ref <= 0 || mp->mnt_lockref < 0 || mp->mnt_writeopcount < 0)
1554                 panic("%s: invalid count(s) on mp %p: ref %d lockref %d writeopcount %d\n",
1555                     __func__, mp, mp->mnt_ref, mp->mnt_lockref, mp->mnt_writeopcount);
1556         MNT_IUNLOCK(mp);
1557         vfs_assert_mount_counters(mp);
1558 }
1559
1560 void
1561 vfs_op_exit_locked(struct mount *mp)
1562 {
1563
1564         mtx_assert(MNT_MTX(mp), MA_OWNED);
1565
1566         if (mp->mnt_vfs_ops <= 0)
1567                 panic("%s: invalid vfs_ops count %d for mp %p\n",
1568                     __func__, mp->mnt_vfs_ops, mp);
1569         mp->mnt_vfs_ops--;
1570 }
1571
1572 void
1573 vfs_op_exit(struct mount *mp)
1574 {
1575
1576         MNT_ILOCK(mp);
1577         vfs_op_exit_locked(mp);
1578         MNT_IUNLOCK(mp);
1579 }
1580
1581 struct vfs_op_barrier_ipi {
1582         struct mount *mp;
1583         struct smp_rendezvous_cpus_retry_arg srcra;
1584 };
1585
1586 static void
1587 vfs_op_action_func(void *arg)
1588 {
1589         struct vfs_op_barrier_ipi *vfsopipi;
1590         struct mount *mp;
1591
1592         vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
1593         mp = vfsopipi->mp;
1594
1595         if (!vfs_op_thread_entered(mp))
1596                 smp_rendezvous_cpus_done(arg);
1597 }
1598
1599 static void
1600 vfs_op_wait_func(void *arg, int cpu)
1601 {
1602         struct vfs_op_barrier_ipi *vfsopipi;
1603         struct mount *mp;
1604         struct mount_pcpu *mpcpu;
1605
1606         vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra);
1607         mp = vfsopipi->mp;
1608
1609         mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1610         while (atomic_load_int(&mpcpu->mntp_thread_in_ops))
1611                 cpu_spinwait();
1612 }
1613
1614 void
1615 vfs_op_barrier_wait(struct mount *mp)
1616 {
1617         struct vfs_op_barrier_ipi vfsopipi;
1618
1619         vfsopipi.mp = mp;
1620
1621         smp_rendezvous_cpus_retry(all_cpus,
1622             smp_no_rendezvous_barrier,
1623             vfs_op_action_func,
1624             smp_no_rendezvous_barrier,
1625             vfs_op_wait_func,
1626             &vfsopipi.srcra);
1627 }
1628
1629 #ifdef DIAGNOSTIC
1630 void
1631 vfs_assert_mount_counters(struct mount *mp)
1632 {
1633         struct mount_pcpu *mpcpu;
1634         int cpu;
1635
1636         if (mp->mnt_vfs_ops == 0)
1637                 return;
1638
1639         CPU_FOREACH(cpu) {
1640                 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1641                 if (mpcpu->mntp_ref != 0 ||
1642                     mpcpu->mntp_lockref != 0 ||
1643                     mpcpu->mntp_writeopcount != 0)
1644                         vfs_dump_mount_counters(mp);
1645         }
1646 }
1647
1648 void
1649 vfs_dump_mount_counters(struct mount *mp)
1650 {
1651         struct mount_pcpu *mpcpu;
1652         int ref, lockref, writeopcount;
1653         int cpu;
1654
1655         printf("%s: mp %p vfs_ops %d\n", __func__, mp, mp->mnt_vfs_ops);
1656
1657         printf("        ref : ");
1658         ref = mp->mnt_ref;
1659         CPU_FOREACH(cpu) {
1660                 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1661                 printf("%d ", mpcpu->mntp_ref);
1662                 ref += mpcpu->mntp_ref;
1663         }
1664         printf("\n");
1665         printf("    lockref : ");
1666         lockref = mp->mnt_lockref;
1667         CPU_FOREACH(cpu) {
1668                 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1669                 printf("%d ", mpcpu->mntp_lockref);
1670                 lockref += mpcpu->mntp_lockref;
1671         }
1672         printf("\n");
1673         printf("writeopcount: ");
1674         writeopcount = mp->mnt_writeopcount;
1675         CPU_FOREACH(cpu) {
1676                 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1677                 printf("%d ", mpcpu->mntp_writeopcount);
1678                 writeopcount += mpcpu->mntp_writeopcount;
1679         }
1680         printf("\n");
1681
1682         printf("counter       struct total\n");
1683         printf("ref             %-5d  %-5d\n", mp->mnt_ref, ref);
1684         printf("lockref         %-5d  %-5d\n", mp->mnt_lockref, lockref);
1685         printf("writeopcount    %-5d  %-5d\n", mp->mnt_writeopcount, writeopcount);
1686
1687         panic("invalid counts on struct mount");
1688 }
1689 #endif
1690
1691 int
1692 vfs_mount_fetch_counter(struct mount *mp, enum mount_counter which)
1693 {
1694         struct mount_pcpu *mpcpu;
1695         int cpu, sum;
1696
1697         switch (which) {
1698         case MNT_COUNT_REF:
1699                 sum = mp->mnt_ref;
1700                 break;
1701         case MNT_COUNT_LOCKREF:
1702                 sum = mp->mnt_lockref;
1703                 break;
1704         case MNT_COUNT_WRITEOPCOUNT:
1705                 sum = mp->mnt_writeopcount;
1706                 break;
1707         }
1708
1709         CPU_FOREACH(cpu) {
1710                 mpcpu = vfs_mount_pcpu_remote(mp, cpu);
1711                 switch (which) {
1712                 case MNT_COUNT_REF:
1713                         sum += mpcpu->mntp_ref;
1714                         break;
1715                 case MNT_COUNT_LOCKREF:
1716                         sum += mpcpu->mntp_lockref;
1717                         break;
1718                 case MNT_COUNT_WRITEOPCOUNT:
1719                         sum += mpcpu->mntp_writeopcount;
1720                         break;
1721                 }
1722         }
1723         return (sum);
1724 }
1725
1726 /*
1727  * Do the actual filesystem unmount.
1728  */
1729 int
1730 dounmount(struct mount *mp, int flags, struct thread *td)
1731 {
1732         struct vnode *coveredvp, *rootvp;
1733         int error;
1734         uint64_t async_flag;
1735         int mnt_gen_r;
1736
1737         if ((coveredvp = mp->mnt_vnodecovered) != NULL) {
1738                 mnt_gen_r = mp->mnt_gen;
1739                 VI_LOCK(coveredvp);
1740                 vholdl(coveredvp);
1741                 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
1742                 /*
1743                  * Check for mp being unmounted while waiting for the
1744                  * covered vnode lock.
1745                  */
1746                 if (coveredvp->v_mountedhere != mp ||
1747                     coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) {
1748                         VOP_UNLOCK(coveredvp);
1749                         vdrop(coveredvp);
1750                         vfs_rel(mp);
1751                         return (EBUSY);
1752                 }
1753         }
1754
1755         /*
1756          * Only privileged root, or (if MNT_USER is set) the user that did the
1757          * original mount is permitted to unmount this filesystem.
1758          */
1759         error = vfs_suser(mp, td);
1760         if (error != 0) {
1761                 if (coveredvp != NULL) {
1762                         VOP_UNLOCK(coveredvp);
1763                         vdrop(coveredvp);
1764                 }
1765                 vfs_rel(mp);
1766                 return (error);
1767         }
1768
1769         vfs_op_enter(mp);
1770
1771         vn_start_write(NULL, &mp, V_WAIT | V_MNTREF);
1772         MNT_ILOCK(mp);
1773         if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 ||
1774             (mp->mnt_flag & MNT_UPDATE) != 0 ||
1775             !TAILQ_EMPTY(&mp->mnt_uppers)) {
1776                 dounmount_cleanup(mp, coveredvp, 0);
1777                 return (EBUSY);
1778         }
1779         mp->mnt_kern_flag |= MNTK_UNMOUNT;
1780         rootvp = vfs_cache_root_clear(mp);
1781         if (coveredvp != NULL)
1782                 vn_seqc_write_begin(coveredvp);
1783         if (flags & MNT_NONBUSY) {
1784                 MNT_IUNLOCK(mp);
1785                 error = vfs_check_usecounts(mp);
1786                 MNT_ILOCK(mp);
1787                 if (error != 0) {
1788                         vn_seqc_write_end(coveredvp);
1789                         dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT);
1790                         if (rootvp != NULL) {
1791                                 vn_seqc_write_end(rootvp);
1792                                 vrele(rootvp);
1793                         }
1794                         return (error);
1795                 }
1796         }
1797         /* Allow filesystems to detect that a forced unmount is in progress. */
1798         if (flags & MNT_FORCE) {
1799                 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
1800                 MNT_IUNLOCK(mp);
1801                 /*
1802                  * Must be done after setting MNTK_UNMOUNTF and before
1803                  * waiting for mnt_lockref to become 0.
1804                  */
1805                 VFS_PURGE(mp);
1806                 MNT_ILOCK(mp);
1807         }
1808         error = 0;
1809         if (mp->mnt_lockref) {
1810                 mp->mnt_kern_flag |= MNTK_DRAINING;
1811                 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS,
1812                     "mount drain", 0);
1813         }
1814         MNT_IUNLOCK(mp);
1815         KASSERT(mp->mnt_lockref == 0,
1816             ("%s: invalid lock refcount in the drain path @ %s:%d",
1817             __func__, __FILE__, __LINE__));
1818         KASSERT(error == 0,
1819             ("%s: invalid return value for msleep in the drain path @ %s:%d",
1820             __func__, __FILE__, __LINE__));
1821
1822         /*
1823          * We want to keep the vnode around so that we can vn_seqc_write_end
1824          * after we are done with unmount. Downgrade our reference to a mere
1825          * hold count so that we don't interefere with anything.
1826          */
1827         if (rootvp != NULL) {
1828                 vhold(rootvp);
1829                 vrele(rootvp);
1830         }
1831
1832         if (mp->mnt_flag & MNT_EXPUBLIC)
1833                 vfs_setpublicfs(NULL, NULL, NULL);
1834
1835         vfs_periodic(mp, MNT_WAIT);
1836         MNT_ILOCK(mp);
1837         async_flag = mp->mnt_flag & MNT_ASYNC;
1838         mp->mnt_flag &= ~MNT_ASYNC;
1839         mp->mnt_kern_flag &= ~MNTK_ASYNC;
1840         MNT_IUNLOCK(mp);
1841         vfs_deallocate_syncvnode(mp);
1842         error = VFS_UNMOUNT(mp, flags);
1843         vn_finished_write(mp);
1844         /*
1845          * If we failed to flush the dirty blocks for this mount point,
1846          * undo all the cdir/rdir and rootvnode changes we made above.
1847          * Unless we failed to do so because the device is reporting that
1848          * it doesn't exist anymore.
1849          */
1850         if (error && error != ENXIO) {
1851                 MNT_ILOCK(mp);
1852                 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1853                         MNT_IUNLOCK(mp);
1854                         vfs_allocate_syncvnode(mp);
1855                         MNT_ILOCK(mp);
1856                 }
1857                 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
1858                 mp->mnt_flag |= async_flag;
1859                 if ((mp->mnt_flag & MNT_ASYNC) != 0 &&
1860                     (mp->mnt_kern_flag & MNTK_NOASYNC) == 0)
1861                         mp->mnt_kern_flag |= MNTK_ASYNC;
1862                 if (mp->mnt_kern_flag & MNTK_MWAIT) {
1863                         mp->mnt_kern_flag &= ~MNTK_MWAIT;
1864                         wakeup(mp);
1865                 }
1866                 vfs_op_exit_locked(mp);
1867                 MNT_IUNLOCK(mp);
1868                 if (coveredvp) {
1869                         vn_seqc_write_end(coveredvp);
1870                         VOP_UNLOCK(coveredvp);
1871                         vdrop(coveredvp);
1872                 }
1873                 if (rootvp != NULL) {
1874                         vn_seqc_write_end(rootvp);
1875                         vdrop(rootvp);
1876                 }
1877                 return (error);
1878         }
1879         mtx_lock(&mountlist_mtx);
1880         TAILQ_REMOVE(&mountlist, mp, mnt_list);
1881         mtx_unlock(&mountlist_mtx);
1882         EVENTHANDLER_DIRECT_INVOKE(vfs_unmounted, mp, td);
1883         if (coveredvp != NULL) {
1884                 coveredvp->v_mountedhere = NULL;
1885                 vn_seqc_write_end(coveredvp);
1886                 VOP_UNLOCK(coveredvp);
1887                 vdrop(coveredvp);
1888         }
1889         mount_devctl_event("UNMOUNT", mp, false);
1890         if (rootvp != NULL) {
1891                 vn_seqc_write_end(rootvp);
1892                 vdrop(rootvp);
1893         }
1894         vfs_event_signal(NULL, VQ_UNMOUNT, 0);
1895         if (rootvnode != NULL && mp == rootvnode->v_mount) {
1896                 vrele(rootvnode);
1897                 rootvnode = NULL;
1898         }
1899         if (mp == rootdevmp)
1900                 rootdevmp = NULL;
1901         vfs_mount_destroy(mp);
1902         return (0);
1903 }
1904
1905 /*
1906  * Report errors during filesystem mounting.
1907  */
1908 void
1909 vfs_mount_error(struct mount *mp, const char *fmt, ...)
1910 {
1911         struct vfsoptlist *moptlist = mp->mnt_optnew;
1912         va_list ap;
1913         int error, len;
1914         char *errmsg;
1915
1916         error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len);
1917         if (error || errmsg == NULL || len <= 0)
1918                 return;
1919
1920         va_start(ap, fmt);
1921         vsnprintf(errmsg, (size_t)len, fmt, ap);
1922         va_end(ap);
1923 }
1924
1925 void
1926 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...)
1927 {
1928         va_list ap;
1929         int error, len;
1930         char *errmsg;
1931
1932         error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len);
1933         if (error || errmsg == NULL || len <= 0)
1934                 return;
1935
1936         va_start(ap, fmt);
1937         vsnprintf(errmsg, (size_t)len, fmt, ap);
1938         va_end(ap);
1939 }
1940
1941 /*
1942  * ---------------------------------------------------------------------
1943  * Functions for querying mount options/arguments from filesystems.
1944  */
1945
1946 /*
1947  * Check that no unknown options are given
1948  */
1949 int
1950 vfs_filteropt(struct vfsoptlist *opts, const char **legal)
1951 {
1952         struct vfsopt *opt;
1953         char errmsg[255];
1954         const char **t, *p, *q;
1955         int ret = 0;
1956
1957         TAILQ_FOREACH(opt, opts, link) {
1958                 p = opt->name;
1959                 q = NULL;
1960                 if (p[0] == 'n' && p[1] == 'o')
1961                         q = p + 2;
1962                 for(t = global_opts; *t != NULL; t++) {
1963                         if (strcmp(*t, p) == 0)
1964                                 break;
1965                         if (q != NULL) {
1966                                 if (strcmp(*t, q) == 0)
1967                                         break;
1968                         }
1969                 }
1970                 if (*t != NULL)
1971                         continue;
1972                 for(t = legal; *t != NULL; t++) {
1973                         if (strcmp(*t, p) == 0)
1974                                 break;
1975                         if (q != NULL) {
1976                                 if (strcmp(*t, q) == 0)
1977                                         break;
1978                         }
1979                 }
1980                 if (*t != NULL)
1981                         continue;
1982                 snprintf(errmsg, sizeof(errmsg),
1983                     "mount option <%s> is unknown", p);
1984                 ret = EINVAL;
1985         }
1986         if (ret != 0) {
1987                 TAILQ_FOREACH(opt, opts, link) {
1988                         if (strcmp(opt->name, "errmsg") == 0) {
1989                                 strncpy((char *)opt->value, errmsg, opt->len);
1990                                 break;
1991                         }
1992                 }
1993                 if (opt == NULL)
1994                         printf("%s\n", errmsg);
1995         }
1996         return (ret);
1997 }
1998
1999 /*
2000  * Get a mount option by its name.
2001  *
2002  * Return 0 if the option was found, ENOENT otherwise.
2003  * If len is non-NULL it will be filled with the length
2004  * of the option. If buf is non-NULL, it will be filled
2005  * with the address of the option.
2006  */
2007 int
2008 vfs_getopt(struct vfsoptlist *opts, const char *name, void **buf, int *len)
2009 {
2010         struct vfsopt *opt;
2011
2012         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2013
2014         TAILQ_FOREACH(opt, opts, link) {
2015                 if (strcmp(name, opt->name) == 0) {
2016                         opt->seen = 1;
2017                         if (len != NULL)
2018                                 *len = opt->len;
2019                         if (buf != NULL)
2020                                 *buf = opt->value;
2021                         return (0);
2022                 }
2023         }
2024         return (ENOENT);
2025 }
2026
2027 int
2028 vfs_getopt_pos(struct vfsoptlist *opts, const char *name)
2029 {
2030         struct vfsopt *opt;
2031
2032         if (opts == NULL)
2033                 return (-1);
2034
2035         TAILQ_FOREACH(opt, opts, link) {
2036                 if (strcmp(name, opt->name) == 0) {
2037                         opt->seen = 1;
2038                         return (opt->pos);
2039                 }
2040         }
2041         return (-1);
2042 }
2043
2044 int
2045 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value)
2046 {
2047         char *opt_value, *vtp;
2048         quad_t iv;
2049         int error, opt_len;
2050
2051         error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len);
2052         if (error != 0)
2053                 return (error);
2054         if (opt_len == 0 || opt_value == NULL)
2055                 return (EINVAL);
2056         if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0')
2057                 return (EINVAL);
2058         iv = strtoq(opt_value, &vtp, 0);
2059         if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0'))
2060                 return (EINVAL);
2061         if (iv < 0)
2062                 return (EINVAL);
2063         switch (vtp[0]) {
2064         case 't': case 'T':
2065                 iv *= 1024;
2066                 /* FALLTHROUGH */
2067         case 'g': case 'G':
2068                 iv *= 1024;
2069                 /* FALLTHROUGH */
2070         case 'm': case 'M':
2071                 iv *= 1024;
2072                 /* FALLTHROUGH */
2073         case 'k': case 'K':
2074                 iv *= 1024;
2075         case '\0':
2076                 break;
2077         default:
2078                 return (EINVAL);
2079         }
2080         *value = iv;
2081
2082         return (0);
2083 }
2084
2085 char *
2086 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error)
2087 {
2088         struct vfsopt *opt;
2089
2090         *error = 0;
2091         TAILQ_FOREACH(opt, opts, link) {
2092                 if (strcmp(name, opt->name) != 0)
2093                         continue;
2094                 opt->seen = 1;
2095                 if (opt->len == 0 ||
2096                     ((char *)opt->value)[opt->len - 1] != '\0') {
2097                         *error = EINVAL;
2098                         return (NULL);
2099                 }
2100                 return (opt->value);
2101         }
2102         *error = ENOENT;
2103         return (NULL);
2104 }
2105
2106 int
2107 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w,
2108         uint64_t val)
2109 {
2110         struct vfsopt *opt;
2111
2112         TAILQ_FOREACH(opt, opts, link) {
2113                 if (strcmp(name, opt->name) == 0) {
2114                         opt->seen = 1;
2115                         if (w != NULL)
2116                                 *w |= val;
2117                         return (1);
2118                 }
2119         }
2120         if (w != NULL)
2121                 *w &= ~val;
2122         return (0);
2123 }
2124
2125 int
2126 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...)
2127 {
2128         va_list ap;
2129         struct vfsopt *opt;
2130         int ret;
2131
2132         KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL"));
2133
2134         TAILQ_FOREACH(opt, opts, link) {
2135                 if (strcmp(name, opt->name) != 0)
2136                         continue;
2137                 opt->seen = 1;
2138                 if (opt->len == 0 || opt->value == NULL)
2139                         return (0);
2140                 if (((char *)opt->value)[opt->len - 1] != '\0')
2141                         return (0);
2142                 va_start(ap, fmt);
2143                 ret = vsscanf(opt->value, fmt, ap);
2144                 va_end(ap);
2145                 return (ret);
2146         }
2147         return (0);
2148 }
2149
2150 int
2151 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len)
2152 {
2153         struct vfsopt *opt;
2154
2155         TAILQ_FOREACH(opt, opts, link) {
2156                 if (strcmp(name, opt->name) != 0)
2157                         continue;
2158                 opt->seen = 1;
2159                 if (opt->value == NULL)
2160                         opt->len = len;
2161                 else {
2162                         if (opt->len != len)
2163                                 return (EINVAL);
2164                         bcopy(value, opt->value, len);
2165                 }
2166                 return (0);
2167         }
2168         return (ENOENT);
2169 }
2170
2171 int
2172 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len)
2173 {
2174         struct vfsopt *opt;
2175
2176         TAILQ_FOREACH(opt, opts, link) {
2177                 if (strcmp(name, opt->name) != 0)
2178                         continue;
2179                 opt->seen = 1;
2180                 if (opt->value == NULL)
2181                         opt->len = len;
2182                 else {
2183                         if (opt->len < len)
2184                                 return (EINVAL);
2185                         opt->len = len;
2186                         bcopy(value, opt->value, len);
2187                 }
2188                 return (0);
2189         }
2190         return (ENOENT);
2191 }
2192
2193 int
2194 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value)
2195 {
2196         struct vfsopt *opt;
2197
2198         TAILQ_FOREACH(opt, opts, link) {
2199                 if (strcmp(name, opt->name) != 0)
2200                         continue;
2201                 opt->seen = 1;
2202                 if (opt->value == NULL)
2203                         opt->len = strlen(value) + 1;
2204                 else if (strlcpy(opt->value, value, opt->len) >= opt->len)
2205                         return (EINVAL);
2206                 return (0);
2207         }
2208         return (ENOENT);
2209 }
2210
2211 /*
2212  * Find and copy a mount option.
2213  *
2214  * The size of the buffer has to be specified
2215  * in len, if it is not the same length as the
2216  * mount option, EINVAL is returned.
2217  * Returns ENOENT if the option is not found.
2218  */
2219 int
2220 vfs_copyopt(struct vfsoptlist *opts, const char *name, void *dest, int len)
2221 {
2222         struct vfsopt *opt;
2223
2224         KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL"));
2225
2226         TAILQ_FOREACH(opt, opts, link) {
2227                 if (strcmp(name, opt->name) == 0) {
2228                         opt->seen = 1;
2229                         if (len != opt->len)
2230                                 return (EINVAL);
2231                         bcopy(opt->value, dest, opt->len);
2232                         return (0);
2233                 }
2234         }
2235         return (ENOENT);
2236 }
2237
2238 int
2239 __vfs_statfs(struct mount *mp, struct statfs *sbp)
2240 {
2241
2242         /*
2243          * Filesystems only fill in part of the structure for updates, we
2244          * have to read the entirety first to get all content.
2245          */
2246         if (sbp != &mp->mnt_stat)
2247                 memcpy(sbp, &mp->mnt_stat, sizeof(*sbp));
2248
2249         /*
2250          * Set these in case the underlying filesystem fails to do so.
2251          */
2252         sbp->f_version = STATFS_VERSION;
2253         sbp->f_namemax = NAME_MAX;
2254         sbp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
2255
2256         return (mp->mnt_op->vfs_statfs(mp, sbp));
2257 }
2258
2259 void
2260 vfs_mountedfrom(struct mount *mp, const char *from)
2261 {
2262
2263         bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname);
2264         strlcpy(mp->mnt_stat.f_mntfromname, from,
2265             sizeof mp->mnt_stat.f_mntfromname);
2266 }
2267
2268 /*
2269  * ---------------------------------------------------------------------
2270  * This is the api for building mount args and mounting filesystems from
2271  * inside the kernel.
2272  *
2273  * The API works by accumulation of individual args.  First error is
2274  * latched.
2275  *
2276  * XXX: should be documented in new manpage kernel_mount(9)
2277  */
2278
2279 /* A memory allocation which must be freed when we are done */
2280 struct mntaarg {
2281         SLIST_ENTRY(mntaarg)    next;
2282 };
2283
2284 /* The header for the mount arguments */
2285 struct mntarg {
2286         struct iovec *v;
2287         int len;
2288         int error;
2289         SLIST_HEAD(, mntaarg)   list;
2290 };
2291
2292 /*
2293  * Add a boolean argument.
2294  *
2295  * flag is the boolean value.
2296  * name must start with "no".
2297  */
2298 struct mntarg *
2299 mount_argb(struct mntarg *ma, int flag, const char *name)
2300 {
2301
2302         KASSERT(name[0] == 'n' && name[1] == 'o',
2303             ("mount_argb(...,%s): name must start with 'no'", name));
2304
2305         return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0));
2306 }
2307
2308 /*
2309  * Add an argument printf style
2310  */
2311 struct mntarg *
2312 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...)
2313 {
2314         va_list ap;
2315         struct mntaarg *maa;
2316         struct sbuf *sb;
2317         int len;
2318
2319         if (ma == NULL) {
2320                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2321                 SLIST_INIT(&ma->list);
2322         }
2323         if (ma->error)
2324                 return (ma);
2325
2326         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2327             M_MOUNT, M_WAITOK);
2328         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2329         ma->v[ma->len].iov_len = strlen(name) + 1;
2330         ma->len++;
2331
2332         sb = sbuf_new_auto();
2333         va_start(ap, fmt);
2334         sbuf_vprintf(sb, fmt, ap);
2335         va_end(ap);
2336         sbuf_finish(sb);
2337         len = sbuf_len(sb) + 1;
2338         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2339         SLIST_INSERT_HEAD(&ma->list, maa, next);
2340         bcopy(sbuf_data(sb), maa + 1, len);
2341         sbuf_delete(sb);
2342
2343         ma->v[ma->len].iov_base = maa + 1;
2344         ma->v[ma->len].iov_len = len;
2345         ma->len++;
2346
2347         return (ma);
2348 }
2349
2350 /*
2351  * Add an argument which is a userland string.
2352  */
2353 struct mntarg *
2354 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len)
2355 {
2356         struct mntaarg *maa;
2357         char *tbuf;
2358
2359         if (val == NULL)
2360                 return (ma);
2361         if (ma == NULL) {
2362                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2363                 SLIST_INIT(&ma->list);
2364         }
2365         if (ma->error)
2366                 return (ma);
2367         maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO);
2368         SLIST_INSERT_HEAD(&ma->list, maa, next);
2369         tbuf = (void *)(maa + 1);
2370         ma->error = copyinstr(val, tbuf, len, NULL);
2371         return (mount_arg(ma, name, tbuf, -1));
2372 }
2373
2374 /*
2375  * Plain argument.
2376  *
2377  * If length is -1, treat value as a C string.
2378  */
2379 struct mntarg *
2380 mount_arg(struct mntarg *ma, const char *name, const void *val, int len)
2381 {
2382
2383         if (ma == NULL) {
2384                 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO);
2385                 SLIST_INIT(&ma->list);
2386         }
2387         if (ma->error)
2388                 return (ma);
2389
2390         ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2),
2391             M_MOUNT, M_WAITOK);
2392         ma->v[ma->len].iov_base = (void *)(uintptr_t)name;
2393         ma->v[ma->len].iov_len = strlen(name) + 1;
2394         ma->len++;
2395
2396         ma->v[ma->len].iov_base = (void *)(uintptr_t)val;
2397         if (len < 0)
2398                 ma->v[ma->len].iov_len = strlen(val) + 1;
2399         else
2400                 ma->v[ma->len].iov_len = len;
2401         ma->len++;
2402         return (ma);
2403 }
2404
2405 /*
2406  * Free a mntarg structure
2407  */
2408 static void
2409 free_mntarg(struct mntarg *ma)
2410 {
2411         struct mntaarg *maa;
2412
2413         while (!SLIST_EMPTY(&ma->list)) {
2414                 maa = SLIST_FIRST(&ma->list);
2415                 SLIST_REMOVE_HEAD(&ma->list, next);
2416                 free(maa, M_MOUNT);
2417         }
2418         free(ma->v, M_MOUNT);
2419         free(ma, M_MOUNT);
2420 }
2421
2422 /*
2423  * Mount a filesystem
2424  */
2425 int
2426 kernel_mount(struct mntarg *ma, uint64_t flags)
2427 {
2428         struct uio auio;
2429         int error;
2430
2431         KASSERT(ma != NULL, ("kernel_mount NULL ma"));
2432         KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v"));
2433         KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len));
2434
2435         auio.uio_iov = ma->v;
2436         auio.uio_iovcnt = ma->len;
2437         auio.uio_segflg = UIO_SYSSPACE;
2438
2439         error = ma->error;
2440         if (!error)
2441                 error = vfs_donmount(curthread, flags, &auio);
2442         free_mntarg(ma);
2443         return (error);
2444 }
2445
2446 /*
2447  * A printflike function to mount a filesystem.
2448  */
2449 int
2450 kernel_vmount(int flags, ...)
2451 {
2452         struct mntarg *ma = NULL;
2453         va_list ap;
2454         const char *cp;
2455         const void *vp;
2456         int error;
2457
2458         va_start(ap, flags);
2459         for (;;) {
2460                 cp = va_arg(ap, const char *);
2461                 if (cp == NULL)
2462                         break;
2463                 vp = va_arg(ap, const void *);
2464                 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0));
2465         }
2466         va_end(ap);
2467
2468         error = kernel_mount(ma, flags);
2469         return (error);
2470 }
2471
2472 /* Map from mount options to printable formats. */
2473 static struct mntoptnames optnames[] = {
2474         MNTOPT_NAMES
2475 };
2476
2477 static void
2478 mount_devctl_event_mntopt(struct sbuf *sb, const char *what, struct vfsoptlist *opts)
2479 {
2480         struct vfsopt *opt;
2481
2482         if (opts == NULL || TAILQ_EMPTY(opts))
2483                 return;
2484         sbuf_printf(sb, " %s=\"", what);
2485         TAILQ_FOREACH(opt, opts, link) {
2486                 if (opt->name[0] == '\0' || (opt->len > 0 && *(char *)opt->value == '\0'))
2487                         continue;
2488                 devctl_safe_quote_sb(sb, opt->name);
2489                 if (opt->len > 0) {
2490                         sbuf_putc(sb, '=');
2491                         devctl_safe_quote_sb(sb, opt->value);
2492                 }
2493                 sbuf_putc(sb, ';');
2494         }
2495         sbuf_putc(sb, '"');
2496 }
2497
2498 #define DEVCTL_LEN 1024
2499 static void
2500 mount_devctl_event(const char *type, struct mount *mp, bool donew)
2501 {
2502         const uint8_t *cp;
2503         struct mntoptnames *fp;
2504         struct sbuf sb;
2505         struct statfs *sfp = &mp->mnt_stat;
2506         char *buf;
2507
2508         buf = malloc(DEVCTL_LEN, M_MOUNT, M_NOWAIT);
2509         if (buf == NULL)
2510                 return;
2511         sbuf_new(&sb, buf, DEVCTL_LEN, SBUF_FIXEDLEN);
2512         sbuf_cpy(&sb, "mount-point=\"");
2513         devctl_safe_quote_sb(&sb, sfp->f_mntonname);
2514         sbuf_cat(&sb, "\" mount-dev=\"");
2515         devctl_safe_quote_sb(&sb, sfp->f_mntfromname);
2516         sbuf_cat(&sb, "\" mount-type=\"");
2517         devctl_safe_quote_sb(&sb, sfp->f_fstypename);
2518         sbuf_cat(&sb, "\" fsid=0x");
2519         cp = (const uint8_t *)&sfp->f_fsid.val[0];
2520         for (int i = 0; i < sizeof(sfp->f_fsid); i++)
2521                 sbuf_printf(&sb, "%02x", cp[i]);
2522         sbuf_printf(&sb, " owner=%u flags=\"", sfp->f_owner);
2523         for (fp = optnames; fp->o_opt != 0; fp++) {
2524                 if ((mp->mnt_flag & fp->o_opt) != 0) {
2525                         sbuf_cat(&sb, fp->o_name);
2526                         sbuf_putc(&sb, ';');
2527                 }
2528         }
2529         sbuf_putc(&sb, '"');
2530         mount_devctl_event_mntopt(&sb, "opt", mp->mnt_opt);
2531         if (donew)
2532                 mount_devctl_event_mntopt(&sb, "optnew", mp->mnt_optnew);
2533         sbuf_finish(&sb);
2534
2535         if (sbuf_error(&sb) == 0)
2536                 devctl_notify("VFS", "FS", type, sbuf_data(&sb));
2537         sbuf_delete(&sb);
2538         free(buf, M_MOUNT);
2539 }
2540
2541 /*
2542  * Suspend write operations on all local writeable filesystems.  Does
2543  * full sync of them in the process.
2544  *
2545  * Iterate over the mount points in reverse order, suspending most
2546  * recently mounted filesystems first.  It handles a case where a
2547  * filesystem mounted from a md(4) vnode-backed device should be
2548  * suspended before the filesystem that owns the vnode.
2549  */
2550 void
2551 suspend_all_fs(void)
2552 {
2553         struct mount *mp;
2554         int error;
2555
2556         mtx_lock(&mountlist_mtx);
2557         TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
2558                 error = vfs_busy(mp, MBF_MNTLSTLOCK | MBF_NOWAIT);
2559                 if (error != 0)
2560                         continue;
2561                 if ((mp->mnt_flag & (MNT_RDONLY | MNT_LOCAL)) != MNT_LOCAL ||
2562                     (mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
2563                         mtx_lock(&mountlist_mtx);
2564                         vfs_unbusy(mp);
2565                         continue;
2566                 }
2567                 error = vfs_write_suspend(mp, 0);
2568                 if (error == 0) {
2569                         MNT_ILOCK(mp);
2570                         MPASS((mp->mnt_kern_flag & MNTK_SUSPEND_ALL) == 0);
2571                         mp->mnt_kern_flag |= MNTK_SUSPEND_ALL;
2572                         MNT_IUNLOCK(mp);
2573                         mtx_lock(&mountlist_mtx);
2574                 } else {
2575                         printf("suspend of %s failed, error %d\n",
2576                             mp->mnt_stat.f_mntonname, error);
2577                         mtx_lock(&mountlist_mtx);
2578                         vfs_unbusy(mp);
2579                 }
2580         }
2581         mtx_unlock(&mountlist_mtx);
2582 }
2583
2584 void
2585 resume_all_fs(void)
2586 {
2587         struct mount *mp;
2588
2589         mtx_lock(&mountlist_mtx);
2590         TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2591                 if ((mp->mnt_kern_flag & MNTK_SUSPEND_ALL) == 0)
2592                         continue;
2593                 mtx_unlock(&mountlist_mtx);
2594                 MNT_ILOCK(mp);
2595                 MPASS((mp->mnt_kern_flag & MNTK_SUSPEND) != 0);
2596                 mp->mnt_kern_flag &= ~MNTK_SUSPEND_ALL;
2597                 MNT_IUNLOCK(mp);
2598                 vfs_write_resume(mp, 0);
2599                 mtx_lock(&mountlist_mtx);
2600                 vfs_unbusy(mp);
2601         }
2602         mtx_unlock(&mountlist_mtx);
2603 }