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