xref: /netbsd-src/sys/dev/vnd.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: vnd.c,v 1.65 2000/03/30 12:45:27 augustss 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, bn, bsize, resid;
318 	caddr_t addr;
319 	int sz, flags, error, wlabel;
320 	struct disklabel *lp;
321 	struct partition *pp;
322 
323 #ifdef DEBUG
324 	if (vnddebug & VDB_FOLLOW)
325 		printf("vndstrategy(%p): unit %d\n", bp, unit);
326 #endif
327 	if ((vnd->sc_flags & VNF_INITED) == 0) {
328 		bp->b_error = ENXIO;
329 		bp->b_flags |= B_ERROR;
330 		goto done;
331 	}
332 
333 	/* If it's a nil transfer, wake up the top half now. */
334 	if (bp->b_bcount == 0)
335 		goto done;
336 
337 	lp = vnd->sc_dkdev.dk_label;
338 
339 	/*
340 	 * The transfer must be a whole number of blocks.
341 	 */
342 	if ((bp->b_bcount % lp->d_secsize) != 0) {
343 		bp->b_error = EINVAL;
344 		bp->b_flags |= B_ERROR;
345 		goto done;
346 	}
347 
348 	/*
349 	 * Do bounds checking and adjust transfer.  If there's an error,
350 	 * the bounds check will flag that for us.
351 	 */
352 	wlabel = vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING);
353 	if (DISKPART(bp->b_dev) != RAW_PART)
354 		if (bounds_check_with_label(bp, lp, wlabel) <= 0)
355 			goto done;
356 
357 	bp->b_resid = bp->b_bcount;
358 
359 	/*
360 	 * Put the block number in terms of the logical blocksize
361 	 * of the "device".
362 	 */
363 	bn = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
364 
365 	/*
366 	 * Translate the partition-relative block number to an absolute.
367 	 */
368 	if (DISKPART(bp->b_dev) != RAW_PART) {
369 		pp = &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
370 		bn += pp->p_offset;
371 	}
372 
373 	/* ...and convert to a byte offset within the file. */
374 	bn *= lp->d_secsize;
375 
376  	bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
377 	addr = bp->b_data;
378 	flags = bp->b_flags | B_CALL;
379 
380 	/* Allocate a header for this transfer and link it to the buffer */
381 	s = splbio();
382 	vnx = VND_GETXFER(vnd);
383 	splx(s);
384 	vnx->vx_flags = VX_BUSY;
385 	vnx->vx_error = 0;
386 	vnx->vx_pending = 0;
387 	vnx->vx_bp = bp;
388 
389 	for (resid = bp->b_resid; resid; resid -= sz) {
390 		struct vndbuf *nbp;
391 		struct vnode *vp;
392 		daddr_t nbn;
393 		int off, nra;
394 
395 		nra = 0;
396 		vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
397 		error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
398 		VOP_UNLOCK(vnd->sc_vp, 0);
399 
400 		if (error == 0 && (long)nbn == -1)
401 			error = EIO;
402 
403 		/*
404 		 * If there was an error or a hole in the file...punt.
405 		 * Note that we may have to wait for any operations
406 		 * that we have already fired off before releasing
407 		 * the buffer.
408 		 *
409 		 * XXX we could deal with holes here but it would be
410 		 * a hassle (in the write case).
411 		 */
412 		if (error) {
413 			s = splbio();
414 			vnx->vx_error = error;
415 			goto out;
416 		}
417 
418 #ifdef DEBUG
419 		if (!dovndcluster)
420 			nra = 0;
421 #endif
422 
423 		if ((off = bn % bsize) != 0)
424 			sz = bsize - off;
425 		else
426 			sz = (1 + nra) * bsize;
427 		if (resid < sz)
428 			sz = resid;
429 #ifdef DEBUG
430 		if (vnddebug & VDB_IO)
431 			printf("vndstrategy: vp %p/%p bn 0x%x/0x%x sz 0x%x\n",
432 			    vnd->sc_vp, vp, bn, nbn, sz);
433 #endif
434 
435 		s = splbio();
436 		nbp = VND_GETBUF(vnd);
437 		splx(s);
438 		nbp->vb_buf.b_flags = flags;
439 		nbp->vb_buf.b_bcount = sz;
440 		nbp->vb_buf.b_bufsize = bp->b_bufsize;
441 		nbp->vb_buf.b_error = 0;
442 		nbp->vb_buf.b_data = addr;
443 		nbp->vb_buf.b_blkno = nbp->vb_buf.b_rawblkno = nbn + btodb(off);
444 		nbp->vb_buf.b_proc = bp->b_proc;
445 		nbp->vb_buf.b_iodone = vndiodone;
446 		nbp->vb_buf.b_vp = NULLVP;
447 		nbp->vb_buf.b_rcred = vnd->sc_cred;	/* XXX crdup? */
448 		nbp->vb_buf.b_wcred = vnd->sc_cred;	/* XXX crdup? */
449 		LIST_INIT(&nbp->vb_buf.b_dep);
450 		if (bp->b_dirtyend == 0) {
451 			nbp->vb_buf.b_dirtyoff = 0;
452 			nbp->vb_buf.b_dirtyend = sz;
453 		} else {
454 			nbp->vb_buf.b_dirtyoff =
455 			    max(0, bp->b_dirtyoff - (bp->b_bcount - resid));
456 			nbp->vb_buf.b_dirtyend =
457 			    min(sz,
458 				max(0, bp->b_dirtyend - (bp->b_bcount-resid)));
459 		}
460 		if (bp->b_validend == 0) {
461 			nbp->vb_buf.b_validoff = 0;
462 			nbp->vb_buf.b_validend = sz;
463 		} else {
464 			nbp->vb_buf.b_validoff =
465 			    max(0, bp->b_validoff - (bp->b_bcount - resid));
466 			nbp->vb_buf.b_validend =
467 			    min(sz,
468 				max(0, bp->b_validend - (bp->b_bcount-resid)));
469 		}
470 
471 		nbp->vb_xfer = vnx;
472 
473 		/*
474 		 * Just sort by block number
475 		 */
476 		s = splbio();
477 		if (vnx->vx_error != 0) {
478 			VND_PUTBUF(vnd, nbp);
479 			goto out;
480 		}
481 		vnx->vx_pending++;
482 		bgetvp(vp, &nbp->vb_buf);
483 		disksort_blkno(&vnd->sc_tab, &nbp->vb_buf);
484 		vndstart(vnd);
485 		splx(s);
486 		bn += sz;
487 		addr += sz;
488 	}
489 
490 	s = splbio();
491 
492 out: /* Arrive here at splbio */
493 	vnx->vx_flags &= ~VX_BUSY;
494 	if (vnx->vx_pending == 0) {
495 		if (vnx->vx_error != 0) {
496 			bp->b_error = vnx->vx_error;
497 			bp->b_flags |= B_ERROR;
498 		}
499 		VND_PUTXFER(vnd, vnx);
500 		biodone(bp);
501 	}
502 	splx(s);
503 	return;
504 
505  done:
506 	biodone(bp);
507 }
508 
509 /*
510  * Feed requests sequentially.
511  * We do it this way to keep from flooding NFS servers if we are connected
512  * to an NFS file.  This places the burden on the client rather than the
513  * server.
514  */
515 void
516 vndstart(vnd)
517 	struct vnd_softc *vnd;
518 {
519 	struct buf	*bp;
520 
521 	/*
522 	 * Dequeue now since lower level strategy routine might
523 	 * queue using same links
524 	 */
525 
526 	if ((vnd->sc_flags & VNF_BUSY) != 0)
527 		return;
528 
529 	vnd->sc_flags |= VNF_BUSY;
530 
531 	while (vnd->sc_active < vnd->sc_maxactive) {
532 		bp = BUFQ_FIRST(&vnd->sc_tab);
533 		if (bp == NULL)
534 			break;
535 		BUFQ_REMOVE(&vnd->sc_tab, bp);
536 		vnd->sc_active++;
537 #ifdef DEBUG
538 		if (vnddebug & VDB_IO)
539 			printf("vndstart(%ld): bp %p vp %p blkno 0x%x addr %p cnt 0x%lx\n",
540 			    (long) (vnd-vnd_softc), bp, bp->b_vp, bp->b_blkno,
541 			    bp->b_data, bp->b_bcount);
542 #endif
543 
544 		/* Instrumentation. */
545 		disk_busy(&vnd->sc_dkdev);
546 
547 		if ((bp->b_flags & B_READ) == 0)
548 			bp->b_vp->v_numoutput++;
549 		VOP_STRATEGY(bp);
550 	}
551 	vnd->sc_flags &= ~VNF_BUSY;
552 }
553 
554 void
555 vndiodone(bp)
556 	struct buf *bp;
557 {
558 	struct vndbuf *vbp = (struct vndbuf *) bp;
559 	struct vndxfer *vnx = (struct vndxfer *)vbp->vb_xfer;
560 	struct buf *pbp = vnx->vx_bp;
561 	struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
562 	int s, resid;
563 
564 	s = splbio();
565 #ifdef DEBUG
566 	if (vnddebug & VDB_IO)
567 		printf("vndiodone(%ld): vbp %p vp %p blkno 0x%x addr %p cnt 0x%lx\n",
568 		    (long) (vnd-vnd_softc), vbp, vbp->vb_buf.b_vp,
569 		    vbp->vb_buf.b_blkno, vbp->vb_buf.b_data,
570 		    vbp->vb_buf.b_bcount);
571 #endif
572 
573 	resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
574 	pbp->b_resid -= resid;
575 	disk_unbusy(&vnd->sc_dkdev, resid);
576 	vnx->vx_pending--;
577 
578 	if (vbp->vb_buf.b_error) {
579 #ifdef DEBUG
580 		if (vnddebug & VDB_IO)
581 			printf("vndiodone: vbp %p error %d\n", vbp,
582 			    vbp->vb_buf.b_error);
583 #endif
584 		vnx->vx_error = vbp->vb_buf.b_error;
585 	}
586 
587 	if (vbp->vb_buf.b_vp != NULLVP)
588 		brelvp(&vbp->vb_buf);
589 
590 	VND_PUTBUF(vnd, vbp);
591 
592 	/*
593 	 * Wrap up this transaction if it has run to completion or, in
594 	 * case of an error, when all auxiliary buffers have returned.
595 	 */
596 	if (vnx->vx_error != 0) {
597 		pbp->b_flags |= B_ERROR;
598 		pbp->b_error = vnx->vx_error;
599 		if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
600 
601 #ifdef DEBUG
602 			if (vnddebug & VDB_IO)
603 				printf("vndiodone: pbp %p iodone: error %d\n",
604 					pbp, vnx->vx_error);
605 #endif
606 			VND_PUTXFER(vnd, vnx);
607 			biodone(pbp);
608 		}
609 	} else if (pbp->b_resid == 0) {
610 
611 #ifdef DIAGNOSTIC
612 		if (vnx->vx_pending != 0)
613 			panic("vndiodone: vnx pending: %d", vnx->vx_pending);
614 #endif
615 
616 		if ((vnx->vx_flags & VX_BUSY) == 0) {
617 #ifdef DEBUG
618 			if (vnddebug & VDB_IO)
619 				printf("vndiodone: pbp %p iodone\n", pbp);
620 #endif
621 			VND_PUTXFER(vnd, vnx);
622 			biodone(pbp);
623 		}
624 	}
625 
626 	vnd->sc_active--;
627 	vndstart(vnd);
628 	splx(s);
629 }
630 
631 /* ARGSUSED */
632 int
633 vndread(dev, uio, flags)
634 	dev_t dev;
635 	struct uio *uio;
636 	int flags;
637 {
638 	int unit = vndunit(dev);
639 	struct vnd_softc *sc;
640 
641 #ifdef DEBUG
642 	if (vnddebug & VDB_FOLLOW)
643 		printf("vndread(0x%x, %p)\n", dev, uio);
644 #endif
645 
646 	if (unit >= numvnd)
647 		return (ENXIO);
648 	sc = &vnd_softc[unit];
649 
650 	if ((sc->sc_flags & VNF_INITED) == 0)
651 		return (ENXIO);
652 
653 	return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
654 }
655 
656 /* ARGSUSED */
657 int
658 vndwrite(dev, uio, flags)
659 	dev_t dev;
660 	struct uio *uio;
661 	int flags;
662 {
663 	int unit = vndunit(dev);
664 	struct vnd_softc *sc;
665 
666 #ifdef DEBUG
667 	if (vnddebug & VDB_FOLLOW)
668 		printf("vndwrite(0x%x, %p)\n", dev, uio);
669 #endif
670 
671 	if (unit >= numvnd)
672 		return (ENXIO);
673 	sc = &vnd_softc[unit];
674 
675 	if ((sc->sc_flags & VNF_INITED) == 0)
676 		return (ENXIO);
677 
678 	return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
679 }
680 
681 /* ARGSUSED */
682 int
683 vndioctl(dev, cmd, data, flag, p)
684 	dev_t dev;
685 	u_long cmd;
686 	caddr_t data;
687 	int flag;
688 	struct proc *p;
689 {
690 	int unit = vndunit(dev);
691 	struct vnd_softc *vnd;
692 	struct vnd_ioctl *vio;
693 	struct vattr vattr;
694 	struct nameidata nd;
695 	int error, part, pmask;
696 	size_t geomsize;
697 
698 #ifdef DEBUG
699 	if (vnddebug & VDB_FOLLOW)
700 		printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
701 		    dev, cmd, data, flag, p, unit);
702 #endif
703 	error = suser(p->p_ucred, &p->p_acflag);
704 	if (error)
705 		return (error);
706 	if (unit >= numvnd)
707 		return (ENXIO);
708 
709 	vnd = &vnd_softc[unit];
710 	vio = (struct vnd_ioctl *)data;
711 
712 	/* Must be open for writes for these commands... */
713 	switch (cmd) {
714 	case VNDIOCSET:
715 	case VNDIOCCLR:
716 	case DIOCSDINFO:
717 	case DIOCWDINFO:
718 	case DIOCWLABEL:
719 		if ((flag & FWRITE) == 0)
720 			return (EBADF);
721 	}
722 
723 	/* Must be initialized for these... */
724 	switch (cmd) {
725 	case VNDIOCCLR:
726 	case DIOCGDINFO:
727 	case DIOCSDINFO:
728 	case DIOCWDINFO:
729 	case DIOCGPART:
730 	case DIOCWLABEL:
731 	case DIOCGDEFLABEL:
732 		if ((vnd->sc_flags & VNF_INITED) == 0)
733 			return (ENXIO);
734 	}
735 
736 	switch (cmd) {
737 	case VNDIOCSET:
738 		if (vnd->sc_flags & VNF_INITED)
739 			return (EBUSY);
740 
741 		if ((error = vndlock(vnd)) != 0)
742 			return (error);
743 
744 		/*
745 		 * Always open for read and write.
746 		 * This is probably bogus, but it lets vn_open()
747 		 * weed out directories, sockets, etc. so we don't
748 		 * have to worry about them.
749 		 */
750 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
751 		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
752 			vndunlock(vnd);
753 			return(error);
754 		}
755 		error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p);
756 		if (error) {
757 			VOP_UNLOCK(nd.ni_vp, 0);
758 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
759 			vndunlock(vnd);
760 			return(error);
761 		}
762 		VOP_UNLOCK(nd.ni_vp, 0);
763 		vnd->sc_vp = nd.ni_vp;
764 		vnd->sc_size = btodb(vattr.va_size);	/* note truncation */
765 
766 		/*
767 		 * Use pseudo-geometry specified.  If none was provided,
768 		 * use "standard" Adaptec fictitious geometry.
769 		 */
770 		if (vio->vnd_flags & VNDIOF_HASGEOM) {
771 
772 			bcopy(&vio->vnd_geom, &vnd->sc_geom,
773 			    sizeof(vio->vnd_geom));
774 
775 			/*
776 			 * Sanity-check the sector size.
777 			 * XXX Don't allow secsize < DEV_BSIZE.  Should
778 			 * XXX we?
779 			 */
780 			if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
781 			    (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0) {
782 				(void) vn_close(nd.ni_vp, FREAD|FWRITE,
783 				    p->p_ucred, p);
784 				vndunlock(vnd);
785 				return (EINVAL);
786 			}
787 
788 			/*
789 			 * Compute the size (in DEV_BSIZE blocks) specified
790 			 * by the geometry.
791 			 */
792 			geomsize = (vnd->sc_geom.vng_nsectors *
793 			    vnd->sc_geom.vng_ntracks *
794 			    vnd->sc_geom.vng_ncylinders) *
795 			    (vnd->sc_geom.vng_secsize / DEV_BSIZE);
796 
797 			/*
798 			 * Sanity-check the size against the specified
799 			 * geometry.
800 			 */
801 			if (vnd->sc_size < geomsize) {
802 				(void) vn_close(nd.ni_vp, FREAD|FWRITE,
803 				    p->p_ucred, p);
804 				vndunlock(vnd);
805 				return (EINVAL);
806 			}
807 		} else {
808 			/*
809 			 * Size must be at least 2048 DEV_BSIZE blocks
810 			 * (1M) in order to use this geometry.
811 			 */
812 			if (vnd->sc_size < (32 * 64)) {
813 				vndunlock(vnd);
814 				return (EINVAL);
815 			}
816 
817 			vnd->sc_geom.vng_secsize = DEV_BSIZE;
818 			vnd->sc_geom.vng_nsectors = 32;
819 			vnd->sc_geom.vng_ntracks = 64;
820 			vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
821 
822 			/*
823 			 * Compute the actual size allowed by this geometry.
824 			 */
825 			geomsize = 32 * 64 * vnd->sc_geom.vng_ncylinders;
826 		}
827 
828 		/*
829 		 * Truncate the size to that specified by
830 		 * the geometry.
831 		 * XXX Should we even bother with this?
832 		 */
833 		vnd->sc_size = geomsize;
834 
835 		if ((error = vndsetcred(vnd, p->p_ucred)) != 0) {
836 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
837 			vndunlock(vnd);
838 			return(error);
839 		}
840 		vndthrottle(vnd, vnd->sc_vp);
841 		vio->vnd_size = dbtob(vnd->sc_size);
842 		vnd->sc_flags |= VNF_INITED;
843 #ifdef DEBUG
844 		if (vnddebug & VDB_INIT)
845 			printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
846 			    vnd->sc_vp, (unsigned long) vnd->sc_size,
847 			    vnd->sc_geom.vng_secsize,
848 			    vnd->sc_geom.vng_nsectors,
849 			    vnd->sc_geom.vng_ntracks,
850 			    vnd->sc_geom.vng_ncylinders);
851 #endif
852 
853 		/* Attach the disk. */
854 		bzero(vnd->sc_xname, sizeof(vnd->sc_xname));	/* XXX */
855 		sprintf(vnd->sc_xname, "vnd%d", unit);		/* XXX */
856 		vnd->sc_dkdev.dk_name = vnd->sc_xname;
857 		disk_attach(&vnd->sc_dkdev);
858 
859 		/* Initialize the xfer and buffer pools. */
860 		pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
861 		    0, 0, "vndxpl", 0, NULL, NULL, M_DEVBUF);
862 		pool_init(&vnd->sc_vbpool, sizeof(struct vndbuf), 0,
863 		    0, 0, "vndbpl", 0, NULL, NULL, M_DEVBUF);
864 
865 		/* Try and read the disklabel. */
866 		vndgetdisklabel(dev);
867 
868 		vndunlock(vnd);
869 
870 		break;
871 
872 	case VNDIOCCLR:
873 		if ((error = vndlock(vnd)) != 0)
874 			return (error);
875 
876 		/*
877 		 * Don't unconfigure if any other partitions are open
878 		 * or if both the character and block flavors of this
879 		 * partition are open.
880 		 */
881 		part = DISKPART(dev);
882 		pmask = (1 << part);
883 		if ((vnd->sc_dkdev.dk_openmask & ~pmask) ||
884 		    ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
885 		    (vnd->sc_dkdev.dk_copenmask & pmask))) {
886 			vndunlock(vnd);
887 			return (EBUSY);
888 		}
889 
890 		vndclear(vnd);
891 #ifdef DEBUG
892 		if (vnddebug & VDB_INIT)
893 			printf("vndioctl: CLRed\n");
894 #endif
895 
896 		/* Destroy the xfer and buffer pools. */
897 		pool_destroy(&vnd->sc_vxpool);
898 		pool_destroy(&vnd->sc_vbpool);
899 
900 		/* Detatch the disk. */
901 		disk_detach(&vnd->sc_dkdev);
902 
903 		vndunlock(vnd);
904 
905 		break;
906 
907 	case DIOCGDINFO:
908 		*(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
909 		break;
910 
911 	case DIOCGPART:
912 		((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
913 		((struct partinfo *)data)->part =
914 		    &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
915 		break;
916 
917 	case DIOCWDINFO:
918 	case DIOCSDINFO:
919 		if ((error = vndlock(vnd)) != 0)
920 			return (error);
921 
922 		vnd->sc_flags |= VNF_LABELLING;
923 
924 		error = setdisklabel(vnd->sc_dkdev.dk_label,
925 		    (struct disklabel *)data, 0, vnd->sc_dkdev.dk_cpulabel);
926 		if (error == 0) {
927 			if (cmd == DIOCWDINFO)
928 				error = writedisklabel(VNDLABELDEV(dev),
929 				    vndstrategy, vnd->sc_dkdev.dk_label,
930 				    vnd->sc_dkdev.dk_cpulabel);
931 		}
932 
933 		vnd->sc_flags &= ~VNF_LABELLING;
934 
935 		vndunlock(vnd);
936 
937 		if (error)
938 			return (error);
939 		break;
940 
941 	case DIOCWLABEL:
942 		if (*(int *)data != 0)
943 			vnd->sc_flags |= VNF_WLABEL;
944 		else
945 			vnd->sc_flags &= ~VNF_WLABEL;
946 		break;
947 
948 	case DIOCGDEFLABEL:
949 		vndgetdefaultlabel(vnd, (struct disklabel *)data);
950 		break;
951 
952 	default:
953 		return (ENOTTY);
954 	}
955 
956 	return (0);
957 }
958 
959 /*
960  * Duplicate the current processes' credentials.  Since we are called only
961  * as the result of a SET ioctl and only root can do that, any future access
962  * to this "disk" is essentially as root.  Note that credentials may change
963  * if some other uid can write directly to the mapped file (NFS).
964  */
965 int
966 vndsetcred(vnd, cred)
967 	struct vnd_softc *vnd;
968 	struct ucred *cred;
969 {
970 	struct uio auio;
971 	struct iovec aiov;
972 	char *tmpbuf;
973 	int error;
974 
975 	vnd->sc_cred = crdup(cred);
976 	tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
977 
978 	/* XXX: Horrible kludge to establish credentials for NFS */
979 	aiov.iov_base = tmpbuf;
980 	aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
981 	auio.uio_iov = &aiov;
982 	auio.uio_iovcnt = 1;
983 	auio.uio_offset = 0;
984 	auio.uio_rw = UIO_READ;
985 	auio.uio_segflg = UIO_SYSSPACE;
986 	auio.uio_resid = aiov.iov_len;
987 	vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
988 	error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
989 	if (error == 0) {
990 		/*
991 		 * Because vnd does all IO directly through the vnode
992 		 * we need to flush (at least) the buffer from the above
993 		 * VOP_READ from the buffer cache to prevent cache
994 		 * incoherencies.  Also, be careful to write dirty
995 		 * buffers back to stable storage.
996 		 */
997 		error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
998 			    curproc, 0, 0);
999 	}
1000 	VOP_UNLOCK(vnd->sc_vp, 0);
1001 
1002 	free(tmpbuf, M_TEMP);
1003 	return (error);
1004 }
1005 
1006 /*
1007  * Set maxactive based on FS type
1008  */
1009 void
1010 vndthrottle(vnd, vp)
1011 	struct vnd_softc *vnd;
1012 	struct vnode *vp;
1013 {
1014 #ifdef NFS
1015 	extern int (**nfsv2_vnodeop_p) __P((void *));
1016 
1017 	if (vp->v_op == nfsv2_vnodeop_p)
1018 		vnd->sc_maxactive = 2;
1019 	else
1020 #endif
1021 		vnd->sc_maxactive = 8;
1022 
1023 	if (vnd->sc_maxactive < 1)
1024 		vnd->sc_maxactive = 1;
1025 }
1026 
1027 void
1028 vndshutdown()
1029 {
1030 	struct vnd_softc *vnd;
1031 
1032 	for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1033 		if (vnd->sc_flags & VNF_INITED)
1034 			vndclear(vnd);
1035 }
1036 
1037 void
1038 vndclear(vnd)
1039 	struct vnd_softc *vnd;
1040 {
1041 	struct vnode *vp = vnd->sc_vp;
1042 	struct proc *p = curproc;		/* XXX */
1043 
1044 #ifdef DEBUG
1045 	if (vnddebug & VDB_FOLLOW)
1046 		printf("vndclear(%p): vp %p\n", vnd, vp);
1047 #endif
1048 	vnd->sc_flags &= ~VNF_INITED;
1049 	if (vp == (struct vnode *)0)
1050 		panic("vndioctl: null vp");
1051 	(void) vn_close(vp, FREAD|FWRITE, vnd->sc_cred, p);
1052 	crfree(vnd->sc_cred);
1053 	vnd->sc_vp = (struct vnode *)0;
1054 	vnd->sc_cred = (struct ucred *)0;
1055 	vnd->sc_size = 0;
1056 }
1057 
1058 int
1059 vndsize(dev)
1060 	dev_t dev;
1061 {
1062 	struct vnd_softc *sc;
1063 	struct disklabel *lp;
1064 	int part, unit, omask;
1065 	int size;
1066 
1067 	unit = vndunit(dev);
1068 	if (unit >= numvnd)
1069 		return (-1);
1070 	sc = &vnd_softc[unit];
1071 
1072 	if ((sc->sc_flags & VNF_INITED) == 0)
1073 		return (-1);
1074 
1075 	part = DISKPART(dev);
1076 	omask = sc->sc_dkdev.dk_openmask & (1 << part);
1077 	lp = sc->sc_dkdev.dk_label;
1078 
1079 	if (omask == 0 && vndopen(dev, 0, S_IFBLK, curproc))
1080 		return (-1);
1081 
1082 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
1083 		size = -1;
1084 	else
1085 		size = lp->d_partitions[part].p_size *
1086 		    (lp->d_secsize / DEV_BSIZE);
1087 
1088 	if (omask == 0 && vndclose(dev, 0, S_IFBLK, curproc))
1089 		return (-1);
1090 
1091 	return (size);
1092 }
1093 
1094 int
1095 vnddump(dev, blkno, va, size)
1096 	dev_t dev;
1097 	daddr_t blkno;
1098 	caddr_t va;
1099 	size_t size;
1100 {
1101 
1102 	/* Not implemented. */
1103 	return ENXIO;
1104 }
1105 
1106 void
1107 vndgetdefaultlabel(sc, lp)
1108 	struct vnd_softc *sc;
1109 	struct disklabel *lp;
1110 {
1111 	struct vndgeom *vng = &sc->sc_geom;
1112 	struct partition *pp;
1113 
1114 	bzero(lp, sizeof(*lp));
1115 
1116 	lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
1117 	lp->d_secsize = vng->vng_secsize;
1118 	lp->d_nsectors = vng->vng_nsectors;
1119 	lp->d_ntracks = vng->vng_ntracks;
1120 	lp->d_ncylinders = vng->vng_ncylinders;
1121 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1122 
1123 	strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1124 	lp->d_type = DTYPE_VND;
1125 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1126 	lp->d_rpm = 3600;
1127 	lp->d_interleave = 1;
1128 	lp->d_flags = 0;
1129 
1130 	pp = &lp->d_partitions[RAW_PART];
1131 	pp->p_offset = 0;
1132 	pp->p_size = lp->d_secperunit;
1133 	pp->p_fstype = FS_UNUSED;
1134 	lp->d_npartitions = RAW_PART + 1;
1135 
1136 	lp->d_magic = DISKMAGIC;
1137 	lp->d_magic2 = DISKMAGIC;
1138 	lp->d_checksum = dkcksum(lp);
1139 }
1140 
1141 /*
1142  * Read the disklabel from a vnd.  If one is not present, create a fake one.
1143  */
1144 void
1145 vndgetdisklabel(dev)
1146 	dev_t dev;
1147 {
1148 	struct vnd_softc *sc = &vnd_softc[vndunit(dev)];
1149 	char *errstring;
1150 	struct disklabel *lp = sc->sc_dkdev.dk_label;
1151 	struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1152 	int i;
1153 
1154 	bzero(clp, sizeof(*clp));
1155 
1156 	vndgetdefaultlabel(sc, lp);
1157 
1158 	/*
1159 	 * Call the generic disklabel extraction routine.
1160 	 */
1161 	errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1162 	if (errstring) {
1163 		/*
1164 		 * Lack of disklabel is common, but we print the warning
1165 		 * anyway, since it might contain other useful information.
1166 		 */
1167 		printf("%s: %s\n", sc->sc_xname, errstring);
1168 
1169 		/*
1170 		 * For historical reasons, if there's no disklabel
1171 		 * present, all partitions must be FS_BSDFFS and
1172 		 * occupy the entire disk.
1173 		 */
1174 		for (i = 0; i < MAXPARTITIONS; i++) {
1175 			/*
1176 			 * Don't wipe out port specific hack (such as
1177 			 * dos partition hack of i386 port).
1178 			 */
1179 			if (lp->d_partitions[i].p_fstype != FS_UNUSED)
1180 				continue;
1181 
1182 			lp->d_partitions[i].p_size = lp->d_secperunit;
1183 			lp->d_partitions[i].p_offset = 0;
1184 			lp->d_partitions[i].p_fstype = FS_BSDFFS;
1185 		}
1186 
1187 		strncpy(lp->d_packname, "default label",
1188 		    sizeof(lp->d_packname));
1189 
1190 		lp->d_checksum = dkcksum(lp);
1191 	}
1192 }
1193 
1194 /*
1195  * Wait interruptibly for an exclusive lock.
1196  *
1197  * XXX
1198  * Several drivers do this; it should be abstracted and made MP-safe.
1199  */
1200 static int
1201 vndlock(sc)
1202 	struct vnd_softc *sc;
1203 {
1204 	int error;
1205 
1206 	while ((sc->sc_flags & VNF_LOCKED) != 0) {
1207 		sc->sc_flags |= VNF_WANTED;
1208 		if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1209 			return (error);
1210 	}
1211 	sc->sc_flags |= VNF_LOCKED;
1212 	return (0);
1213 }
1214 
1215 /*
1216  * Unlock and wake up any waiters.
1217  */
1218 static void
1219 vndunlock(sc)
1220 	struct vnd_softc *sc;
1221 {
1222 
1223 	sc->sc_flags &= ~VNF_LOCKED;
1224 	if ((sc->sc_flags & VNF_WANTED) != 0) {
1225 		sc->sc_flags &= ~VNF_WANTED;
1226 		wakeup(sc);
1227 	}
1228 }
1229