xref: /netbsd-src/sys/dev/vnd.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: vnd.c,v 1.69 2000/11/27 08:39:41 chs Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1988 University of Utah.
41  * Copyright (c) 1990, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * This code is derived from software contributed to Berkeley by
45  * the Systems Programming Group of the University of Utah Computer
46  * Science Department.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. All advertising materials mentioning features or use of this software
57  *    must display the following acknowledgement:
58  *	This product includes software developed by the University of
59  *	California, Berkeley and its contributors.
60  * 4. Neither the name of the University nor the names of its contributors
61  *    may be used to endorse or promote products derived from this software
62  *    without specific prior written permission.
63  *
64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74  * SUCH DAMAGE.
75  *
76  * from: Utah $Hdr: vn.c 1.13 94/04/02$
77  *
78  *	@(#)vn.c	8.9 (Berkeley) 5/14/95
79  */
80 
81 /*
82  * Vnode disk driver.
83  *
84  * Block/character interface to a vnode.  Allows one to treat a file
85  * as a disk (e.g. build a filesystem in it, mount it, etc.).
86  *
87  * NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode
88  * instead of a simple VOP_RDWR.  We do this to avoid distorting the
89  * local buffer cache.
90  *
91  * NOTE 2: There is a security issue involved with this driver.
92  * Once mounted all access to the contents of the "mapped" file via
93  * the special file is controlled by the permissions on the special
94  * file, the protection of the mapped file is ignored (effectively,
95  * by using root credentials in all transactions).
96  *
97  * NOTE 3: Doesn't interact with leases, should it?
98  */
99 
100 #include "fs_nfs.h"
101 
102 #include <sys/param.h>
103 #include <sys/systm.h>
104 #include <sys/namei.h>
105 #include <sys/proc.h>
106 #include <sys/errno.h>
107 #include <sys/buf.h>
108 #include <sys/malloc.h>
109 #include <sys/ioctl.h>
110 #include <sys/disklabel.h>
111 #include <sys/device.h>
112 #include <sys/disk.h>
113 #include <sys/stat.h>
114 #include <sys/mount.h>
115 #include <sys/vnode.h>
116 #include <sys/file.h>
117 #include <sys/uio.h>
118 #include <sys/conf.h>
119 
120 #include <miscfs/specfs/specdev.h>
121 
122 #include <dev/vndvar.h>
123 
124 #if defined(VNDDEBUG) && !defined(DEBUG)
125 #define	DEBUG
126 #endif
127 
128 #ifdef DEBUG
129 int dovndcluster = 1;
130 #define	VDB_FOLLOW	0x01
131 #define	VDB_INIT	0x02
132 #define	VDB_IO		0x04
133 #define	VDB_LABEL	0x08
134 int vnddebug = 0x00;
135 #endif
136 
137 #define	vndunit(x)	DISKUNIT(x)
138 
139 struct vndxfer {
140 	struct buf	*vx_bp;		/* Pointer to parent buffer */
141 	int		vx_error;
142 	int		vx_pending;	/* # of pending aux buffers */
143 	int		vx_flags;
144 #define VX_BUSY		1
145 };
146 
147 struct vndbuf {
148 	struct buf	vb_buf;
149 	struct vndxfer	*vb_xfer;
150 };
151 
152 #define	VND_GETXFER(vnd)	pool_get(&(vnd)->sc_vxpool, PR_NOWAIT)
153 #define	VND_PUTXFER(vnd, vx)	pool_put(&(vnd)->sc_vxpool, (vx))
154 
155 #define	VND_GETBUF(vnd)		pool_get(&(vnd)->sc_vbpool, PR_NOWAIT)
156 #define	VND_PUTBUF(vnd, vb)	pool_put(&(vnd)->sc_vbpool, (vb))
157 
158 struct vnd_softc *vnd_softc;
159 int numvnd = 0;
160 
161 #define	VNDLABELDEV(dev) \
162 	(MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
163 
164 /* called by main() at boot time */
165 void	vndattach __P((int));
166 
167 void	vndclear __P((struct vnd_softc *));
168 void	vndstart __P((struct vnd_softc *));
169 int	vndsetcred __P((struct vnd_softc *, struct ucred *));
170 void	vndthrottle __P((struct vnd_softc *, struct vnode *));
171 void	vndiodone __P((struct buf *));
172 void	vndshutdown __P((void));
173 
174 void	vndgetdefaultlabel __P((struct vnd_softc *, struct disklabel *));
175 void	vndgetdisklabel __P((dev_t));
176 
177 static	int vndlock __P((struct vnd_softc *));
178 static	void vndunlock __P((struct vnd_softc *));
179 
180 void
181 vndattach(num)
182 	int num;
183 {
184 	int i;
185 	char *mem;
186 
187 	if (num <= 0)
188 		return;
189 	i = num * sizeof(struct vnd_softc);
190 	mem = malloc(i, M_DEVBUF, M_NOWAIT);
191 	if (mem == NULL) {
192 		printf("WARNING: no memory for vnode disks\n");
193 		return;
194 	}
195 	bzero(mem, i);
196 	vnd_softc = (struct vnd_softc *)mem;
197 	numvnd = num;
198 
199 	for (i = 0; i < numvnd; i++)
200 		BUFQ_INIT(&vnd_softc[i].sc_tab);
201 }
202 
203 int
204 vndopen(dev, flags, mode, p)
205 	dev_t dev;
206 	int flags, mode;
207 	struct proc *p;
208 {
209 	int unit = vndunit(dev);
210 	struct vnd_softc *sc;
211 	int error = 0, part, pmask;
212 	struct disklabel *lp;
213 
214 #ifdef DEBUG
215 	if (vnddebug & VDB_FOLLOW)
216 		printf("vndopen(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, p);
217 #endif
218 	if (unit >= numvnd)
219 		return (ENXIO);
220 	sc = &vnd_softc[unit];
221 
222 	if ((error = vndlock(sc)) != 0)
223 		return (error);
224 
225 	lp = sc->sc_dkdev.dk_label;
226 
227 	part = DISKPART(dev);
228 	pmask = (1 << part);
229 
230 	/*
231 	 * If we're initialized, check to see if there are any other
232 	 * open partitions.  If not, then it's safe to update the
233 	 * in-core disklabel.
234 	 */
235 	if ((sc->sc_flags & VNF_INITED) && (sc->sc_dkdev.dk_openmask == 0))
236 		vndgetdisklabel(dev);
237 
238 	/* Check that the partitions exists. */
239 	if (part != RAW_PART) {
240 		if (((sc->sc_flags & VNF_INITED) == 0) ||
241 		    ((part >= lp->d_npartitions) ||
242 		     (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
243 			error = ENXIO;
244 			goto done;
245 		}
246 	}
247 
248 	/* Prevent our unit from being unconfigured while open. */
249 	switch (mode) {
250 	case S_IFCHR:
251 		sc->sc_dkdev.dk_copenmask |= pmask;
252 		break;
253 
254 	case S_IFBLK:
255 		sc->sc_dkdev.dk_bopenmask |= pmask;
256 		break;
257 	}
258 	sc->sc_dkdev.dk_openmask =
259 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
260 
261  done:
262 	vndunlock(sc);
263 	return (error);
264 }
265 
266 int
267 vndclose(dev, flags, mode, p)
268 	dev_t dev;
269 	int flags, mode;
270 	struct proc *p;
271 {
272 	int unit = vndunit(dev);
273 	struct vnd_softc *sc;
274 	int error = 0, part;
275 
276 #ifdef DEBUG
277 	if (vnddebug & VDB_FOLLOW)
278 		printf("vndclose(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, p);
279 #endif
280 
281 	if (unit >= numvnd)
282 		return (ENXIO);
283 	sc = &vnd_softc[unit];
284 
285 	if ((error = vndlock(sc)) != 0)
286 		return (error);
287 
288 	part = DISKPART(dev);
289 
290 	/* ...that much closer to allowing unconfiguration... */
291 	switch (mode) {
292 	case S_IFCHR:
293 		sc->sc_dkdev.dk_copenmask &= ~(1 << part);
294 		break;
295 
296 	case S_IFBLK:
297 		sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
298 		break;
299 	}
300 	sc->sc_dkdev.dk_openmask =
301 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
302 
303 	vndunlock(sc);
304 	return (0);
305 }
306 
307 /*
308  * Break the request into bsize pieces and submit using VOP_BMAP/VOP_STRATEGY.
309  */
310 void
311 vndstrategy(bp)
312 	struct buf *bp;
313 {
314 	int unit = vndunit(bp->b_dev);
315 	struct vnd_softc *vnd = &vnd_softc[unit];
316 	struct vndxfer *vnx;
317 	int s, bsize, resid;
318 	off_t bn;
319 	caddr_t addr;
320 	int sz, flags, error, wlabel;
321 	struct disklabel *lp;
322 	struct partition *pp;
323 
324 #ifdef DEBUG
325 	if (vnddebug & VDB_FOLLOW)
326 		printf("vndstrategy(%p): unit %d\n", bp, unit);
327 #endif
328 	if ((vnd->sc_flags & VNF_INITED) == 0) {
329 		bp->b_error = ENXIO;
330 		bp->b_flags |= B_ERROR;
331 		goto done;
332 	}
333 
334 	/* If it's a nil transfer, wake up the top half now. */
335 	if (bp->b_bcount == 0)
336 		goto done;
337 
338 	lp = vnd->sc_dkdev.dk_label;
339 
340 	/*
341 	 * The transfer must be a whole number of blocks.
342 	 */
343 	if ((bp->b_bcount % lp->d_secsize) != 0) {
344 		bp->b_error = EINVAL;
345 		bp->b_flags |= B_ERROR;
346 		goto done;
347 	}
348 
349 	/*
350 	 * Do bounds checking and adjust transfer.  If there's an error,
351 	 * the bounds check will flag that for us.
352 	 */
353 	wlabel = vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING);
354 	if (DISKPART(bp->b_dev) != RAW_PART)
355 		if (bounds_check_with_label(bp, lp, wlabel) <= 0)
356 			goto done;
357 
358 	bp->b_resid = bp->b_bcount;
359 
360 	/*
361 	 * Put the block number in terms of the logical blocksize
362 	 * of the "device".
363 	 */
364 	bn = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
365 
366 	/*
367 	 * Translate the partition-relative block number to an absolute.
368 	 */
369 	if (DISKPART(bp->b_dev) != RAW_PART) {
370 		pp = &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
371 		bn += pp->p_offset;
372 	}
373 
374 	/* ...and convert to a byte offset within the file. */
375 	bn *= lp->d_secsize;
376 
377  	bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
378 	addr = bp->b_data;
379 	flags = (bp->b_flags & (B_READ|B_ASYNC)) | B_CALL;
380 
381 	/* Allocate a header for this transfer and link it to the buffer */
382 	s = splbio();
383 	vnx = VND_GETXFER(vnd);
384 	splx(s);
385 	vnx->vx_flags = VX_BUSY;
386 	vnx->vx_error = 0;
387 	vnx->vx_pending = 0;
388 	vnx->vx_bp = bp;
389 
390 	for (resid = bp->b_resid; resid; resid -= sz) {
391 		struct vndbuf *nbp;
392 		struct vnode *vp;
393 		daddr_t nbn;
394 		int off, nra;
395 
396 		nra = 0;
397 		vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
398 		error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
399 		VOP_UNLOCK(vnd->sc_vp, 0);
400 
401 		if (error == 0 && (long)nbn == -1)
402 			error = EIO;
403 
404 		/*
405 		 * If there was an error or a hole in the file...punt.
406 		 * Note that we may have to wait for any operations
407 		 * that we have already fired off before releasing
408 		 * the buffer.
409 		 *
410 		 * XXX we could deal with holes here but it would be
411 		 * a hassle (in the write case).
412 		 */
413 		if (error) {
414 			s = splbio();
415 			vnx->vx_error = error;
416 			goto out;
417 		}
418 
419 #ifdef DEBUG
420 		if (!dovndcluster)
421 			nra = 0;
422 #endif
423 
424 		if ((off = bn % bsize) != 0)
425 			sz = bsize - off;
426 		else
427 			sz = (1 + nra) * bsize;
428 		if (resid < sz)
429 			sz = resid;
430 #ifdef DEBUG
431 		if (vnddebug & VDB_IO)
432 			printf("vndstrategy: vp %p/%p bn 0x%qx/0x%x sz 0x%x\n",
433 			    vnd->sc_vp, vp, (long long)bn, nbn, sz);
434 #endif
435 
436 		s = splbio();
437 		nbp = VND_GETBUF(vnd);
438 		splx(s);
439 		nbp->vb_buf.b_flags = flags;
440 		nbp->vb_buf.b_bcount = sz;
441 		nbp->vb_buf.b_bufsize = bp->b_bufsize;
442 		nbp->vb_buf.b_error = 0;
443 		nbp->vb_buf.b_data = addr;
444 		nbp->vb_buf.b_blkno = nbp->vb_buf.b_rawblkno = nbn + btodb(off);
445 		nbp->vb_buf.b_proc = bp->b_proc;
446 		nbp->vb_buf.b_iodone = vndiodone;
447 		nbp->vb_buf.b_vp = NULLVP;
448 		LIST_INIT(&nbp->vb_buf.b_dep);
449 
450 		nbp->vb_xfer = vnx;
451 
452 		/*
453 		 * Just sort by block number
454 		 */
455 		s = splbio();
456 		if (vnx->vx_error != 0) {
457 			VND_PUTBUF(vnd, nbp);
458 			goto out;
459 		}
460 		vnx->vx_pending++;
461 		bgetvp(vp, &nbp->vb_buf);
462 		disksort_blkno(&vnd->sc_tab, &nbp->vb_buf);
463 		vndstart(vnd);
464 		splx(s);
465 		bn += sz;
466 		addr += sz;
467 	}
468 
469 	s = splbio();
470 
471 out: /* Arrive here at splbio */
472 	vnx->vx_flags &= ~VX_BUSY;
473 	if (vnx->vx_pending == 0) {
474 		if (vnx->vx_error != 0) {
475 			bp->b_error = vnx->vx_error;
476 			bp->b_flags |= B_ERROR;
477 		}
478 		VND_PUTXFER(vnd, vnx);
479 		biodone(bp);
480 	}
481 	splx(s);
482 	return;
483 
484  done:
485 	biodone(bp);
486 }
487 
488 /*
489  * Feed requests sequentially.
490  * We do it this way to keep from flooding NFS servers if we are connected
491  * to an NFS file.  This places the burden on the client rather than the
492  * server.
493  */
494 void
495 vndstart(vnd)
496 	struct vnd_softc *vnd;
497 {
498 	struct buf	*bp;
499 
500 	/*
501 	 * Dequeue now since lower level strategy routine might
502 	 * queue using same links
503 	 */
504 
505 	if ((vnd->sc_flags & VNF_BUSY) != 0)
506 		return;
507 
508 	vnd->sc_flags |= VNF_BUSY;
509 
510 	while (vnd->sc_active < vnd->sc_maxactive) {
511 		bp = BUFQ_FIRST(&vnd->sc_tab);
512 		if (bp == NULL)
513 			break;
514 		BUFQ_REMOVE(&vnd->sc_tab, bp);
515 		vnd->sc_active++;
516 #ifdef DEBUG
517 		if (vnddebug & VDB_IO)
518 			printf("vndstart(%ld): bp %p vp %p blkno 0x%x"
519 				" flags %lx addr %p cnt 0x%lx\n",
520 			    (long) (vnd-vnd_softc), bp, bp->b_vp, bp->b_blkno,
521 			    bp->b_flags, bp->b_data, bp->b_bcount);
522 #endif
523 
524 		/* Instrumentation. */
525 		disk_busy(&vnd->sc_dkdev);
526 
527 		if ((bp->b_flags & B_READ) == 0)
528 			bp->b_vp->v_numoutput++;
529 		VOP_STRATEGY(bp);
530 	}
531 	vnd->sc_flags &= ~VNF_BUSY;
532 }
533 
534 void
535 vndiodone(bp)
536 	struct buf *bp;
537 {
538 	struct vndbuf *vbp = (struct vndbuf *) bp;
539 	struct vndxfer *vnx = (struct vndxfer *)vbp->vb_xfer;
540 	struct buf *pbp = vnx->vx_bp;
541 	struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
542 	int s, resid;
543 
544 	s = splbio();
545 #ifdef DEBUG
546 	if (vnddebug & VDB_IO)
547 		printf("vndiodone(%ld): vbp %p vp %p blkno 0x%x addr %p cnt 0x%lx\n",
548 		    (long) (vnd-vnd_softc), vbp, vbp->vb_buf.b_vp,
549 		    vbp->vb_buf.b_blkno, vbp->vb_buf.b_data,
550 		    vbp->vb_buf.b_bcount);
551 #endif
552 
553 	resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
554 	pbp->b_resid -= resid;
555 	disk_unbusy(&vnd->sc_dkdev, resid);
556 	vnx->vx_pending--;
557 
558 	if (vbp->vb_buf.b_error) {
559 #ifdef DEBUG
560 		if (vnddebug & VDB_IO)
561 			printf("vndiodone: vbp %p error %d\n", vbp,
562 			    vbp->vb_buf.b_error);
563 #endif
564 		vnx->vx_error = vbp->vb_buf.b_error;
565 	}
566 
567 	if (vbp->vb_buf.b_vp != NULLVP)
568 		brelvp(&vbp->vb_buf);
569 
570 	VND_PUTBUF(vnd, vbp);
571 
572 	/*
573 	 * Wrap up this transaction if it has run to completion or, in
574 	 * case of an error, when all auxiliary buffers have returned.
575 	 */
576 	if (vnx->vx_error != 0) {
577 		pbp->b_flags |= B_ERROR;
578 		pbp->b_error = vnx->vx_error;
579 		if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
580 
581 #ifdef DEBUG
582 			if (vnddebug & VDB_IO)
583 				printf("vndiodone: pbp %p iodone: error %d\n",
584 					pbp, vnx->vx_error);
585 #endif
586 			VND_PUTXFER(vnd, vnx);
587 			biodone(pbp);
588 		}
589 	} else if (pbp->b_resid == 0) {
590 
591 #ifdef DIAGNOSTIC
592 		if (vnx->vx_pending != 0)
593 			panic("vndiodone: vnx pending: %d", vnx->vx_pending);
594 #endif
595 
596 		if ((vnx->vx_flags & VX_BUSY) == 0) {
597 #ifdef DEBUG
598 			if (vnddebug & VDB_IO)
599 				printf("vndiodone: pbp %p iodone\n", pbp);
600 #endif
601 			VND_PUTXFER(vnd, vnx);
602 			biodone(pbp);
603 		}
604 	}
605 
606 	vnd->sc_active--;
607 	vndstart(vnd);
608 	splx(s);
609 }
610 
611 /* ARGSUSED */
612 int
613 vndread(dev, uio, flags)
614 	dev_t dev;
615 	struct uio *uio;
616 	int flags;
617 {
618 	int unit = vndunit(dev);
619 	struct vnd_softc *sc;
620 
621 #ifdef DEBUG
622 	if (vnddebug & VDB_FOLLOW)
623 		printf("vndread(0x%x, %p)\n", dev, uio);
624 #endif
625 
626 	if (unit >= numvnd)
627 		return (ENXIO);
628 	sc = &vnd_softc[unit];
629 
630 	if ((sc->sc_flags & VNF_INITED) == 0)
631 		return (ENXIO);
632 
633 	return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
634 }
635 
636 /* ARGSUSED */
637 int
638 vndwrite(dev, uio, flags)
639 	dev_t dev;
640 	struct uio *uio;
641 	int flags;
642 {
643 	int unit = vndunit(dev);
644 	struct vnd_softc *sc;
645 
646 #ifdef DEBUG
647 	if (vnddebug & VDB_FOLLOW)
648 		printf("vndwrite(0x%x, %p)\n", dev, uio);
649 #endif
650 
651 	if (unit >= numvnd)
652 		return (ENXIO);
653 	sc = &vnd_softc[unit];
654 
655 	if ((sc->sc_flags & VNF_INITED) == 0)
656 		return (ENXIO);
657 
658 	return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
659 }
660 
661 /* ARGSUSED */
662 int
663 vndioctl(dev, cmd, data, flag, p)
664 	dev_t dev;
665 	u_long cmd;
666 	caddr_t data;
667 	int flag;
668 	struct proc *p;
669 {
670 	int unit = vndunit(dev);
671 	struct vnd_softc *vnd;
672 	struct vnd_ioctl *vio;
673 	struct vattr vattr;
674 	struct nameidata nd;
675 	int error, part, pmask;
676 	size_t geomsize;
677 
678 #ifdef DEBUG
679 	if (vnddebug & VDB_FOLLOW)
680 		printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
681 		    dev, cmd, data, flag, p, unit);
682 #endif
683 	error = suser(p->p_ucred, &p->p_acflag);
684 	if (error)
685 		return (error);
686 	if (unit >= numvnd)
687 		return (ENXIO);
688 
689 	vnd = &vnd_softc[unit];
690 	vio = (struct vnd_ioctl *)data;
691 
692 	/* Must be open for writes for these commands... */
693 	switch (cmd) {
694 	case VNDIOCSET:
695 	case VNDIOCCLR:
696 	case DIOCSDINFO:
697 	case DIOCWDINFO:
698 	case DIOCWLABEL:
699 		if ((flag & FWRITE) == 0)
700 			return (EBADF);
701 	}
702 
703 	/* Must be initialized for these... */
704 	switch (cmd) {
705 	case VNDIOCCLR:
706 	case DIOCGDINFO:
707 	case DIOCSDINFO:
708 	case DIOCWDINFO:
709 	case DIOCGPART:
710 	case DIOCWLABEL:
711 	case DIOCGDEFLABEL:
712 		if ((vnd->sc_flags & VNF_INITED) == 0)
713 			return (ENXIO);
714 	}
715 
716 	switch (cmd) {
717 	case VNDIOCSET:
718 		if (vnd->sc_flags & VNF_INITED)
719 			return (EBUSY);
720 
721 		if ((error = vndlock(vnd)) != 0)
722 			return (error);
723 
724 		/*
725 		 * Always open for read and write.
726 		 * This is probably bogus, but it lets vn_open()
727 		 * weed out directories, sockets, etc. so we don't
728 		 * have to worry about them.
729 		 */
730 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
731 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
732 			vndunlock(vnd);
733 			return(error);
734 		}
735 		error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p);
736 		if (error) {
737 			VOP_UNLOCK(nd.ni_vp, 0);
738 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
739 			vndunlock(vnd);
740 			return(error);
741 		}
742 		VOP_UNLOCK(nd.ni_vp, 0);
743 		vnd->sc_vp = nd.ni_vp;
744 		vnd->sc_size = btodb(vattr.va_size);	/* note truncation */
745 
746 		/*
747 		 * Use pseudo-geometry specified.  If none was provided,
748 		 * use "standard" Adaptec fictitious geometry.
749 		 */
750 		if (vio->vnd_flags & VNDIOF_HASGEOM) {
751 
752 			bcopy(&vio->vnd_geom, &vnd->sc_geom,
753 			    sizeof(vio->vnd_geom));
754 
755 			/*
756 			 * Sanity-check the sector size.
757 			 * XXX Don't allow secsize < DEV_BSIZE.  Should
758 			 * XXX we?
759 			 */
760 			if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
761 			    (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0) {
762 				(void) vn_close(nd.ni_vp, FREAD|FWRITE,
763 				    p->p_ucred, p);
764 				vndunlock(vnd);
765 				return (EINVAL);
766 			}
767 
768 			/*
769 			 * Compute the size (in DEV_BSIZE blocks) specified
770 			 * by the geometry.
771 			 */
772 			geomsize = (vnd->sc_geom.vng_nsectors *
773 			    vnd->sc_geom.vng_ntracks *
774 			    vnd->sc_geom.vng_ncylinders) *
775 			    (vnd->sc_geom.vng_secsize / DEV_BSIZE);
776 
777 			/*
778 			 * Sanity-check the size against the specified
779 			 * geometry.
780 			 */
781 			if (vnd->sc_size < geomsize) {
782 				(void) vn_close(nd.ni_vp, FREAD|FWRITE,
783 				    p->p_ucred, p);
784 				vndunlock(vnd);
785 				return (EINVAL);
786 			}
787 		} else {
788 			/*
789 			 * Size must be at least 2048 DEV_BSIZE blocks
790 			 * (1M) in order to use this geometry.
791 			 */
792 			if (vnd->sc_size < (32 * 64)) {
793 				vndunlock(vnd);
794 				return (EINVAL);
795 			}
796 
797 			vnd->sc_geom.vng_secsize = DEV_BSIZE;
798 			vnd->sc_geom.vng_nsectors = 32;
799 			vnd->sc_geom.vng_ntracks = 64;
800 			vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
801 
802 			/*
803 			 * Compute the actual size allowed by this geometry.
804 			 */
805 			geomsize = 32 * 64 * vnd->sc_geom.vng_ncylinders;
806 		}
807 
808 		/*
809 		 * Truncate the size to that specified by
810 		 * the geometry.
811 		 * XXX Should we even bother with this?
812 		 */
813 		vnd->sc_size = geomsize;
814 
815 		if ((error = vndsetcred(vnd, p->p_ucred)) != 0) {
816 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
817 			vndunlock(vnd);
818 			return(error);
819 		}
820 		vndthrottle(vnd, vnd->sc_vp);
821 		vio->vnd_size = dbtob(vnd->sc_size);
822 		vnd->sc_flags |= VNF_INITED;
823 #ifdef DEBUG
824 		if (vnddebug & VDB_INIT)
825 			printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
826 			    vnd->sc_vp, (unsigned long) vnd->sc_size,
827 			    vnd->sc_geom.vng_secsize,
828 			    vnd->sc_geom.vng_nsectors,
829 			    vnd->sc_geom.vng_ntracks,
830 			    vnd->sc_geom.vng_ncylinders);
831 #endif
832 
833 		/* Attach the disk. */
834 		bzero(vnd->sc_xname, sizeof(vnd->sc_xname));	/* XXX */
835 		sprintf(vnd->sc_xname, "vnd%d", unit);		/* XXX */
836 		vnd->sc_dkdev.dk_name = vnd->sc_xname;
837 		disk_attach(&vnd->sc_dkdev);
838 
839 		/* Initialize the xfer and buffer pools. */
840 		pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
841 		    0, 0, "vndxpl", 0, NULL, NULL, M_DEVBUF);
842 		pool_init(&vnd->sc_vbpool, sizeof(struct vndbuf), 0,
843 		    0, 0, "vndbpl", 0, NULL, NULL, M_DEVBUF);
844 
845 		/* Try and read the disklabel. */
846 		vndgetdisklabel(dev);
847 
848 		vndunlock(vnd);
849 
850 		break;
851 
852 	case VNDIOCCLR:
853 		if ((error = vndlock(vnd)) != 0)
854 			return (error);
855 
856 		/*
857 		 * Don't unconfigure if any other partitions are open
858 		 * or if both the character and block flavors of this
859 		 * partition are open.
860 		 */
861 		part = DISKPART(dev);
862 		pmask = (1 << part);
863 		if ((vnd->sc_dkdev.dk_openmask & ~pmask) ||
864 		    ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
865 		    (vnd->sc_dkdev.dk_copenmask & pmask))) {
866 			vndunlock(vnd);
867 			return (EBUSY);
868 		}
869 
870 		vndclear(vnd);
871 #ifdef DEBUG
872 		if (vnddebug & VDB_INIT)
873 			printf("vndioctl: CLRed\n");
874 #endif
875 
876 		/* Destroy the xfer and buffer pools. */
877 		pool_destroy(&vnd->sc_vxpool);
878 		pool_destroy(&vnd->sc_vbpool);
879 
880 		/* Detatch the disk. */
881 		disk_detach(&vnd->sc_dkdev);
882 
883 		vndunlock(vnd);
884 
885 		break;
886 
887 	case DIOCGDINFO:
888 		*(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
889 		break;
890 
891 	case DIOCGPART:
892 		((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
893 		((struct partinfo *)data)->part =
894 		    &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
895 		break;
896 
897 	case DIOCWDINFO:
898 	case DIOCSDINFO:
899 		if ((error = vndlock(vnd)) != 0)
900 			return (error);
901 
902 		vnd->sc_flags |= VNF_LABELLING;
903 
904 		error = setdisklabel(vnd->sc_dkdev.dk_label,
905 		    (struct disklabel *)data, 0, vnd->sc_dkdev.dk_cpulabel);
906 		if (error == 0) {
907 			if (cmd == DIOCWDINFO)
908 				error = writedisklabel(VNDLABELDEV(dev),
909 				    vndstrategy, vnd->sc_dkdev.dk_label,
910 				    vnd->sc_dkdev.dk_cpulabel);
911 		}
912 
913 		vnd->sc_flags &= ~VNF_LABELLING;
914 
915 		vndunlock(vnd);
916 
917 		if (error)
918 			return (error);
919 		break;
920 
921 	case DIOCWLABEL:
922 		if (*(int *)data != 0)
923 			vnd->sc_flags |= VNF_WLABEL;
924 		else
925 			vnd->sc_flags &= ~VNF_WLABEL;
926 		break;
927 
928 	case DIOCGDEFLABEL:
929 		vndgetdefaultlabel(vnd, (struct disklabel *)data);
930 		break;
931 
932 	default:
933 		return (ENOTTY);
934 	}
935 
936 	return (0);
937 }
938 
939 /*
940  * Duplicate the current processes' credentials.  Since we are called only
941  * as the result of a SET ioctl and only root can do that, any future access
942  * to this "disk" is essentially as root.  Note that credentials may change
943  * if some other uid can write directly to the mapped file (NFS).
944  */
945 int
946 vndsetcred(vnd, cred)
947 	struct vnd_softc *vnd;
948 	struct ucred *cred;
949 {
950 	struct uio auio;
951 	struct iovec aiov;
952 	char *tmpbuf;
953 	int error;
954 
955 	vnd->sc_cred = crdup(cred);
956 	tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
957 
958 	/* XXX: Horrible kludge to establish credentials for NFS */
959 	aiov.iov_base = tmpbuf;
960 	aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
961 	auio.uio_iov = &aiov;
962 	auio.uio_iovcnt = 1;
963 	auio.uio_offset = 0;
964 	auio.uio_rw = UIO_READ;
965 	auio.uio_segflg = UIO_SYSSPACE;
966 	auio.uio_resid = aiov.iov_len;
967 	vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
968 	error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
969 	if (error == 0) {
970 		/*
971 		 * Because vnd does all IO directly through the vnode
972 		 * we need to flush (at least) the buffer from the above
973 		 * VOP_READ from the buffer cache to prevent cache
974 		 * incoherencies.  Also, be careful to write dirty
975 		 * buffers back to stable storage.
976 		 */
977 		error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
978 			    curproc, 0, 0);
979 	}
980 	VOP_UNLOCK(vnd->sc_vp, 0);
981 
982 	free(tmpbuf, M_TEMP);
983 	return (error);
984 }
985 
986 /*
987  * Set maxactive based on FS type
988  */
989 void
990 vndthrottle(vnd, vp)
991 	struct vnd_softc *vnd;
992 	struct vnode *vp;
993 {
994 #ifdef NFS
995 	extern int (**nfsv2_vnodeop_p) __P((void *));
996 
997 	if (vp->v_op == nfsv2_vnodeop_p)
998 		vnd->sc_maxactive = 2;
999 	else
1000 #endif
1001 		vnd->sc_maxactive = 8;
1002 
1003 	if (vnd->sc_maxactive < 1)
1004 		vnd->sc_maxactive = 1;
1005 }
1006 
1007 void
1008 vndshutdown()
1009 {
1010 	struct vnd_softc *vnd;
1011 
1012 	for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1013 		if (vnd->sc_flags & VNF_INITED)
1014 			vndclear(vnd);
1015 }
1016 
1017 void
1018 vndclear(vnd)
1019 	struct vnd_softc *vnd;
1020 {
1021 	struct vnode *vp = vnd->sc_vp;
1022 	struct proc *p = curproc;		/* XXX */
1023 
1024 #ifdef DEBUG
1025 	if (vnddebug & VDB_FOLLOW)
1026 		printf("vndclear(%p): vp %p\n", vnd, vp);
1027 #endif
1028 	vnd->sc_flags &= ~VNF_INITED;
1029 	if (vp == (struct vnode *)0)
1030 		panic("vndioctl: null vp");
1031 	(void) vn_close(vp, FREAD|FWRITE, vnd->sc_cred, p);
1032 	crfree(vnd->sc_cred);
1033 	vnd->sc_vp = (struct vnode *)0;
1034 	vnd->sc_cred = (struct ucred *)0;
1035 	vnd->sc_size = 0;
1036 }
1037 
1038 int
1039 vndsize(dev)
1040 	dev_t dev;
1041 {
1042 	struct vnd_softc *sc;
1043 	struct disklabel *lp;
1044 	int part, unit, omask;
1045 	int size;
1046 
1047 	unit = vndunit(dev);
1048 	if (unit >= numvnd)
1049 		return (-1);
1050 	sc = &vnd_softc[unit];
1051 
1052 	if ((sc->sc_flags & VNF_INITED) == 0)
1053 		return (-1);
1054 
1055 	part = DISKPART(dev);
1056 	omask = sc->sc_dkdev.dk_openmask & (1 << part);
1057 	lp = sc->sc_dkdev.dk_label;
1058 
1059 	if (omask == 0 && vndopen(dev, 0, S_IFBLK, curproc))
1060 		return (-1);
1061 
1062 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
1063 		size = -1;
1064 	else
1065 		size = lp->d_partitions[part].p_size *
1066 		    (lp->d_secsize / DEV_BSIZE);
1067 
1068 	if (omask == 0 && vndclose(dev, 0, S_IFBLK, curproc))
1069 		return (-1);
1070 
1071 	return (size);
1072 }
1073 
1074 int
1075 vnddump(dev, blkno, va, size)
1076 	dev_t dev;
1077 	daddr_t blkno;
1078 	caddr_t va;
1079 	size_t size;
1080 {
1081 
1082 	/* Not implemented. */
1083 	return ENXIO;
1084 }
1085 
1086 void
1087 vndgetdefaultlabel(sc, lp)
1088 	struct vnd_softc *sc;
1089 	struct disklabel *lp;
1090 {
1091 	struct vndgeom *vng = &sc->sc_geom;
1092 	struct partition *pp;
1093 
1094 	bzero(lp, sizeof(*lp));
1095 
1096 	lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
1097 	lp->d_secsize = vng->vng_secsize;
1098 	lp->d_nsectors = vng->vng_nsectors;
1099 	lp->d_ntracks = vng->vng_ntracks;
1100 	lp->d_ncylinders = vng->vng_ncylinders;
1101 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1102 
1103 	strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1104 	lp->d_type = DTYPE_VND;
1105 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1106 	lp->d_rpm = 3600;
1107 	lp->d_interleave = 1;
1108 	lp->d_flags = 0;
1109 
1110 	pp = &lp->d_partitions[RAW_PART];
1111 	pp->p_offset = 0;
1112 	pp->p_size = lp->d_secperunit;
1113 	pp->p_fstype = FS_UNUSED;
1114 	lp->d_npartitions = RAW_PART + 1;
1115 
1116 	lp->d_magic = DISKMAGIC;
1117 	lp->d_magic2 = DISKMAGIC;
1118 	lp->d_checksum = dkcksum(lp);
1119 }
1120 
1121 /*
1122  * Read the disklabel from a vnd.  If one is not present, create a fake one.
1123  */
1124 void
1125 vndgetdisklabel(dev)
1126 	dev_t dev;
1127 {
1128 	struct vnd_softc *sc = &vnd_softc[vndunit(dev)];
1129 	char *errstring;
1130 	struct disklabel *lp = sc->sc_dkdev.dk_label;
1131 	struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1132 	int i;
1133 
1134 	bzero(clp, sizeof(*clp));
1135 
1136 	vndgetdefaultlabel(sc, lp);
1137 
1138 	/*
1139 	 * Call the generic disklabel extraction routine.
1140 	 */
1141 	errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1142 	if (errstring) {
1143 		/*
1144 		 * Lack of disklabel is common, but we print the warning
1145 		 * anyway, since it might contain other useful information.
1146 		 */
1147 		printf("%s: %s\n", sc->sc_xname, errstring);
1148 
1149 		/*
1150 		 * For historical reasons, if there's no disklabel
1151 		 * present, all partitions must be FS_BSDFFS and
1152 		 * occupy the entire disk.
1153 		 */
1154 		for (i = 0; i < MAXPARTITIONS; i++) {
1155 			/*
1156 			 * Don't wipe out port specific hack (such as
1157 			 * dos partition hack of i386 port).
1158 			 */
1159 			if (lp->d_partitions[i].p_fstype != FS_UNUSED)
1160 				continue;
1161 
1162 			lp->d_partitions[i].p_size = lp->d_secperunit;
1163 			lp->d_partitions[i].p_offset = 0;
1164 			lp->d_partitions[i].p_fstype = FS_BSDFFS;
1165 		}
1166 
1167 		strncpy(lp->d_packname, "default label",
1168 		    sizeof(lp->d_packname));
1169 
1170 		lp->d_checksum = dkcksum(lp);
1171 	}
1172 }
1173 
1174 /*
1175  * Wait interruptibly for an exclusive lock.
1176  *
1177  * XXX
1178  * Several drivers do this; it should be abstracted and made MP-safe.
1179  */
1180 static int
1181 vndlock(sc)
1182 	struct vnd_softc *sc;
1183 {
1184 	int error;
1185 
1186 	while ((sc->sc_flags & VNF_LOCKED) != 0) {
1187 		sc->sc_flags |= VNF_WANTED;
1188 		if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1189 			return (error);
1190 	}
1191 	sc->sc_flags |= VNF_LOCKED;
1192 	return (0);
1193 }
1194 
1195 /*
1196  * Unlock and wake up any waiters.
1197  */
1198 static void
1199 vndunlock(sc)
1200 	struct vnd_softc *sc;
1201 {
1202 
1203 	sc->sc_flags &= ~VNF_LOCKED;
1204 	if ((sc->sc_flags & VNF_WANTED) != 0) {
1205 		sc->sc_flags &= ~VNF_WANTED;
1206 		wakeup(sc);
1207 	}
1208 }
1209