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