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