#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.292 2026/05/03 16:02:35 thorpej Exp $");
#if defined(_KERNEL_OPT)
#include "opt_vnd.h"
#include "opt_compat_netbsd.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <sys/errno.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/malloc.h>
#include <sys/ioctl.h>
#include <sys/disklabel.h>
#include <sys/device.h>
#include <sys/disk.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/fstrans.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/conf.h>
#include <sys/kauth.h>
#include <sys/module.h>
#include <sys/compat_stub.h>
#include <sys/atomic.h>
#include <uvm/uvm.h>
#include <net/zlib.h>
#include <miscfs/genfs/genfs.h>
#include <miscfs/specfs/specdev.h>
#include <dev/dkvar.h>
#include <dev/vndvar.h>
#include "ioconf.h"
#if defined(VNDDEBUG) && !defined(DEBUG)
#define DEBUG
#endif
#ifdef DEBUG
int dovndcluster = 1;
#define VDB_FOLLOW 0x01
#define VDB_INIT 0x02
#define VDB_IO 0x04
#define VDB_LABEL 0x08
int vnddebug = 0;
#endif
#define vndunit(x) DISKUNIT(x)
struct vndxfer {
struct buf vx_buf;
struct vnd_softc *vx_vnd;
};
#define VND_BUFTOXFER(bp) ((struct vndxfer *)(void *)bp)
#define VND_GETXFER(vnd) pool_get(&(vnd)->sc_vxpool, PR_WAITOK)
#define VND_PUTXFER(vnd, vx) pool_put(&(vnd)->sc_vxpool, (vx))
#define VNDLABELDEV(dev) \
(MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
#define VND_MAXPENDING(vnd) ((vnd)->sc_maxactive * 4)
#define VND_MAXPAGES(vnd) (1024 * 1024 / PAGE_SIZE)
static void vndclear(struct vnd_softc *, int);
static int vnddoclear(struct vnd_softc *, int, int, bool);
static int vndsetcred(struct vnd_softc *, kauth_cred_t);
static void vndthrottle(struct vnd_softc *, struct vnode *);
static void vndiodone(struct buf *);
#if 0
static void vndshutdown(void);
#endif
static void vndgetdefaultlabel(struct vnd_softc *, struct disklabel *);
static void vndgetdisklabel(dev_t, struct vnd_softc *);
static int vndlock(struct vnd_softc *);
static void vndunlock(struct vnd_softc *);
#ifdef VND_COMPRESSION
static void compstrategy(struct buf *, off_t);
static void *vnd_alloc(void *, u_int, u_int);
static void vnd_free(void *, void *);
#endif
static void vndthread(void *);
static bool vnode_has_op(const struct vnode *, int);
static void handle_with_rdwr(struct vnd_softc *, const struct buf *,
struct buf *);
static void handle_with_strategy(struct vnd_softc *, const struct buf *,
struct buf *);
static void vnd_set_geometry(struct vnd_softc *);
static dev_type_open(vndopen);
static dev_type_close(vndclose);
static dev_type_read(vndread);
static dev_type_write(vndwrite);
static dev_type_ioctl(vndioctl);
static dev_type_strategy(vndstrategy);
static dev_type_dump(vnddump);
static dev_type_size(vndsize);
const struct bdevsw vnd_bdevsw = {
.d_open = vndopen,
.d_close = vndclose,
.d_strategy = vndstrategy,
.d_ioctl = vndioctl,
.d_dump = vnddump,
.d_psize = vndsize,
.d_discard = nodiscard,
.d_flag = D_DISK
};
const struct cdevsw vnd_cdevsw = {
.d_open = vndopen,
.d_close = vndclose,
.d_read = vndread,
.d_write = vndwrite,
.d_ioctl = vndioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_DISK
};
static int vnd_match(device_t, cfdata_t, void *);
static void vnd_attach(device_t, device_t, void *);
static int vnd_detach(device_t, int);
CFATTACH_DECL3_NEW(vnd, sizeof(struct vnd_softc),
vnd_match, vnd_attach, vnd_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
static struct vnd_softc *vnd_spawn(int);
static int vnd_destroy(device_t);
static const struct dkdriver vnddkdriver = {
.d_strategy = vndstrategy,
.d_minphys = minphys
};
void
vndattach(int num)
{
int error;
error = config_cfattach_attach(vnd_cd.cd_name, &vnd_ca);
if (error)
aprint_error("%s: unable to register cfattach, error = %d\n",
vnd_cd.cd_name, error);
}
static int
vnd_match(device_t self, cfdata_t cfdata, void *aux)
{
return 1;
}
static void
vnd_attach(device_t parent, device_t self, void *aux)
{
struct vnd_softc *sc = device_private(self);
sc->sc_dev = self;
sc->sc_comp_offsets = NULL;
sc->sc_comp_buff = NULL;
sc->sc_comp_decombuf = NULL;
bufq_alloc(&sc->sc_tab, "disksort", BUFQ_SORT_RAWBLOCK);
disk_init(&sc->sc_dkdev, device_xname(self), &vnddkdriver);
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "couldn't establish power handler\n");
}
static int
vnd_detach(device_t self, int flags)
{
int error;
struct vnd_softc *sc = device_private(self);
if (sc->sc_flags & VNF_INITED) {
error = vnddoclear(sc, 0, -1, (flags & DETACH_FORCE) != 0);
if (error != 0)
return error;
}
pmf_device_deregister(self);
bufq_free(sc->sc_tab);
disk_destroy(&sc->sc_dkdev);
return 0;
}
static struct vnd_softc *
vnd_spawn(int unit)
{
cfdata_t cf;
cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
cf->cf_name = vnd_cd.cd_name;
cf->cf_atname = vnd_cd.cd_name;
cf->cf_unit = unit;
cf->cf_fstate = FSTATE_STAR;
return device_private(config_attach_pseudo(cf));
}
static int
vnd_destroy(device_t dev)
{
int error;
cfdata_t cf;
cf = device_cfdata(dev);
error = config_detach(dev, DETACH_QUIET);
if (error)
return error;
free(cf, M_DEVBUF);
return 0;
}
static int
vndopen(dev_t dev, int flags, int mode, struct lwp *l)
{
int unit = vndunit(dev);
struct vnd_softc *sc;
int error = 0, part, pmask;
struct disklabel *lp;
#ifdef DEBUG
if (vnddebug & VDB_FOLLOW)
printf("vndopen(0x%"PRIx64", 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
#endif
sc = device_lookup_private(&vnd_cd, unit);
if (sc == NULL) {
sc = vnd_spawn(unit);
if (sc == NULL)
return ENOMEM;
sc->sc_flags = VNF_KLABEL;
}
if ((error = vndlock(sc)) != 0)
return error;
mutex_enter(&sc->sc_dkdev.dk_openlock);
if ((sc->sc_flags & VNF_CLEARING) != 0) {
error = ENXIO;
goto done;
}
lp = sc->sc_dkdev.dk_label;
part = DISKPART(dev);
pmask = (1 << part);
if (sc->sc_dkdev.dk_nwedges != 0 && part != RAW_PART) {
error = EBUSY;
goto done;
}
if (sc->sc_flags & VNF_INITED) {
if ((sc->sc_dkdev.dk_openmask & ~(1<<RAW_PART)) != 0) {
if ((sc->sc_flags & VNF_VLABEL) == 0) {
error = EIO;
goto done;
}
} else {
if ((sc->sc_flags & VNF_VLABEL) == 0) {
sc->sc_flags |= VNF_VLABEL;
vndgetdisklabel(dev, sc);
}
}
}
if (part != RAW_PART) {
if (((sc->sc_flags & VNF_INITED) == 0) ||
((part >= lp->d_npartitions) ||
(lp->d_partitions[part].p_fstype == FS_UNUSED))) {
error = ENXIO;
goto done;
}
}
switch (mode) {
case S_IFCHR:
sc->sc_dkdev.dk_copenmask |= pmask;
break;
case S_IFBLK:
sc->sc_dkdev.dk_bopenmask |= pmask;
break;
}
sc->sc_dkdev.dk_openmask =
sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
done:
mutex_exit(&sc->sc_dkdev.dk_openlock);
vndunlock(sc);
return error;
}
static int
vndclose(dev_t dev, int flags, int mode, struct lwp *l)
{
int unit = vndunit(dev);
struct vnd_softc *sc;
int error = 0, part;
#ifdef DEBUG
if (vnddebug & VDB_FOLLOW)
printf("vndclose(0x%"PRIx64", 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
#endif
sc = device_lookup_private(&vnd_cd, unit);
if (sc == NULL)
return ENXIO;
if ((error = vndlock(sc)) != 0)
return error;
mutex_enter(&sc->sc_dkdev.dk_openlock);
part = DISKPART(dev);
switch (mode) {
case S_IFCHR:
sc->sc_dkdev.dk_copenmask &= ~(1 << part);
break;
case S_IFBLK:
sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
break;
}
sc->sc_dkdev.dk_openmask =
sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
if (sc->sc_dkdev.dk_openmask == 0) {
if ((sc->sc_flags & VNF_KLABEL) == 0)
sc->sc_flags &= ~VNF_VLABEL;
}
mutex_exit(&sc->sc_dkdev.dk_openlock);
vndunlock(sc);
if ((sc->sc_flags & VNF_INITED) == 0) {
if ((error = vnd_destroy(sc->sc_dev)) != 0) {
aprint_error_dev(sc->sc_dev,
"unable to detach instance\n");
return error;
}
}
return 0;
}
static void
vndstrategy(struct buf *bp)
{
int unit = vndunit(bp->b_dev);
struct vnd_softc *vnd =
device_lookup_private(&vnd_cd, unit);
struct disklabel *lp;
daddr_t blkno;
int s = splbio();
if (vnd == NULL) {
bp->b_error = ENXIO;
goto done;
}
lp = vnd->sc_dkdev.dk_label;
if ((vnd->sc_flags & VNF_INITED) == 0) {
bp->b_error = ENXIO;
goto done;
}
if ((bp->b_bcount % lp->d_secsize) != 0) {
bp->b_error = EINVAL;
goto done;
}
if ((vnd->sc_flags & VNF_READONLY) && !(bp->b_flags & B_READ)) {
bp->b_error = EACCES;
goto done;
}
if (bp->b_bcount == 0) {
goto done;
}
if (DISKPART(bp->b_dev) == RAW_PART) {
if (bounds_check_with_mediasize(bp, DEV_BSIZE,
vnd->sc_size) <= 0)
goto done;
} else {
if (bounds_check_with_label(&vnd->sc_dkdev,
bp, vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING)) <= 0)
goto done;
}
blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
if (DISKPART(bp->b_dev) != RAW_PART) {
struct partition *pp;
pp = &vnd->sc_dkdev.dk_label->d_partitions[
DISKPART(bp->b_dev)];
blkno += pp->p_offset;
}
bp->b_rawblkno = blkno;
#ifdef DEBUG
if (vnddebug & VDB_FOLLOW)
printf("vndstrategy(%p): unit %d\n", bp, unit);
#endif
if ((vnd->sc_flags & VNF_USE_VN_RDWR)) {
if (curlwp != vnd->sc_kthread &&
!uvm_lwp_is_pagedaemon(curlwp)) {
while (vnd->sc_pending >= VND_MAXPENDING(vnd))
tsleep(&vnd->sc_pending, PRIBIO, "vndpc", 0);
}
vnd->sc_pending++;
KASSERT(vnd->sc_pending > 0);
}
bufq_put(vnd->sc_tab, bp);
wakeup(&vnd->sc_tab);
splx(s);
return;
done:
bp->b_resid = bp->b_bcount;
biodone(bp);
splx(s);
}
static bool
vnode_has_strategy(struct vnd_softc *vnd)
{
return vnode_has_op(vnd->sc_vp, VOFFSET(vop_bmap)) &&
vnode_has_op(vnd->sc_vp, VOFFSET(vop_strategy));
}
static bool
vnode_has_large_blocks(struct vnd_softc *vnd)
{
u_int32_t vnd_secsize, iosize;
iosize = vnd->sc_iosize;
vnd_secsize = vnd->sc_geom.vng_secsize;
return vnd_secsize % iosize != 0;
}
#if notyet
static bool
vnode_strategy_probe(struct vnd_softc *vnd)
{
int error;
daddr_t nbn;
if (!vnode_has_strategy(vnd))
return false;
if (vnode_has_large_blocks(vnd))
return false;
error = 0;
vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
error = VOP_BMAP(vnd->sc_vp, 0, NULL, &nbn, NULL);
VOP_UNLOCK(vnd->sc_vp);
if (error == 0 && (long)nbn == -1)
return false;
return true;
}
#endif
static void
vndthread(void *arg)
{
struct vnd_softc *vnd = arg;
int s;
if ((vnd->sc_flags & VNF_USE_VN_RDWR) == 0 &&
! vnode_has_strategy(vnd))
vnd->sc_flags |= VNF_USE_VN_RDWR;
if ((vnd->sc_flags & VNF_USE_VN_RDWR) == 0 &&
vnode_has_large_blocks(vnd))
vnd->sc_flags |= VNF_USE_VN_RDWR;
#ifdef DEBUG
if (vnddebug & VDB_INIT)
printf("vndthread: vp %p, %s\n", vnd->sc_vp,
(vnd->sc_flags & VNF_USE_VN_RDWR) == 0 ?
"using bmap/strategy operations" :
"using read/write operations");
#endif
s = splbio();
vnd->sc_flags |= VNF_KTHREAD;
wakeup(&vnd->sc_kthread);
while ((vnd->sc_flags & VNF_VUNCONF) == 0) {
struct vndxfer *vnx;
struct buf *obp;
struct buf *bp;
obp = bufq_get(vnd->sc_tab);
if (obp == NULL) {
tsleep(&vnd->sc_tab, PRIBIO, "vndbp", 0);
continue;
};
if ((vnd->sc_flags & VNF_USE_VN_RDWR)) {
KASSERT(vnd->sc_pending > 0);
if (vnd->sc_pending-- == VND_MAXPENDING(vnd))
wakeup(&vnd->sc_pending);
}
splx(s);
#ifdef DEBUG
if (vnddebug & VDB_FOLLOW)
printf("vndthread(%p)\n", obp);
#endif
if (vnd->sc_vp->v_mount == NULL) {
obp->b_error = ENXIO;
goto done;
}
#ifdef VND_COMPRESSION
if ((obp->b_flags & B_READ) != 0 && (vnd->sc_flags & VNF_COMP)) {
off_t bn;
bn = obp->b_rawblkno *
vnd->sc_dkdev.dk_label->d_secsize;
compstrategy(obp, bn);
goto done;
}
#endif
s = splbio();
vnx = VND_GETXFER(vnd);
splx(s);
vnx->vx_vnd = vnd;
s = splbio();
while (vnd->sc_active >= vnd->sc_maxactive) {
tsleep(&vnd->sc_tab, PRIBIO, "vndac", 0);
}
vnd->sc_active++;
splx(s);
disk_busy(&vnd->sc_dkdev);
bp = &vnx->vx_buf;
buf_init(bp);
bp->b_flags = (obp->b_flags & (B_READ | B_PHYS | B_RAW));
bp->b_oflags = obp->b_oflags;
bp->b_cflags = obp->b_cflags;
bp->b_iodone = vndiodone;
bp->b_private = obp;
bp->b_vp = vnd->sc_vp;
bp->b_objlock = bp->b_vp->v_interlock;
bp->b_data = obp->b_data;
bp->b_bcount = obp->b_bcount;
BIO_COPYPRIO(bp, obp);
fstrans_start_lazy(vnd->sc_vp->v_mount);
if ((vnd->sc_flags & VNF_USE_VN_RDWR) == 0)
handle_with_strategy(vnd, obp, bp);
else
handle_with_rdwr(vnd, obp, bp);
fstrans_done(vnd->sc_vp->v_mount);
s = splbio();
continue;
done:
biodone(obp);
s = splbio();
}
vnd->sc_flags &= (~VNF_KTHREAD | VNF_VUNCONF);
wakeup(&vnd->sc_kthread);
splx(s);
kthread_exit(0);
}
static bool
vnode_has_op(const struct vnode *vp, int opoffset)
{
int (*defaultp)(void *);
int (*opp)(void *);
defaultp = vp->v_op[VOFFSET(vop_default)];
opp = vp->v_op[opoffset];
return opp != defaultp && opp != genfs_eopnotsupp &&
opp != genfs_badop && opp != genfs_nullop;
}
static void
handle_with_rdwr(struct vnd_softc *vnd, const struct buf *obp, struct buf *bp)
{
bool doread;
off_t offset;
size_t len, resid;
struct vnode *vp;
int npages;
doread = bp->b_flags & B_READ;
offset = obp->b_rawblkno * vnd->sc_dkdev.dk_label->d_secsize;
len = bp->b_bcount;
vp = vnd->sc_vp;
#if defined(DEBUG)
if (vnddebug & VDB_IO)
printf("vnd (rdwr): vp %p, %s, rawblkno 0x%" PRIx64
", secsize %d, offset %" PRIu64
", bcount %d\n",
vp, doread ? "read" : "write", obp->b_rawblkno,
vnd->sc_dkdev.dk_label->d_secsize, offset,
bp->b_bcount);
#endif
bp->b_error =
vn_rdwr(doread ? UIO_READ : UIO_WRITE,
vp, bp->b_data, len, offset, UIO_SYSSPACE,
IO_ADV_ENCODE(POSIX_FADV_NOREUSE) | IO_DIRECT,
vnd->sc_cred, &resid, NULL);
bp->b_resid = resid;
npages = atomic_load_relaxed(&vp->v_uobj.uo_npages);
if (npages > VND_MAXPAGES(vnd)) {
rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
(void) VOP_PUTPAGES(vp, 0, 0,
PGO_ALLPAGES | PGO_CLEANIT | PGO_FREE);
}
if (!doread) {
mutex_enter(vp->v_interlock);
vp->v_numoutput++;
mutex_exit(vp->v_interlock);
}
biodone(bp);
}
static void
handle_with_strategy(struct vnd_softc *vnd, const struct buf *obp,
struct buf *bp)
{
int bsize, error, flags, skipped;
size_t resid, sz;
off_t bn, offset;
struct vnode *vp;
struct buf *nbp = NULL;
flags = obp->b_flags;
bn = obp->b_rawblkno * vnd->sc_dkdev.dk_label->d_secsize;
bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
if (bsize <= 0)
bsize = BLKDEV_IOSIZE;
skipped = 0;
error = 0;
bp->b_resid = bp->b_bcount;
for (offset = 0, resid = bp->b_resid; ;
resid -= sz, offset += sz) {
daddr_t nbn;
int off, nra;
nra = 0;
vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
VOP_UNLOCK(vnd->sc_vp);
if (error == 0 && (long)nbn == -1)
error = EIO;
if (error) {
skipped += resid;
break;
}
#ifdef DEBUG
if (!dovndcluster)
nra = 0;
#endif
off = bn % bsize;
sz = MIN(((off_t)1 + nra) * bsize - off, resid);
#ifdef DEBUG
if (vnddebug & VDB_IO)
printf("vndstrategy: vp %p/%p bn 0x%qx/0x%" PRIx64
" sz 0x%zx\n", vnd->sc_vp, vp, (long long)bn,
nbn, sz);
#endif
nbp = getiobuf(vp, true);
nestiobuf_setup(bp, nbp, offset, sz);
nbp->b_blkno = nbn + btodb(off);
#if 0
if (vnddebug & VDB_IO)
printf("vndstart(%ld): bp %p vp %p blkno "
"0x%" PRIx64 " flags %x addr %p cnt 0x%x\n",
(long) (vnd-vnd_softc), &nbp->vb_buf,
nbp->vb_buf.b_vp, nbp->vb_buf.b_blkno,
nbp->vb_buf.b_flags, nbp->vb_buf.b_data,
nbp->vb_buf.b_bcount);
#endif
if (resid == sz) {
break;
}
VOP_STRATEGY(vp, nbp);
bn += sz;
}
if (!(flags & B_READ)) {
struct vnode *w_vp;
w_vp = bp->b_vp;
mutex_enter(w_vp->v_interlock);
w_vp->v_numoutput++;
mutex_exit(w_vp->v_interlock);
}
KASSERT(skipped != 0 || nbp != NULL);
if (skipped)
nestiobuf_done(bp, skipped, error);
else
VOP_STRATEGY(vp, nbp);
}
static void
vndiodone(struct buf *bp)
{
struct vndxfer *vnx = VND_BUFTOXFER(bp);
struct vnd_softc *vnd = vnx->vx_vnd;
struct buf *obp = bp->b_private;
int s = splbio();
KERNEL_LOCK(1, NULL);
KASSERT(&vnx->vx_buf == bp);
KASSERT(vnd->sc_active > 0);
#ifdef DEBUG
if (vnddebug & VDB_IO) {
printf("vndiodone1: bp %p iodone: error %d\n",
bp, bp->b_error);
}
#endif
disk_unbusy(&vnd->sc_dkdev, bp->b_bcount - bp->b_resid,
(bp->b_flags & B_READ));
vnd->sc_active--;
if (vnd->sc_active == 0) {
wakeup(&vnd->sc_tab);
}
KERNEL_UNLOCK_ONE(NULL);
splx(s);
obp->b_error = bp->b_error;
obp->b_resid = bp->b_resid;
buf_destroy(bp);
VND_PUTXFER(vnd, vnx);
biodone(obp);
}
static int
vndread(dev_t dev, struct uio *uio, int flags)
{
int unit = vndunit(dev);
struct vnd_softc *sc;
#ifdef DEBUG
if (vnddebug & VDB_FOLLOW)
printf("vndread(0x%"PRIx64", %p)\n", dev, uio);
#endif
sc = device_lookup_private(&vnd_cd, unit);
if (sc == NULL)
return ENXIO;
if ((sc->sc_flags & VNF_INITED) == 0)
return ENXIO;
return physio(vndstrategy, NULL, dev, B_READ, minphys, uio);
}
static int
vndwrite(dev_t dev, struct uio *uio, int flags)
{
int unit = vndunit(dev);
struct vnd_softc *sc;
#ifdef DEBUG
if (vnddebug & VDB_FOLLOW)
printf("vndwrite(0x%"PRIx64", %p)\n", dev, uio);
#endif
sc = device_lookup_private(&vnd_cd, unit);
if (sc == NULL)
return ENXIO;
if ((sc->sc_flags & VNF_INITED) == 0)
return ENXIO;
return physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio);
}
static int
vnd_cget(struct lwp *l, int unit, int *un, struct vattr *va)
{
int error;
struct vnd_softc *vnd;
if (*un == -1)
*un = unit;
if (*un < 0)
return EINVAL;
vnd = device_lookup_private(&vnd_cd, *un);
if (vnd == NULL)
return -1;
if ((vnd->sc_flags & VNF_INITED) == 0)
return -1;
vn_lock(vnd->sc_vp, LK_SHARED | LK_RETRY);
error = VOP_GETATTR(vnd->sc_vp, va, l->l_cred);
VOP_UNLOCK(vnd->sc_vp);
return error;
}
static int
vnddoclear(struct vnd_softc *vnd, int pmask, int minor, bool force)
{
int error;
if ((error = vndlock(vnd)) != 0)
return error;
if (DK_BUSY(vnd, pmask) && !force) {
vndunlock(vnd);
return EBUSY;
}
dkwedge_delall(&vnd->sc_dkdev);
vnd->sc_flags |= VNF_CLEARING;
vndunlock(vnd);
vndclear(vnd, minor);
#ifdef DEBUG
if (vnddebug & VDB_INIT)
printf("%s: CLRed\n", __func__);
#endif
pool_destroy(&vnd->sc_vxpool);
disk_detach(&vnd->sc_dkdev);
return 0;
}
static int
vndioctl_get(struct lwp *l, void *data, int unit, struct vattr *va)
{
int error;
KASSERT(l);
if (*(int *)data >= vnd_cd.cd_ndevs)
return ENXIO;
switch (error = vnd_cget(l, unit, (int *)data, va)) {
case -1:
memset(va, 0, sizeof(*va));
case 0:
return 0;
default:
return error;
}
}
static int
vndioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
bool force;
int unit = vndunit(dev);
struct vnd_softc *vnd;
struct vnd_ioctl *vio;
struct vattr vattr;
struct pathbuf *pb;
struct vnode *vp;
int error, part, pmask;
uint64_t geomsize;
int fflags;
#ifdef __HAVE_OLD_DISKLABEL
struct disklabel newlabel;
#endif
#ifdef DEBUG
if (vnddebug & VDB_FOLLOW)
printf("vndioctl(0x%"PRIx64", 0x%lx, %p, 0x%x, %p): unit %d\n",
dev, cmd, data, flag, l->l_proc, unit);
#endif
switch (cmd) {
case VNDIOCGET:
if ((error = vndioctl_get(l, data, unit, &vattr)) != 0)
return error;
struct vnd_user *vnu = data;
vnu->vnu_dev = vattr.va_fsid;
vnu->vnu_ino = vattr.va_fileid;
return 0;
default:
MODULE_HOOK_CALL(compat_vndioctl_50_hook,
(cmd, l, data, unit, &vattr, vndioctl_get),
enosys(), error);
if (error == ENOSYS) {
error = 0;
break;
}
if (error == EPASSTHROUGH)
MODULE_HOOK_CALL(compat_vndioctl_30_hook,
(cmd, l, data, unit, &vattr, vndioctl_get),
enosys(), error);
if (error == ENOSYS || error == EPASSTHROUGH) {
error = 0;
break;
}
return error;
}
vnd = device_lookup_private(&vnd_cd, unit);
if (vnd == NULL)
return ENXIO;
vio = (struct vnd_ioctl *)data;
switch (cmd) {
case VNDIOCSET50:
case VNDIOCCLR50:
if (!compat_vndioctl_50_hook.hooked)
return EINVAL;
case VNDIOCSET:
case VNDIOCCLR:
case DIOCSDINFO:
case DIOCWDINFO:
#ifdef __HAVE_OLD_DISKLABEL
case ODIOCSDINFO:
case ODIOCWDINFO:
#endif
case DIOCKLABEL:
case DIOCWLABEL:
case DIOCCACHESYNC:
if ((flag & FWRITE) == 0)
return EBADF;
}
switch (cmd) {
case VNDIOCSET50:
case VNDIOCSET:
if (vnd->sc_flags & VNF_INITED)
return EBUSY;
break;
default:
if ((vnd->sc_flags & VNF_INITED) == 0)
return ENXIO;
break;
}
error = disk_ioctl(&vnd->sc_dkdev, dev, cmd, data, flag, l);
if (error != EPASSTHROUGH)
return error;
switch (cmd) {
case VNDIOCSET50:
case VNDIOCSET:
if ((error = vndlock(vnd)) != 0)
return error;
fflags = FREAD;
if ((vio->vnd_flags & VNDIOF_READONLY) == 0)
fflags |= FWRITE;
if ((vio->vnd_flags & VNDIOF_FILEIO) != 0)
vnd->sc_flags |= VNF_USE_VN_RDWR;
error = pathbuf_copyin(vio->vnd_file, &pb);
if (error) {
goto unlock_and_exit;
}
error = vn_open(NULL, pb, 0, fflags, 0, &vp, NULL, NULL);
if (error != 0) {
pathbuf_destroy(pb);
goto unlock_and_exit;
}
KASSERT(l);
error = VOP_GETATTR(vp, &vattr, l->l_cred);
if (!error && vp->v_type != VREG)
error = EOPNOTSUPP;
if (!error && vattr.va_bytes < vattr.va_size)
vnd->sc_flags |= VNF_USE_VN_RDWR;
if (error) {
VOP_UNLOCK(vp);
goto close_and_exit;
}
if (vio->vnd_flags & VNDIOF_COMP) {
#ifdef VND_COMPRESSION
struct vnd_comp_header *ch;
int i;
uint32_t comp_size;
uint32_t comp_maxsize;
ch = malloc(sizeof(struct vnd_comp_header),
M_TEMP, M_WAITOK);
error = vn_rdwr(UIO_READ, vp, (void *)ch,
sizeof(struct vnd_comp_header), 0, UIO_SYSSPACE,
IO_UNIT|IO_NODELOCKED, l->l_cred, NULL, NULL);
if (error) {
free(ch, M_TEMP);
VOP_UNLOCK(vp);
goto close_and_exit;
}
if (be32toh(ch->block_size) == 0 ||
be32toh(ch->num_blocks) > UINT32_MAX - 1) {
free(ch, M_TEMP);
VOP_UNLOCK(vp);
goto close_and_exit;
}
vnd->sc_comp_blksz = be32toh(ch->block_size);
vnd->sc_comp_numoffs = be32toh(ch->num_blocks) + 1;
free(ch, M_TEMP);
if (!DK_DEV_BSIZE_OK(vnd->sc_comp_blksz)) {
VOP_UNLOCK(vp);
error = EINVAL;
goto close_and_exit;
}
KASSERT(0 < vnd->sc_comp_blksz);
KASSERT(0 < vnd->sc_comp_numoffs);
#if SIZE_MAX <= UINT32_MAX*(64/CHAR_BIT)
if (SIZE_MAX/sizeof(uint64_t) < vnd->sc_comp_numoffs) {
VOP_UNLOCK(vp);
error = EINVAL;
goto close_and_exit;
}
#endif
if ((vattr.va_size < sizeof(struct vnd_comp_header)) ||
(vattr.va_size - sizeof(struct vnd_comp_header) <
sizeof(uint64_t)*vnd->sc_comp_numoffs) ||
(UQUAD_MAX/vnd->sc_comp_blksz <
vnd->sc_comp_numoffs - 1)) {
VOP_UNLOCK(vp);
error = EINVAL;
goto close_and_exit;
}
KASSERT(vnd->sc_comp_numoffs - 1 <=
UQUAD_MAX/vnd->sc_comp_blksz);
vattr.va_size =
((u_quad_t)vnd->sc_comp_numoffs - 1) *
(u_quad_t)vnd->sc_comp_blksz;
__CTASSERT(UINT32_MAX <= UQUAD_MAX/sizeof(uint64_t));
vnd->sc_comp_offsets =
malloc(sizeof(uint64_t) * vnd->sc_comp_numoffs,
M_DEVBUF, M_WAITOK);
error = vn_rdwr(UIO_READ, vp,
(void *)vnd->sc_comp_offsets,
sizeof(uint64_t) * vnd->sc_comp_numoffs,
sizeof(struct vnd_comp_header), UIO_SYSSPACE,
IO_UNIT|IO_NODELOCKED, l->l_cred, NULL, NULL);
if (error) {
VOP_UNLOCK(vp);
goto close_and_exit;
}
comp_maxsize = 0;
for (i = 0; i < vnd->sc_comp_numoffs - 1; i++) {
vnd->sc_comp_offsets[i] =
be64toh(vnd->sc_comp_offsets[i]);
comp_size =
be64toh(vnd->sc_comp_offsets[i + 1])
- vnd->sc_comp_offsets[i];
if (comp_size > comp_maxsize)
comp_maxsize = comp_size;
}
vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1] =
be64toh(vnd->sc_comp_offsets[vnd->sc_comp_numoffs
- 1]);
vnd->sc_comp_buff = malloc(comp_maxsize,
M_DEVBUF, M_WAITOK);
vnd->sc_comp_decombuf = malloc(vnd->sc_comp_blksz,
M_DEVBUF, M_WAITOK);
vnd->sc_comp_buffblk = -1;
memset(&vnd->sc_comp_stream, 0, sizeof(z_stream));
vnd->sc_comp_stream.zalloc = vnd_alloc;
vnd->sc_comp_stream.zfree = vnd_free;
error = inflateInit2(&vnd->sc_comp_stream, MAX_WBITS);
if (error) {
if (vnd->sc_comp_stream.msg)
printf("vnd%d: compressed file, %s\n",
unit, vnd->sc_comp_stream.msg);
VOP_UNLOCK(vp);
error = EINVAL;
goto close_and_exit;
}
vnd->sc_flags |= VNF_COMP | VNF_READONLY;
#else
VOP_UNLOCK(vp);
error = EOPNOTSUPP;
goto close_and_exit;
#endif
}
VOP_UNLOCK(vp);
vnd->sc_vp = vp;
vnd->sc_size = btodb(vattr.va_size);
error = bdev_ioctl(vattr.va_fsid, DIOCGSECTORSIZE, &vnd->sc_iosize, FKIOCTL, l);
if (error)
vnd->sc_iosize = vnd->sc_vp->v_mount->mnt_stat.f_frsize;
if (vnd->sc_iosize == 0)
vnd->sc_iosize = DEV_BSIZE;
if (vio->vnd_flags & VNDIOF_HASGEOM) {
memcpy(&vnd->sc_geom, &vio->vnd_geom,
sizeof(vio->vnd_geom));
if (!DK_DEV_BSIZE_OK(vnd->sc_geom.vng_secsize) ||
vnd->sc_geom.vng_ntracks == 0 ||
vnd->sc_geom.vng_nsectors == 0) {
error = EINVAL;
goto close_and_exit;
}
if (vnd->sc_geom.vng_ncylinders == 0)
vnd->sc_geom.vng_ncylinders = vnd->sc_size / (
(vnd->sc_geom.vng_secsize / DEV_BSIZE) *
vnd->sc_geom.vng_ntracks *
vnd->sc_geom.vng_nsectors);
geomsize = (int64_t)vnd->sc_geom.vng_nsectors *
vnd->sc_geom.vng_ntracks *
vnd->sc_geom.vng_ncylinders *
(vnd->sc_geom.vng_secsize / DEV_BSIZE);
if (vnd->sc_size < geomsize) {
error = EINVAL;
goto close_and_exit;
}
} else if (vnd->sc_size >= (32 * 64)) {
vnd->sc_geom.vng_secsize = DEV_BSIZE;
vnd->sc_geom.vng_nsectors = 32;
vnd->sc_geom.vng_ntracks = 64;
vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
} else {
vnd->sc_geom.vng_secsize = DEV_BSIZE;
vnd->sc_geom.vng_nsectors = 1;
vnd->sc_geom.vng_ntracks = 1;
vnd->sc_geom.vng_ncylinders = vnd->sc_size;
}
vnd_set_geometry(vnd);
if (vio->vnd_flags & VNDIOF_READONLY) {
vnd->sc_flags |= VNF_READONLY;
}
if ((error = vndsetcred(vnd, l->l_cred)) != 0)
goto close_and_exit;
vndthrottle(vnd, vnd->sc_vp);
vio->vnd_osize = dbtob(vnd->sc_size);
if (cmd != VNDIOCSET50)
vio->vnd_size = dbtob(vnd->sc_size);
vnd->sc_flags |= VNF_INITED;
error = kthread_create(PRI_NONE, 0, NULL, vndthread, vnd,
&vnd->sc_kthread, "%s", device_xname(vnd->sc_dev));
if (error)
goto close_and_exit;
while ((vnd->sc_flags & VNF_KTHREAD) == 0) {
tsleep(&vnd->sc_kthread, PRIBIO, "vndthr", 0);
}
#ifdef DEBUG
if (vnddebug & VDB_INIT)
printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
vnd->sc_vp, (unsigned long) vnd->sc_size,
vnd->sc_geom.vng_secsize,
vnd->sc_geom.vng_nsectors,
vnd->sc_geom.vng_ntracks,
vnd->sc_geom.vng_ncylinders);
#endif
disk_attach(&vnd->sc_dkdev);
pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
0, 0, "vndxpl", NULL, IPL_BIO);
vndunlock(vnd);
pathbuf_destroy(pb);
dkwedge_discover(&vnd->sc_dkdev);
break;
close_and_exit:
(void) vn_close(vp, fflags, l->l_cred);
pathbuf_destroy(pb);
unlock_and_exit:
#ifdef VND_COMPRESSION
if (vnd->sc_comp_offsets) {
free(vnd->sc_comp_offsets, M_DEVBUF);
vnd->sc_comp_offsets = NULL;
}
if (vnd->sc_comp_buff) {
free(vnd->sc_comp_buff, M_DEVBUF);
vnd->sc_comp_buff = NULL;
}
if (vnd->sc_comp_decombuf) {
free(vnd->sc_comp_decombuf, M_DEVBUF);
vnd->sc_comp_decombuf = NULL;
}
#endif
vndunlock(vnd);
return error;
case VNDIOCCLR50:
case VNDIOCCLR:
part = DISKPART(dev);
pmask = (1 << part);
force = (vio->vnd_flags & VNDIOF_FORCE) != 0;
if ((error = vnddoclear(vnd, pmask, minor(dev), force)) != 0)
return error;
break;
case DIOCWDINFO:
case DIOCSDINFO:
#ifdef __HAVE_OLD_DISKLABEL
case ODIOCWDINFO:
case ODIOCSDINFO:
#endif
{
struct disklabel *lp;
if ((error = vndlock(vnd)) != 0)
return error;
vnd->sc_flags |= VNF_LABELLING;
#ifdef __HAVE_OLD_DISKLABEL
if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
memset(&newlabel, 0, sizeof newlabel);
memcpy(&newlabel, data, sizeof (struct olddisklabel));
lp = &newlabel;
} else
#endif
lp = (struct disklabel *)data;
error = setdisklabel(vnd->sc_dkdev.dk_label,
lp, 0, vnd->sc_dkdev.dk_cpulabel);
if (error == 0) {
if (cmd == DIOCWDINFO
#ifdef __HAVE_OLD_DISKLABEL
|| cmd == ODIOCWDINFO
#endif
)
error = writedisklabel(VNDLABELDEV(dev),
vndstrategy, vnd->sc_dkdev.dk_label,
vnd->sc_dkdev.dk_cpulabel);
}
vnd->sc_flags &= ~VNF_LABELLING;
vndunlock(vnd);
if (error)
return error;
break;
}
case DIOCKLABEL:
if (*(int *)data != 0)
vnd->sc_flags |= VNF_KLABEL;
else
vnd->sc_flags &= ~VNF_KLABEL;
break;
case DIOCWLABEL:
if (*(int *)data != 0)
vnd->sc_flags |= VNF_WLABEL;
else
vnd->sc_flags &= ~VNF_WLABEL;
break;
case DIOCGDEFLABEL:
vndgetdefaultlabel(vnd, (struct disklabel *)data);
break;
#ifdef __HAVE_OLD_DISKLABEL
case ODIOCGDEFLABEL:
vndgetdefaultlabel(vnd, &newlabel);
if (newlabel.d_npartitions > OLDMAXPARTITIONS)
return ENOTTY;
memcpy(data, &newlabel, sizeof (struct olddisklabel));
break;
#endif
case DIOCGSTRATEGY:
{
struct disk_strategy *dks = (void *)data;
strlcpy(dks->dks_name,
bufq_getstrategyname(vnd->sc_tab),
sizeof(dks->dks_name));
dks->dks_paramlen = 0;
break;
}
case DIOCGCACHE:
{
int *bits = (int *)data;
*bits |= DKCACHE_READ | DKCACHE_WRITE;
break;
}
case DIOCCACHESYNC:
vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
error = VOP_FSYNC(vnd->sc_vp, vnd->sc_cred,
FSYNC_WAIT | FSYNC_DATAONLY | FSYNC_CACHE, 0, 0);
VOP_UNLOCK(vnd->sc_vp);
return error;
default:
return ENOTTY;
}
return 0;
}
static int
vndsetcred(struct vnd_softc *vnd, kauth_cred_t cred)
{
struct uio auio;
struct iovec aiov;
char *tmpbuf;
int error;
vnd->sc_cred = kauth_cred_dup(cred);
tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
aiov.iov_base = tmpbuf;
aiov.iov_len = uimin(DEV_BSIZE, dbtob(vnd->sc_size));
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_offset = 0;
auio.uio_rw = UIO_READ;
auio.uio_resid = aiov.iov_len;
UIO_SETUP_SYSSPACE(&auio);
vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
if (error == 0) {
error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
curlwp, 0, 0);
}
VOP_UNLOCK(vnd->sc_vp);
free(tmpbuf, M_TEMP);
return error;
}
static void
vndthrottle(struct vnd_softc *vnd, struct vnode *vp)
{
if (vp->v_tag == VT_NFS)
vnd->sc_maxactive = 2;
else
vnd->sc_maxactive = 8;
if (vnd->sc_maxactive < 1)
vnd->sc_maxactive = 1;
}
#if 0
static void
vndshutdown(void)
{
struct vnd_softc *vnd;
for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
if (vnd->sc_flags & VNF_INITED)
vndclear(vnd);
}
#endif
static void
vndclear(struct vnd_softc *vnd, int myminor)
{
struct vnode *vp = vnd->sc_vp;
int fflags = FREAD;
int bmaj, cmaj, i, mn;
int s;
#ifdef DEBUG
if (vnddebug & VDB_FOLLOW)
printf("vndclear(%p): vp %p\n", vnd, vp);
#endif
bmaj = bdevsw_lookup_major(&vnd_bdevsw);
cmaj = cdevsw_lookup_major(&vnd_cdevsw);
for (i = 0; i < MAXPARTITIONS; i++) {
mn = DISKMINOR(device_unit(vnd->sc_dev), i);
if (mn != myminor) {
vdevgone(bmaj, mn, mn, VBLK);
vdevgone(cmaj, mn, mn, VCHR);
}
}
if ((vnd->sc_flags & VNF_READONLY) == 0)
fflags |= FWRITE;
s = splbio();
bufq_drain(vnd->sc_tab);
splx(s);
vnd->sc_flags |= VNF_VUNCONF;
wakeup(&vnd->sc_tab);
while (vnd->sc_flags & VNF_KTHREAD)
tsleep(&vnd->sc_kthread, PRIBIO, "vnthr", 0);
#ifdef VND_COMPRESSION
if (vnd->sc_flags & VNF_COMP) {
if (vnd->sc_comp_offsets) {
free(vnd->sc_comp_offsets, M_DEVBUF);
vnd->sc_comp_offsets = NULL;
}
if (vnd->sc_comp_buff) {
free(vnd->sc_comp_buff, M_DEVBUF);
vnd->sc_comp_buff = NULL;
}
if (vnd->sc_comp_decombuf) {
free(vnd->sc_comp_decombuf, M_DEVBUF);
vnd->sc_comp_decombuf = NULL;
}
}
#endif
vnd->sc_flags &=
~(VNF_INITED | VNF_READONLY | VNF_KLABEL | VNF_VLABEL
| VNF_VUNCONF | VNF_COMP | VNF_CLEARING);
if (vp == NULL)
panic("vndclear: null vp");
(void) vn_close(vp, fflags, vnd->sc_cred);
kauth_cred_free(vnd->sc_cred);
vnd->sc_vp = NULL;
vnd->sc_cred = NULL;
vnd->sc_size = 0;
}
static int
vndsize(dev_t dev)
{
struct vnd_softc *sc;
struct disklabel *lp;
int part, unit, omask;
int size;
unit = vndunit(dev);
sc = device_lookup_private(&vnd_cd, unit);
if (sc == NULL)
return -1;
if ((sc->sc_flags & VNF_INITED) == 0)
return -1;
part = DISKPART(dev);
omask = sc->sc_dkdev.dk_openmask & (1 << part);
lp = sc->sc_dkdev.dk_label;
if (omask == 0 && vndopen(dev, 0, S_IFBLK, curlwp))
return -1;
if (lp->d_partitions[part].p_fstype != FS_SWAP)
size = -1;
else
size = lp->d_partitions[part].p_size *
(lp->d_secsize / DEV_BSIZE);
if (omask == 0 && vndclose(dev, 0, S_IFBLK, curlwp))
return -1;
return size;
}
static int
vnddump(dev_t dev, daddr_t blkno, void *va,
size_t size)
{
return ENXIO;
}
static void
vndgetdefaultlabel(struct vnd_softc *sc, struct disklabel *lp)
{
struct vndgeom *vng = &sc->sc_geom;
struct partition *pp;
unsigned spb;
memset(lp, 0, sizeof(*lp));
spb = vng->vng_secsize / DEV_BSIZE;
if (sc->sc_size / spb > UINT32_MAX)
lp->d_secperunit = UINT32_MAX;
else
lp->d_secperunit = sc->sc_size / spb;
lp->d_secsize = vng->vng_secsize;
lp->d_nsectors = vng->vng_nsectors;
lp->d_ntracks = vng->vng_ntracks;
lp->d_ncylinders = vng->vng_ncylinders;
lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
lp->d_type = DKTYPE_VND;
strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
lp->d_rpm = 3600;
lp->d_interleave = 1;
lp->d_flags = 0;
pp = &lp->d_partitions[RAW_PART];
pp->p_offset = 0;
pp->p_size = lp->d_secperunit;
pp->p_fstype = FS_UNUSED;
lp->d_npartitions = RAW_PART + 1;
lp->d_magic = DISKMAGIC;
lp->d_magic2 = DISKMAGIC;
lp->d_checksum = dkcksum(lp);
}
static void
vndgetdisklabel(dev_t dev, struct vnd_softc *sc)
{
const char *errstring;
struct disklabel *lp = sc->sc_dkdev.dk_label;
struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
int i;
memset(clp, 0, sizeof(*clp));
vndgetdefaultlabel(sc, lp);
errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
if (errstring) {
aprint_normal_dev(sc->sc_dev, "%s\n", errstring);
for (i = 0; i < MAXPARTITIONS; i++) {
if (lp->d_partitions[i].p_size != 0)
continue;
lp->d_partitions[i].p_size = lp->d_secperunit;
lp->d_partitions[i].p_offset = 0;
lp->d_partitions[i].p_fstype = FS_BSDFFS;
}
strncpy(lp->d_packname, "default label",
sizeof(lp->d_packname));
lp->d_npartitions = MAXPARTITIONS;
lp->d_checksum = dkcksum(lp);
}
}
static int
vndlock(struct vnd_softc *sc)
{
int error;
while ((sc->sc_flags & VNF_LOCKED) != 0) {
sc->sc_flags |= VNF_WANTED;
if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
return error;
}
sc->sc_flags |= VNF_LOCKED;
return 0;
}
static void
vndunlock(struct vnd_softc *sc)
{
sc->sc_flags &= ~VNF_LOCKED;
if ((sc->sc_flags & VNF_WANTED) != 0) {
sc->sc_flags &= ~VNF_WANTED;
wakeup(sc);
}
}
#ifdef VND_COMPRESSION
static void
compstrategy(struct buf *bp, off_t bn)
{
int error;
int unit = vndunit(bp->b_dev);
struct vnd_softc *vnd =
device_lookup_private(&vnd_cd, unit);
u_int32_t comp_block;
struct uio auio;
char *addr;
int s;
auio.uio_rw = UIO_READ;
UIO_SETUP_SYSSPACE(&auio);
addr = bp->b_data;
bp->b_resid = bp->b_bcount;
s = splbio();
while (bp->b_resid > 0) {
unsigned length;
size_t length_in_buffer;
u_int32_t offset_in_buffer;
struct iovec aiov;
comp_block = bn / (off_t)vnd->sc_comp_blksz;
if (comp_block >= vnd->sc_comp_numoffs) {
bp->b_error = EINVAL;
splx(s);
return;
}
if (comp_block != vnd->sc_comp_buffblk) {
length = vnd->sc_comp_offsets[comp_block + 1] -
vnd->sc_comp_offsets[comp_block];
vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
error = vn_rdwr(UIO_READ, vnd->sc_vp, vnd->sc_comp_buff,
length, vnd->sc_comp_offsets[comp_block],
UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, vnd->sc_cred,
NULL, NULL);
if (error) {
bp->b_error = error;
VOP_UNLOCK(vnd->sc_vp);
splx(s);
return;
}
vnd->sc_comp_stream.next_in = vnd->sc_comp_buff;
vnd->sc_comp_stream.avail_in = length;
vnd->sc_comp_stream.next_out = vnd->sc_comp_decombuf;
vnd->sc_comp_stream.avail_out = vnd->sc_comp_blksz;
inflateReset(&vnd->sc_comp_stream);
error = inflate(&vnd->sc_comp_stream, Z_FINISH);
if (error != Z_STREAM_END) {
if (vnd->sc_comp_stream.msg)
aprint_normal_dev(vnd->sc_dev,
"compressed file, %s\n",
vnd->sc_comp_stream.msg);
bp->b_error = EBADMSG;
VOP_UNLOCK(vnd->sc_vp);
splx(s);
return;
}
vnd->sc_comp_buffblk = comp_block;
VOP_UNLOCK(vnd->sc_vp);
}
offset_in_buffer = bn % (off_t)vnd->sc_comp_blksz;
length_in_buffer = vnd->sc_comp_blksz - offset_in_buffer;
if (length_in_buffer > bp->b_resid)
length_in_buffer = bp->b_resid;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
aiov.iov_base = addr;
aiov.iov_len = length_in_buffer;
auio.uio_resid = aiov.iov_len;
auio.uio_offset = 0;
error = uiomove(vnd->sc_comp_decombuf + offset_in_buffer,
length_in_buffer, &auio);
if (error) {
bp->b_error = error;
splx(s);
return;
}
bn += length_in_buffer;
addr += length_in_buffer;
bp->b_resid -= length_in_buffer;
}
splx(s);
}
static void *
vnd_alloc(void *aux, u_int items, u_int siz)
{
return malloc(items * siz, M_TEMP, M_NOWAIT);
}
static void
vnd_free(void *aux, void *ptr)
{
free(ptr, M_TEMP);
}
#endif
static void
vnd_set_geometry(struct vnd_softc *vnd)
{
struct disk_geom *dg = &vnd->sc_dkdev.dk_geom;
unsigned spb;
memset(dg, 0, sizeof(*dg));
spb = vnd->sc_geom.vng_secsize / DEV_BSIZE;
dg->dg_secperunit = vnd->sc_size / spb;
dg->dg_secsize = vnd->sc_geom.vng_secsize;
dg->dg_nsectors = vnd->sc_geom.vng_nsectors;
dg->dg_ntracks = vnd->sc_geom.vng_ntracks;
dg->dg_ncylinders = vnd->sc_geom.vng_ncylinders;
#ifdef DEBUG
if (vnddebug & VDB_LABEL) {
printf("dg->dg_secperunit: %" PRId64 "\n", dg->dg_secperunit);
printf("dg->dg_ncylinders: %u\n", dg->dg_ncylinders);
}
#endif
disk_set_info(vnd->sc_dev, &vnd->sc_dkdev, NULL);
}
#ifdef VND_COMPRESSION
#define VND_DEPENDS "zlib"
#else
#define VND_DEPENDS NULL
#endif
MODULE(MODULE_CLASS_DRIVER, vnd, VND_DEPENDS);
#ifdef _MODULE
int vnd_bmajor = -1, vnd_cmajor = -1;
CFDRIVER_DECL(vnd, DV_DISK, NULL);
#endif
static int
vnd_modcmd(modcmd_t cmd, void *arg)
{
int error = 0;
switch (cmd) {
case MODULE_CMD_INIT:
#ifdef _MODULE
error = devsw_attach("vnd", &vnd_bdevsw, &vnd_bmajor,
&vnd_cdevsw, &vnd_cmajor);
if (error) {
#ifdef DIAGNOSTIC
aprint_error("%s: unable to attach %s devsw, "
"error %d", __func__, vnd_cd.cd_name, error);
#endif
break;
}
error = config_cfdriver_attach(&vnd_cd);
if (error) {
devsw_detach(&vnd_bdevsw, &vnd_cdevsw);
break;
}
error = config_cfattach_attach(vnd_cd.cd_name, &vnd_ca);
if (error) {
config_cfdriver_detach(&vnd_cd);
devsw_detach(&vnd_bdevsw, &vnd_cdevsw);
#ifdef DIAGNOSTIC
aprint_error("%s: unable to register cfattach for \n"
"%s, error %d", __func__, vnd_cd.cd_name, error);
#endif
break;
}
#endif
break;
case MODULE_CMD_FINI:
#ifdef _MODULE
error = config_cfattach_detach(vnd_cd.cd_name, &vnd_ca);
if (error) {
#ifdef DIAGNOSTIC
aprint_error("%s: failed to detach %s cfattach, "
"error %d\n", __func__, vnd_cd.cd_name, error);
#endif
break;
}
error = config_cfdriver_detach(&vnd_cd);
if (error) {
(void)config_cfattach_attach(vnd_cd.cd_name, &vnd_ca);
#ifdef DIAGNOSTIC
aprint_error("%s: failed to detach %s cfdriver, "
"error %d\n", __func__, vnd_cd.cd_name, error);
break;
#endif
}
devsw_detach(&vnd_bdevsw, &vnd_cdevsw);
#endif
break;
case MODULE_CMD_STAT:
return ENOTTY;
default:
return ENOTTY;
}
return error;
}