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