[Midnightbsd-cvs] src [9041] trunk/sys/geom/geom_vfs.c: add mutex and two flags to make orphan call async.

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sat Oct 1 05:56:36 EDT 2016


Revision: 9041
          http://svnweb.midnightbsd.org/src/?rev=9041
Author:   laffer1
Date:     2016-10-01 05:56:36 -0400 (Sat, 01 Oct 2016)
Log Message:
-----------
add mutex and two flags to make orphan call async.

Modified Paths:
--------------
    trunk/sys/geom/geom_vfs.c

Modified: trunk/sys/geom/geom_vfs.c
===================================================================
--- trunk/sys/geom/geom_vfs.c	2016-10-01 09:56:11 UTC (rev 9040)
+++ trunk/sys/geom/geom_vfs.c	2016-10-01 09:56:36 UTC (rev 9041)
@@ -1,4 +1,4 @@
-/* $MidnightBSD: src/sys/geom/geom_vfs.c,v 1.5 2011/12/10 22:55:34 laffer1 Exp $ */
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 2004 Poul-Henning Kamp
  * All rights reserved.
@@ -32,7 +32,9 @@
 #include <sys/systm.h>
 #include <sys/bio.h>
 #include <sys/kernel.h>
+#include <sys/lock.h>
 #include <sys/malloc.h>
+#include <sys/mutex.h>
 #include <sys/vnode.h>
 #include <sys/mount.h>	/* XXX Temporary for VFS_LOCK_GIANT */
 
@@ -46,6 +48,13 @@
  */
 #include <sys/buf.h>
 
+struct g_vfs_softc {
+	struct mtx	 sc_mtx;
+	struct bufobj	*sc_bo;
+	int		 sc_active;
+	int		 sc_orphaned;
+};
+
 static struct buf_ops __g_vfs_bufops = {
 	.bop_name =	"GEOM_VFS",
 	.bop_write =	bufwrite,
@@ -67,25 +76,33 @@
 DECLARE_GEOM_CLASS(g_vfs_class, g_vfs);
 
 static void
+g_vfs_destroy(void *arg, int flags __unused)
+{
+	struct g_consumer *cp;
+
+	g_topology_assert();
+	cp = arg;
+	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
+		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
+	g_detach(cp);
+	if (cp->geom->softc == NULL)
+		g_wither_geom(cp->geom, ENXIO);
+}
+
+static void
 g_vfs_done(struct bio *bip)
 {
+	struct g_consumer *cp;
+	struct g_vfs_softc *sc;
 	struct buf *bp;
-	int vfslocked;
+	int vfslocked, destroy;
 	struct mount *mp;
 	struct vnode *vp;
 	struct cdev *cdevp;
 
+	cp = bip->bio_from;
+	sc = cp->geom->softc;
 	/*
-	 * Provider ('bio_to') could have withered away sometime
-	 * between incrementing the 'nend' in g_io_deliver() and now,
-	 * making 'bio_to' a dangling pointer.  We cannot do that
-	 * in g_wither_geom(), as it would require going over
-	 * the 'g_bio_run_up' list, resetting the pointer.
-	 */
-	if (bip->bio_from->provider == NULL)
-		bip->bio_to = NULL;
-
-	/*
 	 * Collect statistics on synchronous and asynchronous read
 	 * and write counts for disks that have associated filesystems.
 	 * Since this is run by the g_up thread it is single threaded and
@@ -135,6 +152,13 @@
 		bp->b_ioflags |= BIO_ERROR;
 	bp->b_resid = bp->b_bcount - bip->bio_completed;
 	g_destroy_bio(bip);
+
+	mtx_lock(&sc->sc_mtx);
+	destroy = ((--sc->sc_active) == 0 && sc->sc_orphaned);
+	mtx_unlock(&sc->sc_mtx);
+	if (destroy)
+		g_post_event(g_vfs_destroy, cp, M_WAITOK, NULL);
+
 	vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL));
 	bufdone(bp);
 	VFS_UNLOCK_GIANT(vfslocked);
@@ -143,17 +167,20 @@
 void
 g_vfs_strategy(struct bufobj *bo, struct buf *bp)
 {
+	struct g_vfs_softc *sc;
 	struct g_consumer *cp;
 	struct bio *bip;
 	int vfslocked;
 
 	cp = bo->bo_private;
-	/* G_VALID_CONSUMER(cp); We likely lack topology lock */
+	sc = cp->geom->softc;
 
 	/*
 	 * If the provider has orphaned us, just return EXIO.
 	 */
-	if (cp->provider == NULL) {
+	mtx_lock(&sc->sc_mtx);
+	if (sc->sc_orphaned) {
+		mtx_unlock(&sc->sc_mtx);
 		bp->b_error = ENXIO;
 		bp->b_ioflags |= BIO_ERROR;
 		vfslocked = VFS_LOCK_GIANT(((struct mount *)NULL));
@@ -161,6 +188,8 @@
 		VFS_UNLOCK_GIANT(vfslocked);
 		return;
 	}
+	sc->sc_active++;
+	mtx_unlock(&sc->sc_mtx);
 
 	bip = g_alloc_bio();
 	bip->bio_cmd = bp->b_iocmd;
@@ -176,14 +205,20 @@
 g_vfs_orphan(struct g_consumer *cp)
 {
 	struct g_geom *gp;
+	struct g_vfs_softc *sc;
+	int destroy;
 
 	g_topology_assert();
 
 	gp = cp->geom;
+	sc = gp->softc;
 	g_trace(G_T_TOPOLOGY, "g_vfs_orphan(%p(%s))", cp, gp->name);
-	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
-		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
-	g_detach(cp);
+	mtx_lock(&sc->sc_mtx);
+	sc->sc_orphaned = 1;
+	destroy = (sc->sc_active == 0);
+	mtx_unlock(&sc->sc_mtx);
+	if (destroy)
+		g_vfs_destroy(cp, 0);
 
 	/*
 	 * Do not destroy the geom.  Filesystem will do that during unmount.
@@ -196,6 +231,7 @@
 	struct g_geom *gp;
 	struct g_provider *pp;
 	struct g_consumer *cp;
+	struct g_vfs_softc *sc;
 	struct bufobj *bo;
 	int vfslocked;
 	int error;
@@ -211,6 +247,10 @@
 	if (pp == NULL)
 		return (ENOENT);
 	gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name);
+	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
+	mtx_init(&sc->sc_mtx, "g_vfs", NULL, MTX_DEF);
+	sc->sc_bo = bo;
+	gp->softc = sc;
 	cp = g_new_consumer(gp);
 	g_attach(cp, pp);
 	error = g_access(cp, 1, wr, wr);
@@ -226,7 +266,6 @@
 	bo->bo_ops = g_vfs_bufops;
 	bo->bo_private = cp;
 	bo->bo_bsize = pp->sectorsize;
-	gp->softc = bo;
 
 	return (error);
 }
@@ -235,13 +274,17 @@
 g_vfs_close(struct g_consumer *cp)
 {
 	struct g_geom *gp;
-	struct bufobj *bo;
+	struct g_vfs_softc *sc;
 
 	g_topology_assert();
 
 	gp = cp->geom;
-	bo = gp->softc;
-	bufobj_invalbuf(bo, V_SAVE, 0, 0);
-	bo->bo_private = cp->private;
-	g_wither_geom_close(gp, ENXIO);
+	sc = gp->softc;
+	bufobj_invalbuf(sc->sc_bo, V_SAVE, 0, 0);
+	sc->sc_bo->bo_private = cp->private;
+	gp->softc = NULL;
+	mtx_destroy(&sc->sc_mtx);
+	if (!sc->sc_orphaned || cp->provider == NULL)
+		g_wither_geom_close(gp, ENXIO);
+	g_free(sc);
 }



More information about the Midnightbsd-cvs mailing list