2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
40 #include <sys/vnode.h>
41 #include <sys/mount.h>
43 #include <geom/geom.h>
44 #include <geom/geom_vfs.h>
47 * subroutines for use by filesystems.
49 * XXX: should maybe live somewhere else ?
61 static struct buf_ops __g_vfs_bufops = {
62 .bop_name = "GEOM_VFS",
63 .bop_write = bufwrite,
64 .bop_strategy = g_vfs_strategy,
66 .bop_bdflush = bufbdflush
69 struct buf_ops *g_vfs_bufops = &__g_vfs_bufops;
71 static g_orphan_t g_vfs_orphan;
73 static struct g_class g_vfs_class = {
76 .orphan = g_vfs_orphan,
79 DECLARE_GEOM_CLASS(g_vfs_class, g_vfs);
82 g_vfs_destroy(void *arg, int flags __unused)
84 struct g_consumer *cp;
88 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
89 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
91 if (cp->geom->softc == NULL)
92 g_wither_geom(cp->geom, ENXIO);
96 g_vfs_done(struct bio *bip)
98 struct g_consumer *cp;
99 struct g_vfs_softc *sc;
107 * Collect statistics on synchronous and asynchronous read
108 * and write counts for disks that have associated filesystems.
110 bp = bip->bio_caller2;
114 * If not a disk vnode, use its associated mount point
115 * otherwise use the mountpoint associated with the disk.
118 if (vp->v_type != VCHR ||
119 (cdevp = vp->v_rdev) == NULL ||
120 cdevp->si_devsw == NULL ||
121 (cdevp->si_devsw->d_flags & D_DISK) == 0)
124 mp = cdevp->si_mountpt;
126 if (bp->b_iocmd == BIO_READ) {
127 if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
128 mp->mnt_stat.f_asyncreads++;
130 mp->mnt_stat.f_syncreads++;
131 } else if (bp->b_iocmd == BIO_WRITE) {
132 if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
133 mp->mnt_stat.f_asyncwrites++;
135 mp->mnt_stat.f_syncwrites++;
142 sc = cp->geom->softc;
143 if (bip->bio_error != 0 && bip->bio_error != EOPNOTSUPP) {
144 if ((bp->b_xflags & BX_CVTENXIO) != 0)
145 sc->sc_enxio_active = 1;
146 if (sc->sc_enxio_active)
147 bip->bio_error = ENXIO;
148 g_print_bio("g_vfs_done():", bip, "error = %d",
151 bp->b_error = bip->bio_error;
152 bp->b_ioflags = bip->bio_flags;
154 bp->b_ioflags |= BIO_ERROR;
155 bp->b_resid = bp->b_bcount - bip->bio_completed;
158 mtx_lock(&sc->sc_mtx);
159 destroy = ((--sc->sc_active) == 0 && sc->sc_orphaned);
160 mtx_unlock(&sc->sc_mtx);
162 g_post_event(g_vfs_destroy, cp, M_WAITOK, NULL);
168 g_vfs_strategy(struct bufobj *bo, struct buf *bp)
170 struct g_vfs_softc *sc;
171 struct g_consumer *cp;
175 sc = cp->geom->softc;
178 * If the provider has orphaned us, just return ENXIO.
180 mtx_lock(&sc->sc_mtx);
181 if (sc->sc_orphaned || sc->sc_enxio_active) {
182 mtx_unlock(&sc->sc_mtx);
184 bp->b_ioflags |= BIO_ERROR;
189 mtx_unlock(&sc->sc_mtx);
192 bip->bio_cmd = bp->b_iocmd;
193 bip->bio_offset = bp->b_iooffset;
194 bip->bio_length = bp->b_bcount;
196 if ((bp->b_flags & B_BARRIER) != 0) {
197 bip->bio_flags |= BIO_ORDERED;
198 bp->b_flags &= ~B_BARRIER;
200 if (bp->b_iocmd == BIO_SPEEDUP)
201 bip->bio_flags |= bp->b_ioflags;
202 bip->bio_done = g_vfs_done;
203 bip->bio_caller2 = bp;
204 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
205 buf_track(bp, __func__);
206 bip->bio_track_bp = bp;
208 g_io_request(bip, cp);
212 g_vfs_orphan(struct g_consumer *cp)
215 struct g_vfs_softc *sc;
221 g_trace(G_T_TOPOLOGY, "g_vfs_orphan(%p(%s))", cp, gp->name);
225 mtx_lock(&sc->sc_mtx);
227 destroy = (sc->sc_active == 0);
228 mtx_unlock(&sc->sc_mtx);
230 g_vfs_destroy(cp, 0);
233 * Do not destroy the geom. Filesystem will do that during unmount.
238 g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr)
241 struct g_provider *pp;
242 struct g_consumer *cp;
243 struct g_vfs_softc *sc;
251 if (bo->bo_private != vp)
254 pp = g_dev_getprovider(vp->v_rdev);
257 gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name);
258 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
259 mtx_init(&sc->sc_mtx, "g_vfs", NULL, MTX_DEF);
262 cp = g_new_consumer(gp);
264 error = g_access(cp, 1, wr, wr);
266 g_wither_geom(gp, ENXIO);
269 vnode_create_vobject(vp, pp->mediasize, curthread);
272 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
273 bo->bo_ops = g_vfs_bufops;
275 bo->bo_bsize = pp->sectorsize;
281 g_vfs_close(struct g_consumer *cp)
284 struct g_vfs_softc *sc;
290 bufobj_invalbuf(sc->sc_bo, V_SAVE, 0, 0);
291 sc->sc_bo->bo_private = cp->private;
293 mtx_destroy(&sc->sc_mtx);
294 if (!sc->sc_orphaned || cp->provider == NULL)
295 g_wither_geom_close(gp, ENXIO);