/*-
* Copyright (c) 1996, 1997, 1998, 2008, 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* the Systems Programming Group of the University of Utah Computer
* Science Department.
*
* 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.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* from: Utah $Hdr: vn.c 1.13 94/04/02$
*
* @(#)vn.c 8.9 (Berkeley) 5/14/95
*/
/*
* Vnode disk driver.
*
* Block/character interface to a vnode. Allows one to treat a file
* as a disk (e.g. build a filesystem in it, mount it, etc.).
*
* NOTE 1: If the vnode supports the VOP_BMAP and VOP_STRATEGY operations,
* this uses them to avoid distorting the local buffer cache. If those
* block-level operations are not available, this falls back to the regular
* read and write calls. Using these may distort the cache in some cases
* but better have the driver working than preventing it to work on file
* systems where the block-level operations are not implemented for
* whatever reason.
*
* NOTE 2: There is a security issue involved with this driver.
* Once mounted all access to the contents of the "mapped" file via
* the special file is controlled by the permissions on the special
* file, the protection of the mapped file is ignored (effectively,
* by using root credentials in all transactions).
*
* NOTE 3: Doesn't interact with leases, should it?
*/
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;
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 any non-raw partition is open, but the disk
* has been invalidated, disallow further opens.
*/
if ((sc->sc_flags & VNF_VLABEL) == 0) {
error = EIO;
goto done;
}
} else {
/*
* Load the partition info if not already loaded.
*/
if ((sc->sc_flags & VNF_VLABEL) == 0) {
sc->sc_flags |= VNF_VLABEL;
vndgetdisklabel(dev, sc);
}
}
}
/* Check that the partitions exists. */
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;
}
}
/* Prevent our unit from being unconfigured while open. */
switch (mode) {
case S_IFCHR:
sc->sc_dkdev.dk_copenmask |= pmask;
break;
/* are we last opener ? */
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;
}
/*
* Queue the request, and wakeup the kernel thread to handle it.
*/
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 it's a nil transfer, wake up the top half now. */
if (bp->b_bcount == 0) {
goto done;
}
/*
* Do bounds checking and adjust transfer. If there's an error,
* the bounds check will flag that for us.
*/
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;
}
/*
* Put the block number in terms of the logical blocksize
* of the "device".
*/
#ifdef DEBUG
if (vnddebug & VDB_FOLLOW)
printf("vndstrategy(%p): unit %d\n", bp, unit);
#endif
if ((vnd->sc_flags & VNF_USE_VN_RDWR)) {
/*
* Limit the number of pending requests to not exhaust
* resources needed for I/O but always allow the worker
* thread to add requests, as a wedge on vnd queues
* requests with biodone() -> dkstart() -> vndstrategy().
*/
if (curlwp != vnd->sc_kthread && curlwp != uvm.pagedaemon_lwp) {
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;
/* Verify that I/O requests cannot be smaller than the
* smallest I/O size supported by the backend.
*/
static bool
vnode_has_large_blocks(struct vnd_softc *vnd)
{
u_int32_t vnd_secsize, iosize;
/* XXX this function needs a reliable check to detect
* sparse files. Otherwise, bmap/strategy may be used
* and fail on non-allocated blocks. VOP_READ/VOP_WRITE
* works on sparse files.
*/
#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;
/* Convert the first logical block number to its
* physical block number.
*/
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);
/* Test if that worked. */
if (error == 0 && (long)nbn == -1)
return false;
/* Determine whether we can *use* VOP_BMAP and VOP_STRATEGY to
* directly access the backing vnode. If we can, use these two
* operations to avoid messing with the local buffer cache.
* Otherwise fall back to regular VOP_READ/VOP_WRITE operations
* which are guaranteed to work with any file system. */
if ((vnd->sc_flags & VNF_USE_VN_RDWR) == 0 &&
! vnode_has_strategy(vnd))
vnd->sc_flags |= VNF_USE_VN_RDWR;
/* VOP_STRATEGY can only be used if the backing vnode allows
* to access blocks as small as defined by the vnd geometry.
*/
if ((vnd->sc_flags & VNF_USE_VN_RDWR) == 0 &&
vnode_has_large_blocks(vnd))
vnd->sc_flags |= VNF_USE_VN_RDWR;
/*
* Checks if the given vnode supports the requested operation.
* The operation is specified the offset returned by VOFFSET.
*
* XXX The test below used to determine this is quite fragile
* because it relies on the file system to use genfs to specify
* unimplemented operations. There might be another way to do
* it more cleanly.
*/
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;
}
/*
* Handles the read/write request given in 'bp' using the vnode's VOP_READ
* and VOP_WRITE operations.
*
* 'obp' is a pointer to the original request fed to the vnd device.
*/
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;
/*
* Avoid caching too many pages, the vnd user
* is usually a filesystem and caches itself.
* We need some amount of caching to not hinder
* read-ahead and write-behind operations.
*/
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);
}
/* We need to increase the number of outputs on the vnode if
* there was any write to it. */
if (!doread) {
mutex_enter(vp->v_interlock);
vp->v_numoutput++;
mutex_exit(vp->v_interlock);
}
biodone(bp);
}
/*
* Handes the read/write request given in 'bp' using the vnode's VOP_BMAP
* and VOP_STRATEGY operations.
*
* 'obp' is a pointer to the original request fed to the vnd device.
*/
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;
/* convert to a byte offset within the file. */
bn = obp->b_rawblkno * vnd->sc_dkdev.dk_label->d_secsize;
bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
/* use default if the filesystem didn't specify a block size */
if (bsize <= 0)
bsize = BLKDEV_IOSIZE;
skipped = 0;
/*
* Break the request into bsize pieces and feed them
* sequentially using VOP_BMAP/VOP_STRATEGY.
* We do it this way to keep from flooding NFS servers if we
* are connected to an NFS file. This places the burden on
* the client rather than the server.
*/
error = 0;
bp->b_resid = bp->b_bcount;
for (offset = 0, resid = bp->b_resid; /* true */;
resid -= sz, offset += sz) {
daddr_t nbn;
int off, nra;
/*
* If there was an error or a hole in the file...punt.
* Note that we may have to wait for any operations
* that we have already fired off before releasing
* the buffer.
*
* XXX we could deal with holes here but it would be
* a hassle (in the write case).
*/
if (error) {
skipped += resid;
break;
}
#if 0 /* XXX #ifdef DEBUG */
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;
/*
* this is the last nested buf, account for
* the parent buf write too.
* This has to be done last, so that
* fsync won't wait for this write which
* has no chance to complete before all nested bufs
* have been queued. But it has to be done
* before the last VOP_STRATEGY()
* or the call to nestiobuf_done().
*/
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 int
vnddoclear(struct vnd_softc *vnd, int pmask, int minor, bool force)
{
int error;
if ((error = vndlock(vnd)) != 0)
return error;
/*
* Don't unconfigure if any other partitions are open
* or if both the character and block flavors of this
* partition are open.
*/
if (DK_BUSY(vnd, pmask) && !force) {
vndunlock(vnd);
return EBUSY;
}
/* Delete all of our wedges */
dkwedge_delall(&vnd->sc_dkdev);
/*
* XXX vndclear() might call vndclose() implicitly;
* release lock to avoid recursion
*
* Set VNF_CLEARING to prevent vndopen() from
* sneaking in after we vndunlock().
*/
vnd->sc_flags |= VNF_CLEARING;
vndunlock(vnd);
vndclear(vnd, minor);
#ifdef DEBUG
if (vnddebug & VDB_INIT)
printf("%s: CLRed\n", __func__);
#endif
/* Destroy the xfer and buffer pools. */
pool_destroy(&vnd->sc_vxpool);
/* Detach the disk. */
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);
/* the first member is always int vnd_unit in all the versions */
if (*(int *)data >= vnd_cd.cd_ndevs)
return ENXIO;
switch (error = vnd_cget(l, unit, (int *)data, va)) {
case -1:
/* unused is not an error */
memset(va, 0, sizeof(*va));
/*FALLTHROUGH*/
case 0:
return 0;
default:
return error;
}
}
/* ARGSUSED */
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
/* Do the get's first; they don't need initialization or verification */
switch (cmd) {
case VNDIOCGET:
if ((error = vndioctl_get(l, data, unit, &vattr)) != 0)
return error;
default:
/* First check for COMPAT_50 hook */
MODULE_HOOK_CALL(compat_vndioctl_50_hook,
(cmd, l, data, unit, &vattr, vndioctl_get),
enosys(), error);
/*
* If not present, then COMPAT_30 hook also not
* present, so just continue with checks for the
* "write" commands
*/
if (error == ENOSYS) {
error = 0;
break;
}
/* If not already handled, try the COMPAT_30 hook */
if (error == EPASSTHROUGH)
MODULE_HOOK_CALL(compat_vndioctl_30_hook,
(cmd, l, data, unit, &vattr, vndioctl_get),
enosys(), error);
/* If no COMPAT_30 module, or not handled, check writes */
if (error == ENOSYS || error == EPASSTHROUGH) {
error = 0;
break;
}
return error;
}
/* Must be open for writes for these commands... */
switch (cmd) {
case VNDIOCSET50:
case VNDIOCCLR50:
if (!compat_vndioctl_50_hook.hooked)
return EINVAL;
/* FALLTHROUGH */
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:
/* Must not be initialized */
if (vnd->sc_flags & VNF_INITED)
return EBUSY;
break;
default:
/* Must be initialized */
if ((vnd->sc_flags & VNF_INITED) == 0)
return ENXIO;
break;
}
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)
/* File is definitely sparse, use vn_rdwr() */
vnd->sc_flags |= VNF_USE_VN_RDWR;
if (error) {
VOP_UNLOCK(vp);
goto close_and_exit;
}
/* If using a compressed file, initialize its info */
/* (or abort with an error if kernel has no compression) */
if (vio->vnd_flags & VNDIOF_COMP) {
#ifdef VND_COMPRESSION
struct vnd_comp_header *ch;
int i;
uint32_t comp_size;
uint32_t comp_maxsize;
/* allocate space for compressed file header */
ch = malloc(sizeof(struct vnd_comp_header),
M_TEMP, M_WAITOK);
/* allocate space for all the compressed offsets */
__CTASSERT(UINT32_MAX <= UQUAD_MAX/sizeof(uint64_t));
vnd->sc_comp_offsets =
malloc(sizeof(uint64_t) * vnd->sc_comp_numoffs,
M_DEVBUF, M_WAITOK);
/* read in the offsets */
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;
}
/*
* find largest block size (used for allocation limit).
* Also convert offset to native byte order.
*/
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]);
/* get smallest I/O size for underlying device, fall back to
* fundamental I/O size of underlying filesystem
*/
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;
/* Default I/O size to DEV_BSIZE */
if (vnd->sc_iosize == 0)
vnd->sc_iosize = DEV_BSIZE;
/*
* Use pseudo-geometry specified. If none was provided,
* use "standard" Adaptec fictitious geometry.
*/
if (vio->vnd_flags & VNDIOF_HASGEOM) {
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;
/* No lock needed, never changed */
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;
}
/*
* Duplicate the current processes' credentials. Since we are called only
* as the result of a SET ioctl and only root can do that, any future access
* to this "disk" is essentially as root. Note that credentials may change
* if some other uid can write directly to the mapped file (NFS).
*/
static int
vndsetcred(struct vnd_softc *vnd, kauth_cred_t cred)
{
struct uio auio;
struct iovec aiov;
char *tmpbuf;
int error;
/* XXX: Horrible kludge to establish credentials for NFS */
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) {
/*
* Because vnd does all IO directly through the vnode
* we need to flush (at least) the buffer from the above
* VOP_READ from the buffer cache to prevent cache
* incoherencies. Also, be careful to write dirty
* buffers back to stable storage.
*/
error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
curlwp, 0, 0);
}
VOP_UNLOCK(vnd->sc_vp);
free(tmpbuf, M_TEMP);
return error;
}
/*
* Set maxactive based on FS type
*/
static void
vndthrottle(struct vnd_softc *vnd, struct vnode *vp)
{
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
/* locate the major number */
bmaj = bdevsw_lookup_major(&vnd_bdevsw);
cmaj = cdevsw_lookup_major(&vnd_cdevsw);
/* Nuke the vnodes for any open instances */
for (i = 0; i < MAXPARTITIONS; i++) {
mn = DISKMINOR(device_unit(vnd->sc_dev), i);
if (mn != myminor) { /* XXX avoid to kill own vnode */
vdevgone(bmaj, mn, mn, VBLK);
vdevgone(cmaj, mn, mn, VCHR);
}
}
if ((vnd->sc_flags & VNF_READONLY) == 0)
fflags |= FWRITE;
/*
* Read the disklabel from a vnd. If one is not present, create a fake one.
*/
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);
/*
* Call the generic disklabel extraction routine.
*/
errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
if (errstring) {
/*
* Lack of disklabel is common, but we print the warning
* anyway, since it might contain other useful information.
*/
aprint_normal_dev(sc->sc_dev, "%s\n", errstring);
/*
* For historical reasons, if there's no disklabel
* present, all partitions must be FS_BSDFFS and
* occupy the entire disk.
*/
for (i = 0; i < MAXPARTITIONS; i++) {
/*
* Don't wipe out port specific hack (such as
* dos partition hack of i386 port).
*/
if (lp->d_partitions[i].p_size != 0)
continue;
/*
* Wait interruptibly for an exclusive lock.
*
* XXX
* Several drivers do this; it should be abstracted and made MP-safe.
*/
static int
vndlock(struct vnd_softc *sc)
{
int error;
#ifdef VND_COMPRESSION
/* compressed file read */
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;
/* set up constants for data move */
auio.uio_rw = UIO_READ;
UIO_SETUP_SYSSPACE(&auio);
/* read, and transfer the data */
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;
/* calculate the compressed block number */
comp_block = bn / (off_t)vnd->sc_comp_blksz;
/* check for good block number */
if (comp_block >= vnd->sc_comp_numoffs) {
bp->b_error = EINVAL;
splx(s);
return;
}
/* read in the compressed block, if not in buffer */
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;
}
/* uncompress the buffer */
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);
}