Mon Mar 6 10:10:43 2017 UTC ()
Deny unmounting file systems below layered file systems.


(hannken)
diff -r1.48 -r1.49 src/sys/kern/vfs_mount.c

cvs diff -r1.48 -r1.49 src/sys/kern/vfs_mount.c (switch to unified diff)

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