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