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