xref: /freebsd/sys/geom/geom_vfs.c (revision 206b73d0)
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 };
59 
60 static struct buf_ops __g_vfs_bufops = {
61 	.bop_name =	"GEOM_VFS",
62 	.bop_write =	bufwrite,
63 	.bop_strategy =	g_vfs_strategy,
64 	.bop_sync =	bufsync,
65 	.bop_bdflush =	bufbdflush
66 };
67 
68 struct buf_ops *g_vfs_bufops = &__g_vfs_bufops;
69 
70 static g_orphan_t g_vfs_orphan;
71 
72 static struct g_class g_vfs_class = {
73 	.name =		"VFS",
74 	.version =	G_VERSION,
75 	.orphan =	g_vfs_orphan,
76 };
77 
78 DECLARE_GEOM_CLASS(g_vfs_class, g_vfs);
79 
80 static void
81 g_vfs_destroy(void *arg, int flags __unused)
82 {
83 	struct g_consumer *cp;
84 
85 	g_topology_assert();
86 	cp = arg;
87 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
88 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
89 	g_detach(cp);
90 	if (cp->geom->softc == NULL)
91 		g_wither_geom(cp->geom, ENXIO);
92 }
93 
94 static void
95 g_vfs_done(struct bio *bip)
96 {
97 	struct g_consumer *cp;
98 	struct g_vfs_softc *sc;
99 	struct buf *bp;
100 	int destroy;
101 	struct mount *mp;
102 	struct vnode *vp;
103 	struct cdev *cdevp;
104 
105 	/*
106 	 * Collect statistics on synchronous and asynchronous read
107 	 * and write counts for disks that have associated filesystems.
108 	 */
109 	bp = bip->bio_caller2;
110 	vp = bp->b_vp;
111 	if (vp != NULL) {
112 		/*
113 		 * If not a disk vnode, use its associated mount point
114 		 * otherwise use the mountpoint associated with the disk.
115 		 */
116 		VI_LOCK(vp);
117 		if (vp->v_type != VCHR ||
118 		    (cdevp = vp->v_rdev) == NULL ||
119 		    cdevp->si_devsw == NULL ||
120 		    (cdevp->si_devsw->d_flags & D_DISK) == 0)
121 			mp = vp->v_mount;
122 		else
123 			mp = cdevp->si_mountpt;
124 		if (mp != NULL) {
125 			if (bp->b_iocmd == BIO_READ) {
126 				if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
127 					mp->mnt_stat.f_asyncreads++;
128 				else
129 					mp->mnt_stat.f_syncreads++;
130 			} else if (bp->b_iocmd == BIO_WRITE) {
131 				if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
132 					mp->mnt_stat.f_asyncwrites++;
133 				else
134 					mp->mnt_stat.f_syncwrites++;
135 			}
136 		}
137 		VI_UNLOCK(vp);
138 	}
139 
140 	cp = bip->bio_from;
141 	sc = cp->geom->softc;
142 	if (bip->bio_error)
143 		g_print_bio("g_vfs_done():", bip, "error = %d",
144 		    bip->bio_error);
145 	bp->b_error = bip->bio_error;
146 	bp->b_ioflags = bip->bio_flags;
147 	if (bip->bio_error)
148 		bp->b_ioflags |= BIO_ERROR;
149 	bp->b_resid = bp->b_bcount - bip->bio_completed;
150 	g_destroy_bio(bip);
151 
152 	mtx_lock(&sc->sc_mtx);
153 	destroy = ((--sc->sc_active) == 0 && sc->sc_orphaned);
154 	mtx_unlock(&sc->sc_mtx);
155 	if (destroy)
156 		g_post_event(g_vfs_destroy, cp, M_WAITOK, NULL);
157 
158 	bufdone(bp);
159 }
160 
161 void
162 g_vfs_strategy(struct bufobj *bo, struct buf *bp)
163 {
164 	struct g_vfs_softc *sc;
165 	struct g_consumer *cp;
166 	struct bio *bip;
167 
168 	cp = bo->bo_private;
169 	sc = cp->geom->softc;
170 
171 	/*
172 	 * If the provider has orphaned us, just return ENXIO.
173 	 */
174 	mtx_lock(&sc->sc_mtx);
175 	if (sc->sc_orphaned) {
176 		mtx_unlock(&sc->sc_mtx);
177 		bp->b_error = ENXIO;
178 		bp->b_ioflags |= BIO_ERROR;
179 		bufdone(bp);
180 		return;
181 	}
182 	sc->sc_active++;
183 	mtx_unlock(&sc->sc_mtx);
184 
185 	bip = g_alloc_bio();
186 	bip->bio_cmd = bp->b_iocmd;
187 	bip->bio_offset = bp->b_iooffset;
188 	bip->bio_length = bp->b_bcount;
189 	bdata2bio(bp, bip);
190 	if ((bp->b_flags & B_BARRIER) != 0) {
191 		bip->bio_flags |= BIO_ORDERED;
192 		bp->b_flags &= ~B_BARRIER;
193 	}
194 	bip->bio_done = g_vfs_done;
195 	bip->bio_caller2 = bp;
196 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
197 	buf_track(bp, __func__);
198 	bip->bio_track_bp = bp;
199 #endif
200 	g_io_request(bip, cp);
201 }
202 
203 static void
204 g_vfs_orphan(struct g_consumer *cp)
205 {
206 	struct g_geom *gp;
207 	struct g_vfs_softc *sc;
208 	int destroy;
209 
210 	g_topology_assert();
211 
212 	gp = cp->geom;
213 	g_trace(G_T_TOPOLOGY, "g_vfs_orphan(%p(%s))", cp, gp->name);
214 	sc = gp->softc;
215 	if (sc == NULL)
216 		return;
217 	mtx_lock(&sc->sc_mtx);
218 	sc->sc_orphaned = 1;
219 	destroy = (sc->sc_active == 0);
220 	mtx_unlock(&sc->sc_mtx);
221 	if (destroy)
222 		g_vfs_destroy(cp, 0);
223 
224 	/*
225 	 * Do not destroy the geom.  Filesystem will do that during unmount.
226 	 */
227 }
228 
229 int
230 g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr)
231 {
232 	struct g_geom *gp;
233 	struct g_provider *pp;
234 	struct g_consumer *cp;
235 	struct g_vfs_softc *sc;
236 	struct bufobj *bo;
237 	int error;
238 
239 	g_topology_assert();
240 
241 	*cpp = NULL;
242 	bo = &vp->v_bufobj;
243 	if (bo->bo_private != vp)
244 		return (EBUSY);
245 
246 	pp = g_dev_getprovider(vp->v_rdev);
247 	if (pp == NULL)
248 		return (ENOENT);
249 	gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name);
250 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
251 	mtx_init(&sc->sc_mtx, "g_vfs", NULL, MTX_DEF);
252 	sc->sc_bo = bo;
253 	gp->softc = sc;
254 	cp = g_new_consumer(gp);
255 	g_attach(cp, pp);
256 	error = g_access(cp, 1, wr, wr);
257 	if (error) {
258 		g_wither_geom(gp, ENXIO);
259 		return (error);
260 	}
261 	vnode_create_vobject(vp, pp->mediasize, curthread);
262 	*cpp = cp;
263 	cp->private = vp;
264 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
265 	bo->bo_ops = g_vfs_bufops;
266 	bo->bo_private = cp;
267 	bo->bo_bsize = pp->sectorsize;
268 
269 	return (error);
270 }
271 
272 void
273 g_vfs_close(struct g_consumer *cp)
274 {
275 	struct g_geom *gp;
276 	struct g_vfs_softc *sc;
277 
278 	g_topology_assert();
279 
280 	gp = cp->geom;
281 	sc = gp->softc;
282 	bufobj_invalbuf(sc->sc_bo, V_SAVE, 0, 0);
283 	sc->sc_bo->bo_private = cp->private;
284 	gp->softc = NULL;
285 	mtx_destroy(&sc->sc_mtx);
286 	if (!sc->sc_orphaned || cp->provider == NULL)
287 		g_wither_geom_close(gp, ENXIO);
288 	g_free(sc);
289 }
290