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