1 /* $NetBSD: vfs_mount.c,v 1.102 2023/02/24 11:02:27 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 1997-2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1989, 1993
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.102 2023/02/24 11:02:27 riastradh Exp $");
71
72 #include <sys/param.h>
73 #include <sys/kernel.h>
74
75 #include <sys/atomic.h>
76 #include <sys/buf.h>
77 #include <sys/conf.h>
78 #include <sys/fcntl.h>
79 #include <sys/filedesc.h>
80 #include <sys/device.h>
81 #include <sys/kauth.h>
82 #include <sys/kmem.h>
83 #include <sys/module.h>
84 #include <sys/mount.h>
85 #include <sys/fstrans.h>
86 #include <sys/namei.h>
87 #include <sys/extattr.h>
88 #include <sys/syscallargs.h>
89 #include <sys/sysctl.h>
90 #include <sys/systm.h>
91 #include <sys/vfs_syscalls.h>
92 #include <sys/vnode_impl.h>
93
94 #include <miscfs/deadfs/deadfs.h>
95 #include <miscfs/genfs/genfs.h>
96 #include <miscfs/specfs/specdev.h>
97
98 #include <uvm/uvm_swap.h>
99
100 enum mountlist_type {
101 ME_MOUNT,
102 ME_MARKER
103 };
104 struct mountlist_entry {
105 TAILQ_ENTRY(mountlist_entry) me_list; /* Mount list. */
106 struct mount *me_mount; /* Actual mount if ME_MOUNT,
107 current mount else. */
108 enum mountlist_type me_type; /* Mount or marker. */
109 };
110 struct mount_iterator {
111 struct mountlist_entry mi_entry;
112 };
113
114 static struct vnode *vfs_vnode_iterator_next1(struct vnode_iterator *,
115 bool (*)(void *, struct vnode *), void *, bool);
116
117 /* Root filesystem. */
118 vnode_t * rootvnode;
119
120 /* Mounted filesystem list. */
121 static TAILQ_HEAD(mountlist, mountlist_entry) mountlist;
122 static kmutex_t mountlist_lock __cacheline_aligned;
123 int vnode_offset_next_by_lru /* XXX: ugly hack for pstat.c */
124 = offsetof(vnode_impl_t, vi_lrulist.tqe_next);
125
126 kmutex_t vfs_list_lock __cacheline_aligned;
127
128 static specificdata_domain_t mount_specificdata_domain;
129 static kmutex_t mntid_lock;
130
131 static kmutex_t mountgen_lock __cacheline_aligned;
132 static uint64_t mountgen;
133
134 void
vfs_mount_sysinit(void)135 vfs_mount_sysinit(void)
136 {
137
138 TAILQ_INIT(&mountlist);
139 mutex_init(&mountlist_lock, MUTEX_DEFAULT, IPL_NONE);
140 mutex_init(&vfs_list_lock, MUTEX_DEFAULT, IPL_NONE);
141
142 mount_specificdata_domain = specificdata_domain_create();
143 mutex_init(&mntid_lock, MUTEX_DEFAULT, IPL_NONE);
144 mutex_init(&mountgen_lock, MUTEX_DEFAULT, IPL_NONE);
145 mountgen = 0;
146 }
147
148 struct mount *
vfs_mountalloc(struct vfsops * vfsops,vnode_t * vp)149 vfs_mountalloc(struct vfsops *vfsops, vnode_t *vp)
150 {
151 struct mount *mp;
152 int error __diagused;
153
154 mp = kmem_zalloc(sizeof(*mp), KM_SLEEP);
155 mp->mnt_op = vfsops;
156 mp->mnt_refcnt = 1;
157 TAILQ_INIT(&mp->mnt_vnodelist);
158 mp->mnt_renamelock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
159 mp->mnt_vnodelock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
160 mp->mnt_updating = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
161 mp->mnt_vnodecovered = vp;
162 mount_initspecific(mp);
163
164 error = fstrans_mount(mp);
165 KASSERT(error == 0);
166
167 mutex_enter(&mountgen_lock);
168 mp->mnt_gen = mountgen++;
169 mutex_exit(&mountgen_lock);
170
171 return mp;
172 }
173
174 /*
175 * vfs_rootmountalloc: lookup a filesystem type, and if found allocate and
176 * initialize a mount structure for it.
177 *
178 * Devname is usually updated by mount(8) after booting.
179 */
180 int
vfs_rootmountalloc(const char * fstypename,const char * devname,struct mount ** mpp)181 vfs_rootmountalloc(const char *fstypename, const char *devname,
182 struct mount **mpp)
183 {
184 struct vfsops *vfsp = NULL;
185 struct mount *mp;
186 int error __diagused;
187
188 mutex_enter(&vfs_list_lock);
189 LIST_FOREACH(vfsp, &vfs_list, vfs_list)
190 if (!strncmp(vfsp->vfs_name, fstypename,
191 sizeof(mp->mnt_stat.f_fstypename)))
192 break;
193 if (vfsp == NULL) {
194 mutex_exit(&vfs_list_lock);
195 return (ENODEV);
196 }
197 vfsp->vfs_refcount++;
198 mutex_exit(&vfs_list_lock);
199
200 if ((mp = vfs_mountalloc(vfsp, NULL)) == NULL)
201 return ENOMEM;
202 error = vfs_busy(mp);
203 KASSERT(error == 0);
204 mp->mnt_flag = MNT_RDONLY;
205 (void)strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name,
206 sizeof(mp->mnt_stat.f_fstypename));
207 mp->mnt_stat.f_mntonname[0] = '/';
208 mp->mnt_stat.f_mntonname[1] = '\0';
209 mp->mnt_stat.f_mntfromname[sizeof(mp->mnt_stat.f_mntfromname) - 1] =
210 '\0';
211 (void)copystr(devname, mp->mnt_stat.f_mntfromname,
212 sizeof(mp->mnt_stat.f_mntfromname) - 1, 0);
213 *mpp = mp;
214 return 0;
215 }
216
217 /*
218 * vfs_getnewfsid: get a new unique fsid.
219 */
220 void
vfs_getnewfsid(struct mount * mp)221 vfs_getnewfsid(struct mount *mp)
222 {
223 static u_short xxxfs_mntid;
224 struct mountlist_entry *me;
225 fsid_t tfsid;
226 int mtype;
227
228 mutex_enter(&mntid_lock);
229 if (xxxfs_mntid == 0)
230 ++xxxfs_mntid;
231 mtype = makefstype(mp->mnt_op->vfs_name);
232 tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid);
233 tfsid.__fsid_val[1] = mtype;
234 /* Always increment to not return the same fsid to parallel mounts. */
235 xxxfs_mntid++;
236
237 /*
238 * Directly walk mountlist to prevent deadlock through
239 * mountlist_iterator_next() -> vfs_busy().
240 */
241 mutex_enter(&mountlist_lock);
242 for (me = TAILQ_FIRST(&mountlist); me != TAILQ_END(&mountlist); ) {
243 if (me->me_type == ME_MOUNT &&
244 me->me_mount->mnt_stat.f_fsidx.__fsid_val[0] ==
245 tfsid.__fsid_val[0] &&
246 me->me_mount->mnt_stat.f_fsidx.__fsid_val[1] ==
247 tfsid.__fsid_val[1]) {
248 tfsid.__fsid_val[0]++;
249 xxxfs_mntid++;
250 me = TAILQ_FIRST(&mountlist);
251 } else {
252 me = TAILQ_NEXT(me, me_list);
253 }
254 }
255 mutex_exit(&mountlist_lock);
256
257 mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0];
258 mp->mnt_stat.f_fsidx.__fsid_val[1] = tfsid.__fsid_val[1];
259 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
260 mutex_exit(&mntid_lock);
261 }
262
263 /*
264 * Lookup a mount point by filesystem identifier.
265 *
266 * XXX Needs to add a reference to the mount point.
267 */
268 struct mount *
vfs_getvfs(fsid_t * fsid)269 vfs_getvfs(fsid_t *fsid)
270 {
271 mount_iterator_t *iter;
272 struct mount *mp;
273
274 mountlist_iterator_init(&iter);
275 while ((mp = mountlist_iterator_next(iter)) != NULL) {
276 if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
277 mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
278 mountlist_iterator_destroy(iter);
279 return mp;
280 }
281 }
282 mountlist_iterator_destroy(iter);
283 return NULL;
284 }
285
286 /*
287 * Take a reference to a mount structure.
288 */
289 void
vfs_ref(struct mount * mp)290 vfs_ref(struct mount *mp)
291 {
292
293 KASSERT(mp->mnt_refcnt > 0 || mutex_owned(&mountlist_lock));
294
295 atomic_inc_uint(&mp->mnt_refcnt);
296 }
297
298 /*
299 * Drop a reference to a mount structure, freeing if the last reference.
300 */
301 void
vfs_rele(struct mount * mp)302 vfs_rele(struct mount *mp)
303 {
304
305 membar_release();
306 if (__predict_true((int)atomic_dec_uint_nv(&mp->mnt_refcnt) > 0)) {
307 return;
308 }
309 membar_acquire();
310
311 /*
312 * Nothing else has visibility of the mount: we can now
313 * free the data structures.
314 */
315 KASSERT(mp->mnt_refcnt == 0);
316 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
317 mutex_obj_free(mp->mnt_updating);
318 mutex_obj_free(mp->mnt_renamelock);
319 mutex_obj_free(mp->mnt_vnodelock);
320 if (mp->mnt_op != NULL) {
321 vfs_delref(mp->mnt_op);
322 }
323 fstrans_unmount(mp);
324 /*
325 * Final free of mp gets done from fstrans_mount_dtor().
326 *
327 * Prevents this memory to be reused as a mount before
328 * fstrans releases all references to it.
329 */
330 }
331
332 /*
333 * Mark a mount point as busy, and gain a new reference to it. Used to
334 * prevent the file system from being unmounted during critical sections.
335 *
336 * vfs_busy can be called multiple times and by multiple threads
337 * and must be accompanied by the same number of vfs_unbusy calls.
338 *
339 * => The caller must hold a pre-existing reference to the mount.
340 * => Will fail if the file system is being unmounted, or is unmounted.
341 */
342 static inline int
_vfs_busy(struct mount * mp,bool wait)343 _vfs_busy(struct mount *mp, bool wait)
344 {
345
346 KASSERT(mp->mnt_refcnt > 0);
347
348 if (wait) {
349 fstrans_start(mp);
350 } else {
351 if (fstrans_start_nowait(mp))
352 return EBUSY;
353 }
354 if (__predict_false((mp->mnt_iflag & IMNT_GONE) != 0)) {
355 fstrans_done(mp);
356 return ENOENT;
357 }
358 vfs_ref(mp);
359 return 0;
360 }
361
362 int
vfs_busy(struct mount * mp)363 vfs_busy(struct mount *mp)
364 {
365
366 return _vfs_busy(mp, true);
367 }
368
369 int
vfs_trybusy(struct mount * mp)370 vfs_trybusy(struct mount *mp)
371 {
372
373 return _vfs_busy(mp, false);
374 }
375
376 /*
377 * Unbusy a busy filesystem.
378 *
379 * Every successful vfs_busy() call must be undone by a vfs_unbusy() call.
380 */
381 void
vfs_unbusy(struct mount * mp)382 vfs_unbusy(struct mount *mp)
383 {
384
385 KASSERT(mp->mnt_refcnt > 0);
386
387 fstrans_done(mp);
388 vfs_rele(mp);
389 }
390
391 /*
392 * Change a file systems lower mount.
393 * Both the current and the new lower mount may be NULL. The caller
394 * guarantees exclusive access to the mount and holds a pre-existing
395 * reference to the new lower mount.
396 */
397 int
vfs_set_lowermount(struct mount * mp,struct mount * lowermp)398 vfs_set_lowermount(struct mount *mp, struct mount *lowermp)
399 {
400 struct mount *oldlowermp;
401 int error;
402
403 #ifdef DEBUG
404 /*
405 * Limit the depth of file system stack so kernel sanitizers
406 * may stress mount/unmount without exhausting the kernel stack.
407 */
408 int depth;
409 struct mount *mp2;
410
411 for (depth = 0, mp2 = lowermp; mp2; depth++, mp2 = mp2->mnt_lower) {
412 if (depth == 23)
413 return EINVAL;
414 }
415 #endif
416
417 if (lowermp) {
418 if (lowermp == dead_rootmount)
419 return ENOENT;
420 error = vfs_busy(lowermp);
421 if (error)
422 return error;
423 vfs_ref(lowermp);
424 }
425
426 oldlowermp = mp->mnt_lower;
427 mp->mnt_lower = lowermp;
428
429 if (lowermp)
430 vfs_unbusy(lowermp);
431
432 if (oldlowermp)
433 vfs_rele(oldlowermp);
434
435 return 0;
436 }
437
438 struct vnode_iterator {
439 vnode_impl_t vi_vnode;
440 };
441
442 void
vfs_vnode_iterator_init(struct mount * mp,struct vnode_iterator ** vnip)443 vfs_vnode_iterator_init(struct mount *mp, struct vnode_iterator **vnip)
444 {
445 vnode_t *vp;
446 vnode_impl_t *vip;
447
448 vp = vnalloc_marker(mp);
449 vip = VNODE_TO_VIMPL(vp);
450
451 mutex_enter(mp->mnt_vnodelock);
452 TAILQ_INSERT_HEAD(&mp->mnt_vnodelist, vip, vi_mntvnodes);
453 vp->v_usecount = 1;
454 mutex_exit(mp->mnt_vnodelock);
455
456 *vnip = (struct vnode_iterator *)vip;
457 }
458
459 void
vfs_vnode_iterator_destroy(struct vnode_iterator * vni)460 vfs_vnode_iterator_destroy(struct vnode_iterator *vni)
461 {
462 vnode_impl_t *mvip = &vni->vi_vnode;
463 vnode_t *mvp = VIMPL_TO_VNODE(mvip);
464 kmutex_t *lock;
465
466 KASSERT(vnis_marker(mvp));
467 if (vrefcnt(mvp) != 0) {
468 lock = mvp->v_mount->mnt_vnodelock;
469 mutex_enter(lock);
470 TAILQ_REMOVE(&mvp->v_mount->mnt_vnodelist, mvip, vi_mntvnodes);
471 mvp->v_usecount = 0;
472 mutex_exit(lock);
473 }
474 vnfree_marker(mvp);
475 }
476
477 static struct vnode *
vfs_vnode_iterator_next1(struct vnode_iterator * vni,bool (* f)(void *,struct vnode *),void * cl,bool do_wait)478 vfs_vnode_iterator_next1(struct vnode_iterator *vni,
479 bool (*f)(void *, struct vnode *), void *cl, bool do_wait)
480 {
481 vnode_impl_t *mvip = &vni->vi_vnode;
482 struct mount *mp = VIMPL_TO_VNODE(mvip)->v_mount;
483 vnode_t *vp;
484 vnode_impl_t *vip;
485 kmutex_t *lock;
486 int error;
487
488 KASSERT(vnis_marker(VIMPL_TO_VNODE(mvip)));
489
490 lock = mp->mnt_vnodelock;
491 do {
492 mutex_enter(lock);
493 vip = TAILQ_NEXT(mvip, vi_mntvnodes);
494 TAILQ_REMOVE(&mp->mnt_vnodelist, mvip, vi_mntvnodes);
495 VIMPL_TO_VNODE(mvip)->v_usecount = 0;
496 again:
497 if (vip == NULL) {
498 mutex_exit(lock);
499 return NULL;
500 }
501 vp = VIMPL_TO_VNODE(vip);
502 KASSERT(vp != NULL);
503 mutex_enter(vp->v_interlock);
504 if (vnis_marker(vp) ||
505 vdead_check(vp, (do_wait ? 0 : VDEAD_NOWAIT)) ||
506 (f && !(*f)(cl, vp))) {
507 mutex_exit(vp->v_interlock);
508 vip = TAILQ_NEXT(vip, vi_mntvnodes);
509 goto again;
510 }
511
512 TAILQ_INSERT_AFTER(&mp->mnt_vnodelist, vip, mvip, vi_mntvnodes);
513 VIMPL_TO_VNODE(mvip)->v_usecount = 1;
514 mutex_exit(lock);
515 error = vcache_vget(vp);
516 KASSERT(error == 0 || error == ENOENT);
517 } while (error != 0);
518
519 return vp;
520 }
521
522 struct vnode *
vfs_vnode_iterator_next(struct vnode_iterator * vni,bool (* f)(void *,struct vnode *),void * cl)523 vfs_vnode_iterator_next(struct vnode_iterator *vni,
524 bool (*f)(void *, struct vnode *), void *cl)
525 {
526
527 return vfs_vnode_iterator_next1(vni, f, cl, false);
528 }
529
530 /*
531 * Move a vnode from one mount queue to another.
532 */
533 void
vfs_insmntque(vnode_t * vp,struct mount * mp)534 vfs_insmntque(vnode_t *vp, struct mount *mp)
535 {
536 vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
537 struct mount *omp;
538 kmutex_t *lock;
539
540 KASSERT(mp == NULL || (mp->mnt_iflag & IMNT_UNMOUNT) == 0 ||
541 vp->v_tag == VT_VFS);
542
543 /*
544 * Delete from old mount point vnode list, if on one.
545 */
546 if ((omp = vp->v_mount) != NULL) {
547 lock = omp->mnt_vnodelock;
548 mutex_enter(lock);
549 TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vip, vi_mntvnodes);
550 mutex_exit(lock);
551 }
552
553 /*
554 * Insert into list of vnodes for the new mount point, if
555 * available. The caller must take a reference on the mount
556 * structure and donate to the vnode.
557 */
558 if ((vp->v_mount = mp) != NULL) {
559 lock = mp->mnt_vnodelock;
560 mutex_enter(lock);
561 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vip, vi_mntvnodes);
562 mutex_exit(lock);
563 }
564
565 if (omp != NULL) {
566 /* Release reference to old mount. */
567 vfs_rele(omp);
568 }
569 }
570
571 /*
572 * Remove any vnodes in the vnode table belonging to mount point mp.
573 *
574 * If FORCECLOSE is not specified, there should not be any active ones,
575 * return error if any are found (nb: this is a user error, not a
576 * system error). If FORCECLOSE is specified, detach any active vnodes
577 * that are found.
578 *
579 * If WRITECLOSE is set, only flush out regular file vnodes open for
580 * writing.
581 *
582 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
583 */
584 #ifdef DEBUG
585 int busyprt = 0; /* print out busy vnodes */
586 struct ctldebug debug1 = { "busyprt", &busyprt };
587 #endif
588
589 static vnode_t *
vflushnext(struct vnode_iterator * marker,int * when)590 vflushnext(struct vnode_iterator *marker, int *when)
591 {
592 if (getticks() > *when) {
593 yield();
594 *when = getticks() + hz / 10;
595 }
596 preempt_point();
597 return vfs_vnode_iterator_next1(marker, NULL, NULL, true);
598 }
599
600 /*
601 * Flush one vnode. Referenced on entry, unreferenced on return.
602 */
603 static int
vflush_one(vnode_t * vp,vnode_t * skipvp,int flags)604 vflush_one(vnode_t *vp, vnode_t *skipvp, int flags)
605 {
606 int error;
607 struct vattr vattr;
608
609 if (vp == skipvp ||
610 ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM))) {
611 vrele(vp);
612 return 0;
613 }
614 /*
615 * If WRITECLOSE is set, only flush out regular file
616 * vnodes open for writing or open and unlinked.
617 */
618 if ((flags & WRITECLOSE)) {
619 if (vp->v_type != VREG) {
620 vrele(vp);
621 return 0;
622 }
623 error = vn_lock(vp, LK_EXCLUSIVE);
624 if (error) {
625 KASSERT(error == ENOENT);
626 vrele(vp);
627 return 0;
628 }
629 error = VOP_FSYNC(vp, curlwp->l_cred, FSYNC_WAIT, 0, 0);
630 if (error == 0)
631 error = VOP_GETATTR(vp, &vattr, curlwp->l_cred);
632 VOP_UNLOCK(vp);
633 if (error) {
634 vrele(vp);
635 return error;
636 }
637 if (vp->v_writecount == 0 && vattr.va_nlink > 0) {
638 vrele(vp);
639 return 0;
640 }
641 }
642 /*
643 * First try to recycle the vnode.
644 */
645 if (vrecycle(vp))
646 return 0;
647 /*
648 * If FORCECLOSE is set, forcibly close the vnode.
649 * For block or character devices, revert to an
650 * anonymous device. For all other files, just
651 * kill them.
652 */
653 if (flags & FORCECLOSE) {
654 if (vrefcnt(vp) > 1 &&
655 (vp->v_type == VBLK || vp->v_type == VCHR))
656 vcache_make_anon(vp);
657 else
658 vgone(vp);
659 return 0;
660 }
661 vrele(vp);
662 return EBUSY;
663 }
664
665 int
vflush(struct mount * mp,vnode_t * skipvp,int flags)666 vflush(struct mount *mp, vnode_t *skipvp, int flags)
667 {
668 vnode_t *vp;
669 struct vnode_iterator *marker;
670 int busy, error, when, retries = 2;
671
672 do {
673 busy = error = when = 0;
674
675 /*
676 * First, flush out any vnode references from the
677 * deferred vrele list.
678 */
679 vrele_flush(mp);
680
681 vfs_vnode_iterator_init(mp, &marker);
682
683 while ((vp = vflushnext(marker, &when)) != NULL) {
684 error = vflush_one(vp, skipvp, flags);
685 if (error == EBUSY) {
686 error = 0;
687 busy++;
688 #ifdef DEBUG
689 if (busyprt && retries == 0)
690 vprint("vflush: busy vnode", vp);
691 #endif
692 } else if (error != 0) {
693 break;
694 }
695 }
696
697 vfs_vnode_iterator_destroy(marker);
698 } while (error == 0 && busy > 0 && retries-- > 0);
699
700 if (error)
701 return error;
702 if (busy)
703 return EBUSY;
704 return 0;
705 }
706
707 /*
708 * Mount a file system.
709 */
710
711 /*
712 * Scan all active processes to see if any of them have a current or root
713 * directory onto which the new filesystem has just been mounted. If so,
714 * replace them with the new mount point.
715 */
716 static void
mount_checkdirs(vnode_t * olddp)717 mount_checkdirs(vnode_t *olddp)
718 {
719 vnode_t *newdp, *rele1, *rele2;
720 struct cwdinfo *cwdi;
721 struct proc *p;
722 bool retry;
723
724 if (vrefcnt(olddp) == 1) {
725 return;
726 }
727 if (VFS_ROOT(olddp->v_mountedhere, LK_EXCLUSIVE, &newdp))
728 panic("mount: lost mount");
729
730 do {
731 retry = false;
732 mutex_enter(&proc_lock);
733 PROCLIST_FOREACH(p, &allproc) {
734 if ((cwdi = p->p_cwdi) == NULL)
735 continue;
736 /*
737 * Cannot change to the old directory any more,
738 * so even if we see a stale value it is not a
739 * problem.
740 */
741 if (cwdi->cwdi_cdir != olddp &&
742 cwdi->cwdi_rdir != olddp)
743 continue;
744 retry = true;
745 rele1 = NULL;
746 rele2 = NULL;
747 atomic_inc_uint(&cwdi->cwdi_refcnt);
748 mutex_exit(&proc_lock);
749 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
750 if (cwdi->cwdi_cdir == olddp) {
751 rele1 = cwdi->cwdi_cdir;
752 vref(newdp);
753 cwdi->cwdi_cdir = newdp;
754 }
755 if (cwdi->cwdi_rdir == olddp) {
756 rele2 = cwdi->cwdi_rdir;
757 vref(newdp);
758 cwdi->cwdi_rdir = newdp;
759 }
760 rw_exit(&cwdi->cwdi_lock);
761 cwdfree(cwdi);
762 if (rele1 != NULL)
763 vrele(rele1);
764 if (rele2 != NULL)
765 vrele(rele2);
766 mutex_enter(&proc_lock);
767 break;
768 }
769 mutex_exit(&proc_lock);
770 } while (retry);
771
772 if (rootvnode == olddp) {
773 vrele(rootvnode);
774 vref(newdp);
775 rootvnode = newdp;
776 }
777 vput(newdp);
778 }
779
780 /*
781 * Start extended attributes
782 */
783 static int
start_extattr(struct mount * mp)784 start_extattr(struct mount *mp)
785 {
786 int error;
787
788 error = VFS_EXTATTRCTL(mp, EXTATTR_CMD_START, NULL, 0, NULL);
789 if (error)
790 printf("%s: failed to start extattr: error = %d\n",
791 mp->mnt_stat.f_mntonname, error);
792
793 return error;
794 }
795
796 int
mount_domount(struct lwp * l,vnode_t ** vpp,struct vfsops * vfsops,const char * path,int flags,void * data,size_t * data_len)797 mount_domount(struct lwp *l, vnode_t **vpp, struct vfsops *vfsops,
798 const char *path, int flags, void *data, size_t *data_len)
799 {
800 vnode_t *vp = *vpp;
801 struct mount *mp;
802 struct pathbuf *pb;
803 struct nameidata nd;
804 int error, error2;
805
806 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
807 KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data);
808 if (error) {
809 vfs_delref(vfsops);
810 return error;
811 }
812
813 /* Cannot make a non-dir a mount-point (from here anyway). */
814 if (vp->v_type != VDIR) {
815 vfs_delref(vfsops);
816 return ENOTDIR;
817 }
818
819 if (flags & MNT_EXPORTED) {
820 vfs_delref(vfsops);
821 return EINVAL;
822 }
823
824 if ((mp = vfs_mountalloc(vfsops, vp)) == NULL) {
825 vfs_delref(vfsops);
826 return ENOMEM;
827 }
828
829 mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred);
830
831 /*
832 * The underlying file system may refuse the mount for
833 * various reasons. Allow the user to force it to happen.
834 *
835 * Set the mount level flags.
836 */
837 mp->mnt_flag = flags & (MNT_BASIC_FLAGS | MNT_FORCE | MNT_IGNORE);
838
839 error = VFS_MOUNT(mp, path, data, data_len);
840 mp->mnt_flag &= ~MNT_OP_FLAGS;
841
842 if (error != 0) {
843 vfs_rele(mp);
844 return error;
845 }
846
847 /* Suspend new file system before taking mnt_updating. */
848 do {
849 error2 = vfs_suspend(mp, 0);
850 } while (error2 == EINTR || error2 == ERESTART);
851 KASSERT(error2 == 0 || error2 == EOPNOTSUPP);
852 mutex_enter(mp->mnt_updating);
853
854 /*
855 * Validate and prepare the mount point.
856 */
857 error = pathbuf_copyin(path, &pb);
858 if (error != 0) {
859 goto err_mounted;
860 }
861 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
862 error = namei(&nd);
863 pathbuf_destroy(pb);
864 if (error != 0) {
865 goto err_mounted;
866 }
867 if (nd.ni_vp != vp) {
868 vput(nd.ni_vp);
869 error = EINVAL;
870 goto err_mounted;
871 }
872 if (vp->v_mountedhere != NULL) {
873 vput(nd.ni_vp);
874 error = EBUSY;
875 goto err_mounted;
876 }
877 error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0);
878 if (error != 0) {
879 vput(nd.ni_vp);
880 goto err_mounted;
881 }
882
883 /*
884 * Put the new filesystem on the mount list after root.
885 */
886 cache_purge(vp);
887 mp->mnt_iflag &= ~IMNT_WANTRDWR;
888
889 mountlist_append(mp);
890 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
891 vfs_syncer_add_to_worklist(mp);
892 vp->v_mountedhere = mp;
893 vput(nd.ni_vp);
894
895 mount_checkdirs(vp);
896 mutex_exit(mp->mnt_updating);
897 if (error2 == 0)
898 vfs_resume(mp);
899
900 /* Hold an additional reference to the mount across VFS_START(). */
901 vfs_ref(mp);
902 (void) VFS_STATVFS(mp, &mp->mnt_stat);
903 error = VFS_START(mp, 0);
904 if (error) {
905 vrele(vp);
906 } else if (flags & MNT_EXTATTR) {
907 if (start_extattr(mp) != 0)
908 mp->mnt_flag &= ~MNT_EXTATTR;
909 }
910 /* Drop reference held for VFS_START(). */
911 vfs_rele(mp);
912 *vpp = NULL;
913 return error;
914
915 err_mounted:
916 if (VFS_UNMOUNT(mp, MNT_FORCE) != 0)
917 panic("Unmounting fresh file system failed");
918 mutex_exit(mp->mnt_updating);
919 if (error2 == 0)
920 vfs_resume(mp);
921 vfs_set_lowermount(mp, NULL);
922 vfs_rele(mp);
923
924 return error;
925 }
926
927 /*
928 * Do the actual file system unmount. File system is assumed to have
929 * been locked by the caller.
930 *
931 * => Caller hold reference to the mount, explicitly for dounmount().
932 */
933 int
dounmount(struct mount * mp,int flags,struct lwp * l)934 dounmount(struct mount *mp, int flags, struct lwp *l)
935 {
936 vnode_t *coveredvp;
937 int error, async, used_syncer, used_extattr;
938 const bool was_suspended = fstrans_is_owner(mp);
939
940 #if NVERIEXEC > 0
941 error = veriexec_unmountchk(mp);
942 if (error)
943 return (error);
944 #endif /* NVERIEXEC > 0 */
945
946 if (!was_suspended) {
947 error = vfs_suspend(mp, 0);
948 if (error) {
949 return error;
950 }
951 }
952
953 KASSERT((mp->mnt_iflag & IMNT_GONE) == 0);
954
955 used_syncer = (mp->mnt_iflag & IMNT_ONWORKLIST) != 0;
956 used_extattr = mp->mnt_flag & MNT_EXTATTR;
957
958 mp->mnt_iflag |= IMNT_UNMOUNT;
959 mutex_enter(mp->mnt_updating);
960 async = mp->mnt_flag & MNT_ASYNC;
961 mp->mnt_flag &= ~MNT_ASYNC;
962 cache_purgevfs(mp); /* remove cache entries for this file sys */
963 if (used_syncer)
964 vfs_syncer_remove_from_worklist(mp);
965 error = 0;
966 if (((mp->mnt_flag & MNT_RDONLY) == 0) && ((flags & MNT_FORCE) == 0)) {
967 error = VFS_SYNC(mp, MNT_WAIT, l->l_cred);
968 }
969 if (error == 0 || (flags & MNT_FORCE)) {
970 error = VFS_UNMOUNT(mp, flags);
971 }
972 if (error) {
973 mp->mnt_iflag &= ~IMNT_UNMOUNT;
974 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
975 vfs_syncer_add_to_worklist(mp);
976 mp->mnt_flag |= async;
977 mutex_exit(mp->mnt_updating);
978 if (!was_suspended)
979 vfs_resume(mp);
980 if (used_extattr) {
981 if (start_extattr(mp) != 0)
982 mp->mnt_flag &= ~MNT_EXTATTR;
983 else
984 mp->mnt_flag |= MNT_EXTATTR;
985 }
986 return (error);
987 }
988 mutex_exit(mp->mnt_updating);
989
990 /*
991 * mark filesystem as gone to prevent further umounts
992 * after mnt_umounting lock is gone, this also prevents
993 * vfs_busy() from succeeding.
994 */
995 mp->mnt_iflag |= IMNT_GONE;
996 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
997 coveredvp->v_mountedhere = NULL;
998 }
999 if (!was_suspended)
1000 vfs_resume(mp);
1001
1002 mountlist_remove(mp);
1003 if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
1004 panic("unmount: dangling vnode");
1005 vfs_hooks_unmount(mp);
1006
1007 vfs_set_lowermount(mp, NULL);
1008 vfs_rele(mp); /* reference from mount() */
1009 if (coveredvp != NULLVP) {
1010 vrele(coveredvp);
1011 }
1012 return (0);
1013 }
1014
1015 /*
1016 * Unmount all file systems.
1017 * We traverse the list in reverse order under the assumption that doing so
1018 * will avoid needing to worry about dependencies.
1019 */
1020 bool
vfs_unmountall(struct lwp * l)1021 vfs_unmountall(struct lwp *l)
1022 {
1023
1024 printf("unmounting file systems...\n");
1025 return vfs_unmountall1(l, true, true);
1026 }
1027
1028 static void
vfs_unmount_print(struct mount * mp,const char * pfx)1029 vfs_unmount_print(struct mount *mp, const char *pfx)
1030 {
1031
1032 aprint_verbose("%sunmounted %s on %s type %s\n", pfx,
1033 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname,
1034 mp->mnt_stat.f_fstypename);
1035 }
1036
1037 /*
1038 * Return the mount with the highest generation less than "gen".
1039 */
1040 static struct mount *
vfs_unmount_next(uint64_t gen)1041 vfs_unmount_next(uint64_t gen)
1042 {
1043 mount_iterator_t *iter;
1044 struct mount *mp, *nmp;
1045
1046 nmp = NULL;
1047
1048 mountlist_iterator_init(&iter);
1049 while ((mp = mountlist_iterator_next(iter)) != NULL) {
1050 if ((nmp == NULL || mp->mnt_gen > nmp->mnt_gen) &&
1051 mp->mnt_gen < gen) {
1052 if (nmp != NULL)
1053 vfs_rele(nmp);
1054 nmp = mp;
1055 vfs_ref(nmp);
1056 }
1057 }
1058 mountlist_iterator_destroy(iter);
1059
1060 return nmp;
1061 }
1062
1063 bool
vfs_unmount_forceone(struct lwp * l)1064 vfs_unmount_forceone(struct lwp *l)
1065 {
1066 struct mount *mp;
1067 int error;
1068
1069 mp = vfs_unmount_next(mountgen);
1070 if (mp == NULL) {
1071 return false;
1072 }
1073
1074 #ifdef DEBUG
1075 printf("forcefully unmounting %s (%s)...\n",
1076 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
1077 #endif
1078 if ((error = dounmount(mp, MNT_FORCE, l)) == 0) {
1079 vfs_unmount_print(mp, "forcefully ");
1080 return true;
1081 } else {
1082 vfs_rele(mp);
1083 }
1084
1085 #ifdef DEBUG
1086 printf("forceful unmount of %s failed with error %d\n",
1087 mp->mnt_stat.f_mntonname, error);
1088 #endif
1089
1090 return false;
1091 }
1092
1093 bool
vfs_unmountall1(struct lwp * l,bool force,bool verbose)1094 vfs_unmountall1(struct lwp *l, bool force, bool verbose)
1095 {
1096 struct mount *mp;
1097 mount_iterator_t *iter;
1098 bool any_error = false, progress = false;
1099 uint64_t gen;
1100 int error;
1101
1102 gen = mountgen;
1103 for (;;) {
1104 mp = vfs_unmount_next(gen);
1105 if (mp == NULL)
1106 break;
1107 gen = mp->mnt_gen;
1108
1109 #ifdef DEBUG
1110 printf("unmounting %p %s (%s)...\n",
1111 (void *)mp, mp->mnt_stat.f_mntonname,
1112 mp->mnt_stat.f_mntfromname);
1113 #endif
1114 if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) {
1115 vfs_unmount_print(mp, "");
1116 progress = true;
1117 } else {
1118 vfs_rele(mp);
1119 if (verbose) {
1120 printf("unmount of %s failed with error %d\n",
1121 mp->mnt_stat.f_mntonname, error);
1122 }
1123 any_error = true;
1124 }
1125 }
1126 if (verbose) {
1127 printf("unmounting done\n");
1128 }
1129 if (any_error && verbose) {
1130 printf("WARNING: some file systems would not unmount\n");
1131 }
1132 /* If the mountlist is empty it is time to remove swap. */
1133 mountlist_iterator_init(&iter);
1134 if (mountlist_iterator_next(iter) == NULL) {
1135 uvm_swap_shutdown(l);
1136 }
1137 mountlist_iterator_destroy(iter);
1138
1139 return progress;
1140 }
1141
1142 void
vfs_sync_all(struct lwp * l)1143 vfs_sync_all(struct lwp *l)
1144 {
1145 printf("syncing disks... ");
1146
1147 /* remove user processes from run queue */
1148 suspendsched();
1149 (void)spl0();
1150
1151 /* avoid coming back this way again if we panic. */
1152 doing_shutdown = 1;
1153
1154 do_sys_sync(l);
1155
1156 /* Wait for sync to finish. */
1157 if (vfs_syncwait() != 0) {
1158 #if defined(DDB) && defined(DEBUG_HALT_BUSY)
1159 Debugger();
1160 #endif
1161 printf("giving up\n");
1162 return;
1163 } else
1164 printf("done\n");
1165 }
1166
1167 /*
1168 * Sync and unmount file systems before shutting down.
1169 */
1170 void
vfs_shutdown(void)1171 vfs_shutdown(void)
1172 {
1173 lwp_t *l = curlwp;
1174
1175 vfs_sync_all(l);
1176
1177 /*
1178 * If we have panicked - do not make the situation potentially
1179 * worse by unmounting the file systems.
1180 */
1181 if (panicstr != NULL) {
1182 return;
1183 }
1184
1185 /* Unmount file systems. */
1186 vfs_unmountall(l);
1187 }
1188
1189 /*
1190 * Print a list of supported file system types (used by vfs_mountroot)
1191 */
1192 static void
vfs_print_fstypes(void)1193 vfs_print_fstypes(void)
1194 {
1195 struct vfsops *v;
1196 int cnt = 0;
1197
1198 mutex_enter(&vfs_list_lock);
1199 LIST_FOREACH(v, &vfs_list, vfs_list)
1200 ++cnt;
1201 mutex_exit(&vfs_list_lock);
1202
1203 if (cnt == 0) {
1204 printf("WARNING: No file system modules have been loaded.\n");
1205 return;
1206 }
1207
1208 printf("Supported file systems:");
1209 mutex_enter(&vfs_list_lock);
1210 LIST_FOREACH(v, &vfs_list, vfs_list) {
1211 printf(" %s", v->vfs_name);
1212 }
1213 mutex_exit(&vfs_list_lock);
1214 printf("\n");
1215 }
1216
1217 /*
1218 * Mount the root file system. If the operator didn't specify a
1219 * file system to use, try all possible file systems until one
1220 * succeeds.
1221 */
1222 int
vfs_mountroot(void)1223 vfs_mountroot(void)
1224 {
1225 struct vfsops *v;
1226 int error = ENODEV;
1227
1228 if (root_device == NULL)
1229 panic("vfs_mountroot: root device unknown");
1230
1231 switch (device_class(root_device)) {
1232 case DV_IFNET:
1233 if (rootdev != NODEV)
1234 panic("vfs_mountroot: rootdev set for DV_IFNET "
1235 "(0x%llx -> %llu,%llu)",
1236 (unsigned long long)rootdev,
1237 (unsigned long long)major(rootdev),
1238 (unsigned long long)minor(rootdev));
1239 break;
1240
1241 case DV_DISK:
1242 if (rootdev == NODEV)
1243 panic("vfs_mountroot: rootdev not set for DV_DISK");
1244 if (bdevvp(rootdev, &rootvp))
1245 panic("vfs_mountroot: can't get vnode for rootdev");
1246 vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY);
1247 error = VOP_OPEN(rootvp, FREAD, FSCRED);
1248 VOP_UNLOCK(rootvp);
1249 if (error) {
1250 printf("vfs_mountroot: can't open root device\n");
1251 return (error);
1252 }
1253 break;
1254
1255 case DV_VIRTUAL:
1256 break;
1257
1258 default:
1259 printf("%s: inappropriate for root file system\n",
1260 device_xname(root_device));
1261 return (ENODEV);
1262 }
1263
1264 /*
1265 * If user specified a root fs type, use it. Make sure the
1266 * specified type exists and has a mount_root()
1267 */
1268 if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) {
1269 v = vfs_getopsbyname(rootfstype);
1270 error = EFTYPE;
1271 if (v != NULL) {
1272 if (v->vfs_mountroot != NULL) {
1273 error = (v->vfs_mountroot)();
1274 }
1275 v->vfs_refcount--;
1276 }
1277 goto done;
1278 }
1279
1280 /*
1281 * Try each file system currently configured into the kernel.
1282 */
1283 mutex_enter(&vfs_list_lock);
1284 LIST_FOREACH(v, &vfs_list, vfs_list) {
1285 if (v->vfs_mountroot == NULL)
1286 continue;
1287 #ifdef DEBUG
1288 aprint_normal("mountroot: trying %s...\n", v->vfs_name);
1289 #endif
1290 v->vfs_refcount++;
1291 mutex_exit(&vfs_list_lock);
1292 error = (*v->vfs_mountroot)();
1293 mutex_enter(&vfs_list_lock);
1294 v->vfs_refcount--;
1295 if (!error) {
1296 aprint_normal("root file system type: %s\n",
1297 v->vfs_name);
1298 break;
1299 }
1300 }
1301 mutex_exit(&vfs_list_lock);
1302
1303 if (v == NULL) {
1304 vfs_print_fstypes();
1305 printf("no file system for %s", device_xname(root_device));
1306 if (device_class(root_device) == DV_DISK)
1307 printf(" (dev 0x%llx)", (unsigned long long)rootdev);
1308 printf("\n");
1309 error = EFTYPE;
1310 }
1311
1312 done:
1313 if (error && device_class(root_device) == DV_DISK) {
1314 vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY);
1315 VOP_CLOSE(rootvp, FREAD, FSCRED);
1316 VOP_UNLOCK(rootvp);
1317 vrele(rootvp);
1318 }
1319 if (error == 0) {
1320 mount_iterator_t *iter;
1321 struct mount *mp;
1322
1323 mountlist_iterator_init(&iter);
1324 mp = mountlist_iterator_next(iter);
1325 KASSERT(mp != NULL);
1326 mountlist_iterator_destroy(iter);
1327
1328 mp->mnt_flag |= MNT_ROOTFS;
1329 mp->mnt_op->vfs_refcount++;
1330
1331 /*
1332 * Get the vnode for '/'. Set cwdi0.cwdi_cdir to
1333 * reference it, and donate it the reference grabbed
1334 * with VFS_ROOT().
1335 */
1336 error = VFS_ROOT(mp, LK_NONE, &rootvnode);
1337 if (error)
1338 panic("cannot find root vnode, error=%d", error);
1339 cwdi0.cwdi_cdir = rootvnode;
1340 cwdi0.cwdi_rdir = NULL;
1341
1342 /*
1343 * Now that root is mounted, we can fixup initproc's CWD
1344 * info. All other processes are kthreads, which merely
1345 * share proc0's CWD info.
1346 */
1347 initproc->p_cwdi->cwdi_cdir = rootvnode;
1348 vref(initproc->p_cwdi->cwdi_cdir);
1349 initproc->p_cwdi->cwdi_rdir = NULL;
1350 /*
1351 * Enable loading of modules from the filesystem
1352 */
1353 module_load_vfs_init();
1354
1355 }
1356 return (error);
1357 }
1358
1359 /*
1360 * mount_specific_key_create --
1361 * Create a key for subsystem mount-specific data.
1362 */
1363 int
mount_specific_key_create(specificdata_key_t * keyp,specificdata_dtor_t dtor)1364 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
1365 {
1366
1367 return specificdata_key_create(mount_specificdata_domain, keyp, dtor);
1368 }
1369
1370 /*
1371 * mount_specific_key_delete --
1372 * Delete a key for subsystem mount-specific data.
1373 */
1374 void
mount_specific_key_delete(specificdata_key_t key)1375 mount_specific_key_delete(specificdata_key_t key)
1376 {
1377
1378 specificdata_key_delete(mount_specificdata_domain, key);
1379 }
1380
1381 /*
1382 * mount_initspecific --
1383 * Initialize a mount's specificdata container.
1384 */
1385 void
mount_initspecific(struct mount * mp)1386 mount_initspecific(struct mount *mp)
1387 {
1388 int error __diagused;
1389
1390 error = specificdata_init(mount_specificdata_domain,
1391 &mp->mnt_specdataref);
1392 KASSERT(error == 0);
1393 }
1394
1395 /*
1396 * mount_finispecific --
1397 * Finalize a mount's specificdata container.
1398 */
1399 void
mount_finispecific(struct mount * mp)1400 mount_finispecific(struct mount *mp)
1401 {
1402
1403 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
1404 }
1405
1406 /*
1407 * mount_getspecific --
1408 * Return mount-specific data corresponding to the specified key.
1409 */
1410 void *
mount_getspecific(struct mount * mp,specificdata_key_t key)1411 mount_getspecific(struct mount *mp, specificdata_key_t key)
1412 {
1413
1414 return specificdata_getspecific(mount_specificdata_domain,
1415 &mp->mnt_specdataref, key);
1416 }
1417
1418 /*
1419 * mount_setspecific --
1420 * Set mount-specific data corresponding to the specified key.
1421 */
1422 void
mount_setspecific(struct mount * mp,specificdata_key_t key,void * data)1423 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data)
1424 {
1425
1426 specificdata_setspecific(mount_specificdata_domain,
1427 &mp->mnt_specdataref, key, data);
1428 }
1429
1430 /*
1431 * Check to see if a filesystem is mounted on a block device.
1432 */
1433 int
vfs_mountedon(vnode_t * vp)1434 vfs_mountedon(vnode_t *vp)
1435 {
1436 vnode_t *vq;
1437 int error = 0;
1438
1439 if (vp->v_type != VBLK)
1440 return ENOTBLK;
1441 if (spec_node_getmountedfs(vp) != NULL)
1442 return EBUSY;
1443 if (spec_node_lookup_by_dev(vp->v_type, vp->v_rdev, VDEAD_NOWAIT, &vq)
1444 == 0) {
1445 if (spec_node_getmountedfs(vq) != NULL)
1446 error = EBUSY;
1447 vrele(vq);
1448 }
1449
1450 return error;
1451 }
1452
1453 /*
1454 * Check if a device pointed to by vp is mounted.
1455 *
1456 * Returns:
1457 * EINVAL if it's not a disk
1458 * EBUSY if it's a disk and mounted
1459 * 0 if it's a disk and not mounted
1460 */
1461 int
rawdev_mounted(vnode_t * vp,vnode_t ** bvpp)1462 rawdev_mounted(vnode_t *vp, vnode_t **bvpp)
1463 {
1464 vnode_t *bvp;
1465 dev_t dev;
1466 int d_type;
1467
1468 bvp = NULL;
1469 d_type = D_OTHER;
1470
1471 if (iskmemvp(vp))
1472 return EINVAL;
1473
1474 switch (vp->v_type) {
1475 case VCHR: {
1476 const struct cdevsw *cdev;
1477
1478 dev = vp->v_rdev;
1479 cdev = cdevsw_lookup(dev);
1480 if (cdev != NULL) {
1481 dev_t blkdev;
1482
1483 blkdev = devsw_chr2blk(dev);
1484 if (blkdev != NODEV) {
1485 if (vfinddev(blkdev, VBLK, &bvp) != 0) {
1486 d_type = (cdev->d_flag & D_TYPEMASK);
1487 /* XXX: what if bvp disappears? */
1488 vrele(bvp);
1489 }
1490 }
1491 }
1492
1493 break;
1494 }
1495
1496 case VBLK: {
1497 const struct bdevsw *bdev;
1498
1499 dev = vp->v_rdev;
1500 bdev = bdevsw_lookup(dev);
1501 if (bdev != NULL)
1502 d_type = (bdev->d_flag & D_TYPEMASK);
1503
1504 bvp = vp;
1505
1506 break;
1507 }
1508
1509 default:
1510 break;
1511 }
1512
1513 if (d_type != D_DISK)
1514 return EINVAL;
1515
1516 if (bvpp != NULL)
1517 *bvpp = bvp;
1518
1519 /*
1520 * XXX: This is bogus. We should be failing the request
1521 * XXX: not only if this specific slice is mounted, but
1522 * XXX: if it's on a disk with any other mounted slice.
1523 */
1524 if (vfs_mountedon(bvp))
1525 return EBUSY;
1526
1527 return 0;
1528 }
1529
1530 /*
1531 * Make a 'unique' number from a mount type name.
1532 */
1533 long
makefstype(const char * type)1534 makefstype(const char *type)
1535 {
1536 long rv;
1537
1538 for (rv = 0; *type; type++) {
1539 rv <<= 2;
1540 rv ^= *type;
1541 }
1542 return rv;
1543 }
1544
1545 static struct mountlist_entry *
mountlist_alloc(enum mountlist_type type,struct mount * mp)1546 mountlist_alloc(enum mountlist_type type, struct mount *mp)
1547 {
1548 struct mountlist_entry *me;
1549
1550 me = kmem_zalloc(sizeof(*me), KM_SLEEP);
1551 me->me_mount = mp;
1552 me->me_type = type;
1553
1554 return me;
1555 }
1556
1557 static void
mountlist_free(struct mountlist_entry * me)1558 mountlist_free(struct mountlist_entry *me)
1559 {
1560
1561 kmem_free(me, sizeof(*me));
1562 }
1563
1564 void
mountlist_iterator_init(mount_iterator_t ** mip)1565 mountlist_iterator_init(mount_iterator_t **mip)
1566 {
1567 struct mountlist_entry *me;
1568
1569 me = mountlist_alloc(ME_MARKER, NULL);
1570 mutex_enter(&mountlist_lock);
1571 TAILQ_INSERT_HEAD(&mountlist, me, me_list);
1572 mutex_exit(&mountlist_lock);
1573 *mip = (mount_iterator_t *)me;
1574 }
1575
1576 void
mountlist_iterator_destroy(mount_iterator_t * mi)1577 mountlist_iterator_destroy(mount_iterator_t *mi)
1578 {
1579 struct mountlist_entry *marker = &mi->mi_entry;
1580
1581 if (marker->me_mount != NULL)
1582 vfs_unbusy(marker->me_mount);
1583
1584 mutex_enter(&mountlist_lock);
1585 TAILQ_REMOVE(&mountlist, marker, me_list);
1586 mutex_exit(&mountlist_lock);
1587
1588 mountlist_free(marker);
1589
1590 }
1591
1592 /*
1593 * Return the next mount or NULL for this iterator.
1594 * Mark it busy on success.
1595 */
1596 static inline struct mount *
_mountlist_iterator_next(mount_iterator_t * mi,bool wait)1597 _mountlist_iterator_next(mount_iterator_t *mi, bool wait)
1598 {
1599 struct mountlist_entry *me, *marker = &mi->mi_entry;
1600 struct mount *mp;
1601 int error;
1602
1603 if (marker->me_mount != NULL) {
1604 vfs_unbusy(marker->me_mount);
1605 marker->me_mount = NULL;
1606 }
1607
1608 mutex_enter(&mountlist_lock);
1609 for (;;) {
1610 KASSERT(marker->me_type == ME_MARKER);
1611
1612 me = TAILQ_NEXT(marker, me_list);
1613 if (me == NULL) {
1614 /* End of list: keep marker and return. */
1615 mutex_exit(&mountlist_lock);
1616 return NULL;
1617 }
1618 TAILQ_REMOVE(&mountlist, marker, me_list);
1619 TAILQ_INSERT_AFTER(&mountlist, me, marker, me_list);
1620
1621 /* Skip other markers. */
1622 if (me->me_type != ME_MOUNT)
1623 continue;
1624
1625 /* Take an initial reference for vfs_busy() below. */
1626 mp = me->me_mount;
1627 KASSERT(mp != NULL);
1628 vfs_ref(mp);
1629 mutex_exit(&mountlist_lock);
1630
1631 /* Try to mark this mount busy and return on success. */
1632 if (wait)
1633 error = vfs_busy(mp);
1634 else
1635 error = vfs_trybusy(mp);
1636 if (error == 0) {
1637 vfs_rele(mp);
1638 marker->me_mount = mp;
1639 return mp;
1640 }
1641 vfs_rele(mp);
1642 mutex_enter(&mountlist_lock);
1643 }
1644 }
1645
1646 struct mount *
mountlist_iterator_next(mount_iterator_t * mi)1647 mountlist_iterator_next(mount_iterator_t *mi)
1648 {
1649
1650 return _mountlist_iterator_next(mi, true);
1651 }
1652
1653 struct mount *
mountlist_iterator_trynext(mount_iterator_t * mi)1654 mountlist_iterator_trynext(mount_iterator_t *mi)
1655 {
1656
1657 return _mountlist_iterator_next(mi, false);
1658 }
1659
1660 /*
1661 * Attach new mount to the end of the mount list.
1662 */
1663 void
mountlist_append(struct mount * mp)1664 mountlist_append(struct mount *mp)
1665 {
1666 struct mountlist_entry *me;
1667
1668 me = mountlist_alloc(ME_MOUNT, mp);
1669 mutex_enter(&mountlist_lock);
1670 TAILQ_INSERT_TAIL(&mountlist, me, me_list);
1671 mutex_exit(&mountlist_lock);
1672 }
1673
1674 /*
1675 * Remove mount from mount list.
1676 */void
mountlist_remove(struct mount * mp)1677 mountlist_remove(struct mount *mp)
1678 {
1679 struct mountlist_entry *me;
1680
1681 mutex_enter(&mountlist_lock);
1682 TAILQ_FOREACH(me, &mountlist, me_list)
1683 if (me->me_type == ME_MOUNT && me->me_mount == mp)
1684 break;
1685 KASSERT(me != NULL);
1686 TAILQ_REMOVE(&mountlist, me, me_list);
1687 mutex_exit(&mountlist_lock);
1688 mountlist_free(me);
1689 }
1690
1691 /*
1692 * Unlocked variant to traverse the mountlist.
1693 * To be used from DDB only.
1694 */
1695 struct mount *
_mountlist_next(struct mount * mp)1696 _mountlist_next(struct mount *mp)
1697 {
1698 struct mountlist_entry *me;
1699
1700 if (mp == NULL) {
1701 me = TAILQ_FIRST(&mountlist);
1702 } else {
1703 TAILQ_FOREACH(me, &mountlist, me_list)
1704 if (me->me_type == ME_MOUNT && me->me_mount == mp)
1705 break;
1706 if (me != NULL)
1707 me = TAILQ_NEXT(me, me_list);
1708 }
1709
1710 while (me != NULL && me->me_type != ME_MOUNT)
1711 me = TAILQ_NEXT(me, me_list);
1712
1713 return (me ? me->me_mount : NULL);
1714 }
1715