xref: /netbsd-src/sys/kern/vfs_mount.c (revision d11b170b9000ada93db553723522a63d5deac310)
1 /*	$NetBSD: vfs_mount.c,v 1.29 2014/05/24 16:34:04 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997-2011 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.29 2014/05/24 16:34:04 christos Exp $");
71 
72 #define _VFS_VNODE_PRIVATE
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/fcntl.h>
81 #include <sys/filedesc.h>
82 #include <sys/device.h>
83 #include <sys/kauth.h>
84 #include <sys/kmem.h>
85 #include <sys/module.h>
86 #include <sys/mount.h>
87 #include <sys/namei.h>
88 #include <sys/extattr.h>
89 #include <sys/syscallargs.h>
90 #include <sys/sysctl.h>
91 #include <sys/systm.h>
92 #include <sys/vfs_syscalls.h>
93 #include <sys/vnode.h>
94 
95 #include <miscfs/genfs/genfs.h>
96 #include <miscfs/syncfs/syncfs.h>
97 #include <miscfs/specfs/specdev.h>
98 
99 /* Root filesystem and device. */
100 vnode_t *			rootvnode;
101 device_t			root_device;
102 
103 /* Mounted filesystem list. */
104 struct mntlist			mountlist;
105 kmutex_t			mountlist_lock;
106 
107 kmutex_t			mntvnode_lock;
108 kmutex_t			vfs_list_lock;
109 
110 static specificdata_domain_t	mount_specificdata_domain;
111 static kmutex_t			mntid_lock;
112 
113 static kmutex_t			mountgen_lock;
114 static uint64_t			mountgen;
115 
116 void
117 vfs_mount_sysinit(void)
118 {
119 
120 	TAILQ_INIT(&mountlist);
121 	mutex_init(&mountlist_lock, MUTEX_DEFAULT, IPL_NONE);
122 	mutex_init(&mntvnode_lock, MUTEX_DEFAULT, IPL_NONE);
123 	mutex_init(&vfs_list_lock, MUTEX_DEFAULT, IPL_NONE);
124 
125 	mount_specificdata_domain = specificdata_domain_create();
126 	mutex_init(&mntid_lock, MUTEX_DEFAULT, IPL_NONE);
127 	mutex_init(&mountgen_lock, MUTEX_DEFAULT, IPL_NONE);
128 	mountgen = 0;
129 }
130 
131 struct mount *
132 vfs_mountalloc(struct vfsops *vfsops, vnode_t *vp)
133 {
134 	struct mount *mp;
135 	int error __diagused;
136 
137 	mp = kmem_zalloc(sizeof(*mp), KM_SLEEP);
138 	if (mp == NULL)
139 		return NULL;
140 
141 	mp->mnt_op = vfsops;
142 	mp->mnt_refcnt = 1;
143 	TAILQ_INIT(&mp->mnt_vnodelist);
144 	mutex_init(&mp->mnt_unmounting, MUTEX_DEFAULT, IPL_NONE);
145 	mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
146 	mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
147 	error = vfs_busy(mp, NULL);
148 	KASSERT(error == 0);
149 	mp->mnt_vnodecovered = vp;
150 	mount_initspecific(mp);
151 
152 	mutex_enter(&mountgen_lock);
153 	mp->mnt_gen = mountgen++;
154 	mutex_exit(&mountgen_lock);
155 
156 	return mp;
157 }
158 
159 /*
160  * vfs_rootmountalloc: lookup a filesystem type, and if found allocate and
161  * initialize a mount structure for it.
162  *
163  * Devname is usually updated by mount(8) after booting.
164  */
165 int
166 vfs_rootmountalloc(const char *fstypename, const char *devname,
167     struct mount **mpp)
168 {
169 	struct vfsops *vfsp = NULL;
170 	struct mount *mp;
171 
172 	mutex_enter(&vfs_list_lock);
173 	LIST_FOREACH(vfsp, &vfs_list, vfs_list)
174 		if (!strncmp(vfsp->vfs_name, fstypename,
175 		    sizeof(mp->mnt_stat.f_fstypename)))
176 			break;
177 	if (vfsp == NULL) {
178 		mutex_exit(&vfs_list_lock);
179 		return (ENODEV);
180 	}
181 	vfsp->vfs_refcount++;
182 	mutex_exit(&vfs_list_lock);
183 
184 	if ((mp = vfs_mountalloc(vfsp, NULL)) == NULL)
185 		return ENOMEM;
186 	mp->mnt_flag = MNT_RDONLY;
187 	(void)strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name,
188 	    sizeof(mp->mnt_stat.f_fstypename));
189 	mp->mnt_stat.f_mntonname[0] = '/';
190 	mp->mnt_stat.f_mntonname[1] = '\0';
191 	mp->mnt_stat.f_mntfromname[sizeof(mp->mnt_stat.f_mntfromname) - 1] =
192 	    '\0';
193 	(void)copystr(devname, mp->mnt_stat.f_mntfromname,
194 	    sizeof(mp->mnt_stat.f_mntfromname) - 1, 0);
195 	*mpp = mp;
196 	return 0;
197 }
198 
199 /*
200  * vfs_getnewfsid: get a new unique fsid.
201  */
202 void
203 vfs_getnewfsid(struct mount *mp)
204 {
205 	static u_short xxxfs_mntid;
206 	fsid_t tfsid;
207 	int mtype;
208 
209 	mutex_enter(&mntid_lock);
210 	mtype = makefstype(mp->mnt_op->vfs_name);
211 	mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0);
212 	mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype;
213 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
214 	if (xxxfs_mntid == 0)
215 		++xxxfs_mntid;
216 	tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid);
217 	tfsid.__fsid_val[1] = mtype;
218 	if (!TAILQ_EMPTY(&mountlist)) {
219 		while (vfs_getvfs(&tfsid)) {
220 			tfsid.__fsid_val[0]++;
221 			xxxfs_mntid++;
222 		}
223 	}
224 	mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0];
225 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
226 	mutex_exit(&mntid_lock);
227 }
228 
229 /*
230  * Lookup a mount point by filesystem identifier.
231  *
232  * XXX Needs to add a reference to the mount point.
233  */
234 struct mount *
235 vfs_getvfs(fsid_t *fsid)
236 {
237 	struct mount *mp;
238 
239 	mutex_enter(&mountlist_lock);
240 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
241 		if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
242 		    mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
243 			mutex_exit(&mountlist_lock);
244 			return (mp);
245 		}
246 	}
247 	mutex_exit(&mountlist_lock);
248 	return NULL;
249 }
250 
251 /*
252  * Drop a reference to a mount structure, freeing if the last reference.
253  */
254 void
255 vfs_destroy(struct mount *mp)
256 {
257 
258 	if (__predict_true((int)atomic_dec_uint_nv(&mp->mnt_refcnt) > 0)) {
259 		return;
260 	}
261 
262 	/*
263 	 * Nothing else has visibility of the mount: we can now
264 	 * free the data structures.
265 	 */
266 	KASSERT(mp->mnt_refcnt == 0);
267 	specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
268 	mutex_destroy(&mp->mnt_unmounting);
269 	mutex_destroy(&mp->mnt_updating);
270 	mutex_destroy(&mp->mnt_renamelock);
271 	if (mp->mnt_op != NULL) {
272 		vfs_delref(mp->mnt_op);
273 	}
274 	kmem_free(mp, sizeof(*mp));
275 }
276 
277 /*
278  * Mark a mount point as busy, and gain a new reference to it.  Used to
279  * prevent the file system from being unmounted during critical sections.
280  *
281  * vfs_busy can be called multiple times and by multiple threads
282  * and must be accompanied by the same number of vfs_unbusy calls.
283  *
284  * => The caller must hold a pre-existing reference to the mount.
285  * => Will fail if the file system is being unmounted, or is unmounted.
286  */
287 int
288 vfs_busy(struct mount *mp, struct mount **nextp)
289 {
290 
291 	KASSERT(mp->mnt_refcnt > 0);
292 
293 	mutex_enter(&mp->mnt_unmounting);
294 	if (__predict_false((mp->mnt_iflag & IMNT_GONE) != 0)) {
295 		mutex_exit(&mp->mnt_unmounting);
296 		if (nextp != NULL) {
297 			KASSERT(mutex_owned(&mountlist_lock));
298 			*nextp = TAILQ_NEXT(mp, mnt_list);
299 		}
300 		return ENOENT;
301 	}
302 	++mp->mnt_busynest;
303 	KASSERT(mp->mnt_busynest != 0);
304 	mutex_exit(&mp->mnt_unmounting);
305 	if (nextp != NULL) {
306 		mutex_exit(&mountlist_lock);
307 	}
308 	atomic_inc_uint(&mp->mnt_refcnt);
309 	return 0;
310 }
311 
312 /*
313  * Unbusy a busy filesystem.
314  *
315  * Every successful vfs_busy() call must be undone by a vfs_unbusy() call.
316  *
317  * => If keepref is true, preserve reference added by vfs_busy().
318  * => If nextp != NULL, acquire mountlist_lock.
319  */
320 void
321 vfs_unbusy(struct mount *mp, bool keepref, struct mount **nextp)
322 {
323 
324 	KASSERT(mp->mnt_refcnt > 0);
325 
326 	if (nextp != NULL) {
327 		mutex_enter(&mountlist_lock);
328 	}
329 	mutex_enter(&mp->mnt_unmounting);
330 	KASSERT(mp->mnt_busynest != 0);
331 	mp->mnt_busynest--;
332 	mutex_exit(&mp->mnt_unmounting);
333 	if (!keepref) {
334 		vfs_destroy(mp);
335 	}
336 	if (nextp != NULL) {
337 		KASSERT(mutex_owned(&mountlist_lock));
338 		*nextp = TAILQ_NEXT(mp, mnt_list);
339 	}
340 }
341 
342 struct vnode_iterator {
343 	struct vnode vi_vnode;
344 };
345 
346 void
347 vfs_vnode_iterator_init(struct mount *mp, struct vnode_iterator **vip)
348 {
349 	struct vnode *vp;
350 
351 	vp = vnalloc(mp);
352 
353 	mutex_enter(&mntvnode_lock);
354 	TAILQ_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
355 	vp->v_usecount = 1;
356 	mutex_exit(&mntvnode_lock);
357 
358 	*vip = (struct vnode_iterator *)vp;
359 }
360 
361 void
362 vfs_vnode_iterator_destroy(struct vnode_iterator *vi)
363 {
364 	struct vnode *mvp = &vi->vi_vnode;
365 
366 	mutex_enter(&mntvnode_lock);
367 	KASSERT(ISSET(mvp->v_iflag, VI_MARKER));
368 	if (mvp->v_usecount != 0)
369 		TAILQ_REMOVE(&mvp->v_mount->mnt_vnodelist, mvp, v_mntvnodes);
370 	mutex_exit(&mntvnode_lock);
371 	vnfree(mvp);
372 }
373 
374 struct vnode *
375 vfs_vnode_iterator_next(struct vnode_iterator *vi,
376     bool (*f)(void *, struct vnode *), void *cl)
377 {
378 	struct vnode *mvp = &vi->vi_vnode;
379 	struct mount *mp = mvp->v_mount;
380 	struct vnode *vp;
381 	int error;
382 
383 	KASSERT(ISSET(mvp->v_iflag, VI_MARKER));
384 
385 	do {
386 		mutex_enter(&mntvnode_lock);
387 		vp = TAILQ_NEXT(mvp, v_mntvnodes);
388 		TAILQ_REMOVE(&mp->mnt_vnodelist, mvp, v_mntvnodes);
389 		mvp->v_usecount = 0;
390 again:
391 		if (vp == NULL) {
392 	       		mutex_exit(&mntvnode_lock);
393 	       		return NULL;
394 		}
395 		mutex_enter(vp->v_interlock);
396 		while (ISSET(vp->v_iflag, VI_MARKER) ||
397 		    ISSET(vp->v_iflag, VI_XLOCK) ||
398 		    (f && !(*f)(cl, vp))) {
399 			mutex_exit(vp->v_interlock);
400 			vp = TAILQ_NEXT(vp, v_mntvnodes);
401 			goto again;
402 		}
403 
404 		TAILQ_INSERT_AFTER(&mp->mnt_vnodelist, vp, mvp, v_mntvnodes);
405 		mvp->v_usecount = 1;
406 		mutex_exit(&mntvnode_lock);
407 		error = vget(vp, 0);
408 		KASSERT(error == 0 || error == ENOENT);
409 	} while (error != 0);
410 
411 	return vp;
412 }
413 
414 /*
415  * Move a vnode from one mount queue to another.
416  */
417 void
418 vfs_insmntque(vnode_t *vp, struct mount *mp)
419 {
420 	struct mount *omp;
421 
422 	KASSERT(mp == NULL || (mp->mnt_iflag & IMNT_UNMOUNT) == 0 ||
423 	    vp->v_tag == VT_VFS);
424 
425 	mutex_enter(&mntvnode_lock);
426 	/*
427 	 * Delete from old mount point vnode list, if on one.
428 	 */
429 	if ((omp = vp->v_mount) != NULL)
430 		TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vp, v_mntvnodes);
431 	/*
432 	 * Insert into list of vnodes for the new mount point, if
433 	 * available.  The caller must take a reference on the mount
434 	 * structure and donate to the vnode.
435 	 */
436 	if ((vp->v_mount = mp) != NULL)
437 		TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes);
438 	mutex_exit(&mntvnode_lock);
439 
440 	if (omp != NULL) {
441 		/* Release reference to old mount. */
442 		vfs_destroy(omp);
443 	}
444 }
445 
446 /*
447  * Remove any vnodes in the vnode table belonging to mount point mp.
448  *
449  * If FORCECLOSE is not specified, there should not be any active ones,
450  * return error if any are found (nb: this is a user error, not a
451  * system error). If FORCECLOSE is specified, detach any active vnodes
452  * that are found.
453  *
454  * If WRITECLOSE is set, only flush out regular file vnodes open for
455  * writing.
456  *
457  * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
458  */
459 #ifdef DEBUG
460 int busyprt = 0;	/* print out busy vnodes */
461 struct ctldebug debug1 = { "busyprt", &busyprt };
462 #endif
463 
464 struct vflush_ctx {
465 	const struct vnode *skipvp;
466 	int flags;
467 };
468 
469 static bool
470 vflush_selector(void *cl, struct vnode *vp)
471 {
472 	struct vflush_ctx *c = cl;
473 	/*
474 	 * Skip over a selected vnode.
475 	 */
476 	if (vp == c->skipvp)
477 		return false;
478 	/*
479 	 * Skip over a vnodes marked VSYSTEM.
480 	 */
481 	if ((c->flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM))
482 		return false;
483 
484 	/*
485 	 * If WRITECLOSE is set, only flush out regular file
486 	 * vnodes open for writing.
487 	 */
488 	if ((c->flags & WRITECLOSE) && vp->v_type == VREG) {
489 		if (vp->v_writecount == 0)
490 			return false;
491 	}
492 	return true;
493 }
494 
495 static vnode_t *
496 vflushnext(struct vnode_iterator *marker, void *ctx, int *when)
497 {
498 	if (hardclock_ticks > *when) {
499 		yield();
500 		*when = hardclock_ticks + hz / 10;
501 	}
502 	return vfs_vnode_iterator_next(marker, vflush_selector, ctx);
503 }
504 
505 
506 int
507 vflush(struct mount *mp, vnode_t *skipvp, int flags)
508 {
509 	vnode_t *vp;
510 	struct vnode_iterator *marker;
511 	int busy = 0, when = 0;
512 	struct vflush_ctx ctx;
513 
514 	/* First, flush out any vnode references from vrele_list. */
515 	vrele_flush();
516 
517 	vfs_vnode_iterator_init(mp, &marker);
518 
519 	ctx.skipvp = skipvp;
520 	ctx.flags = flags;
521 	while ((vp = vflushnext(marker, &ctx, &when)) != NULL) {
522 		/*
523 		 * First try to recycle the vnode.
524 		 */
525 		if (vrecycle(vp))
526 			continue;
527 		/*
528 		 * If FORCECLOSE is set, forcibly close the vnode.
529 		 */
530 		if (flags & FORCECLOSE) {
531 			vgone(vp);
532 			continue;
533 		}
534 #ifdef DEBUG
535 		if (busyprt)
536 			vprint("vflush: busy vnode", vp);
537 #endif
538 		vrele(vp);
539 		busy++;
540 	}
541 	vfs_vnode_iterator_destroy(marker);
542 	if (busy)
543 		return (EBUSY);
544 	return (0);
545 }
546 
547 /*
548  * Mount a file system.
549  */
550 
551 /*
552  * Scan all active processes to see if any of them have a current or root
553  * directory onto which the new filesystem has just been  mounted. If so,
554  * replace them with the new mount point.
555  */
556 static void
557 mount_checkdirs(vnode_t *olddp)
558 {
559 	vnode_t *newdp, *rele1, *rele2;
560 	struct cwdinfo *cwdi;
561 	struct proc *p;
562 	bool retry;
563 
564 	if (olddp->v_usecount == 1) {
565 		return;
566 	}
567 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
568 		panic("mount: lost mount");
569 
570 	do {
571 		retry = false;
572 		mutex_enter(proc_lock);
573 		PROCLIST_FOREACH(p, &allproc) {
574 			if ((cwdi = p->p_cwdi) == NULL)
575 				continue;
576 			/*
577 			 * Cannot change to the old directory any more,
578 			 * so even if we see a stale value it is not a
579 			 * problem.
580 			 */
581 			if (cwdi->cwdi_cdir != olddp &&
582 			    cwdi->cwdi_rdir != olddp)
583 				continue;
584 			retry = true;
585 			rele1 = NULL;
586 			rele2 = NULL;
587 			atomic_inc_uint(&cwdi->cwdi_refcnt);
588 			mutex_exit(proc_lock);
589 			rw_enter(&cwdi->cwdi_lock, RW_WRITER);
590 			if (cwdi->cwdi_cdir == olddp) {
591 				rele1 = cwdi->cwdi_cdir;
592 				vref(newdp);
593 				cwdi->cwdi_cdir = newdp;
594 			}
595 			if (cwdi->cwdi_rdir == olddp) {
596 				rele2 = cwdi->cwdi_rdir;
597 				vref(newdp);
598 				cwdi->cwdi_rdir = newdp;
599 			}
600 			rw_exit(&cwdi->cwdi_lock);
601 			cwdfree(cwdi);
602 			if (rele1 != NULL)
603 				vrele(rele1);
604 			if (rele2 != NULL)
605 				vrele(rele2);
606 			mutex_enter(proc_lock);
607 			break;
608 		}
609 		mutex_exit(proc_lock);
610 	} while (retry);
611 
612 	if (rootvnode == olddp) {
613 		vrele(rootvnode);
614 		vref(newdp);
615 		rootvnode = newdp;
616 	}
617 	vput(newdp);
618 }
619 
620 int
621 mount_domount(struct lwp *l, vnode_t **vpp, struct vfsops *vfsops,
622     const char *path, int flags, void *data, size_t *data_len)
623 {
624 	vnode_t *vp = *vpp;
625 	struct mount *mp;
626 	struct pathbuf *pb;
627 	struct nameidata nd;
628 	int error;
629 
630 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
631 	    KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data);
632 	if (error) {
633 		vfs_delref(vfsops);
634 		return error;
635 	}
636 
637 	/* Cannot make a non-dir a mount-point (from here anyway). */
638 	if (vp->v_type != VDIR) {
639 		vfs_delref(vfsops);
640 		return ENOTDIR;
641 	}
642 
643 	if (flags & MNT_EXPORTED) {
644 		vfs_delref(vfsops);
645 		return EINVAL;
646 	}
647 
648 	if ((mp = vfs_mountalloc(vfsops, vp)) == NULL) {
649 		vfs_delref(vfsops);
650 		return ENOMEM;
651 	}
652 
653 	mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred);
654 
655 	/*
656 	 * The underlying file system may refuse the mount for
657 	 * various reasons.  Allow the user to force it to happen.
658 	 *
659 	 * Set the mount level flags.
660 	 */
661 	mp->mnt_flag = flags & (MNT_BASIC_FLAGS | MNT_FORCE | MNT_IGNORE);
662 
663 	mutex_enter(&mp->mnt_updating);
664 	error = VFS_MOUNT(mp, path, data, data_len);
665 	mp->mnt_flag &= ~MNT_OP_FLAGS;
666 
667 	if (error != 0)
668 		goto err_unmounted;
669 
670 	/*
671 	 * Validate and prepare the mount point.
672 	 */
673 	error = pathbuf_copyin(path, &pb);
674 	if (error != 0) {
675 		goto err_mounted;
676 	}
677 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
678 	error = namei(&nd);
679 	pathbuf_destroy(pb);
680 	if (error != 0) {
681 		goto err_mounted;
682 	}
683 	if (nd.ni_vp != vp) {
684 		vput(nd.ni_vp);
685 		error = EINVAL;
686 		goto err_mounted;
687 	}
688 	if (vp->v_mountedhere != NULL) {
689 		vput(nd.ni_vp);
690 		error = EBUSY;
691 		goto err_mounted;
692 	}
693 	error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0);
694 	if (error != 0) {
695 		vput(nd.ni_vp);
696 		goto err_mounted;
697 	}
698 
699 	/*
700 	 * Put the new filesystem on the mount list after root.
701 	 */
702 	cache_purge(vp);
703 	mp->mnt_iflag &= ~IMNT_WANTRDWR;
704 
705 	mutex_enter(&mountlist_lock);
706 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
707 	mutex_exit(&mountlist_lock);
708 	if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
709 		error = vfs_allocate_syncvnode(mp);
710 	if (error == 0)
711 		vp->v_mountedhere = mp;
712 	vput(nd.ni_vp);
713 	if (error != 0)
714 		goto err_onmountlist;
715 
716 	mount_checkdirs(vp);
717 	mutex_exit(&mp->mnt_updating);
718 
719 	/* Hold an additional reference to the mount across VFS_START(). */
720 	vfs_unbusy(mp, true, NULL);
721 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
722 	error = VFS_START(mp, 0);
723        if (error) {
724 		vrele(vp);
725        } else if (flags & MNT_EXTATTR) {
726 	       error = VFS_EXTATTRCTL(vp->v_mountedhere,
727 		   EXTATTR_CMD_START, NULL, 0, NULL);
728 	       if (error)
729 		       printf("%s: failed to start extattr: error = %d\n",
730 			   vp->v_mountedhere->mnt_stat.f_mntonname, error);
731        }
732 	/* Drop reference held for VFS_START(). */
733 	vfs_destroy(mp);
734 	*vpp = NULL;
735 	return error;
736 
737 err_onmountlist:
738 	mutex_enter(&mountlist_lock);
739 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
740 	mp->mnt_iflag |= IMNT_GONE;
741 	mutex_exit(&mountlist_lock);
742 
743 err_mounted:
744 	if (VFS_UNMOUNT(mp, MNT_FORCE) != 0)
745 		panic("Unmounting fresh file system failed");
746 
747 err_unmounted:
748 	vp->v_mountedhere = NULL;
749 	mutex_exit(&mp->mnt_updating);
750 	vfs_unbusy(mp, false, NULL);
751 	vfs_destroy(mp);
752 
753 	return error;
754 }
755 
756 /*
757  * Do the actual file system unmount.  File system is assumed to have
758  * been locked by the caller.
759  *
760  * => Caller hold reference to the mount, explicitly for dounmount().
761  */
762 int
763 dounmount(struct mount *mp, int flags, struct lwp *l)
764 {
765 	vnode_t *coveredvp;
766 	int error, async, used_syncer;
767 
768 #if NVERIEXEC > 0
769 	error = veriexec_unmountchk(mp);
770 	if (error)
771 		return (error);
772 #endif /* NVERIEXEC > 0 */
773 
774 	/*
775 	 * XXX Freeze syncer.  Must do this before locking the
776 	 * mount point.  See dounmount() for details.
777 	 */
778 	mutex_enter(&syncer_mutex);
779 
780 	/*
781 	 * Abort unmount attempt when the filesystem is in use
782 	 */
783 	mutex_enter(&mp->mnt_unmounting);
784 	if (mp->mnt_busynest != 0) {
785 		mutex_exit(&mp->mnt_unmounting);
786 		mutex_exit(&syncer_mutex);
787 		return EBUSY;
788 	}
789 
790 	/*
791 	 * Abort unmount attempt when the filesystem is not mounted
792 	 */
793 	if ((mp->mnt_iflag & IMNT_GONE) != 0) {
794 		mutex_exit(&mp->mnt_unmounting);
795 		mutex_exit(&syncer_mutex);
796 		return ENOENT;
797 	}
798 
799 	used_syncer = (mp->mnt_syncer != NULL);
800 
801 	/*
802 	 * XXX Syncer must be frozen when we get here.  This should really
803 	 * be done on a per-mountpoint basis, but the syncer doesn't work
804 	 * like that.
805 	 *
806 	 * The caller of dounmount() must acquire syncer_mutex because
807 	 * the syncer itself acquires locks in syncer_mutex -> vfs_busy
808 	 * order, and we must preserve that order to avoid deadlock.
809 	 *
810 	 * So, if the file system did not use the syncer, now is
811 	 * the time to release the syncer_mutex.
812 	 */
813 	if (used_syncer == 0) {
814 		mutex_exit(&syncer_mutex);
815 	}
816 	mp->mnt_iflag |= IMNT_UNMOUNT;
817 	mutex_enter(&mp->mnt_updating);
818 	async = mp->mnt_flag & MNT_ASYNC;
819 	mp->mnt_flag &= ~MNT_ASYNC;
820 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
821 	if (mp->mnt_syncer != NULL)
822 		vfs_deallocate_syncvnode(mp);
823 	error = 0;
824 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
825 		error = VFS_SYNC(mp, MNT_WAIT, l->l_cred);
826 	}
827 	if (error == 0 || (flags & MNT_FORCE)) {
828 		error = VFS_UNMOUNT(mp, flags);
829 	}
830 	if (error) {
831 		mp->mnt_iflag &= ~IMNT_UNMOUNT;
832 		mutex_exit(&mp->mnt_unmounting);
833 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
834 			(void) vfs_allocate_syncvnode(mp);
835 		mp->mnt_flag |= async;
836 		mutex_exit(&mp->mnt_updating);
837 		if (used_syncer)
838 			mutex_exit(&syncer_mutex);
839 		return (error);
840 	}
841 	mutex_exit(&mp->mnt_updating);
842 
843 	/*
844 	 * release mnt_umounting lock here, because other code calls
845 	 * vfs_busy() while holding the mountlist_lock.
846 	 *
847 	 * mark filesystem as gone to prevent further umounts
848 	 * after mnt_umounting lock is gone, this also prevents
849 	 * vfs_busy() from succeeding.
850 	 */
851 	mp->mnt_iflag |= IMNT_GONE;
852 	mutex_exit(&mp->mnt_unmounting);
853 
854 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
855 		vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY);
856 		coveredvp->v_mountedhere = NULL;
857 		VOP_UNLOCK(coveredvp);
858 	}
859 	mutex_enter(&mountlist_lock);
860 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
861 	mutex_exit(&mountlist_lock);
862 	if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
863 		panic("unmount: dangling vnode");
864 	if (used_syncer)
865 		mutex_exit(&syncer_mutex);
866 	vfs_hooks_unmount(mp);
867 
868 	vfs_destroy(mp);	/* reference from mount() */
869 	if (coveredvp != NULLVP) {
870 		vrele(coveredvp);
871 	}
872 	return (0);
873 }
874 
875 /*
876  * Unmount all file systems.
877  * We traverse the list in reverse order under the assumption that doing so
878  * will avoid needing to worry about dependencies.
879  */
880 bool
881 vfs_unmountall(struct lwp *l)
882 {
883 
884 	printf("unmounting file systems...\n");
885 	return vfs_unmountall1(l, true, true);
886 }
887 
888 static void
889 vfs_unmount_print(struct mount *mp, const char *pfx)
890 {
891 
892 	aprint_verbose("%sunmounted %s on %s type %s\n", pfx,
893 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname,
894 	    mp->mnt_stat.f_fstypename);
895 }
896 
897 bool
898 vfs_unmount_forceone(struct lwp *l)
899 {
900 	struct mount *mp, *nmp;
901 	int error;
902 
903 	nmp = NULL;
904 
905 	TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) {
906 		if (nmp == NULL || mp->mnt_gen > nmp->mnt_gen) {
907 			nmp = mp;
908 		}
909 	}
910 	if (nmp == NULL) {
911 		return false;
912 	}
913 
914 #ifdef DEBUG
915 	printf("forcefully unmounting %s (%s)...\n",
916 	    nmp->mnt_stat.f_mntonname, nmp->mnt_stat.f_mntfromname);
917 #endif
918 	atomic_inc_uint(&nmp->mnt_refcnt);
919 	if ((error = dounmount(nmp, MNT_FORCE, l)) == 0) {
920 		vfs_unmount_print(nmp, "forcefully ");
921 		return true;
922 	} else {
923 		vfs_destroy(nmp);
924 	}
925 
926 #ifdef DEBUG
927 	printf("forceful unmount of %s failed with error %d\n",
928 	    nmp->mnt_stat.f_mntonname, error);
929 #endif
930 
931 	return false;
932 }
933 
934 bool
935 vfs_unmountall1(struct lwp *l, bool force, bool verbose)
936 {
937 	struct mount *mp, *nmp;
938 	bool any_error = false, progress = false;
939 	int error;
940 
941 	TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, nmp) {
942 #ifdef DEBUG
943 		printf("unmounting %p %s (%s)...\n",
944 		    (void *)mp, mp->mnt_stat.f_mntonname,
945 		    mp->mnt_stat.f_mntfromname);
946 #endif
947 		atomic_inc_uint(&mp->mnt_refcnt);
948 		if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) {
949 			vfs_unmount_print(mp, "");
950 			progress = true;
951 		} else {
952 			vfs_destroy(mp);
953 			if (verbose) {
954 				printf("unmount of %s failed with error %d\n",
955 				    mp->mnt_stat.f_mntonname, error);
956 			}
957 			any_error = true;
958 		}
959 	}
960 	if (verbose) {
961 		printf("unmounting done\n");
962 	}
963 	if (any_error && verbose) {
964 		printf("WARNING: some file systems would not unmount\n");
965 	}
966 	return progress;
967 }
968 
969 void
970 vfs_sync_all(struct lwp *l)
971 {
972 	printf("syncing disks... ");
973 
974 	/* remove user processes from run queue */
975 	suspendsched();
976 	(void)spl0();
977 
978 	/* avoid coming back this way again if we panic. */
979 	doing_shutdown = 1;
980 
981 	do_sys_sync(l);
982 
983 	/* Wait for sync to finish. */
984 	if (buf_syncwait() != 0) {
985 #if defined(DDB) && defined(DEBUG_HALT_BUSY)
986 		Debugger();
987 #endif
988 		printf("giving up\n");
989 		return;
990 	} else
991 		printf("done\n");
992 }
993 
994 /*
995  * Sync and unmount file systems before shutting down.
996  */
997 void
998 vfs_shutdown(void)
999 {
1000 	lwp_t *l = curlwp;
1001 
1002 	vfs_sync_all(l);
1003 
1004 	/*
1005 	 * If we have paniced - do not make the situation potentially
1006 	 * worse by unmounting the file systems.
1007 	 */
1008 	if (panicstr != NULL) {
1009 		return;
1010 	}
1011 
1012 	/* Unmount file systems. */
1013 	vfs_unmountall(l);
1014 }
1015 
1016 /*
1017  * Print a list of supported file system types (used by vfs_mountroot)
1018  */
1019 static void
1020 vfs_print_fstypes(void)
1021 {
1022 	struct vfsops *v;
1023 	int cnt = 0;
1024 
1025 	mutex_enter(&vfs_list_lock);
1026 	LIST_FOREACH(v, &vfs_list, vfs_list)
1027 		++cnt;
1028 	mutex_exit(&vfs_list_lock);
1029 
1030 	if (cnt == 0) {
1031 		printf("WARNING: No file system modules have been loaded.\n");
1032 		return;
1033 	}
1034 
1035 	printf("Supported file systems:");
1036 	mutex_enter(&vfs_list_lock);
1037 	LIST_FOREACH(v, &vfs_list, vfs_list) {
1038 		printf(" %s", v->vfs_name);
1039 	}
1040 	mutex_exit(&vfs_list_lock);
1041 	printf("\n");
1042 }
1043 
1044 /*
1045  * Mount the root file system.  If the operator didn't specify a
1046  * file system to use, try all possible file systems until one
1047  * succeeds.
1048  */
1049 int
1050 vfs_mountroot(void)
1051 {
1052 	struct vfsops *v;
1053 	int error = ENODEV;
1054 
1055 	if (root_device == NULL)
1056 		panic("vfs_mountroot: root device unknown");
1057 
1058 	switch (device_class(root_device)) {
1059 	case DV_IFNET:
1060 		if (rootdev != NODEV)
1061 			panic("vfs_mountroot: rootdev set for DV_IFNET "
1062 			    "(0x%llx -> %llu,%llu)",
1063 			    (unsigned long long)rootdev,
1064 			    (unsigned long long)major(rootdev),
1065 			    (unsigned long long)minor(rootdev));
1066 		break;
1067 
1068 	case DV_DISK:
1069 		if (rootdev == NODEV)
1070 			panic("vfs_mountroot: rootdev not set for DV_DISK");
1071 	        if (bdevvp(rootdev, &rootvp))
1072 	                panic("vfs_mountroot: can't get vnode for rootdev");
1073 		error = VOP_OPEN(rootvp, FREAD, FSCRED);
1074 		if (error) {
1075 			printf("vfs_mountroot: can't open root device\n");
1076 			return (error);
1077 		}
1078 		break;
1079 
1080 	case DV_VIRTUAL:
1081 		break;
1082 
1083 	default:
1084 		printf("%s: inappropriate for root file system\n",
1085 		    device_xname(root_device));
1086 		return (ENODEV);
1087 	}
1088 
1089 	/*
1090 	 * If user specified a root fs type, use it.  Make sure the
1091 	 * specified type exists and has a mount_root()
1092 	 */
1093 	if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) {
1094 		v = vfs_getopsbyname(rootfstype);
1095 		error = EFTYPE;
1096 		if (v != NULL) {
1097 			if (v->vfs_mountroot != NULL) {
1098 				error = (v->vfs_mountroot)();
1099 			}
1100 			v->vfs_refcount--;
1101 		}
1102 		goto done;
1103 	}
1104 
1105 	/*
1106 	 * Try each file system currently configured into the kernel.
1107 	 */
1108 	mutex_enter(&vfs_list_lock);
1109 	LIST_FOREACH(v, &vfs_list, vfs_list) {
1110 		if (v->vfs_mountroot == NULL)
1111 			continue;
1112 #ifdef DEBUG
1113 		aprint_normal("mountroot: trying %s...\n", v->vfs_name);
1114 #endif
1115 		v->vfs_refcount++;
1116 		mutex_exit(&vfs_list_lock);
1117 		error = (*v->vfs_mountroot)();
1118 		mutex_enter(&vfs_list_lock);
1119 		v->vfs_refcount--;
1120 		if (!error) {
1121 			aprint_normal("root file system type: %s\n",
1122 			    v->vfs_name);
1123 			break;
1124 		}
1125 	}
1126 	mutex_exit(&vfs_list_lock);
1127 
1128 	if (v == NULL) {
1129 		vfs_print_fstypes();
1130 		printf("no file system for %s", device_xname(root_device));
1131 		if (device_class(root_device) == DV_DISK)
1132 			printf(" (dev 0x%llx)", (unsigned long long)rootdev);
1133 		printf("\n");
1134 		error = EFTYPE;
1135 	}
1136 
1137 done:
1138 	if (error && device_class(root_device) == DV_DISK) {
1139 		VOP_CLOSE(rootvp, FREAD, FSCRED);
1140 		vrele(rootvp);
1141 	}
1142 	if (error == 0) {
1143 		struct mount *mp;
1144 		extern struct cwdinfo cwdi0;
1145 
1146 		mp = TAILQ_FIRST(&mountlist);
1147 		mp->mnt_flag |= MNT_ROOTFS;
1148 		mp->mnt_op->vfs_refcount++;
1149 
1150 		/*
1151 		 * Get the vnode for '/'.  Set cwdi0.cwdi_cdir to
1152 		 * reference it.
1153 		 */
1154 		error = VFS_ROOT(mp, &rootvnode);
1155 		if (error)
1156 			panic("cannot find root vnode, error=%d", error);
1157 		cwdi0.cwdi_cdir = rootvnode;
1158 		vref(cwdi0.cwdi_cdir);
1159 		VOP_UNLOCK(rootvnode);
1160 		cwdi0.cwdi_rdir = NULL;
1161 
1162 		/*
1163 		 * Now that root is mounted, we can fixup initproc's CWD
1164 		 * info.  All other processes are kthreads, which merely
1165 		 * share proc0's CWD info.
1166 		 */
1167 		initproc->p_cwdi->cwdi_cdir = rootvnode;
1168 		vref(initproc->p_cwdi->cwdi_cdir);
1169 		initproc->p_cwdi->cwdi_rdir = NULL;
1170 		/*
1171 		 * Enable loading of modules from the filesystem
1172 		 */
1173 		module_load_vfs_init();
1174 
1175 	}
1176 	return (error);
1177 }
1178 
1179 /*
1180  * mount_specific_key_create --
1181  *	Create a key for subsystem mount-specific data.
1182  */
1183 int
1184 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
1185 {
1186 
1187 	return specificdata_key_create(mount_specificdata_domain, keyp, dtor);
1188 }
1189 
1190 /*
1191  * mount_specific_key_delete --
1192  *	Delete a key for subsystem mount-specific data.
1193  */
1194 void
1195 mount_specific_key_delete(specificdata_key_t key)
1196 {
1197 
1198 	specificdata_key_delete(mount_specificdata_domain, key);
1199 }
1200 
1201 /*
1202  * mount_initspecific --
1203  *	Initialize a mount's specificdata container.
1204  */
1205 void
1206 mount_initspecific(struct mount *mp)
1207 {
1208 	int error __diagused;
1209 
1210 	error = specificdata_init(mount_specificdata_domain,
1211 				  &mp->mnt_specdataref);
1212 	KASSERT(error == 0);
1213 }
1214 
1215 /*
1216  * mount_finispecific --
1217  *	Finalize a mount's specificdata container.
1218  */
1219 void
1220 mount_finispecific(struct mount *mp)
1221 {
1222 
1223 	specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref);
1224 }
1225 
1226 /*
1227  * mount_getspecific --
1228  *	Return mount-specific data corresponding to the specified key.
1229  */
1230 void *
1231 mount_getspecific(struct mount *mp, specificdata_key_t key)
1232 {
1233 
1234 	return specificdata_getspecific(mount_specificdata_domain,
1235 					 &mp->mnt_specdataref, key);
1236 }
1237 
1238 /*
1239  * mount_setspecific --
1240  *	Set mount-specific data corresponding to the specified key.
1241  */
1242 void
1243 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data)
1244 {
1245 
1246 	specificdata_setspecific(mount_specificdata_domain,
1247 				 &mp->mnt_specdataref, key, data);
1248 }
1249 
1250 /*
1251  * Check to see if a filesystem is mounted on a block device.
1252  */
1253 int
1254 vfs_mountedon(vnode_t *vp)
1255 {
1256 	vnode_t *vq;
1257 	int error = 0;
1258 
1259 	if (vp->v_type != VBLK)
1260 		return ENOTBLK;
1261 	if (spec_node_getmountedfs(vp) != NULL)
1262 		return EBUSY;
1263 	if (spec_node_lookup_by_dev(vp->v_type, vp->v_rdev, &vq) == 0) {
1264 		if (spec_node_getmountedfs(vq) != NULL)
1265 			error = EBUSY;
1266 		vrele(vq);
1267 	}
1268 
1269 	return error;
1270 }
1271 
1272 /*
1273  * Check if a device pointed to by vp is mounted.
1274  *
1275  * Returns:
1276  *   EINVAL	if it's not a disk
1277  *   EBUSY	if it's a disk and mounted
1278  *   0		if it's a disk and not mounted
1279  */
1280 int
1281 rawdev_mounted(vnode_t *vp, vnode_t **bvpp)
1282 {
1283 	vnode_t *bvp;
1284 	dev_t dev;
1285 	int d_type;
1286 
1287 	bvp = NULL;
1288 	d_type = D_OTHER;
1289 
1290 	if (iskmemvp(vp))
1291 		return EINVAL;
1292 
1293 	switch (vp->v_type) {
1294 	case VCHR: {
1295 		const struct cdevsw *cdev;
1296 
1297 		dev = vp->v_rdev;
1298 		cdev = cdevsw_lookup(dev);
1299 		if (cdev != NULL) {
1300 			dev_t blkdev;
1301 
1302 			blkdev = devsw_chr2blk(dev);
1303 			if (blkdev != NODEV) {
1304 				if (vfinddev(blkdev, VBLK, &bvp) != 0) {
1305 					d_type = (cdev->d_flag & D_TYPEMASK);
1306 					/* XXX: what if bvp disappears? */
1307 					vrele(bvp);
1308 				}
1309 			}
1310 		}
1311 
1312 		break;
1313 		}
1314 
1315 	case VBLK: {
1316 		const struct bdevsw *bdev;
1317 
1318 		dev = vp->v_rdev;
1319 		bdev = bdevsw_lookup(dev);
1320 		if (bdev != NULL)
1321 			d_type = (bdev->d_flag & D_TYPEMASK);
1322 
1323 		bvp = vp;
1324 
1325 		break;
1326 		}
1327 
1328 	default:
1329 		break;
1330 	}
1331 
1332 	if (d_type != D_DISK)
1333 		return EINVAL;
1334 
1335 	if (bvpp != NULL)
1336 		*bvpp = bvp;
1337 
1338 	/*
1339 	 * XXX: This is bogus. We should be failing the request
1340 	 * XXX: not only if this specific slice is mounted, but
1341 	 * XXX: if it's on a disk with any other mounted slice.
1342 	 */
1343 	if (vfs_mountedon(bvp))
1344 		return EBUSY;
1345 
1346 	return 0;
1347 }
1348 
1349 /*
1350  * Make a 'unique' number from a mount type name.
1351  */
1352 long
1353 makefstype(const char *type)
1354 {
1355 	long rv;
1356 
1357 	for (rv = 0; *type; type++) {
1358 		rv <<= 2;
1359 		rv ^= *type;
1360 	}
1361 	return rv;
1362 }
1363 
1364 void
1365 mountlist_append(struct mount *mp)
1366 {
1367 	mutex_enter(&mountlist_lock);
1368 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
1369 	mutex_exit(&mountlist_lock);
1370 }
1371