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