2 * Copyright (c) 2004 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/vnode.h>
38 #include <sys/mount.h>
40 #include <geom/geom.h>
41 #include <geom/geom_vfs.h>
44 * subroutines for use by filesystems.
46 * XXX: should maybe live somewhere else ?
57 static struct buf_ops __g_vfs_bufops = {
58 .bop_name = "GEOM_VFS",
59 .bop_write = bufwrite,
60 .bop_strategy = g_vfs_strategy,
62 .bop_bdflush = bufbdflush
65 struct buf_ops *g_vfs_bufops = &__g_vfs_bufops;
67 static g_orphan_t g_vfs_orphan;
69 static struct g_class g_vfs_class = {
72 .orphan = g_vfs_orphan,
75 DECLARE_GEOM_CLASS(g_vfs_class, g_vfs);
78 g_vfs_destroy(void *arg, int flags __unused)
80 struct g_consumer *cp;
84 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
85 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
87 if (cp->geom->softc == NULL)
88 g_wither_geom(cp->geom, ENXIO);
92 g_vfs_done(struct bio *bip)
94 struct g_consumer *cp;
95 struct g_vfs_softc *sc;
103 * Collect statistics on synchronous and asynchronous read
104 * and write counts for disks that have associated filesystems.
105 * Since this run by the g_up thread it is single threaded and
106 * we do not need to use atomic increments on the counters.
108 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;
128 if (bp->b_iocmd == BIO_WRITE) {
129 if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
130 mp->mnt_stat.f_asyncwrites++;
132 mp->mnt_stat.f_syncwrites++;
134 if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
135 mp->mnt_stat.f_asyncreads++;
137 mp->mnt_stat.f_syncreads++;
142 sc = cp->geom->softc;
143 if (bip->bio_error) {
144 printf("g_vfs_done():");
146 printf("error = %d\n", bip->bio_error);
148 bp->b_error = bip->bio_error;
149 bp->b_ioflags = bip->bio_flags;
151 bp->b_ioflags |= BIO_ERROR;
152 bp->b_resid = bp->b_bcount - bip->bio_completed;
155 mtx_lock(&sc->sc_mtx);
156 destroy = ((--sc->sc_active) == 0 && sc->sc_orphaned);
157 mtx_unlock(&sc->sc_mtx);
159 g_post_event(g_vfs_destroy, cp, M_WAITOK, NULL);
165 g_vfs_strategy(struct bufobj *bo, struct buf *bp)
167 struct g_vfs_softc *sc;
168 struct g_consumer *cp;
172 sc = cp->geom->softc;
175 * If the provider has orphaned us, just return EXIO.
177 mtx_lock(&sc->sc_mtx);
178 if (sc->sc_orphaned) {
179 mtx_unlock(&sc->sc_mtx);
181 bp->b_ioflags |= BIO_ERROR;
186 mtx_unlock(&sc->sc_mtx);
189 bip->bio_cmd = bp->b_iocmd;
190 bip->bio_offset = bp->b_iooffset;
191 bip->bio_length = bp->b_bcount;
193 if ((bp->b_flags & B_BARRIER) != 0) {
194 bip->bio_flags |= BIO_ORDERED;
195 bp->b_flags &= ~B_BARRIER;
197 bip->bio_done = g_vfs_done;
198 bip->bio_caller2 = bp;
199 g_io_request(bip, cp);
203 g_vfs_orphan(struct g_consumer *cp)
206 struct g_vfs_softc *sc;
212 g_trace(G_T_TOPOLOGY, "g_vfs_orphan(%p(%s))", cp, gp->name);
216 mtx_lock(&sc->sc_mtx);
218 destroy = (sc->sc_active == 0);
219 mtx_unlock(&sc->sc_mtx);
221 g_vfs_destroy(cp, 0);
224 * Do not destroy the geom. Filesystem will do that during unmount.
229 g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr)
232 struct g_provider *pp;
233 struct g_consumer *cp;
234 struct g_vfs_softc *sc;
242 if (bo->bo_private != vp)
245 pp = g_dev_getprovider(vp->v_rdev);
248 gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name);
249 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
250 mtx_init(&sc->sc_mtx, "g_vfs", NULL, MTX_DEF);
253 cp = g_new_consumer(gp);
255 error = g_access(cp, 1, wr, wr);
257 g_wither_geom(gp, ENXIO);
260 vnode_create_vobject(vp, pp->mediasize, curthread);
263 bo->bo_ops = g_vfs_bufops;
265 bo->bo_bsize = pp->sectorsize;
271 g_vfs_close(struct g_consumer *cp)
274 struct g_vfs_softc *sc;
280 bufobj_invalbuf(sc->sc_bo, V_SAVE, 0, 0);
281 sc->sc_bo->bo_private = cp->private;
283 mtx_destroy(&sc->sc_mtx);
284 if (!sc->sc_orphaned || cp->provider == NULL)
285 g_wither_geom_close(gp, ENXIO);