Home | History | Annotate | Line # | Download | only in dev
vnd.c revision 1.21
      1  1.21  mycroft /*	$NetBSD: vnd.c,v 1.21 1995/10/05 06:20:57 mycroft Exp $	*/
      2  1.11      cgd 
      3   1.1   brezak /*
      4   1.1   brezak  * Copyright (c) 1988 University of Utah.
      5   1.1   brezak  * Copyright (c) 1990, 1993
      6   1.1   brezak  *	The Regents of the University of California.  All rights reserved.
      7   1.1   brezak  *
      8   1.1   brezak  * This code is derived from software contributed to Berkeley by
      9   1.1   brezak  * the Systems Programming Group of the University of Utah Computer
     10   1.1   brezak  * Science Department.
     11   1.1   brezak  *
     12   1.1   brezak  * Redistribution and use in source and binary forms, with or without
     13   1.1   brezak  * modification, are permitted provided that the following conditions
     14   1.1   brezak  * are met:
     15   1.1   brezak  * 1. Redistributions of source code must retain the above copyright
     16   1.1   brezak  *    notice, this list of conditions and the following disclaimer.
     17   1.1   brezak  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1   brezak  *    notice, this list of conditions and the following disclaimer in the
     19   1.1   brezak  *    documentation and/or other materials provided with the distribution.
     20   1.1   brezak  * 3. All advertising materials mentioning features or use of this software
     21   1.1   brezak  *    must display the following acknowledgement:
     22   1.1   brezak  *	This product includes software developed by the University of
     23   1.1   brezak  *	California, Berkeley and its contributors.
     24   1.1   brezak  * 4. Neither the name of the University nor the names of its contributors
     25   1.1   brezak  *    may be used to endorse or promote products derived from this software
     26   1.1   brezak  *    without specific prior written permission.
     27   1.1   brezak  *
     28   1.1   brezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   1.1   brezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   1.1   brezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   1.1   brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   1.1   brezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   1.1   brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   1.1   brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   1.1   brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   1.1   brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   1.1   brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   1.1   brezak  * SUCH DAMAGE.
     39   1.1   brezak  *
     40   1.5      cgd  * from: Utah $Hdr: vn.c 1.13 94/04/02$
     41   1.1   brezak  *
     42   1.5      cgd  *	@(#)vn.c	8.6 (Berkeley) 4/1/94
     43   1.1   brezak  */
     44   1.1   brezak 
     45   1.1   brezak /*
     46   1.1   brezak  * Vnode disk driver.
     47   1.1   brezak  *
     48   1.1   brezak  * Block/character interface to a vnode.  Allows one to treat a file
     49   1.1   brezak  * as a disk (e.g. build a filesystem in it, mount it, etc.).
     50   1.1   brezak  *
     51   1.1   brezak  * NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode
     52   1.1   brezak  * instead of a simple VOP_RDWR.  We do this to avoid distorting the
     53   1.1   brezak  * local buffer cache.
     54   1.1   brezak  *
     55   1.1   brezak  * NOTE 2: There is a security issue involved with this driver.
     56   1.1   brezak  * Once mounted all access to the contents of the "mapped" file via
     57   1.1   brezak  * the special file is controlled by the permissions on the special
     58   1.1   brezak  * file, the protection of the mapped file is ignored (effectively,
     59   1.1   brezak  * by using root credentials in all transactions).
     60   1.1   brezak  *
     61   1.1   brezak  * NOTE 3: Doesn't interact with leases, should it?
     62   1.1   brezak  */
     63  1.17      cgd #include "vnd.h"
     64  1.17      cgd #if NVND > 0
     65   1.1   brezak 
     66   1.1   brezak #include <sys/param.h>
     67   1.1   brezak #include <sys/systm.h>
     68   1.1   brezak #include <sys/namei.h>
     69   1.1   brezak #include <sys/proc.h>
     70   1.1   brezak #include <sys/errno.h>
     71   1.1   brezak #include <sys/dkstat.h>
     72   1.1   brezak #include <sys/buf.h>
     73   1.1   brezak #include <sys/malloc.h>
     74   1.1   brezak #include <sys/ioctl.h>
     75  1.16      cgd #include <sys/disklabel.h>
     76   1.1   brezak #include <sys/mount.h>
     77   1.1   brezak #include <sys/vnode.h>
     78   1.1   brezak #include <sys/file.h>
     79   1.1   brezak #include <sys/uio.h>
     80   1.1   brezak 
     81   1.1   brezak #include <miscfs/specfs/specdev.h>
     82   1.1   brezak 
     83  1.17      cgd #include <dev/vndioctl.h>
     84   1.1   brezak 
     85   1.1   brezak #ifdef DEBUG
     86  1.17      cgd int dovndcluster = 1;
     87  1.17      cgd int vnddebug = 0x00;
     88   1.1   brezak #define VDB_FOLLOW	0x01
     89   1.1   brezak #define VDB_INIT	0x02
     90   1.1   brezak #define VDB_IO		0x04
     91   1.1   brezak #endif
     92   1.1   brezak 
     93   1.1   brezak #define b_cylin	b_resid
     94   1.1   brezak 
     95  1.18      cgd #define	vndunit(x)	DISKUNIT(x)
     96  1.18      cgd 
     97  1.18      cgd struct vndbuf {
     98  1.18      cgd 	struct buf	vb_buf;
     99  1.18      cgd 	struct buf	*vb_obp;
    100  1.18      cgd };
    101   1.1   brezak 
    102  1.17      cgd #define	getvndbuf()	\
    103  1.18      cgd 	((struct vndbuf *)malloc(sizeof(struct vndbuf), M_DEVBUF, M_WAITOK))
    104  1.18      cgd #define putvndbuf(vbp)	\
    105  1.18      cgd 	free((caddr_t)(vbp), M_DEVBUF)
    106   1.1   brezak 
    107  1.17      cgd struct vnd_softc {
    108   1.1   brezak 	int		 sc_flags;	/* flags */
    109  1.17      cgd 	size_t		 sc_size;	/* size of vnd */
    110   1.1   brezak 	struct vnode	*sc_vp;		/* vnode */
    111   1.1   brezak 	struct ucred	*sc_cred;	/* credentials */
    112   1.1   brezak 	int		 sc_maxactive;	/* max # of active requests */
    113   1.1   brezak 	struct buf	 sc_tab;	/* transfer queue */
    114   1.1   brezak };
    115   1.1   brezak 
    116   1.1   brezak /* sc_flags */
    117   1.1   brezak #define	VNF_ALIVE	0x01
    118   1.1   brezak #define VNF_INITED	0x02
    119   1.1   brezak 
    120   1.1   brezak #if 0	/* if you need static allocation */
    121  1.17      cgd struct vnd_softc vn_softc[NVND];
    122  1.17      cgd int numvnd = NVND;
    123   1.1   brezak #else
    124  1.17      cgd struct vnd_softc *vnd_softc;
    125   1.1   brezak int numvnd;
    126   1.1   brezak #endif
    127   1.1   brezak 
    128  1.17      cgd void	vndclear __P((struct vnd_softc *));
    129  1.17      cgd void	vndstart __P((struct vnd_softc *));
    130  1.17      cgd int	vndsetcred __P((struct vnd_softc *, struct ucred *));
    131  1.17      cgd void	vndthrottle __P((struct vnd_softc *, struct vnode *));
    132  1.16      cgd 
    133   1.1   brezak void
    134  1.17      cgd vndattach(num)
    135   1.1   brezak 	int num;
    136   1.1   brezak {
    137   1.1   brezak 	char *mem;
    138   1.1   brezak 	register u_long size;
    139   1.1   brezak 
    140   1.1   brezak 	if (num <= 0)
    141   1.1   brezak 		return;
    142  1.17      cgd 	size = num * sizeof(struct vnd_softc);
    143   1.1   brezak 	mem = malloc(size, M_DEVBUF, M_NOWAIT);
    144   1.1   brezak 	if (mem == NULL) {
    145   1.1   brezak 		printf("WARNING: no memory for vnode disks\n");
    146   1.1   brezak 		return;
    147   1.1   brezak 	}
    148   1.1   brezak 	bzero(mem, size);
    149  1.17      cgd 	vnd_softc = (struct vnd_softc *)mem;
    150   1.1   brezak 	numvnd = num;
    151   1.1   brezak }
    152   1.1   brezak 
    153   1.1   brezak int
    154  1.17      cgd vndopen(dev, flags, mode, p)
    155   1.1   brezak 	dev_t dev;
    156   1.1   brezak 	int flags, mode;
    157   1.1   brezak 	struct proc *p;
    158   1.1   brezak {
    159  1.17      cgd 	int unit = vndunit(dev);
    160   1.1   brezak 
    161   1.1   brezak #ifdef DEBUG
    162  1.17      cgd 	if (vnddebug & VDB_FOLLOW)
    163  1.17      cgd 		printf("vndopen(%x, %x, %x, %x)\n", dev, flags, mode, p);
    164   1.1   brezak #endif
    165   1.1   brezak 	if (unit >= numvnd)
    166   1.1   brezak 		return(ENXIO);
    167   1.1   brezak 	return(0);
    168   1.1   brezak }
    169   1.1   brezak 
    170   1.4  deraadt int
    171  1.17      cgd vndclose(dev, flags, mode, p)
    172   1.4  deraadt 	dev_t dev;
    173   1.4  deraadt 	int flags, mode;
    174   1.4  deraadt 	struct proc *p;
    175   1.4  deraadt {
    176   1.4  deraadt #ifdef DEBUG
    177  1.17      cgd 	if (vnddebug & VDB_FOLLOW)
    178  1.17      cgd 		printf("vndclose(%x, %x, %x, %x)\n", dev, flags, mode, p);
    179   1.4  deraadt #endif
    180   1.4  deraadt 	return 0;
    181   1.4  deraadt }
    182   1.4  deraadt 
    183   1.1   brezak /*
    184   1.1   brezak  * Break the request into bsize pieces and submit using VOP_BMAP/VOP_STRATEGY.
    185   1.1   brezak  * Note that this driver can only be used for swapping over NFS on the hp
    186   1.1   brezak  * since nfs_strategy on the vax cannot handle u-areas and page tables.
    187   1.1   brezak  */
    188   1.6  mycroft void
    189  1.17      cgd vndstrategy(bp)
    190   1.1   brezak 	register struct buf *bp;
    191   1.1   brezak {
    192  1.17      cgd 	int unit = vndunit(bp->b_dev);
    193  1.17      cgd 	register struct vnd_softc *vnd = &vnd_softc[unit];
    194  1.18      cgd 	register struct vndbuf *nbp;
    195   1.1   brezak 	register int bn, bsize, resid;
    196   1.1   brezak 	register caddr_t addr;
    197   1.5      cgd 	int sz, flags, error;
    198  1.17      cgd 	extern void vndiodone();
    199   1.1   brezak 
    200   1.1   brezak #ifdef DEBUG
    201  1.17      cgd 	if (vnddebug & VDB_FOLLOW)
    202  1.17      cgd 		printf("vndstrategy(%x): unit %d\n", bp, unit);
    203   1.1   brezak #endif
    204  1.17      cgd 	if ((vnd->sc_flags & VNF_INITED) == 0) {
    205   1.1   brezak 		bp->b_error = ENXIO;
    206   1.1   brezak 		bp->b_flags |= B_ERROR;
    207   1.1   brezak 		biodone(bp);
    208   1.1   brezak 		return;
    209   1.1   brezak 	}
    210   1.1   brezak 	bn = bp->b_blkno;
    211   1.1   brezak 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    212   1.1   brezak 	bp->b_resid = bp->b_bcount;
    213  1.17      cgd 	if (bn < 0 || bn + sz > vnd->sc_size) {
    214  1.17      cgd 		if (bn != vnd->sc_size) {
    215   1.1   brezak 			bp->b_error = EINVAL;
    216   1.1   brezak 			bp->b_flags |= B_ERROR;
    217   1.1   brezak 		}
    218   1.1   brezak 		biodone(bp);
    219   1.1   brezak 		return;
    220   1.1   brezak 	}
    221   1.1   brezak 	bn = dbtob(bn);
    222  1.17      cgd  	bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
    223   1.5      cgd 	addr = bp->b_data;
    224   1.1   brezak 	flags = bp->b_flags | B_CALL;
    225   1.1   brezak 	for (resid = bp->b_resid; resid; resid -= sz) {
    226   1.1   brezak 		struct vnode *vp;
    227   1.1   brezak 		daddr_t nbn;
    228   1.5      cgd 		int off, s, nra;
    229   1.1   brezak 
    230   1.5      cgd 		nra = 0;
    231  1.21  mycroft 		VOP_LOCK(vnd->sc_vp);
    232  1.17      cgd 		error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
    233  1.21  mycroft 		VOP_UNLOCK(vnd->sc_vp);
    234   1.5      cgd 		if (error == 0 && (long)nbn == -1)
    235   1.5      cgd 			error = EIO;
    236   1.5      cgd #ifdef DEBUG
    237  1.17      cgd 		if (!dovndcluster)
    238   1.5      cgd 			nra = 0;
    239   1.1   brezak #endif
    240   1.5      cgd 
    241   1.5      cgd 		if (off = bn % bsize)
    242   1.5      cgd 			sz = bsize - off;
    243   1.5      cgd 		else
    244   1.5      cgd 			sz = (1 + nra) * bsize;
    245   1.5      cgd 		if (resid < sz)
    246   1.5      cgd 			sz = resid;
    247   1.1   brezak #ifdef DEBUG
    248  1.17      cgd 		if (vnddebug & VDB_IO)
    249  1.17      cgd 			printf("vndstrategy: vp %x/%x bn %x/%x sz %x\n",
    250  1.17      cgd 			       vnd->sc_vp, vp, bn, nbn, sz);
    251   1.1   brezak #endif
    252   1.5      cgd 
    253  1.17      cgd 		nbp = getvndbuf();
    254  1.18      cgd 		nbp->vb_buf.b_flags = flags;
    255  1.18      cgd 		nbp->vb_buf.b_bcount = sz;
    256  1.18      cgd 		nbp->vb_buf.b_bufsize = bp->b_bufsize;
    257  1.18      cgd 		nbp->vb_buf.b_error = 0;
    258   1.1   brezak 		if (vp->v_type == VBLK || vp->v_type == VCHR)
    259  1.18      cgd 			nbp->vb_buf.b_dev = vp->v_rdev;
    260   1.1   brezak 		else
    261  1.18      cgd 			nbp->vb_buf.b_dev = NODEV;
    262  1.18      cgd 		nbp->vb_buf.b_data = addr;
    263  1.18      cgd 		nbp->vb_buf.b_blkno = nbn + btodb(off);
    264  1.18      cgd 		nbp->vb_buf.b_proc = bp->b_proc;
    265  1.18      cgd 		nbp->vb_buf.b_iodone = vndiodone;
    266  1.18      cgd 		nbp->vb_buf.b_vp = vp;
    267  1.18      cgd 		nbp->vb_buf.b_rcred = vnd->sc_cred;	/* XXX crdup? */
    268  1.18      cgd 		nbp->vb_buf.b_wcred = vnd->sc_cred;	/* XXX crdup? */
    269  1.18      cgd 		nbp->vb_buf.b_dirtyoff = bp->b_dirtyoff;
    270  1.18      cgd 		nbp->vb_buf.b_dirtyend = bp->b_dirtyend;
    271  1.18      cgd 		nbp->vb_buf.b_validoff = bp->b_validoff;
    272  1.18      cgd 		nbp->vb_buf.b_validend = bp->b_validend;
    273  1.18      cgd 
    274  1.18      cgd 		/* save a reference to the old buffer */
    275  1.18      cgd 		nbp->vb_obp = bp;
    276  1.18      cgd 
    277   1.1   brezak 		/*
    278   1.5      cgd 		 * If there was an error or a hole in the file...punt.
    279   1.1   brezak 		 * Note that we deal with this after the nbp allocation.
    280   1.1   brezak 		 * This ensures that we properly clean up any operations
    281   1.1   brezak 		 * that we have already fired off.
    282   1.1   brezak 		 *
    283   1.5      cgd 		 * XXX we could deal with holes here but it would be
    284   1.1   brezak 		 * a hassle (in the write case).
    285   1.1   brezak 		 */
    286   1.5      cgd 		if (error) {
    287  1.18      cgd 			nbp->vb_buf.b_error = error;
    288  1.18      cgd 			nbp->vb_buf.b_flags |= B_ERROR;
    289   1.1   brezak 			bp->b_resid -= (resid - sz);
    290  1.18      cgd 			biodone(&nbp->vb_buf);
    291   1.1   brezak 			return;
    292   1.1   brezak 		}
    293   1.1   brezak 		/*
    294   1.1   brezak 		 * Just sort by block number
    295   1.1   brezak 		 */
    296  1.18      cgd 		nbp->vb_buf.b_cylin = nbp->vb_buf.b_blkno;
    297   1.1   brezak 		s = splbio();
    298  1.18      cgd 		disksort(&vnd->sc_tab, &nbp->vb_buf);
    299  1.17      cgd 		if (vnd->sc_tab.b_active < vnd->sc_maxactive) {
    300  1.17      cgd 			vnd->sc_tab.b_active++;
    301  1.17      cgd 			vndstart(vnd);
    302   1.1   brezak 		}
    303   1.1   brezak 		splx(s);
    304   1.1   brezak 		bn += sz;
    305   1.1   brezak 		addr += sz;
    306   1.1   brezak 	}
    307   1.1   brezak }
    308   1.1   brezak 
    309   1.1   brezak /*
    310   1.1   brezak  * Feed requests sequentially.
    311   1.1   brezak  * We do it this way to keep from flooding NFS servers if we are connected
    312   1.1   brezak  * to an NFS file.  This places the burden on the client rather than the
    313   1.1   brezak  * server.
    314   1.1   brezak  */
    315  1.16      cgd void
    316  1.17      cgd vndstart(vnd)
    317  1.17      cgd 	register struct vnd_softc *vnd;
    318   1.1   brezak {
    319   1.1   brezak 	register struct buf *bp;
    320   1.1   brezak 
    321   1.1   brezak 	/*
    322   1.1   brezak 	 * Dequeue now since lower level strategy routine might
    323   1.1   brezak 	 * queue using same links
    324   1.1   brezak 	 */
    325  1.17      cgd 	bp = vnd->sc_tab.b_actf;
    326  1.17      cgd 	vnd->sc_tab.b_actf = bp->b_actf;
    327   1.1   brezak #ifdef DEBUG
    328  1.17      cgd 	if (vnddebug & VDB_IO)
    329  1.17      cgd 		printf("vndstart(%d): bp %x vp %x blkno %x addr %x cnt %x\n",
    330  1.17      cgd 		    vnd-vnd_softc, bp, bp->b_vp, bp->b_blkno, bp->b_data,
    331   1.5      cgd 		    bp->b_bcount);
    332   1.1   brezak #endif
    333   1.1   brezak 	if ((bp->b_flags & B_READ) == 0)
    334   1.1   brezak 		bp->b_vp->v_numoutput++;
    335   1.1   brezak 	VOP_STRATEGY(bp);
    336   1.1   brezak }
    337   1.1   brezak 
    338   1.1   brezak void
    339  1.18      cgd vndiodone(vbp)
    340  1.18      cgd 	register struct vndbuf *vbp;
    341   1.1   brezak {
    342  1.18      cgd 	register struct buf *pbp = vbp->vb_obp;
    343  1.17      cgd 	register struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
    344   1.1   brezak 	int s;
    345   1.1   brezak 
    346   1.1   brezak 	s = splbio();
    347   1.1   brezak #ifdef DEBUG
    348  1.17      cgd 	if (vnddebug & VDB_IO)
    349  1.18      cgd 		printf("vndiodone(%d): vbp %x vp %x blkno %x addr %x cnt %x\n",
    350  1.18      cgd 		    vnd-vnd_softc, vbp, vbp->vb_buf.b_vp, vbp->vb_buf.b_blkno,
    351  1.18      cgd 		    vbp->vb_buf.b_data, vbp->vb_buf.b_bcount);
    352   1.1   brezak #endif
    353  1.18      cgd 	if (vbp->vb_buf.b_error) {
    354   1.1   brezak #ifdef DEBUG
    355  1.17      cgd 		if (vnddebug & VDB_IO)
    356  1.18      cgd 			printf("vndiodone: vbp %x error %d\n", vbp,
    357  1.18      cgd 			    vbp->vb_buf.b_error);
    358   1.1   brezak #endif
    359   1.1   brezak 		pbp->b_flags |= B_ERROR;
    360  1.18      cgd 		pbp->b_error = biowait(&vbp->vb_buf);
    361   1.1   brezak 	}
    362  1.18      cgd 	pbp->b_resid -= vbp->vb_buf.b_bcount;
    363  1.18      cgd 	putvndbuf(vbp);
    364   1.1   brezak 	if (pbp->b_resid == 0) {
    365   1.1   brezak #ifdef DEBUG
    366  1.17      cgd 		if (vnddebug & VDB_IO)
    367  1.17      cgd 			printf("vndiodone: pbp %x iodone\n", pbp);
    368   1.1   brezak #endif
    369   1.1   brezak 		biodone(pbp);
    370   1.1   brezak 	}
    371  1.17      cgd 	if (vnd->sc_tab.b_actf)
    372  1.17      cgd 		vndstart(vnd);
    373   1.1   brezak 	else
    374  1.17      cgd 		vnd->sc_tab.b_active--;
    375   1.1   brezak 	splx(s);
    376  1.20  mycroft }
    377  1.20  mycroft 
    378  1.20  mycroft int
    379  1.20  mycroft vndread(dev, uio)
    380  1.20  mycroft 	dev_t dev;
    381  1.20  mycroft 	struct uio *uio;
    382  1.20  mycroft {
    383  1.20  mycroft 
    384  1.20  mycroft #ifdef DEBUG
    385  1.20  mycroft 	if (vnddebug & VDB_FOLLOW)
    386  1.20  mycroft 		printf("vndread(%x, %x)\n", dev, uio);
    387  1.20  mycroft #endif
    388  1.20  mycroft 	return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
    389  1.20  mycroft }
    390  1.20  mycroft 
    391  1.20  mycroft int
    392  1.20  mycroft vndwrite(dev, uio)
    393  1.20  mycroft 	dev_t dev;
    394  1.20  mycroft 	struct uio *uio;
    395  1.20  mycroft {
    396  1.20  mycroft 
    397  1.20  mycroft #ifdef DEBUG
    398  1.20  mycroft 	if (vnddebug & VDB_FOLLOW)
    399  1.20  mycroft 		printf("vndwrite(%x, %x)\n", dev, uio);
    400  1.20  mycroft #endif
    401  1.20  mycroft 	return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
    402   1.1   brezak }
    403   1.1   brezak 
    404   1.1   brezak /* ARGSUSED */
    405  1.16      cgd int
    406  1.17      cgd vndioctl(dev, cmd, data, flag, p)
    407   1.1   brezak 	dev_t dev;
    408   1.1   brezak 	u_long cmd;
    409   1.1   brezak 	caddr_t data;
    410   1.1   brezak 	int flag;
    411   1.1   brezak 	struct proc *p;
    412   1.1   brezak {
    413  1.17      cgd 	int unit = vndunit(dev);
    414  1.17      cgd 	register struct vnd_softc *vnd;
    415  1.17      cgd 	struct vnd_ioctl *vio;
    416   1.1   brezak 	struct vattr vattr;
    417   1.1   brezak 	struct nameidata nd;
    418   1.1   brezak 	int error;
    419   1.1   brezak 
    420   1.1   brezak #ifdef DEBUG
    421  1.17      cgd 	if (vnddebug & VDB_FOLLOW)
    422  1.17      cgd 		printf("vndioctl(%x, %lx, %x, %x, %x): unit %d\n",
    423   1.5      cgd 		    dev, cmd, data, flag, p, unit);
    424   1.1   brezak #endif
    425   1.1   brezak 	error = suser(p->p_ucred, &p->p_acflag);
    426   1.1   brezak 	if (error)
    427   1.1   brezak 		return (error);
    428   1.1   brezak 	if (unit >= numvnd)
    429   1.1   brezak 		return (ENXIO);
    430   1.1   brezak 
    431  1.17      cgd 	vnd = &vnd_softc[unit];
    432  1.17      cgd 	vio = (struct vnd_ioctl *)data;
    433   1.1   brezak 	switch (cmd) {
    434   1.1   brezak 
    435  1.17      cgd 	case VNDIOCSET:
    436  1.17      cgd 		if (vnd->sc_flags & VNF_INITED)
    437   1.1   brezak 			return(EBUSY);
    438   1.1   brezak 		/*
    439   1.1   brezak 		 * Always open for read and write.
    440   1.1   brezak 		 * This is probably bogus, but it lets vn_open()
    441   1.1   brezak 		 * weed out directories, sockets, etc. so we don't
    442   1.1   brezak 		 * have to worry about them.
    443   1.1   brezak 		 */
    444  1.17      cgd 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
    445  1.15  mycroft 		if (error = vn_open(&nd, FREAD|FWRITE, 0))
    446   1.1   brezak 			return(error);
    447   1.1   brezak 		if (error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p)) {
    448   1.1   brezak 			VOP_UNLOCK(nd.ni_vp);
    449   1.1   brezak 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
    450   1.1   brezak 			return(error);
    451   1.1   brezak 		}
    452   1.1   brezak 		VOP_UNLOCK(nd.ni_vp);
    453  1.17      cgd 		vnd->sc_vp = nd.ni_vp;
    454  1.17      cgd 		vnd->sc_size = btodb(vattr.va_size);	/* note truncation */
    455  1.17      cgd 		if (error = vndsetcred(vnd, p->p_ucred)) {
    456   1.5      cgd 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
    457   1.1   brezak 			return(error);
    458   1.1   brezak 		}
    459  1.17      cgd 		vndthrottle(vnd, vnd->sc_vp);
    460  1.17      cgd 		vio->vnd_size = dbtob(vnd->sc_size);
    461  1.17      cgd 		vnd->sc_flags |= VNF_INITED;
    462  1.17      cgd #ifdef DEBUG
    463  1.17      cgd 		if (vnddebug & VDB_INIT)
    464  1.17      cgd 			printf("vndioctl: SET vp %x size %x\n",
    465  1.17      cgd 			    vnd->sc_vp, vnd->sc_size);
    466   1.1   brezak #endif
    467   1.1   brezak 		break;
    468   1.1   brezak 
    469  1.17      cgd 	case VNDIOCCLR:
    470  1.17      cgd 		if ((vnd->sc_flags & VNF_INITED) == 0)
    471   1.1   brezak 			return(ENXIO);
    472  1.17      cgd 		vndclear(vnd);
    473   1.1   brezak #ifdef DEBUG
    474  1.17      cgd 		if (vnddebug & VDB_INIT)
    475  1.17      cgd 			printf("vndioctl: CLRed\n");
    476   1.1   brezak #endif
    477   1.1   brezak 		break;
    478   1.1   brezak 
    479   1.1   brezak 	default:
    480  1.12      cgd 		return(ENOTTY);
    481   1.1   brezak 	}
    482   1.1   brezak 	return(0);
    483   1.1   brezak }
    484   1.1   brezak 
    485   1.1   brezak /*
    486   1.1   brezak  * Duplicate the current processes' credentials.  Since we are called only
    487   1.1   brezak  * as the result of a SET ioctl and only root can do that, any future access
    488   1.1   brezak  * to this "disk" is essentially as root.  Note that credentials may change
    489   1.1   brezak  * if some other uid can write directly to the mapped file (NFS).
    490   1.1   brezak  */
    491  1.16      cgd int
    492  1.17      cgd vndsetcred(vnd, cred)
    493  1.17      cgd 	register struct vnd_softc *vnd;
    494   1.1   brezak 	struct ucred *cred;
    495   1.1   brezak {
    496   1.1   brezak 	struct uio auio;
    497   1.1   brezak 	struct iovec aiov;
    498   1.5      cgd 	char *tmpbuf;
    499   1.5      cgd 	int error;
    500   1.1   brezak 
    501  1.17      cgd 	vnd->sc_cred = crdup(cred);
    502   1.5      cgd 	tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
    503   1.5      cgd 
    504   1.1   brezak 	/* XXX: Horrible kludge to establish credentials for NFS */
    505   1.1   brezak 	aiov.iov_base = tmpbuf;
    506  1.17      cgd 	aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
    507   1.1   brezak 	auio.uio_iov = &aiov;
    508   1.1   brezak 	auio.uio_iovcnt = 1;
    509   1.1   brezak 	auio.uio_offset = 0;
    510   1.1   brezak 	auio.uio_rw = UIO_READ;
    511   1.1   brezak 	auio.uio_segflg = UIO_SYSSPACE;
    512   1.1   brezak 	auio.uio_resid = aiov.iov_len;
    513  1.21  mycroft 	VOP_LOCK(vnd->sc_vp);
    514  1.17      cgd 	error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
    515  1.21  mycroft 	VOP_UNLOCK(vnd->sc_vp);
    516   1.5      cgd 
    517   1.5      cgd 	free(tmpbuf, M_TEMP);
    518   1.5      cgd 	return (error);
    519   1.1   brezak }
    520   1.1   brezak 
    521   1.1   brezak /*
    522   1.1   brezak  * Set maxactive based on FS type
    523   1.1   brezak  */
    524  1.16      cgd void
    525  1.17      cgd vndthrottle(vnd, vp)
    526  1.17      cgd 	register struct vnd_softc *vnd;
    527   1.1   brezak 	struct vnode *vp;
    528   1.1   brezak {
    529   1.1   brezak #ifdef NFSCLIENT
    530   1.1   brezak 	extern int (**nfsv2_vnodeop_p)();
    531   1.5      cgd 
    532   1.2   brezak 	if (vp->v_op == nfsv2_vnodeop_p)
    533  1.17      cgd 		vnd->sc_maxactive = 2;
    534   1.1   brezak 	else
    535   1.1   brezak #endif
    536  1.17      cgd 		vnd->sc_maxactive = 8;
    537   1.1   brezak 
    538  1.17      cgd 	if (vnd->sc_maxactive < 1)
    539  1.17      cgd 		vnd->sc_maxactive = 1;
    540   1.1   brezak }
    541   1.1   brezak 
    542  1.16      cgd void
    543  1.17      cgd vndshutdown()
    544   1.1   brezak {
    545  1.17      cgd 	register struct vnd_softc *vnd;
    546   1.1   brezak 
    547  1.17      cgd 	for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
    548  1.17      cgd 		if (vnd->sc_flags & VNF_INITED)
    549  1.17      cgd 			vndclear(vnd);
    550   1.1   brezak }
    551   1.1   brezak 
    552  1.16      cgd void
    553  1.17      cgd vndclear(vnd)
    554  1.17      cgd 	register struct vnd_softc *vnd;
    555   1.1   brezak {
    556  1.17      cgd 	register struct vnode *vp = vnd->sc_vp;
    557   1.1   brezak 	struct proc *p = curproc;		/* XXX */
    558   1.1   brezak 
    559   1.1   brezak #ifdef DEBUG
    560  1.17      cgd 	if (vnddebug & VDB_FOLLOW)
    561  1.17      cgd 		printf("vndclear(%x): vp %x\n", vp);
    562   1.1   brezak #endif
    563  1.17      cgd 	vnd->sc_flags &= ~VNF_INITED;
    564   1.1   brezak 	if (vp == (struct vnode *)0)
    565  1.17      cgd 		panic("vndioctl: null vp");
    566  1.17      cgd 	(void) vn_close(vp, FREAD|FWRITE, vnd->sc_cred, p);
    567  1.17      cgd 	crfree(vnd->sc_cred);
    568  1.17      cgd 	vnd->sc_vp = (struct vnode *)0;
    569  1.17      cgd 	vnd->sc_cred = (struct ucred *)0;
    570  1.17      cgd 	vnd->sc_size = 0;
    571   1.1   brezak }
    572   1.1   brezak 
    573  1.16      cgd int
    574  1.17      cgd vndsize(dev)
    575   1.1   brezak 	dev_t dev;
    576   1.1   brezak {
    577  1.17      cgd 	int unit = vndunit(dev);
    578  1.17      cgd 	register struct vnd_softc *vnd = &vnd_softc[unit];
    579   1.1   brezak 
    580  1.17      cgd 	if (unit >= numvnd || (vnd->sc_flags & VNF_INITED) == 0)
    581   1.1   brezak 		return(-1);
    582  1.17      cgd 	return(vnd->sc_size);
    583   1.1   brezak }
    584   1.1   brezak 
    585  1.16      cgd int
    586  1.19      cgd vnddump(dev, blkno, va, size)
    587  1.16      cgd 	dev_t dev;
    588  1.19      cgd 	daddr_t blkno;
    589  1.19      cgd 	caddr_t va;
    590  1.19      cgd 	size_t size;
    591   1.1   brezak {
    592  1.16      cgd 
    593  1.19      cgd 	/* Not implemented. */
    594  1.19      cgd 	return ENXIO;
    595   1.1   brezak }
    596   1.1   brezak #endif
    597