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