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>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
42 #include <geom/geom.h>
43 #include <geom/geom_vfs.h>
46 * subroutines for use by filesystems.
48 * XXX: should maybe live somewhere else ?
59 static struct buf_ops __g_vfs_bufops = {
60 .bop_name = "GEOM_VFS",
61 .bop_write = bufwrite,
62 .bop_strategy = g_vfs_strategy,
64 .bop_bdflush = bufbdflush
67 struct buf_ops *g_vfs_bufops = &__g_vfs_bufops;
69 static g_orphan_t g_vfs_orphan;
71 static struct g_class g_vfs_class = {
74 .orphan = g_vfs_orphan,
77 DECLARE_GEOM_CLASS(g_vfs_class, g_vfs);
80 g_vfs_destroy(void *arg, int flags __unused)
82 struct g_consumer *cp;
86 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
87 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
89 if (cp->geom->softc == NULL)
90 g_wither_geom(cp->geom, ENXIO);
94 g_vfs_done(struct bio *bip)
96 struct g_consumer *cp;
97 struct g_vfs_softc *sc;
105 * Collect statistics on synchronous and asynchronous read
106 * and write counts for disks that have associated filesystems.
108 bp = bip->bio_caller2;
112 * If not a disk vnode, use its associated mount point
113 * otherwise use the mountpoint associated with the disk.
116 if (vp->v_type != VCHR ||
117 (cdevp = vp->v_rdev) == NULL ||
118 cdevp->si_devsw == NULL ||
119 (cdevp->si_devsw->d_flags & D_DISK) == 0)
122 mp = cdevp->si_mountpt;
124 if (bp->b_iocmd == BIO_READ) {
125 if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
126 mp->mnt_stat.f_asyncreads++;
128 mp->mnt_stat.f_syncreads++;
129 } else if (bp->b_iocmd == BIO_WRITE) {
130 if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
131 mp->mnt_stat.f_asyncwrites++;
133 mp->mnt_stat.f_syncwrites++;
140 sc = cp->geom->softc;
141 if (bip->bio_error) {
142 printf("g_vfs_done():");
144 printf("error = %d\n", bip->bio_error);
146 bp->b_error = bip->bio_error;
147 bp->b_ioflags = bip->bio_flags;
149 bp->b_ioflags |= BIO_ERROR;
150 bp->b_resid = bp->b_bcount - bip->bio_completed;
153 mtx_lock(&sc->sc_mtx);
154 destroy = ((--sc->sc_active) == 0 && sc->sc_orphaned);
155 mtx_unlock(&sc->sc_mtx);
157 g_post_event(g_vfs_destroy, cp, M_WAITOK, NULL);
163 g_vfs_strategy(struct bufobj *bo, struct buf *bp)
165 struct g_vfs_softc *sc;
166 struct g_consumer *cp;
170 sc = cp->geom->softc;
173 * If the provider has orphaned us, just return ENXIO.
175 mtx_lock(&sc->sc_mtx);
176 if (sc->sc_orphaned) {
177 mtx_unlock(&sc->sc_mtx);
179 bp->b_ioflags |= BIO_ERROR;
184 mtx_unlock(&sc->sc_mtx);
187 bip->bio_cmd = bp->b_iocmd;
188 bip->bio_offset = bp->b_iooffset;
189 bip->bio_length = bp->b_bcount;
191 if ((bp->b_flags & B_BARRIER) != 0) {
192 bip->bio_flags |= BIO_ORDERED;
193 bp->b_flags &= ~B_BARRIER;
195 bip->bio_done = g_vfs_done;
196 bip->bio_caller2 = bp;
197 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
198 buf_track(bp, __func__);
199 bip->bio_track_bp = bp;
201 g_io_request(bip, cp);
205 g_vfs_orphan(struct g_consumer *cp)
208 struct g_vfs_softc *sc;
214 g_trace(G_T_TOPOLOGY, "g_vfs_orphan(%p(%s))", cp, gp->name);
218 mtx_lock(&sc->sc_mtx);
220 destroy = (sc->sc_active == 0);
221 mtx_unlock(&sc->sc_mtx);
223 g_vfs_destroy(cp, 0);
226 * Do not destroy the geom. Filesystem will do that during unmount.
231 g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr)
234 struct g_provider *pp;
235 struct g_consumer *cp;
236 struct g_vfs_softc *sc;
244 if (bo->bo_private != vp)
247 pp = g_dev_getprovider(vp->v_rdev);
250 gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name);
251 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
252 mtx_init(&sc->sc_mtx, "g_vfs", NULL, MTX_DEF);
255 cp = g_new_consumer(gp);
257 error = g_access(cp, 1, wr, wr);
259 g_wither_geom(gp, ENXIO);
262 vnode_create_vobject(vp, pp->mediasize, curthread);
265 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
266 bo->bo_ops = g_vfs_bufops;
268 bo->bo_bsize = pp->sectorsize;
274 g_vfs_close(struct g_consumer *cp)
277 struct g_vfs_softc *sc;
283 bufobj_invalbuf(sc->sc_bo, V_SAVE, 0, 0);
284 sc->sc_bo->bo_private = cp->private;
286 mtx_destroy(&sc->sc_mtx);
287 if (!sc->sc_orphaned || cp->provider == NULL)
288 g_wither_geom_close(gp, ENXIO);