xref: /freebsd/sys/geom/geom_vfs.c (revision 3ef51c5fb9163f2aafb1c14729e06a8bf0c4d113)
1 /*-
2  * Copyright (c) 2004 Poul-Henning Kamp
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bio.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/vnode.h>
38 #include <sys/mount.h>	/* XXX Temporary for VFS_LOCK_GIANT */
39 
40 #include <geom/geom.h>
41 #include <geom/geom_vfs.h>
42 
43 /*
44  * subroutines for use by filesystems.
45  *
46  * XXX: should maybe live somewhere else ?
47  */
48 #include <sys/buf.h>
49 
50 struct g_vfs_softc {
51 	struct mtx	 sc_mtx;
52 	struct bufobj	*sc_bo;
53 	int		 sc_active;
54 	int		 sc_orphaned;
55 };
56 
57 static struct buf_ops __g_vfs_bufops = {
58 	.bop_name =	"GEOM_VFS",
59 	.bop_write =	bufwrite,
60 	.bop_strategy =	g_vfs_strategy,
61 	.bop_sync =	bufsync,
62 	.bop_bdflush =	bufbdflush
63 };
64 
65 struct buf_ops *g_vfs_bufops = &__g_vfs_bufops;
66 
67 static g_orphan_t g_vfs_orphan;
68 
69 static struct g_class g_vfs_class = {
70 	.name =		"VFS",
71 	.version =	G_VERSION,
72 	.orphan =	g_vfs_orphan,
73 };
74 
75 DECLARE_GEOM_CLASS(g_vfs_class, g_vfs);
76 
77 static void
78 g_vfs_destroy(void *arg, int flags __unused)
79 {
80 	struct g_consumer *cp;
81 
82 	g_topology_assert();
83 	cp = arg;
84 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
85 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
86 	g_detach(cp);
87 	if (cp->geom->softc == NULL)
88 		g_wither_geom(cp->geom, ENXIO);
89 }
90 
91 static void
92 g_vfs_done(struct bio *bip)
93 {
94 	struct g_consumer *cp;
95 	struct g_vfs_softc *sc;
96 	struct buf *bp;
97 	int vfslocked, destroy;
98 	struct mount *mp;
99 	struct vnode *vp;
100 
101 	/*
102 	 * Collect statistics on synchronous and asynchronous read
103 	 * and write counts for disks that have associated filesystems.
104 	 * Since this run by the g_up thread it is single threaded and
105 	 * we do not need to use atomic increments on the counters.
106 	 */
107 	bp = bip->bio_caller2;
108 	vp = bp->b_vp;
109 	if (vp == NULL)
110 		mp = NULL;
111 	else if (vn_isdisk(vp, NULL))
112 		mp = vp->v_rdev->si_mountpt;
113 	else
114 		mp = vp->v_mount;
115 	if (mp != NULL) {
116 		if (bp->b_iocmd == BIO_WRITE) {
117 			if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
118 				mp->mnt_stat.f_asyncwrites++;
119 			else
120 				mp->mnt_stat.f_syncwrites++;
121 		} else {
122 			if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
123 				mp->mnt_stat.f_asyncreads++;
124 			else
125 				mp->mnt_stat.f_syncreads++;
126 		}
127 	}
128 
129 	cp = bip->bio_from;
130 	sc = cp->geom->softc;
131 	if (bip->bio_error) {
132 		printf("g_vfs_done():");
133 		g_print_bio(bip);
134 		printf("error = %d\n", bip->bio_error);
135 	}
136 	bp->b_error = bip->bio_error;
137 	bp->b_ioflags = bip->bio_flags;
138 	if (bip->bio_error)
139 		bp->b_ioflags |= BIO_ERROR;
140 	bp->b_resid = bp->b_bcount - bip->bio_completed;
141 	g_destroy_bio(bip);
142 
143 	mtx_lock(&sc->sc_mtx);
144 	destroy = ((--sc->sc_active) == 0 && sc->sc_orphaned);
145 	mtx_unlock(&sc->sc_mtx);
146 	if (destroy)
147 		g_post_event(g_vfs_destroy, cp, M_WAITOK, NULL);
148 
149 	vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL));
150 	bufdone(bp);
151 	VFS_UNLOCK_GIANT(vfslocked);
152 }
153 
154 void
155 g_vfs_strategy(struct bufobj *bo, struct buf *bp)
156 {
157 	struct g_vfs_softc *sc;
158 	struct g_consumer *cp;
159 	struct bio *bip;
160 	int vfslocked;
161 
162 	cp = bo->bo_private;
163 	sc = cp->geom->softc;
164 
165 	/*
166 	 * If the provider has orphaned us, just return EXIO.
167 	 */
168 	mtx_lock(&sc->sc_mtx);
169 	if (sc->sc_orphaned) {
170 		mtx_unlock(&sc->sc_mtx);
171 		bp->b_error = ENXIO;
172 		bp->b_ioflags |= BIO_ERROR;
173 		vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL));
174 		bufdone(bp);
175 		VFS_UNLOCK_GIANT(vfslocked);
176 		return;
177 	}
178 	sc->sc_active++;
179 	mtx_unlock(&sc->sc_mtx);
180 
181 	bip = g_alloc_bio();
182 	bip->bio_cmd = bp->b_iocmd;
183 	bip->bio_offset = bp->b_iooffset;
184 	bip->bio_data = bp->b_data;
185 	bip->bio_done = g_vfs_done;
186 	bip->bio_caller2 = bp;
187 	bip->bio_length = bp->b_bcount;
188 	g_io_request(bip, cp);
189 }
190 
191 static void
192 g_vfs_orphan(struct g_consumer *cp)
193 {
194 	struct g_geom *gp;
195 	struct g_vfs_softc *sc;
196 	int destroy;
197 
198 	g_topology_assert();
199 
200 	gp = cp->geom;
201 	g_trace(G_T_TOPOLOGY, "g_vfs_orphan(%p(%s))", cp, gp->name);
202 	sc = gp->softc;
203 	if (sc == NULL)
204 		return;
205 	mtx_lock(&sc->sc_mtx);
206 	sc->sc_orphaned = 1;
207 	destroy = (sc->sc_active == 0);
208 	mtx_unlock(&sc->sc_mtx);
209 	if (destroy)
210 		g_vfs_destroy(cp, 0);
211 
212 	/*
213 	 * Do not destroy the geom.  Filesystem will do that during unmount.
214 	 */
215 }
216 
217 int
218 g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr)
219 {
220 	struct g_geom *gp;
221 	struct g_provider *pp;
222 	struct g_consumer *cp;
223 	struct g_vfs_softc *sc;
224 	struct bufobj *bo;
225 	int vfslocked;
226 	int error;
227 
228 	g_topology_assert();
229 
230 	*cpp = NULL;
231 	bo = &vp->v_bufobj;
232 	if (bo->bo_private != vp)
233 		return (EBUSY);
234 
235 	pp = g_dev_getprovider(vp->v_rdev);
236 	if (pp == NULL)
237 		return (ENOENT);
238 	gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name);
239 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
240 	mtx_init(&sc->sc_mtx, "g_vfs", NULL, MTX_DEF);
241 	sc->sc_bo = bo;
242 	gp->softc = sc;
243 	cp = g_new_consumer(gp);
244 	g_attach(cp, pp);
245 	error = g_access(cp, 1, wr, wr);
246 	if (error) {
247 		g_wither_geom(gp, ENXIO);
248 		return (error);
249 	}
250 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
251 	vnode_create_vobject(vp, pp->mediasize, curthread);
252 	VFS_UNLOCK_GIANT(vfslocked);
253 	*cpp = cp;
254 	cp->private = vp;
255 	bo->bo_ops = g_vfs_bufops;
256 	bo->bo_private = cp;
257 	bo->bo_bsize = pp->sectorsize;
258 
259 	return (error);
260 }
261 
262 void
263 g_vfs_close(struct g_consumer *cp)
264 {
265 	struct g_geom *gp;
266 	struct g_vfs_softc *sc;
267 
268 	g_topology_assert();
269 
270 	gp = cp->geom;
271 	sc = gp->softc;
272 	bufobj_invalbuf(sc->sc_bo, V_SAVE, 0, 0);
273 	sc->sc_bo->bo_private = cp->private;
274 	gp->softc = NULL;
275 	mtx_destroy(&sc->sc_mtx);
276 	if (!sc->sc_orphaned || cp->provider == NULL)
277 		g_wither_geom_close(gp, ENXIO);
278 	g_free(sc);
279 }
280