2004-10-29 09:56:56 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2004 Poul-Henning Kamp
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/bio.h>
|
|
|
|
#include <sys/kernel.h>
|
2011-11-02 09:24:59 +00:00
|
|
|
#include <sys/lock.h>
|
2004-10-29 09:56:56 +00:00
|
|
|
#include <sys/malloc.h>
|
2011-11-02 09:24:59 +00:00
|
|
|
#include <sys/mutex.h>
|
2004-10-29 09:56:56 +00:00
|
|
|
#include <sys/vnode.h>
|
2012-10-22 17:50:54 +00:00
|
|
|
#include <sys/mount.h>
|
2004-10-29 09:56:56 +00:00
|
|
|
|
|
|
|
#include <geom/geom.h>
|
|
|
|
#include <geom/geom_vfs.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* subroutines for use by filesystems.
|
|
|
|
*
|
|
|
|
* XXX: should maybe live somewhere else ?
|
|
|
|
*/
|
|
|
|
#include <sys/buf.h>
|
|
|
|
|
2011-11-02 09:24:59 +00:00
|
|
|
struct g_vfs_softc {
|
|
|
|
struct mtx sc_mtx;
|
|
|
|
struct bufobj *sc_bo;
|
|
|
|
int sc_active;
|
|
|
|
int sc_orphaned;
|
|
|
|
};
|
|
|
|
|
2004-10-29 09:56:56 +00:00
|
|
|
static struct buf_ops __g_vfs_bufops = {
|
|
|
|
.bop_name = "GEOM_VFS",
|
|
|
|
.bop_write = bufwrite,
|
|
|
|
.bop_strategy = g_vfs_strategy,
|
2005-01-11 10:43:08 +00:00
|
|
|
.bop_sync = bufsync,
|
Cylinder group bitmaps and blocks containing inode for a snapshot
file are after snaplock, while other ffs device buffers are before
snaplock in global lock order. By itself, this could cause deadlock
when bdwrite() tries to flush dirty buffers on snapshotted ffs. If,
during the flush, COW activity for snapshot needs to allocate block
and ffs_alloccg() selects the cylinder group that is being written
by bdwrite(), then kernel would panic due to recursive buffer lock
acquision.
Avoid dealing with buffers in bdwrite() that are from other side of
snaplock divisor in the lock order then the buffer being written. Add
new BOP, bop_bdwrite(), to do dirty buffer flushing for same vnode in
the bdwrite(). Default implementation, bufbdflush(), refactors the code
from bdwrite(). For ffs device buffers, specialized implementation is
used.
Reviewed by: tegge, jeff, Russell Cattelan (cattelan xfs org, xfs changes)
Tested by: Peter Holm
X-MFC after: 3 weeks (if ever: it changes ABI)
2007-01-23 10:01:19 +00:00
|
|
|
.bop_bdflush = bufbdflush
|
2004-10-29 09:56:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct buf_ops *g_vfs_bufops = &__g_vfs_bufops;
|
|
|
|
|
2005-02-10 12:10:35 +00:00
|
|
|
static g_orphan_t g_vfs_orphan;
|
|
|
|
|
2004-10-29 09:56:56 +00:00
|
|
|
static struct g_class g_vfs_class = {
|
|
|
|
.name = "VFS",
|
|
|
|
.version = G_VERSION,
|
|
|
|
.orphan = g_vfs_orphan,
|
|
|
|
};
|
|
|
|
|
|
|
|
DECLARE_GEOM_CLASS(g_vfs_class, g_vfs);
|
|
|
|
|
2011-11-02 09:24:59 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-10-29 09:56:56 +00:00
|
|
|
static void
|
|
|
|
g_vfs_done(struct bio *bip)
|
|
|
|
{
|
2011-11-02 09:24:59 +00:00
|
|
|
struct g_consumer *cp;
|
|
|
|
struct g_vfs_softc *sc;
|
2004-10-29 09:56:56 +00:00
|
|
|
struct buf *bp;
|
2012-10-22 17:50:54 +00:00
|
|
|
int destroy;
|
2012-03-28 20:49:11 +00:00
|
|
|
struct mount *mp;
|
|
|
|
struct vnode *vp;
|
2012-04-08 06:20:21 +00:00
|
|
|
struct cdev *cdevp;
|
2012-03-28 20:49:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Collect statistics on synchronous and asynchronous read
|
|
|
|
* and write counts for disks that have associated filesystems.
|
|
|
|
* Since this run by the g_up thread it is single threaded and
|
|
|
|
* we do not need to use atomic increments on the counters.
|
|
|
|
*/
|
|
|
|
bp = bip->bio_caller2;
|
|
|
|
vp = bp->b_vp;
|
2012-04-08 06:20:21 +00:00
|
|
|
if (vp == NULL) {
|
2012-03-28 20:49:11 +00:00
|
|
|
mp = NULL;
|
2012-04-08 06:20:21 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If not a disk vnode, use its associated mount point
|
|
|
|
* otherwise use the mountpoint associated with the disk.
|
|
|
|
*/
|
|
|
|
VI_LOCK(vp);
|
|
|
|
if (vp->v_type != VCHR ||
|
|
|
|
(cdevp = vp->v_rdev) == NULL ||
|
|
|
|
cdevp->si_devsw == NULL ||
|
|
|
|
(cdevp->si_devsw->d_flags & D_DISK) == 0)
|
|
|
|
mp = vp->v_mount;
|
|
|
|
else
|
|
|
|
mp = cdevp->si_mountpt;
|
|
|
|
VI_UNLOCK(vp);
|
|
|
|
}
|
2012-03-28 20:49:11 +00:00
|
|
|
if (mp != NULL) {
|
|
|
|
if (bp->b_iocmd == BIO_WRITE) {
|
|
|
|
if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
|
|
|
|
mp->mnt_stat.f_asyncwrites++;
|
|
|
|
else
|
|
|
|
mp->mnt_stat.f_syncwrites++;
|
|
|
|
} else {
|
|
|
|
if (LK_HOLDER(bp->b_lock.lk_lock) == LK_KERNPROC)
|
|
|
|
mp->mnt_stat.f_asyncreads++;
|
|
|
|
else
|
|
|
|
mp->mnt_stat.f_syncreads++;
|
|
|
|
}
|
|
|
|
}
|
2009-01-11 13:51:04 +00:00
|
|
|
|
2011-11-02 09:24:59 +00:00
|
|
|
cp = bip->bio_from;
|
|
|
|
sc = cp->geom->softc;
|
2004-10-29 09:56:56 +00:00
|
|
|
if (bip->bio_error) {
|
2004-11-04 07:17:08 +00:00
|
|
|
printf("g_vfs_done():");
|
2004-10-29 09:56:56 +00:00
|
|
|
g_print_bio(bip);
|
|
|
|
printf("error = %d\n", bip->bio_error);
|
|
|
|
}
|
|
|
|
bp->b_error = bip->bio_error;
|
|
|
|
bp->b_ioflags = bip->bio_flags;
|
|
|
|
if (bip->bio_error)
|
|
|
|
bp->b_ioflags |= BIO_ERROR;
|
|
|
|
bp->b_resid = bp->b_bcount - bip->bio_completed;
|
|
|
|
g_destroy_bio(bip);
|
2011-11-02 09:24:59 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2004-10-29 09:56:56 +00:00
|
|
|
bufdone(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
g_vfs_strategy(struct bufobj *bo, struct buf *bp)
|
|
|
|
{
|
2011-11-02 09:24:59 +00:00
|
|
|
struct g_vfs_softc *sc;
|
2004-10-29 09:56:56 +00:00
|
|
|
struct g_consumer *cp;
|
|
|
|
struct bio *bip;
|
|
|
|
|
|
|
|
cp = bo->bo_private;
|
2011-11-02 09:24:59 +00:00
|
|
|
sc = cp->geom->softc;
|
2004-10-29 09:56:56 +00:00
|
|
|
|
2008-12-16 17:04:52 +00:00
|
|
|
/*
|
2011-02-21 09:01:34 +00:00
|
|
|
* If the provider has orphaned us, just return EXIO.
|
2008-12-16 17:04:52 +00:00
|
|
|
*/
|
2011-11-02 09:24:59 +00:00
|
|
|
mtx_lock(&sc->sc_mtx);
|
|
|
|
if (sc->sc_orphaned) {
|
|
|
|
mtx_unlock(&sc->sc_mtx);
|
2008-12-16 17:04:52 +00:00
|
|
|
bp->b_error = ENXIO;
|
|
|
|
bp->b_ioflags |= BIO_ERROR;
|
|
|
|
bufdone(bp);
|
|
|
|
return;
|
|
|
|
}
|
2011-11-02 09:24:59 +00:00
|
|
|
sc->sc_active++;
|
|
|
|
mtx_unlock(&sc->sc_mtx);
|
2008-12-16 17:04:52 +00:00
|
|
|
|
2004-10-29 09:56:56 +00:00
|
|
|
bip = g_alloc_bio();
|
|
|
|
bip->bio_cmd = bp->b_iocmd;
|
|
|
|
bip->bio_offset = bp->b_iooffset;
|
|
|
|
bip->bio_data = bp->b_data;
|
|
|
|
bip->bio_done = g_vfs_done;
|
|
|
|
bip->bio_caller2 = bp;
|
|
|
|
bip->bio_length = bp->b_bcount;
|
|
|
|
g_io_request(bip, cp);
|
|
|
|
}
|
|
|
|
|
2005-02-10 12:10:35 +00:00
|
|
|
static void
|
2004-10-29 09:56:56 +00:00
|
|
|
g_vfs_orphan(struct g_consumer *cp)
|
|
|
|
{
|
2008-12-16 17:04:52 +00:00
|
|
|
struct g_geom *gp;
|
2011-11-02 09:24:59 +00:00
|
|
|
struct g_vfs_softc *sc;
|
|
|
|
int destroy;
|
2008-12-16 17:04:52 +00:00
|
|
|
|
|
|
|
g_topology_assert();
|
|
|
|
|
|
|
|
gp = cp->geom;
|
|
|
|
g_trace(G_T_TOPOLOGY, "g_vfs_orphan(%p(%s))", cp, gp->name);
|
2011-12-02 17:09:48 +00:00
|
|
|
sc = gp->softc;
|
|
|
|
if (sc == NULL)
|
|
|
|
return;
|
2011-11-02 09:24:59 +00:00
|
|
|
mtx_lock(&sc->sc_mtx);
|
|
|
|
sc->sc_orphaned = 1;
|
2012-07-29 20:04:09 +00:00
|
|
|
destroy = (sc->sc_active == 0);
|
2011-11-02 09:24:59 +00:00
|
|
|
mtx_unlock(&sc->sc_mtx);
|
|
|
|
if (destroy)
|
|
|
|
g_vfs_destroy(cp, 0);
|
2004-10-29 09:56:56 +00:00
|
|
|
|
|
|
|
/*
|
2009-01-11 13:51:04 +00:00
|
|
|
* Do not destroy the geom. Filesystem will do that during unmount.
|
2004-10-29 09:56:56 +00:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
g_vfs_open(struct vnode *vp, struct g_consumer **cpp, const char *fsname, int wr)
|
|
|
|
{
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_provider *pp;
|
|
|
|
struct g_consumer *cp;
|
2011-11-02 09:24:59 +00:00
|
|
|
struct g_vfs_softc *sc;
|
2004-10-29 09:56:56 +00:00
|
|
|
struct bufobj *bo;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
g_topology_assert();
|
|
|
|
|
|
|
|
*cpp = NULL;
|
2010-04-03 08:53:53 +00:00
|
|
|
bo = &vp->v_bufobj;
|
|
|
|
if (bo->bo_private != vp)
|
|
|
|
return (EBUSY);
|
|
|
|
|
2004-10-29 09:56:56 +00:00
|
|
|
pp = g_dev_getprovider(vp->v_rdev);
|
|
|
|
if (pp == NULL)
|
|
|
|
return (ENOENT);
|
|
|
|
gp = g_new_geomf(&g_vfs_class, "%s.%s", fsname, pp->name);
|
2011-11-02 09:24:59 +00:00
|
|
|
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;
|
2004-10-29 09:56:56 +00:00
|
|
|
cp = g_new_consumer(gp);
|
|
|
|
g_attach(cp, pp);
|
2011-07-10 00:41:31 +00:00
|
|
|
error = g_access(cp, 1, wr, wr);
|
2004-10-29 09:56:56 +00:00
|
|
|
if (error) {
|
|
|
|
g_wither_geom(gp, ENXIO);
|
|
|
|
return (error);
|
|
|
|
}
|
2005-01-24 22:41:21 +00:00
|
|
|
vnode_create_vobject(vp, pp->mediasize, curthread);
|
2004-10-29 09:56:56 +00:00
|
|
|
*cpp = cp;
|
2010-04-03 08:53:53 +00:00
|
|
|
cp->private = vp;
|
2004-10-29 09:56:56 +00:00
|
|
|
bo->bo_ops = g_vfs_bufops;
|
|
|
|
bo->bo_private = cp;
|
2010-04-02 15:12:31 +00:00
|
|
|
bo->bo_bsize = pp->sectorsize;
|
2004-10-29 09:56:56 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
2005-01-25 15:52:04 +00:00
|
|
|
|
|
|
|
void
|
2008-10-10 21:23:50 +00:00
|
|
|
g_vfs_close(struct g_consumer *cp)
|
2005-01-25 15:52:04 +00:00
|
|
|
{
|
|
|
|
struct g_geom *gp;
|
2011-11-02 09:24:59 +00:00
|
|
|
struct g_vfs_softc *sc;
|
2005-01-25 15:52:04 +00:00
|
|
|
|
|
|
|
g_topology_assert();
|
|
|
|
|
|
|
|
gp = cp->geom;
|
2011-11-02 09:24:59 +00:00
|
|
|
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);
|
2005-01-25 15:52:04 +00:00
|
|
|
}
|