Home | History | Annotate | Line # | Download | only in dev
vnd.c revision 1.22
      1  1.22  thorpej /*	$NetBSD: vnd.c,v 1.22 1995/11/06 20:28:09 thorpej 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.1   brezak 
     64   1.1   brezak #include <sys/param.h>
     65   1.1   brezak #include <sys/systm.h>
     66   1.1   brezak #include <sys/namei.h>
     67   1.1   brezak #include <sys/proc.h>
     68   1.1   brezak #include <sys/errno.h>
     69   1.1   brezak #include <sys/dkstat.h>
     70   1.1   brezak #include <sys/buf.h>
     71   1.1   brezak #include <sys/malloc.h>
     72   1.1   brezak #include <sys/ioctl.h>
     73  1.16      cgd #include <sys/disklabel.h>
     74  1.22  thorpej #include <sys/device.h>
     75  1.22  thorpej #include <sys/disk.h>
     76  1.22  thorpej #include <sys/stat.h>
     77  1.22  thorpej #include <sys/conf.h>
     78   1.1   brezak #include <sys/mount.h>
     79   1.1   brezak #include <sys/vnode.h>
     80   1.1   brezak #include <sys/file.h>
     81   1.1   brezak #include <sys/uio.h>
     82   1.1   brezak 
     83   1.1   brezak #include <miscfs/specfs/specdev.h>
     84   1.1   brezak 
     85  1.17      cgd #include <dev/vndioctl.h>
     86   1.1   brezak 
     87   1.1   brezak #ifdef DEBUG
     88  1.17      cgd int dovndcluster = 1;
     89  1.17      cgd int vnddebug = 0x00;
     90   1.1   brezak #define VDB_FOLLOW	0x01
     91   1.1   brezak #define VDB_INIT	0x02
     92   1.1   brezak #define VDB_IO		0x04
     93   1.1   brezak #endif
     94   1.1   brezak 
     95   1.1   brezak #define b_cylin	b_resid
     96   1.1   brezak 
     97  1.18      cgd #define	vndunit(x)	DISKUNIT(x)
     98  1.18      cgd 
     99  1.18      cgd struct vndbuf {
    100  1.18      cgd 	struct buf	vb_buf;
    101  1.18      cgd 	struct buf	*vb_obp;
    102  1.18      cgd };
    103   1.1   brezak 
    104  1.17      cgd #define	getvndbuf()	\
    105  1.18      cgd 	((struct vndbuf *)malloc(sizeof(struct vndbuf), M_DEVBUF, M_WAITOK))
    106  1.18      cgd #define putvndbuf(vbp)	\
    107  1.18      cgd 	free((caddr_t)(vbp), M_DEVBUF)
    108   1.1   brezak 
    109  1.17      cgd struct vnd_softc {
    110   1.1   brezak 	int		 sc_flags;	/* flags */
    111  1.17      cgd 	size_t		 sc_size;	/* size of vnd */
    112   1.1   brezak 	struct vnode	*sc_vp;		/* vnode */
    113   1.1   brezak 	struct ucred	*sc_cred;	/* credentials */
    114   1.1   brezak 	int		 sc_maxactive;	/* max # of active requests */
    115   1.1   brezak 	struct buf	 sc_tab;	/* transfer queue */
    116  1.22  thorpej 	struct dkdevice  sc_dkdev;	/* generic disk device info */
    117   1.1   brezak };
    118   1.1   brezak 
    119   1.1   brezak /* sc_flags */
    120   1.1   brezak #define	VNF_ALIVE	0x01
    121   1.1   brezak #define VNF_INITED	0x02
    122  1.22  thorpej #define VNF_WANTED	0x40
    123  1.22  thorpej #define VNF_LOCKED	0x80
    124   1.1   brezak 
    125  1.17      cgd struct vnd_softc *vnd_softc;
    126  1.22  thorpej int numvnd = 0;
    127  1.22  thorpej 
    128  1.22  thorpej /* {b,c}devsw[] function prototypes */
    129  1.22  thorpej dev_type_open(vndopen);
    130  1.22  thorpej dev_type_close(vndclose);
    131  1.22  thorpej dev_type_strategy(vndstrategy);
    132  1.22  thorpej dev_type_ioctl(vndioctl);
    133  1.22  thorpej dev_type_read(vndread);
    134  1.22  thorpej dev_type_write(vndwrite);
    135  1.22  thorpej 
    136  1.22  thorpej /* called by main() at boot time */
    137  1.22  thorpej void	vndattach __P((int));
    138   1.1   brezak 
    139  1.17      cgd void	vndclear __P((struct vnd_softc *));
    140  1.17      cgd void	vndstart __P((struct vnd_softc *));
    141  1.17      cgd int	vndsetcred __P((struct vnd_softc *, struct ucred *));
    142  1.17      cgd void	vndthrottle __P((struct vnd_softc *, struct vnode *));
    143  1.16      cgd 
    144  1.22  thorpej static	int vndlock __P((struct vnd_softc *));
    145  1.22  thorpej static	void vndunlock __P((struct vnd_softc *));
    146  1.22  thorpej 
    147   1.1   brezak void
    148  1.17      cgd vndattach(num)
    149   1.1   brezak 	int num;
    150   1.1   brezak {
    151   1.1   brezak 	char *mem;
    152   1.1   brezak 	register u_long size;
    153   1.1   brezak 
    154   1.1   brezak 	if (num <= 0)
    155   1.1   brezak 		return;
    156  1.17      cgd 	size = num * sizeof(struct vnd_softc);
    157   1.1   brezak 	mem = malloc(size, M_DEVBUF, M_NOWAIT);
    158   1.1   brezak 	if (mem == NULL) {
    159   1.1   brezak 		printf("WARNING: no memory for vnode disks\n");
    160   1.1   brezak 		return;
    161   1.1   brezak 	}
    162   1.1   brezak 	bzero(mem, size);
    163  1.17      cgd 	vnd_softc = (struct vnd_softc *)mem;
    164   1.1   brezak 	numvnd = num;
    165   1.1   brezak }
    166   1.1   brezak 
    167   1.1   brezak int
    168  1.17      cgd vndopen(dev, flags, mode, p)
    169   1.1   brezak 	dev_t dev;
    170   1.1   brezak 	int flags, mode;
    171   1.1   brezak 	struct proc *p;
    172   1.1   brezak {
    173  1.17      cgd 	int unit = vndunit(dev);
    174  1.22  thorpej 	struct vnd_softc *sc;
    175  1.22  thorpej 	int error = 0, part, pmask;
    176  1.22  thorpej 
    177  1.22  thorpej 	/*
    178  1.22  thorpej 	 * XXX Should support disklabels.
    179  1.22  thorpej 	 */
    180   1.1   brezak 
    181   1.1   brezak #ifdef DEBUG
    182  1.17      cgd 	if (vnddebug & VDB_FOLLOW)
    183  1.17      cgd 		printf("vndopen(%x, %x, %x, %x)\n", dev, flags, mode, p);
    184   1.1   brezak #endif
    185   1.1   brezak 	if (unit >= numvnd)
    186  1.22  thorpej 		return (ENXIO);
    187  1.22  thorpej 	sc = &vnd_softc[unit];
    188  1.22  thorpej 
    189  1.22  thorpej 	if (error = vndlock(sc))
    190  1.22  thorpej 		return (error);
    191  1.22  thorpej 
    192  1.22  thorpej 	part = DISKPART(dev);
    193  1.22  thorpej 	pmask = (1 << part);
    194  1.22  thorpej 
    195  1.22  thorpej 	/* Prevent our unit from being unconfigured while open. */
    196  1.22  thorpej 	switch (mode) {
    197  1.22  thorpej 	case S_IFCHR:
    198  1.22  thorpej 		sc->sc_dkdev.dk_copenmask |= pmask;
    199  1.22  thorpej 		break;
    200  1.22  thorpej 
    201  1.22  thorpej 	case S_IFBLK:
    202  1.22  thorpej 		sc->sc_dkdev.dk_bopenmask |= pmask;
    203  1.22  thorpej 		break;
    204  1.22  thorpej 	}
    205  1.22  thorpej 	sc->sc_dkdev.dk_openmask =
    206  1.22  thorpej 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
    207  1.22  thorpej 
    208  1.22  thorpej 	vndunlock(sc);
    209  1.22  thorpej 	return (0);
    210   1.1   brezak }
    211   1.1   brezak 
    212   1.4  deraadt int
    213  1.17      cgd vndclose(dev, flags, mode, p)
    214   1.4  deraadt 	dev_t dev;
    215   1.4  deraadt 	int flags, mode;
    216   1.4  deraadt 	struct proc *p;
    217   1.4  deraadt {
    218  1.22  thorpej 	int unit = vndunit(dev);
    219  1.22  thorpej 	struct vnd_softc *sc;
    220  1.22  thorpej 	int error = 0, part;
    221  1.22  thorpej 
    222   1.4  deraadt #ifdef DEBUG
    223  1.17      cgd 	if (vnddebug & VDB_FOLLOW)
    224  1.17      cgd 		printf("vndclose(%x, %x, %x, %x)\n", dev, flags, mode, p);
    225   1.4  deraadt #endif
    226  1.22  thorpej 
    227  1.22  thorpej 	if (unit >= numvnd)
    228  1.22  thorpej 		return (ENXIO);
    229  1.22  thorpej 	sc = &vnd_softc[unit];
    230  1.22  thorpej 
    231  1.22  thorpej 	if (error = vndlock(sc))
    232  1.22  thorpej 		return (error);
    233  1.22  thorpej 
    234  1.22  thorpej 	part = DISKPART(dev);
    235  1.22  thorpej 
    236  1.22  thorpej 	/* ...that much closer to allowing unconfiguration... */
    237  1.22  thorpej 	switch (mode) {
    238  1.22  thorpej 	case S_IFCHR:
    239  1.22  thorpej 		sc->sc_dkdev.dk_copenmask &= ~(1 << part);
    240  1.22  thorpej 		break;
    241  1.22  thorpej 
    242  1.22  thorpej 	case S_IFBLK:
    243  1.22  thorpej 		sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
    244  1.22  thorpej 		break;
    245  1.22  thorpej 	}
    246  1.22  thorpej 	sc->sc_dkdev.dk_openmask =
    247  1.22  thorpej 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
    248  1.22  thorpej 
    249  1.22  thorpej 	vndunlock(sc);
    250  1.22  thorpej 	return (0);
    251   1.4  deraadt }
    252   1.4  deraadt 
    253   1.1   brezak /*
    254   1.1   brezak  * Break the request into bsize pieces and submit using VOP_BMAP/VOP_STRATEGY.
    255   1.1   brezak  * Note that this driver can only be used for swapping over NFS on the hp
    256   1.1   brezak  * since nfs_strategy on the vax cannot handle u-areas and page tables.
    257   1.1   brezak  */
    258   1.6  mycroft void
    259  1.17      cgd vndstrategy(bp)
    260   1.1   brezak 	register struct buf *bp;
    261   1.1   brezak {
    262  1.17      cgd 	int unit = vndunit(bp->b_dev);
    263  1.17      cgd 	register struct vnd_softc *vnd = &vnd_softc[unit];
    264  1.18      cgd 	register struct vndbuf *nbp;
    265   1.1   brezak 	register int bn, bsize, resid;
    266   1.1   brezak 	register caddr_t addr;
    267   1.5      cgd 	int sz, flags, error;
    268  1.17      cgd 	extern void vndiodone();
    269   1.1   brezak 
    270   1.1   brezak #ifdef DEBUG
    271  1.17      cgd 	if (vnddebug & VDB_FOLLOW)
    272  1.17      cgd 		printf("vndstrategy(%x): unit %d\n", bp, unit);
    273   1.1   brezak #endif
    274  1.17      cgd 	if ((vnd->sc_flags & VNF_INITED) == 0) {
    275   1.1   brezak 		bp->b_error = ENXIO;
    276   1.1   brezak 		bp->b_flags |= B_ERROR;
    277   1.1   brezak 		biodone(bp);
    278   1.1   brezak 		return;
    279   1.1   brezak 	}
    280   1.1   brezak 	bn = bp->b_blkno;
    281   1.1   brezak 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    282   1.1   brezak 	bp->b_resid = bp->b_bcount;
    283  1.17      cgd 	if (bn < 0 || bn + sz > vnd->sc_size) {
    284  1.17      cgd 		if (bn != vnd->sc_size) {
    285   1.1   brezak 			bp->b_error = EINVAL;
    286   1.1   brezak 			bp->b_flags |= B_ERROR;
    287   1.1   brezak 		}
    288   1.1   brezak 		biodone(bp);
    289   1.1   brezak 		return;
    290   1.1   brezak 	}
    291   1.1   brezak 	bn = dbtob(bn);
    292  1.17      cgd  	bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
    293   1.5      cgd 	addr = bp->b_data;
    294   1.1   brezak 	flags = bp->b_flags | B_CALL;
    295   1.1   brezak 	for (resid = bp->b_resid; resid; resid -= sz) {
    296   1.1   brezak 		struct vnode *vp;
    297   1.1   brezak 		daddr_t nbn;
    298   1.5      cgd 		int off, s, nra;
    299   1.1   brezak 
    300   1.5      cgd 		nra = 0;
    301  1.21  mycroft 		VOP_LOCK(vnd->sc_vp);
    302  1.17      cgd 		error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
    303  1.21  mycroft 		VOP_UNLOCK(vnd->sc_vp);
    304   1.5      cgd 		if (error == 0 && (long)nbn == -1)
    305   1.5      cgd 			error = EIO;
    306   1.5      cgd #ifdef DEBUG
    307  1.17      cgd 		if (!dovndcluster)
    308   1.5      cgd 			nra = 0;
    309   1.1   brezak #endif
    310   1.5      cgd 
    311   1.5      cgd 		if (off = bn % bsize)
    312   1.5      cgd 			sz = bsize - off;
    313   1.5      cgd 		else
    314   1.5      cgd 			sz = (1 + nra) * bsize;
    315   1.5      cgd 		if (resid < sz)
    316   1.5      cgd 			sz = resid;
    317   1.1   brezak #ifdef DEBUG
    318  1.17      cgd 		if (vnddebug & VDB_IO)
    319  1.17      cgd 			printf("vndstrategy: vp %x/%x bn %x/%x sz %x\n",
    320  1.17      cgd 			       vnd->sc_vp, vp, bn, nbn, sz);
    321   1.1   brezak #endif
    322   1.5      cgd 
    323  1.17      cgd 		nbp = getvndbuf();
    324  1.18      cgd 		nbp->vb_buf.b_flags = flags;
    325  1.18      cgd 		nbp->vb_buf.b_bcount = sz;
    326  1.18      cgd 		nbp->vb_buf.b_bufsize = bp->b_bufsize;
    327  1.18      cgd 		nbp->vb_buf.b_error = 0;
    328   1.1   brezak 		if (vp->v_type == VBLK || vp->v_type == VCHR)
    329  1.18      cgd 			nbp->vb_buf.b_dev = vp->v_rdev;
    330   1.1   brezak 		else
    331  1.18      cgd 			nbp->vb_buf.b_dev = NODEV;
    332  1.18      cgd 		nbp->vb_buf.b_data = addr;
    333  1.18      cgd 		nbp->vb_buf.b_blkno = nbn + btodb(off);
    334  1.18      cgd 		nbp->vb_buf.b_proc = bp->b_proc;
    335  1.18      cgd 		nbp->vb_buf.b_iodone = vndiodone;
    336  1.18      cgd 		nbp->vb_buf.b_vp = vp;
    337  1.18      cgd 		nbp->vb_buf.b_rcred = vnd->sc_cred;	/* XXX crdup? */
    338  1.18      cgd 		nbp->vb_buf.b_wcred = vnd->sc_cred;	/* XXX crdup? */
    339  1.18      cgd 		nbp->vb_buf.b_dirtyoff = bp->b_dirtyoff;
    340  1.18      cgd 		nbp->vb_buf.b_dirtyend = bp->b_dirtyend;
    341  1.18      cgd 		nbp->vb_buf.b_validoff = bp->b_validoff;
    342  1.18      cgd 		nbp->vb_buf.b_validend = bp->b_validend;
    343  1.18      cgd 
    344  1.18      cgd 		/* save a reference to the old buffer */
    345  1.18      cgd 		nbp->vb_obp = bp;
    346  1.18      cgd 
    347   1.1   brezak 		/*
    348   1.5      cgd 		 * If there was an error or a hole in the file...punt.
    349   1.1   brezak 		 * Note that we deal with this after the nbp allocation.
    350   1.1   brezak 		 * This ensures that we properly clean up any operations
    351   1.1   brezak 		 * that we have already fired off.
    352   1.1   brezak 		 *
    353   1.5      cgd 		 * XXX we could deal with holes here but it would be
    354   1.1   brezak 		 * a hassle (in the write case).
    355   1.1   brezak 		 */
    356   1.5      cgd 		if (error) {
    357  1.18      cgd 			nbp->vb_buf.b_error = error;
    358  1.18      cgd 			nbp->vb_buf.b_flags |= B_ERROR;
    359   1.1   brezak 			bp->b_resid -= (resid - sz);
    360  1.18      cgd 			biodone(&nbp->vb_buf);
    361   1.1   brezak 			return;
    362   1.1   brezak 		}
    363   1.1   brezak 		/*
    364   1.1   brezak 		 * Just sort by block number
    365   1.1   brezak 		 */
    366  1.18      cgd 		nbp->vb_buf.b_cylin = nbp->vb_buf.b_blkno;
    367   1.1   brezak 		s = splbio();
    368  1.18      cgd 		disksort(&vnd->sc_tab, &nbp->vb_buf);
    369  1.17      cgd 		if (vnd->sc_tab.b_active < vnd->sc_maxactive) {
    370  1.17      cgd 			vnd->sc_tab.b_active++;
    371  1.17      cgd 			vndstart(vnd);
    372   1.1   brezak 		}
    373   1.1   brezak 		splx(s);
    374   1.1   brezak 		bn += sz;
    375   1.1   brezak 		addr += sz;
    376   1.1   brezak 	}
    377   1.1   brezak }
    378   1.1   brezak 
    379   1.1   brezak /*
    380   1.1   brezak  * Feed requests sequentially.
    381   1.1   brezak  * We do it this way to keep from flooding NFS servers if we are connected
    382   1.1   brezak  * to an NFS file.  This places the burden on the client rather than the
    383   1.1   brezak  * server.
    384   1.1   brezak  */
    385  1.16      cgd void
    386  1.17      cgd vndstart(vnd)
    387  1.17      cgd 	register struct vnd_softc *vnd;
    388   1.1   brezak {
    389   1.1   brezak 	register struct buf *bp;
    390   1.1   brezak 
    391   1.1   brezak 	/*
    392   1.1   brezak 	 * Dequeue now since lower level strategy routine might
    393   1.1   brezak 	 * queue using same links
    394   1.1   brezak 	 */
    395  1.17      cgd 	bp = vnd->sc_tab.b_actf;
    396  1.17      cgd 	vnd->sc_tab.b_actf = bp->b_actf;
    397   1.1   brezak #ifdef DEBUG
    398  1.17      cgd 	if (vnddebug & VDB_IO)
    399  1.17      cgd 		printf("vndstart(%d): bp %x vp %x blkno %x addr %x cnt %x\n",
    400  1.17      cgd 		    vnd-vnd_softc, bp, bp->b_vp, bp->b_blkno, bp->b_data,
    401   1.5      cgd 		    bp->b_bcount);
    402   1.1   brezak #endif
    403   1.1   brezak 	if ((bp->b_flags & B_READ) == 0)
    404   1.1   brezak 		bp->b_vp->v_numoutput++;
    405   1.1   brezak 	VOP_STRATEGY(bp);
    406   1.1   brezak }
    407   1.1   brezak 
    408   1.1   brezak void
    409  1.18      cgd vndiodone(vbp)
    410  1.18      cgd 	register struct vndbuf *vbp;
    411   1.1   brezak {
    412  1.18      cgd 	register struct buf *pbp = vbp->vb_obp;
    413  1.17      cgd 	register struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
    414   1.1   brezak 	int s;
    415   1.1   brezak 
    416   1.1   brezak 	s = splbio();
    417   1.1   brezak #ifdef DEBUG
    418  1.17      cgd 	if (vnddebug & VDB_IO)
    419  1.18      cgd 		printf("vndiodone(%d): vbp %x vp %x blkno %x addr %x cnt %x\n",
    420  1.18      cgd 		    vnd-vnd_softc, vbp, vbp->vb_buf.b_vp, vbp->vb_buf.b_blkno,
    421  1.18      cgd 		    vbp->vb_buf.b_data, vbp->vb_buf.b_bcount);
    422   1.1   brezak #endif
    423  1.18      cgd 	if (vbp->vb_buf.b_error) {
    424   1.1   brezak #ifdef DEBUG
    425  1.17      cgd 		if (vnddebug & VDB_IO)
    426  1.18      cgd 			printf("vndiodone: vbp %x error %d\n", vbp,
    427  1.18      cgd 			    vbp->vb_buf.b_error);
    428   1.1   brezak #endif
    429   1.1   brezak 		pbp->b_flags |= B_ERROR;
    430  1.18      cgd 		pbp->b_error = biowait(&vbp->vb_buf);
    431   1.1   brezak 	}
    432  1.18      cgd 	pbp->b_resid -= vbp->vb_buf.b_bcount;
    433  1.18      cgd 	putvndbuf(vbp);
    434   1.1   brezak 	if (pbp->b_resid == 0) {
    435   1.1   brezak #ifdef DEBUG
    436  1.17      cgd 		if (vnddebug & VDB_IO)
    437  1.17      cgd 			printf("vndiodone: pbp %x iodone\n", pbp);
    438   1.1   brezak #endif
    439   1.1   brezak 		biodone(pbp);
    440   1.1   brezak 	}
    441  1.17      cgd 	if (vnd->sc_tab.b_actf)
    442  1.17      cgd 		vndstart(vnd);
    443   1.1   brezak 	else
    444  1.17      cgd 		vnd->sc_tab.b_active--;
    445   1.1   brezak 	splx(s);
    446  1.20  mycroft }
    447  1.20  mycroft 
    448  1.22  thorpej /* ARGSUSED */
    449  1.20  mycroft int
    450  1.22  thorpej vndread(dev, uio, flags)
    451  1.20  mycroft 	dev_t dev;
    452  1.20  mycroft 	struct uio *uio;
    453  1.22  thorpej 	int flags;
    454  1.20  mycroft {
    455  1.22  thorpej 	int unit = vndunit(dev);
    456  1.22  thorpej 	struct vnd_softc *sc;
    457  1.20  mycroft 
    458  1.20  mycroft #ifdef DEBUG
    459  1.20  mycroft 	if (vnddebug & VDB_FOLLOW)
    460  1.20  mycroft 		printf("vndread(%x, %x)\n", dev, uio);
    461  1.20  mycroft #endif
    462  1.22  thorpej 
    463  1.22  thorpej 	if (unit >= numvnd)
    464  1.22  thorpej 		return (ENXIO);
    465  1.22  thorpej 	sc = &vnd_softc[unit];
    466  1.22  thorpej 
    467  1.22  thorpej 	if ((sc->sc_flags & VNF_INITED) == 0)
    468  1.22  thorpej 		return (ENXIO);
    469  1.22  thorpej 
    470  1.20  mycroft 	return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
    471  1.20  mycroft }
    472  1.20  mycroft 
    473  1.22  thorpej /* ARGSUSED */
    474  1.20  mycroft int
    475  1.22  thorpej vndwrite(dev, uio, flags)
    476  1.20  mycroft 	dev_t dev;
    477  1.20  mycroft 	struct uio *uio;
    478  1.22  thorpej 	int flags;
    479  1.20  mycroft {
    480  1.22  thorpej 	int unit = vndunit(dev);
    481  1.22  thorpej 	struct vnd_softc *sc;
    482  1.20  mycroft 
    483  1.20  mycroft #ifdef DEBUG
    484  1.20  mycroft 	if (vnddebug & VDB_FOLLOW)
    485  1.20  mycroft 		printf("vndwrite(%x, %x)\n", dev, uio);
    486  1.20  mycroft #endif
    487  1.22  thorpej 
    488  1.22  thorpej 	if (unit >= numvnd)
    489  1.22  thorpej 		return (ENXIO);
    490  1.22  thorpej 	sc = &vnd_softc[unit];
    491  1.22  thorpej 
    492  1.22  thorpej 	if ((sc->sc_flags & VNF_INITED) == 0)
    493  1.22  thorpej 		return (ENXIO);
    494  1.22  thorpej 
    495  1.20  mycroft 	return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
    496   1.1   brezak }
    497   1.1   brezak 
    498   1.1   brezak /* ARGSUSED */
    499  1.16      cgd int
    500  1.17      cgd vndioctl(dev, cmd, data, flag, p)
    501   1.1   brezak 	dev_t dev;
    502   1.1   brezak 	u_long cmd;
    503   1.1   brezak 	caddr_t data;
    504   1.1   brezak 	int flag;
    505   1.1   brezak 	struct proc *p;
    506   1.1   brezak {
    507  1.17      cgd 	int unit = vndunit(dev);
    508  1.17      cgd 	register struct vnd_softc *vnd;
    509  1.17      cgd 	struct vnd_ioctl *vio;
    510   1.1   brezak 	struct vattr vattr;
    511   1.1   brezak 	struct nameidata nd;
    512  1.22  thorpej 	int error, part, pmask, s;
    513   1.1   brezak 
    514   1.1   brezak #ifdef DEBUG
    515  1.17      cgd 	if (vnddebug & VDB_FOLLOW)
    516  1.17      cgd 		printf("vndioctl(%x, %lx, %x, %x, %x): unit %d\n",
    517   1.5      cgd 		    dev, cmd, data, flag, p, unit);
    518   1.1   brezak #endif
    519   1.1   brezak 	error = suser(p->p_ucred, &p->p_acflag);
    520   1.1   brezak 	if (error)
    521   1.1   brezak 		return (error);
    522   1.1   brezak 	if (unit >= numvnd)
    523   1.1   brezak 		return (ENXIO);
    524   1.1   brezak 
    525  1.17      cgd 	vnd = &vnd_softc[unit];
    526  1.17      cgd 	vio = (struct vnd_ioctl *)data;
    527   1.1   brezak 	switch (cmd) {
    528   1.1   brezak 
    529  1.17      cgd 	case VNDIOCSET:
    530  1.17      cgd 		if (vnd->sc_flags & VNF_INITED)
    531  1.22  thorpej 			return (EBUSY);
    532  1.22  thorpej 
    533  1.22  thorpej 		if (error = vndlock(vnd))
    534  1.22  thorpej 			return (error);
    535  1.22  thorpej 
    536   1.1   brezak 		/*
    537   1.1   brezak 		 * Always open for read and write.
    538   1.1   brezak 		 * This is probably bogus, but it lets vn_open()
    539   1.1   brezak 		 * weed out directories, sockets, etc. so we don't
    540   1.1   brezak 		 * have to worry about them.
    541   1.1   brezak 		 */
    542  1.17      cgd 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
    543  1.22  thorpej 		if (error = vn_open(&nd, FREAD|FWRITE, 0)) {
    544  1.22  thorpej 			vndunlock(vnd);
    545   1.1   brezak 			return(error);
    546  1.22  thorpej 		}
    547   1.1   brezak 		if (error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p)) {
    548   1.1   brezak 			VOP_UNLOCK(nd.ni_vp);
    549   1.1   brezak 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
    550  1.22  thorpej 			vndunlock(vnd);
    551   1.1   brezak 			return(error);
    552   1.1   brezak 		}
    553   1.1   brezak 		VOP_UNLOCK(nd.ni_vp);
    554  1.17      cgd 		vnd->sc_vp = nd.ni_vp;
    555  1.17      cgd 		vnd->sc_size = btodb(vattr.va_size);	/* note truncation */
    556  1.17      cgd 		if (error = vndsetcred(vnd, p->p_ucred)) {
    557   1.5      cgd 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
    558  1.22  thorpej 			vndunlock(vnd);
    559   1.1   brezak 			return(error);
    560   1.1   brezak 		}
    561  1.17      cgd 		vndthrottle(vnd, vnd->sc_vp);
    562  1.17      cgd 		vio->vnd_size = dbtob(vnd->sc_size);
    563  1.17      cgd 		vnd->sc_flags |= VNF_INITED;
    564  1.17      cgd #ifdef DEBUG
    565  1.17      cgd 		if (vnddebug & VDB_INIT)
    566  1.17      cgd 			printf("vndioctl: SET vp %x size %x\n",
    567  1.17      cgd 			    vnd->sc_vp, vnd->sc_size);
    568   1.1   brezak #endif
    569  1.22  thorpej 
    570  1.22  thorpej 		vndunlock(vnd);
    571  1.22  thorpej 
    572   1.1   brezak 		break;
    573   1.1   brezak 
    574  1.17      cgd 	case VNDIOCCLR:
    575  1.17      cgd 		if ((vnd->sc_flags & VNF_INITED) == 0)
    576  1.22  thorpej 			return (ENXIO);
    577  1.22  thorpej 
    578  1.22  thorpej 		if (error = vndlock(vnd))
    579  1.22  thorpej 			return (error);
    580  1.22  thorpej 
    581  1.22  thorpej 		/*
    582  1.22  thorpej 		 * Don't unconfigure if any other partitions are open
    583  1.22  thorpej 		 * or if both the character and block flavors of this
    584  1.22  thorpej 		 * partition are open.
    585  1.22  thorpej 		 */
    586  1.22  thorpej 		part = DISKPART(dev);
    587  1.22  thorpej 		pmask = (1 << part);
    588  1.22  thorpej 		if ((vnd->sc_dkdev.dk_openmask & ~pmask) ||
    589  1.22  thorpej 		    ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
    590  1.22  thorpej 		    (vnd->sc_dkdev.dk_copenmask & pmask))) {
    591  1.22  thorpej 			vndunlock(vnd);
    592  1.22  thorpej 			return (EBUSY);
    593  1.22  thorpej 		}
    594  1.22  thorpej 
    595  1.17      cgd 		vndclear(vnd);
    596   1.1   brezak #ifdef DEBUG
    597  1.17      cgd 		if (vnddebug & VDB_INIT)
    598  1.17      cgd 			printf("vndioctl: CLRed\n");
    599   1.1   brezak #endif
    600  1.22  thorpej 
    601  1.22  thorpej 		/* This must be atomic. */
    602  1.22  thorpej 		s = splhigh();
    603  1.22  thorpej 		vndunlock(vnd);
    604  1.22  thorpej 		bzero(vnd, sizeof(struct vnd_softc));
    605  1.22  thorpej 		splx(s);
    606  1.22  thorpej 
    607   1.1   brezak 		break;
    608   1.1   brezak 
    609  1.22  thorpej 	/*
    610  1.22  thorpej 	 * XXX Should support disklabels.
    611  1.22  thorpej 	 */
    612  1.22  thorpej 
    613   1.1   brezak 	default:
    614  1.12      cgd 		return(ENOTTY);
    615   1.1   brezak 	}
    616  1.22  thorpej 
    617  1.22  thorpej 	return (0);
    618   1.1   brezak }
    619   1.1   brezak 
    620   1.1   brezak /*
    621   1.1   brezak  * Duplicate the current processes' credentials.  Since we are called only
    622   1.1   brezak  * as the result of a SET ioctl and only root can do that, any future access
    623   1.1   brezak  * to this "disk" is essentially as root.  Note that credentials may change
    624   1.1   brezak  * if some other uid can write directly to the mapped file (NFS).
    625   1.1   brezak  */
    626  1.16      cgd int
    627  1.17      cgd vndsetcred(vnd, cred)
    628  1.17      cgd 	register struct vnd_softc *vnd;
    629   1.1   brezak 	struct ucred *cred;
    630   1.1   brezak {
    631   1.1   brezak 	struct uio auio;
    632   1.1   brezak 	struct iovec aiov;
    633   1.5      cgd 	char *tmpbuf;
    634   1.5      cgd 	int error;
    635   1.1   brezak 
    636  1.17      cgd 	vnd->sc_cred = crdup(cred);
    637   1.5      cgd 	tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
    638   1.5      cgd 
    639   1.1   brezak 	/* XXX: Horrible kludge to establish credentials for NFS */
    640   1.1   brezak 	aiov.iov_base = tmpbuf;
    641  1.17      cgd 	aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
    642   1.1   brezak 	auio.uio_iov = &aiov;
    643   1.1   brezak 	auio.uio_iovcnt = 1;
    644   1.1   brezak 	auio.uio_offset = 0;
    645   1.1   brezak 	auio.uio_rw = UIO_READ;
    646   1.1   brezak 	auio.uio_segflg = UIO_SYSSPACE;
    647   1.1   brezak 	auio.uio_resid = aiov.iov_len;
    648  1.21  mycroft 	VOP_LOCK(vnd->sc_vp);
    649  1.17      cgd 	error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
    650  1.21  mycroft 	VOP_UNLOCK(vnd->sc_vp);
    651   1.5      cgd 
    652   1.5      cgd 	free(tmpbuf, M_TEMP);
    653   1.5      cgd 	return (error);
    654   1.1   brezak }
    655   1.1   brezak 
    656   1.1   brezak /*
    657   1.1   brezak  * Set maxactive based on FS type
    658   1.1   brezak  */
    659  1.16      cgd void
    660  1.17      cgd vndthrottle(vnd, vp)
    661  1.17      cgd 	register struct vnd_softc *vnd;
    662   1.1   brezak 	struct vnode *vp;
    663   1.1   brezak {
    664   1.1   brezak #ifdef NFSCLIENT
    665   1.1   brezak 	extern int (**nfsv2_vnodeop_p)();
    666   1.5      cgd 
    667   1.2   brezak 	if (vp->v_op == nfsv2_vnodeop_p)
    668  1.17      cgd 		vnd->sc_maxactive = 2;
    669   1.1   brezak 	else
    670   1.1   brezak #endif
    671  1.17      cgd 		vnd->sc_maxactive = 8;
    672   1.1   brezak 
    673  1.17      cgd 	if (vnd->sc_maxactive < 1)
    674  1.17      cgd 		vnd->sc_maxactive = 1;
    675   1.1   brezak }
    676   1.1   brezak 
    677  1.16      cgd void
    678  1.17      cgd vndshutdown()
    679   1.1   brezak {
    680  1.17      cgd 	register struct vnd_softc *vnd;
    681   1.1   brezak 
    682  1.17      cgd 	for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
    683  1.17      cgd 		if (vnd->sc_flags & VNF_INITED)
    684  1.17      cgd 			vndclear(vnd);
    685   1.1   brezak }
    686   1.1   brezak 
    687  1.16      cgd void
    688  1.17      cgd vndclear(vnd)
    689  1.17      cgd 	register struct vnd_softc *vnd;
    690   1.1   brezak {
    691  1.17      cgd 	register struct vnode *vp = vnd->sc_vp;
    692   1.1   brezak 	struct proc *p = curproc;		/* XXX */
    693   1.1   brezak 
    694   1.1   brezak #ifdef DEBUG
    695  1.17      cgd 	if (vnddebug & VDB_FOLLOW)
    696  1.17      cgd 		printf("vndclear(%x): vp %x\n", vp);
    697   1.1   brezak #endif
    698  1.17      cgd 	vnd->sc_flags &= ~VNF_INITED;
    699   1.1   brezak 	if (vp == (struct vnode *)0)
    700  1.17      cgd 		panic("vndioctl: null vp");
    701  1.17      cgd 	(void) vn_close(vp, FREAD|FWRITE, vnd->sc_cred, p);
    702  1.17      cgd 	crfree(vnd->sc_cred);
    703  1.17      cgd 	vnd->sc_vp = (struct vnode *)0;
    704  1.17      cgd 	vnd->sc_cred = (struct ucred *)0;
    705  1.17      cgd 	vnd->sc_size = 0;
    706   1.1   brezak }
    707   1.1   brezak 
    708  1.16      cgd int
    709  1.17      cgd vndsize(dev)
    710   1.1   brezak 	dev_t dev;
    711   1.1   brezak {
    712  1.17      cgd 	int unit = vndunit(dev);
    713  1.17      cgd 	register struct vnd_softc *vnd = &vnd_softc[unit];
    714   1.1   brezak 
    715  1.17      cgd 	if (unit >= numvnd || (vnd->sc_flags & VNF_INITED) == 0)
    716   1.1   brezak 		return(-1);
    717  1.17      cgd 	return(vnd->sc_size);
    718   1.1   brezak }
    719   1.1   brezak 
    720  1.16      cgd int
    721  1.19      cgd vnddump(dev, blkno, va, size)
    722  1.16      cgd 	dev_t dev;
    723  1.19      cgd 	daddr_t blkno;
    724  1.19      cgd 	caddr_t va;
    725  1.19      cgd 	size_t size;
    726   1.1   brezak {
    727  1.16      cgd 
    728  1.19      cgd 	/* Not implemented. */
    729  1.19      cgd 	return ENXIO;
    730   1.1   brezak }
    731  1.22  thorpej 
    732  1.22  thorpej /*
    733  1.22  thorpej  * Wait interruptibly for an exclusive lock.
    734  1.22  thorpej  *
    735  1.22  thorpej  * XXX
    736  1.22  thorpej  * Several drivers do this; it should be abstracted and made MP-safe.
    737  1.22  thorpej  */
    738  1.22  thorpej static int
    739  1.22  thorpej vndlock(sc)
    740  1.22  thorpej 	struct vnd_softc *sc;
    741  1.22  thorpej {
    742  1.22  thorpej 	int error;
    743  1.22  thorpej 
    744  1.22  thorpej 	while ((sc->sc_flags & VNF_LOCKED) != 0) {
    745  1.22  thorpej 		sc->sc_flags |= VNF_WANTED;
    746  1.22  thorpej 		if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
    747  1.22  thorpej 			return (error);
    748  1.22  thorpej 	}
    749  1.22  thorpej 	sc->sc_flags |= VNF_LOCKED;
    750  1.22  thorpej 	return (0);
    751  1.22  thorpej }
    752  1.22  thorpej 
    753  1.22  thorpej /*
    754  1.22  thorpej  * Unlock and wake up any waiters.
    755  1.22  thorpej  */
    756  1.22  thorpej static void
    757  1.22  thorpej vndunlock(sc)
    758  1.22  thorpej 	struct vnd_softc *sc;
    759  1.22  thorpej {
    760  1.22  thorpej 
    761  1.22  thorpej 	sc->sc_flags &= ~VNF_LOCKED;
    762  1.22  thorpej 	if ((sc->sc_flags & VNF_WANTED) != 0) {
    763  1.22  thorpej 		sc->sc_flags &= ~VNF_WANTED;
    764  1.22  thorpej 		wakeup(sc);
    765  1.22  thorpej 	}
    766  1.22  thorpej }
    767