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>
34 #include <sys/malloc.h>
35 #include <sys/vnode.h>
36 #include <sys/mount.h> /* XXX Temporary for VFS_LOCK_GIANT */
38 #include <geom/geom.h>
39 #include <geom/geom_vfs.h>
42 * subroutines for use by filesystems.
44 * XXX: should maybe live somewhere else ?
48 static struct buf_ops __g_vfs_bufops = {
49 .bop_name = "GEOM_VFS",
50 .bop_write = bufwrite,
51 .bop_strategy = g_vfs_strategy,
53 .bop_bdflush = bufbdflush
56 struct buf_ops *g_vfs_bufops = &__g_vfs_bufops;
58 static g_orphan_t g_vfs_orphan;
60 static struct g_class g_vfs_class = {
63 .orphan = g_vfs_orphan,
66 DECLARE_GEOM_CLASS(g_vfs_class, g_vfs);
69 g_vfs_done(struct bio *bip)
75 * Provider ('bio_to') could have withered away sometime
76 * between incrementing the 'nend' in g_io_deliver() and now,
77 * making 'bio_to' a dangling pointer. We cannot do that
78 * in g_wither_geom(), as it would require going over
79 * the 'g_bio_run_up' list, resetting the pointer.
81 if (bip->bio_from->provider == NULL)
85 printf("g_vfs_done():");
87 printf("error = %d\n", bip->bio_error);
89 bp = bip->bio_caller2;
90 bp->b_error = bip->bio_error;
91 bp->b_ioflags = bip->bio_flags;
93 bp->b_ioflags |= BIO_ERROR;
94 bp->b_resid = bp->b_bcount - bip->bio_completed;
96 vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL));
98 VFS_UNLOCK_GIANT(vfslocked);
102 g_vfs_strategy(struct bufobj *bo, struct buf *bp)
104 struct g_consumer *cp;
109 G_VALID_CONSUMER(cp);
112 * If the the provider has orphaned us, just return EXIO.
114 if (cp->provider == NULL) {
116 bp->b_ioflags |= BIO_ERROR;
117 vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL));
119 VFS_UNLOCK_GIANT(vfslocked);
124 bip->bio_cmd = bp->b_iocmd;
125 bip->bio_offset = bp->b_iooffset;
126 bip->bio_data = bp->b_data;
127 bip->bio_done = g_vfs_done;
128 bip->bio_caller2 = bp;
129 bip->bio_length = bp->b_bcount;
130 g_io_request(bip, cp);
134 g_vfs_orphan(struct g_consumer *cp)
143 g_trace(G_T_TOPOLOGY, "g_vfs_orphan(%p(%s))", cp, gp->name);
144 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
145 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
149 * Do not destroy the geom. Filesystem will do that during unmount.
154 g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr)
157 struct g_provider *pp;
158 struct g_consumer *cp;
166 pp = g_dev_getprovider(vp->v_rdev);
169 gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name);
170 cp = g_new_consumer(gp);
172 error = g_access(cp, 1, wr, 1);
174 g_wither_geom(gp, ENXIO);
177 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
178 vnode_create_vobject(vp, pp->mediasize, curthread);
179 VFS_UNLOCK_GIANT(vfslocked);
182 bo->bo_ops = g_vfs_bufops;
184 bo->bo_bsize = pp->sectorsize;
191 g_vfs_close(struct g_consumer *cp, struct thread *td)
200 bufobj_invalbuf(bo, V_SAVE, td, 0, 0);
201 g_wither_geom_close(gp, ENXIO);