vnd.c revision 1.91
1/*	$NetBSD: vnd.c,v 1.91 2003/02/05 21:38:39 pk 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 <sys/cdefs.h>
101__KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.91 2003/02/05 21:38:39 pk Exp $");
102
103#if defined(_KERNEL_OPT)
104#include "fs_nfs.h"
105#endif
106
107#include <sys/param.h>
108#include <sys/systm.h>
109#include <sys/namei.h>
110#include <sys/proc.h>
111#include <sys/errno.h>
112#include <sys/buf.h>
113#include <sys/malloc.h>
114#include <sys/ioctl.h>
115#include <sys/disklabel.h>
116#include <sys/device.h>
117#include <sys/disk.h>
118#include <sys/stat.h>
119#include <sys/mount.h>
120#include <sys/vnode.h>
121#include <sys/file.h>
122#include <sys/uio.h>
123#include <sys/conf.h>
124
125#include <miscfs/specfs/specdev.h>
126
127#include <dev/vndvar.h>
128
129#if defined(VNDDEBUG) && !defined(DEBUG)
130#define	DEBUG
131#endif
132
133#ifdef DEBUG
134int dovndcluster = 1;
135#define	VDB_FOLLOW	0x01
136#define	VDB_INIT	0x02
137#define	VDB_IO		0x04
138#define	VDB_LABEL	0x08
139int vnddebug = 0x00;
140#endif
141
142#define	vndunit(x)	DISKUNIT(x)
143
144struct vndxfer {
145	struct buf	*vx_bp;		/* Pointer to parent buffer */
146	int		vx_error;
147	int		vx_pending;	/* # of pending aux buffers */
148	int		vx_flags;
149#define VX_BUSY		1
150};
151
152struct vndbuf {
153	struct buf	vb_buf;
154	struct vndxfer	*vb_xfer;
155};
156
157#define	VND_GETXFER(vnd)	pool_get(&(vnd)->sc_vxpool, PR_NOWAIT)
158#define	VND_PUTXFER(vnd, vx)	pool_put(&(vnd)->sc_vxpool, (vx))
159
160#define	VND_GETBUF(vnd)		pool_get(&(vnd)->sc_vbpool, PR_NOWAIT)
161#define	VND_PUTBUF(vnd, vb)	pool_put(&(vnd)->sc_vbpool, (vb))
162
163struct vnd_softc *vnd_softc;
164int numvnd = 0;
165
166#define	VNDLABELDEV(dev) \
167	(MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
168
169/* called by main() at boot time (XXX: and the LKM driver) */
170void	vndattach __P((int));
171int	vnddetach __P((void));
172
173void	vndclear __P((struct vnd_softc *));
174void	vndstart __P((struct vnd_softc *));
175int	vndsetcred __P((struct vnd_softc *, struct ucred *));
176void	vndthrottle __P((struct vnd_softc *, struct vnode *));
177void	vndiodone __P((struct buf *));
178void	vndshutdown __P((void));
179
180void	vndgetdefaultlabel __P((struct vnd_softc *, struct disklabel *));
181void	vndgetdisklabel __P((dev_t));
182
183static	int vndlock __P((struct vnd_softc *));
184static	void vndunlock __P((struct vnd_softc *));
185
186dev_type_open(vndopen);
187dev_type_close(vndclose);
188dev_type_read(vndread);
189dev_type_write(vndwrite);
190dev_type_ioctl(vndioctl);
191dev_type_strategy(vndstrategy);
192dev_type_dump(vnddump);
193dev_type_size(vndsize);
194
195const struct bdevsw vnd_bdevsw = {
196	vndopen, vndclose, vndstrategy, vndioctl, vnddump, vndsize, D_DISK
197};
198
199const struct cdevsw vnd_cdevsw = {
200	vndopen, vndclose, vndread, vndwrite, vndioctl,
201	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
202};
203
204int vndattached = 0;
205
206void
207vndattach(num)
208	int num;
209{
210	int i;
211	char *mem;
212
213	if (vndattached)
214		return;
215	vndattached = 1;
216	if (num <= 0)
217		return;
218	i = num * sizeof(struct vnd_softc);
219	mem = malloc(i, M_DEVBUF, M_NOWAIT|M_ZERO);
220	if (mem == NULL) {
221		printf("WARNING: no memory for vnode disks\n");
222		return;
223	}
224	vnd_softc = (struct vnd_softc *)mem;
225	numvnd = num;
226
227	for (i = 0; i < numvnd; i++)
228		bufq_alloc(&vnd_softc[i].sc_tab,
229		    BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
230}
231
232int
233vnddetach()
234{
235	int i;
236
237	/* First check we aren't in use. */
238	for (i = 0; i < numvnd; i++)
239		if (vnd_softc[i].sc_flags & VNF_INITED)
240			return (EBUSY);
241
242	for (i = 0; i < numvnd; i++)
243		bufq_free(&vnd_softc[i].sc_tab);
244
245	free(vnd_softc, M_DEVBUF);
246	vndattached = 0;
247
248	return (0);
249}
250
251int
252vndopen(dev, flags, mode, p)
253	dev_t dev;
254	int flags, mode;
255	struct proc *p;
256{
257	int unit = vndunit(dev);
258	struct vnd_softc *sc;
259	int error = 0, part, pmask;
260	struct disklabel *lp;
261
262#ifdef DEBUG
263	if (vnddebug & VDB_FOLLOW)
264		printf("vndopen(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, p);
265#endif
266	if (unit >= numvnd)
267		return (ENXIO);
268	sc = &vnd_softc[unit];
269
270	if ((error = vndlock(sc)) != 0)
271		return (error);
272
273	lp = sc->sc_dkdev.dk_label;
274
275	part = DISKPART(dev);
276	pmask = (1 << part);
277
278	/*
279	 * If we're initialized, check to see if there are any other
280	 * open partitions.  If not, then it's safe to update the
281	 * in-core disklabel.
282	 */
283	if ((sc->sc_flags & VNF_INITED) && (sc->sc_dkdev.dk_openmask == 0))
284		vndgetdisklabel(dev);
285
286	/* Check that the partitions exists. */
287	if (part != RAW_PART) {
288		if (((sc->sc_flags & VNF_INITED) == 0) ||
289		    ((part >= lp->d_npartitions) ||
290		     (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
291			error = ENXIO;
292			goto done;
293		}
294	}
295
296	/* Prevent our unit from being unconfigured while open. */
297	switch (mode) {
298	case S_IFCHR:
299		sc->sc_dkdev.dk_copenmask |= pmask;
300		break;
301
302	case S_IFBLK:
303		sc->sc_dkdev.dk_bopenmask |= pmask;
304		break;
305	}
306	sc->sc_dkdev.dk_openmask =
307	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
308
309 done:
310	vndunlock(sc);
311	return (error);
312}
313
314int
315vndclose(dev, flags, mode, p)
316	dev_t dev;
317	int flags, mode;
318	struct proc *p;
319{
320	int unit = vndunit(dev);
321	struct vnd_softc *sc;
322	int error = 0, part;
323
324#ifdef DEBUG
325	if (vnddebug & VDB_FOLLOW)
326		printf("vndclose(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, p);
327#endif
328
329	if (unit >= numvnd)
330		return (ENXIO);
331	sc = &vnd_softc[unit];
332
333	if ((error = vndlock(sc)) != 0)
334		return (error);
335
336	part = DISKPART(dev);
337
338	/* ...that much closer to allowing unconfiguration... */
339	switch (mode) {
340	case S_IFCHR:
341		sc->sc_dkdev.dk_copenmask &= ~(1 << part);
342		break;
343
344	case S_IFBLK:
345		sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
346		break;
347	}
348	sc->sc_dkdev.dk_openmask =
349	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
350
351	vndunlock(sc);
352	return (0);
353}
354
355/*
356 * Break the request into bsize pieces and submit using VOP_BMAP/VOP_STRATEGY.
357 */
358void
359vndstrategy(bp)
360	struct buf *bp;
361{
362	int unit = vndunit(bp->b_dev);
363	struct vnd_softc *vnd = &vnd_softc[unit];
364	struct vndxfer *vnx;
365	int s, bsize, resid;
366	off_t bn;
367	caddr_t addr;
368	int sz, flags, error, wlabel;
369	struct disklabel *lp;
370	struct partition *pp;
371
372#ifdef DEBUG
373	if (vnddebug & VDB_FOLLOW)
374		printf("vndstrategy(%p): unit %d\n", bp, unit);
375#endif
376	if ((vnd->sc_flags & VNF_INITED) == 0) {
377		bp->b_error = ENXIO;
378		bp->b_flags |= B_ERROR;
379		goto done;
380	}
381
382	/* If it's a nil transfer, wake up the top half now. */
383	if (bp->b_bcount == 0)
384		goto done;
385
386	lp = vnd->sc_dkdev.dk_label;
387
388	/*
389	 * The transfer must be a whole number of blocks.
390	 */
391	if ((bp->b_bcount % lp->d_secsize) != 0) {
392		bp->b_error = EINVAL;
393		bp->b_flags |= B_ERROR;
394		goto done;
395	}
396
397	/*
398	 * Do bounds checking and adjust transfer.  If there's an error,
399	 * the bounds check will flag that for us.
400	 */
401	wlabel = vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING);
402	if (DISKPART(bp->b_dev) != RAW_PART)
403		if (bounds_check_with_label(bp, lp, wlabel) <= 0)
404			goto done;
405
406	bp->b_resid = bp->b_bcount;
407
408	/*
409	 * Put the block number in terms of the logical blocksize
410	 * of the "device".
411	 */
412	bn = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
413
414	/*
415	 * Translate the partition-relative block number to an absolute.
416	 */
417	if (DISKPART(bp->b_dev) != RAW_PART) {
418		pp = &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
419		bn += pp->p_offset;
420	}
421
422	/* ...and convert to a byte offset within the file. */
423	bn *= lp->d_secsize;
424
425	if (vnd->sc_vp->v_mount == NULL) {
426		bp->b_error = ENXIO;
427		bp->b_flags |= B_ERROR;
428		goto done;
429	}
430 	bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
431	addr = bp->b_data;
432	flags = (bp->b_flags & (B_READ|B_ASYNC)) | B_CALL;
433
434	/* Allocate a header for this transfer and link it to the buffer */
435	s = splbio();
436	vnx = VND_GETXFER(vnd);
437	splx(s);
438	vnx->vx_flags = VX_BUSY;
439	vnx->vx_error = 0;
440	vnx->vx_pending = 0;
441	vnx->vx_bp = bp;
442
443	for (resid = bp->b_resid; resid; resid -= sz) {
444		struct vndbuf *nbp;
445		struct vnode *vp;
446		daddr_t nbn;
447		int off, nra;
448
449		nra = 0;
450		vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
451		error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
452		VOP_UNLOCK(vnd->sc_vp, 0);
453
454		if (error == 0 && (long)nbn == -1)
455			error = EIO;
456
457		/*
458		 * If there was an error or a hole in the file...punt.
459		 * Note that we may have to wait for any operations
460		 * that we have already fired off before releasing
461		 * the buffer.
462		 *
463		 * XXX we could deal with holes here but it would be
464		 * a hassle (in the write case).
465		 */
466		if (error) {
467			s = splbio();
468			vnx->vx_error = error;
469			goto out;
470		}
471
472#ifdef DEBUG
473		if (!dovndcluster)
474			nra = 0;
475#endif
476
477		if ((off = bn % bsize) != 0)
478			sz = bsize - off;
479		else
480			sz = (1 + nra) * bsize;
481		if (resid < sz)
482			sz = resid;
483#ifdef DEBUG
484		if (vnddebug & VDB_IO)
485			printf("vndstrategy: vp %p/%p bn 0x%qx/0x%" PRIx64
486			       " sz 0x%x\n",
487			    vnd->sc_vp, vp, (long long)bn, nbn, sz);
488#endif
489
490		s = splbio();
491		nbp = VND_GETBUF(vnd);
492		splx(s);
493		simple_lock_init(&nbp->vb_buf.b_interlock);
494		nbp->vb_buf.b_flags = flags;
495		nbp->vb_buf.b_bcount = sz;
496		nbp->vb_buf.b_bufsize = round_page((ulong)addr + sz)
497		    - trunc_page((ulong) addr);
498		nbp->vb_buf.b_error = 0;
499		nbp->vb_buf.b_data = addr;
500		nbp->vb_buf.b_blkno = nbp->vb_buf.b_rawblkno = nbn + btodb(off);
501		nbp->vb_buf.b_proc = bp->b_proc;
502		nbp->vb_buf.b_iodone = vndiodone;
503		nbp->vb_buf.b_vp = NULLVP;
504		LIST_INIT(&nbp->vb_buf.b_dep);
505
506		nbp->vb_xfer = vnx;
507
508		/*
509		 * Just sort by block number
510		 */
511		s = splbio();
512		if (vnx->vx_error != 0) {
513			VND_PUTBUF(vnd, nbp);
514			goto out;
515		}
516		vnx->vx_pending++;
517		bgetvp(vp, &nbp->vb_buf);
518		BUFQ_PUT(&vnd->sc_tab, &nbp->vb_buf);
519		vndstart(vnd);
520		splx(s);
521		bn += sz;
522		addr += sz;
523	}
524
525	s = splbio();
526
527out: /* Arrive here at splbio */
528	vnx->vx_flags &= ~VX_BUSY;
529	if (vnx->vx_pending == 0) {
530		if (vnx->vx_error != 0) {
531			bp->b_error = vnx->vx_error;
532			bp->b_flags |= B_ERROR;
533		}
534		VND_PUTXFER(vnd, vnx);
535		biodone(bp);
536	}
537	splx(s);
538	return;
539
540 done:
541	biodone(bp);
542}
543
544/*
545 * Feed requests sequentially.
546 * We do it this way to keep from flooding NFS servers if we are connected
547 * to an NFS file.  This places the burden on the client rather than the
548 * server.
549 */
550void
551vndstart(vnd)
552	struct vnd_softc *vnd;
553{
554	struct buf	*bp;
555
556	/*
557	 * Dequeue now since lower level strategy routine might
558	 * queue using same links
559	 */
560
561	if ((vnd->sc_flags & VNF_BUSY) != 0)
562		return;
563
564	vnd->sc_flags |= VNF_BUSY;
565
566	while (vnd->sc_active < vnd->sc_maxactive) {
567		bp = BUFQ_GET(&vnd->sc_tab);
568		if (bp == NULL)
569			break;
570		vnd->sc_active++;
571#ifdef DEBUG
572		if (vnddebug & VDB_IO)
573			printf("vndstart(%ld): bp %p vp %p blkno 0x%" PRIx64
574				" flags %lx addr %p cnt 0x%lx\n",
575			    (long) (vnd-vnd_softc), bp, bp->b_vp, bp->b_blkno,
576			    bp->b_flags, bp->b_data, bp->b_bcount);
577#endif
578
579		/* Instrumentation. */
580		disk_busy(&vnd->sc_dkdev);
581
582		if ((bp->b_flags & B_READ) == 0)
583			bp->b_vp->v_numoutput++;
584		VOP_STRATEGY(bp);
585	}
586	vnd->sc_flags &= ~VNF_BUSY;
587}
588
589void
590vndiodone(bp)
591	struct buf *bp;
592{
593	struct vndbuf *vbp = (struct vndbuf *) bp;
594	struct vndxfer *vnx = (struct vndxfer *)vbp->vb_xfer;
595	struct buf *pbp = vnx->vx_bp;
596	struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
597	int s, resid;
598
599	s = splbio();
600#ifdef DEBUG
601	if (vnddebug & VDB_IO)
602		printf("vndiodone(%ld): vbp %p vp %p blkno 0x%" PRIx64
603		       " addr %p cnt 0x%lx\n",
604		    (long) (vnd-vnd_softc), vbp, vbp->vb_buf.b_vp,
605		    vbp->vb_buf.b_blkno, vbp->vb_buf.b_data,
606		    vbp->vb_buf.b_bcount);
607#endif
608
609	resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
610	pbp->b_resid -= resid;
611	disk_unbusy(&vnd->sc_dkdev, resid, (pbp->b_flags & B_READ));
612	vnx->vx_pending--;
613
614	if (vbp->vb_buf.b_error) {
615#ifdef DEBUG
616		if (vnddebug & VDB_IO)
617			printf("vndiodone: vbp %p error %d\n", vbp,
618			    vbp->vb_buf.b_error);
619#endif
620		vnx->vx_error = vbp->vb_buf.b_error;
621	}
622
623	if (vbp->vb_buf.b_vp != NULLVP)
624		brelvp(&vbp->vb_buf);
625
626	VND_PUTBUF(vnd, vbp);
627
628	/*
629	 * Wrap up this transaction if it has run to completion or, in
630	 * case of an error, when all auxiliary buffers have returned.
631	 */
632	if (vnx->vx_error != 0) {
633		pbp->b_flags |= B_ERROR;
634		pbp->b_error = vnx->vx_error;
635		if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
636
637#ifdef DEBUG
638			if (vnddebug & VDB_IO)
639				printf("vndiodone: pbp %p iodone: error %d\n",
640					pbp, vnx->vx_error);
641#endif
642			VND_PUTXFER(vnd, vnx);
643			biodone(pbp);
644		}
645	} else if (pbp->b_resid == 0) {
646
647#ifdef DIAGNOSTIC
648		if (vnx->vx_pending != 0)
649			panic("vndiodone: vnx pending: %d", vnx->vx_pending);
650#endif
651
652		if ((vnx->vx_flags & VX_BUSY) == 0) {
653#ifdef DEBUG
654			if (vnddebug & VDB_IO)
655				printf("vndiodone: pbp %p iodone\n", pbp);
656#endif
657			VND_PUTXFER(vnd, vnx);
658			biodone(pbp);
659		}
660	}
661
662	vnd->sc_active--;
663	vndstart(vnd);
664	splx(s);
665}
666
667/* ARGSUSED */
668int
669vndread(dev, uio, flags)
670	dev_t dev;
671	struct uio *uio;
672	int flags;
673{
674	int unit = vndunit(dev);
675	struct vnd_softc *sc;
676
677#ifdef DEBUG
678	if (vnddebug & VDB_FOLLOW)
679		printf("vndread(0x%x, %p)\n", dev, uio);
680#endif
681
682	if (unit >= numvnd)
683		return (ENXIO);
684	sc = &vnd_softc[unit];
685
686	if ((sc->sc_flags & VNF_INITED) == 0)
687		return (ENXIO);
688
689	return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
690}
691
692/* ARGSUSED */
693int
694vndwrite(dev, uio, flags)
695	dev_t dev;
696	struct uio *uio;
697	int flags;
698{
699	int unit = vndunit(dev);
700	struct vnd_softc *sc;
701
702#ifdef DEBUG
703	if (vnddebug & VDB_FOLLOW)
704		printf("vndwrite(0x%x, %p)\n", dev, uio);
705#endif
706
707	if (unit >= numvnd)
708		return (ENXIO);
709	sc = &vnd_softc[unit];
710
711	if ((sc->sc_flags & VNF_INITED) == 0)
712		return (ENXIO);
713
714	return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
715}
716
717/* ARGSUSED */
718int
719vndioctl(dev, cmd, data, flag, p)
720	dev_t dev;
721	u_long cmd;
722	caddr_t data;
723	int flag;
724	struct proc *p;
725{
726	int unit = vndunit(dev);
727	struct vnd_softc *vnd;
728	struct vnd_ioctl *vio;
729	struct vattr vattr;
730	struct nameidata nd;
731	int error, part, pmask;
732	size_t geomsize;
733#ifdef __HAVE_OLD_DISKLABEL
734	struct disklabel newlabel;
735#endif
736
737#ifdef DEBUG
738	if (vnddebug & VDB_FOLLOW)
739		printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
740		    dev, cmd, data, flag, p, unit);
741#endif
742	error = suser(p->p_ucred, &p->p_acflag);
743	if (error)
744		return (error);
745	if (unit >= numvnd)
746		return (ENXIO);
747
748	vnd = &vnd_softc[unit];
749	vio = (struct vnd_ioctl *)data;
750
751	/* Must be open for writes for these commands... */
752	switch (cmd) {
753	case VNDIOCSET:
754	case VNDIOCCLR:
755	case DIOCSDINFO:
756	case DIOCWDINFO:
757#ifdef __HAVE_OLD_DISKLABEL
758	case ODIOCSDINFO:
759	case ODIOCWDINFO:
760#endif
761	case DIOCWLABEL:
762		if ((flag & FWRITE) == 0)
763			return (EBADF);
764	}
765
766	/* Must be initialized for these... */
767	switch (cmd) {
768	case VNDIOCCLR:
769	case DIOCGDINFO:
770	case DIOCSDINFO:
771	case DIOCWDINFO:
772	case DIOCGPART:
773	case DIOCWLABEL:
774	case DIOCGDEFLABEL:
775#ifdef __HAVE_OLD_DISKLABEL
776	case ODIOCGDINFO:
777	case ODIOCSDINFO:
778	case ODIOCWDINFO:
779	case ODIOCGDEFLABEL:
780#endif
781		if ((vnd->sc_flags & VNF_INITED) == 0)
782			return (ENXIO);
783	}
784
785	switch (cmd) {
786	case VNDIOCSET:
787		if (vnd->sc_flags & VNF_INITED)
788			return (EBUSY);
789
790		if ((error = vndlock(vnd)) != 0)
791			return (error);
792
793		/*
794		 * Always open for read and write.
795		 * This is probably bogus, but it lets vn_open()
796		 * weed out directories, sockets, etc. so we don't
797		 * have to worry about them.
798		 */
799		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
800		if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0)
801			goto unlock_and_exit;
802		error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p);
803		VOP_UNLOCK(nd.ni_vp, 0);
804		if (error)
805			goto close_and_exit;
806		vnd->sc_vp = nd.ni_vp;
807		vnd->sc_size = btodb(vattr.va_size);	/* note truncation */
808
809		/*
810		 * Use pseudo-geometry specified.  If none was provided,
811		 * use "standard" Adaptec fictitious geometry.
812		 */
813		if (vio->vnd_flags & VNDIOF_HASGEOM) {
814
815			memcpy(&vnd->sc_geom, &vio->vnd_geom,
816			    sizeof(vio->vnd_geom));
817
818			/*
819			 * Sanity-check the sector size.
820			 * XXX Don't allow secsize < DEV_BSIZE.  Should
821			 * XXX we?
822			 */
823			if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
824			    (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0) {
825				error = EINVAL;
826				goto close_and_exit;
827			}
828
829			/*
830			 * Compute the size (in DEV_BSIZE blocks) specified
831			 * by the geometry.
832			 */
833			geomsize = (vnd->sc_geom.vng_nsectors *
834			    vnd->sc_geom.vng_ntracks *
835			    vnd->sc_geom.vng_ncylinders) *
836			    (vnd->sc_geom.vng_secsize / DEV_BSIZE);
837
838			/*
839			 * Sanity-check the size against the specified
840			 * geometry.
841			 */
842			if (vnd->sc_size < geomsize) {
843				error = EINVAL;
844				goto close_and_exit;
845			}
846		} else {
847			/*
848			 * Size must be at least 2048 DEV_BSIZE blocks
849			 * (1M) in order to use this geometry.
850			 */
851			if (vnd->sc_size < (32 * 64)) {
852				error = EINVAL;
853				goto close_and_exit;
854			}
855
856			vnd->sc_geom.vng_secsize = DEV_BSIZE;
857			vnd->sc_geom.vng_nsectors = 32;
858			vnd->sc_geom.vng_ntracks = 64;
859			vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
860		}
861
862		if ((error = vndsetcred(vnd, p->p_ucred)) != 0)
863			goto close_and_exit;
864		vndthrottle(vnd, vnd->sc_vp);
865		vio->vnd_size = dbtob(vnd->sc_size);
866		vnd->sc_flags |= VNF_INITED;
867#ifdef DEBUG
868		if (vnddebug & VDB_INIT)
869			printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
870			    vnd->sc_vp, (unsigned long) vnd->sc_size,
871			    vnd->sc_geom.vng_secsize,
872			    vnd->sc_geom.vng_nsectors,
873			    vnd->sc_geom.vng_ntracks,
874			    vnd->sc_geom.vng_ncylinders);
875#endif
876
877		/* Attach the disk. */
878		memset(vnd->sc_xname, 0, sizeof(vnd->sc_xname)); /* XXX */
879		sprintf(vnd->sc_xname, "vnd%d", unit);		/* XXX */
880		vnd->sc_dkdev.dk_name = vnd->sc_xname;
881		disk_attach(&vnd->sc_dkdev);
882
883		/* Initialize the xfer and buffer pools. */
884		pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
885		    0, 0, "vndxpl", NULL);
886		pool_init(&vnd->sc_vbpool, sizeof(struct vndbuf), 0,
887		    0, 0, "vndbpl", NULL);
888
889		/* Try and read the disklabel. */
890		vndgetdisklabel(dev);
891
892		vndunlock(vnd);
893
894		break;
895
896close_and_exit:
897		(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
898unlock_and_exit:
899		vndunlock(vnd);
900		return (error);
901
902	case VNDIOCCLR:
903		if ((error = vndlock(vnd)) != 0)
904			return (error);
905
906		/*
907		 * Don't unconfigure if any other partitions are open
908		 * or if both the character and block flavors of this
909		 * partition are open.
910		 */
911		part = DISKPART(dev);
912		pmask = (1 << part);
913		if ((vnd->sc_dkdev.dk_openmask & ~pmask) ||
914		    ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
915		    (vnd->sc_dkdev.dk_copenmask & pmask))) {
916			vndunlock(vnd);
917			return (EBUSY);
918		}
919
920		vndclear(vnd);
921#ifdef DEBUG
922		if (vnddebug & VDB_INIT)
923			printf("vndioctl: CLRed\n");
924#endif
925
926		/* Destroy the xfer and buffer pools. */
927		pool_destroy(&vnd->sc_vxpool);
928		pool_destroy(&vnd->sc_vbpool);
929
930		/* Detatch the disk. */
931		disk_detach(&vnd->sc_dkdev);
932
933		vndunlock(vnd);
934
935		break;
936
937	case VNDIOCGET: {
938		struct vnd_user *vnu;
939		struct vattr va;
940
941		vnu = (struct vnd_user *)data;
942
943		if (vnu->vnu_unit == -1)
944			vnu->vnu_unit = unit;
945		if (vnu->vnu_unit >= numvnd)
946			return (ENXIO);
947		if (vnu->vnu_unit < 0)
948			return (EINVAL);
949
950		vnd = &vnd_softc[vnu->vnu_unit];
951
952		if (vnd->sc_flags & VNF_INITED) {
953			error = VOP_GETATTR(vnd->sc_vp, &va, p->p_ucred, p);
954			if (error)
955				return (error);
956			vnu->vnu_dev = va.va_fsid;
957			vnu->vnu_ino = va.va_fileid;
958		}
959		else {
960			/* unused is not an error */
961			vnu->vnu_dev = 0;
962			vnu->vnu_ino = 0;
963		}
964
965		break;
966	}
967
968	case DIOCGDINFO:
969		*(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
970		break;
971
972#ifdef __HAVE_OLD_DISKLABEL
973	case ODIOCGDINFO:
974		newlabel = *(vnd->sc_dkdev.dk_label);
975		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
976			return ENOTTY;
977		memcpy(data, &newlabel, sizeof (struct olddisklabel));
978		break;
979#endif
980
981	case DIOCGPART:
982		((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
983		((struct partinfo *)data)->part =
984		    &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
985		break;
986
987	case DIOCWDINFO:
988	case DIOCSDINFO:
989#ifdef __HAVE_OLD_DISKLABEL
990	case ODIOCWDINFO:
991	case ODIOCSDINFO:
992#endif
993	{
994		struct disklabel *lp;
995
996		if ((error = vndlock(vnd)) != 0)
997			return (error);
998
999		vnd->sc_flags |= VNF_LABELLING;
1000
1001#ifdef __HAVE_OLD_DISKLABEL
1002		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1003			memset(&newlabel, 0, sizeof newlabel);
1004			memcpy(&newlabel, data, sizeof (struct olddisklabel));
1005			lp = &newlabel;
1006		} else
1007#endif
1008		lp = (struct disklabel *)data;
1009
1010		error = setdisklabel(vnd->sc_dkdev.dk_label,
1011		    lp, 0, vnd->sc_dkdev.dk_cpulabel);
1012		if (error == 0) {
1013			if (cmd == DIOCWDINFO
1014#ifdef __HAVE_OLD_DISKLABEL
1015			    || cmd == ODIOCWDINFO
1016#endif
1017			   )
1018				error = writedisklabel(VNDLABELDEV(dev),
1019				    vndstrategy, vnd->sc_dkdev.dk_label,
1020				    vnd->sc_dkdev.dk_cpulabel);
1021		}
1022
1023		vnd->sc_flags &= ~VNF_LABELLING;
1024
1025		vndunlock(vnd);
1026
1027		if (error)
1028			return (error);
1029		break;
1030	}
1031
1032	case DIOCWLABEL:
1033		if (*(int *)data != 0)
1034			vnd->sc_flags |= VNF_WLABEL;
1035		else
1036			vnd->sc_flags &= ~VNF_WLABEL;
1037		break;
1038
1039	case DIOCGDEFLABEL:
1040		vndgetdefaultlabel(vnd, (struct disklabel *)data);
1041		break;
1042
1043#ifdef __HAVE_OLD_DISKLABEL
1044	case ODIOCGDEFLABEL:
1045		vndgetdefaultlabel(vnd, &newlabel);
1046		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1047			return ENOTTY;
1048		memcpy(data, &newlabel, sizeof (struct olddisklabel));
1049		break;
1050#endif
1051
1052	default:
1053		return (ENOTTY);
1054	}
1055
1056	return (0);
1057}
1058
1059/*
1060 * Duplicate the current processes' credentials.  Since we are called only
1061 * as the result of a SET ioctl and only root can do that, any future access
1062 * to this "disk" is essentially as root.  Note that credentials may change
1063 * if some other uid can write directly to the mapped file (NFS).
1064 */
1065int
1066vndsetcred(vnd, cred)
1067	struct vnd_softc *vnd;
1068	struct ucred *cred;
1069{
1070	struct uio auio;
1071	struct iovec aiov;
1072	char *tmpbuf;
1073	int error;
1074
1075	vnd->sc_cred = crdup(cred);
1076	tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1077
1078	/* XXX: Horrible kludge to establish credentials for NFS */
1079	aiov.iov_base = tmpbuf;
1080	aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
1081	auio.uio_iov = &aiov;
1082	auio.uio_iovcnt = 1;
1083	auio.uio_offset = 0;
1084	auio.uio_rw = UIO_READ;
1085	auio.uio_segflg = UIO_SYSSPACE;
1086	auio.uio_resid = aiov.iov_len;
1087	vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1088	error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
1089	if (error == 0) {
1090		/*
1091		 * Because vnd does all IO directly through the vnode
1092		 * we need to flush (at least) the buffer from the above
1093		 * VOP_READ from the buffer cache to prevent cache
1094		 * incoherencies.  Also, be careful to write dirty
1095		 * buffers back to stable storage.
1096		 */
1097		error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
1098			    curproc, 0, 0);
1099	}
1100	VOP_UNLOCK(vnd->sc_vp, 0);
1101
1102	free(tmpbuf, M_TEMP);
1103	return (error);
1104}
1105
1106/*
1107 * Set maxactive based on FS type
1108 */
1109void
1110vndthrottle(vnd, vp)
1111	struct vnd_softc *vnd;
1112	struct vnode *vp;
1113{
1114#ifdef NFS
1115	extern int (**nfsv2_vnodeop_p) __P((void *));
1116
1117	if (vp->v_op == nfsv2_vnodeop_p)
1118		vnd->sc_maxactive = 2;
1119	else
1120#endif
1121		vnd->sc_maxactive = 8;
1122
1123	if (vnd->sc_maxactive < 1)
1124		vnd->sc_maxactive = 1;
1125}
1126
1127void
1128vndshutdown()
1129{
1130	struct vnd_softc *vnd;
1131
1132	for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1133		if (vnd->sc_flags & VNF_INITED)
1134			vndclear(vnd);
1135}
1136
1137void
1138vndclear(vnd)
1139	struct vnd_softc *vnd;
1140{
1141	struct vnode *vp = vnd->sc_vp;
1142	struct proc *p = curproc;		/* XXX */
1143
1144#ifdef DEBUG
1145	if (vnddebug & VDB_FOLLOW)
1146		printf("vndclear(%p): vp %p\n", vnd, vp);
1147#endif
1148	vnd->sc_flags &= ~VNF_INITED;
1149	if (vp == (struct vnode *)0)
1150		panic("vndioctl: null vp");
1151	(void) vn_close(vp, FREAD|FWRITE, vnd->sc_cred, p);
1152	crfree(vnd->sc_cred);
1153	vnd->sc_vp = (struct vnode *)0;
1154	vnd->sc_cred = (struct ucred *)0;
1155	vnd->sc_size = 0;
1156}
1157
1158int
1159vndsize(dev)
1160	dev_t dev;
1161{
1162	struct vnd_softc *sc;
1163	struct disklabel *lp;
1164	int part, unit, omask;
1165	int size;
1166
1167	unit = vndunit(dev);
1168	if (unit >= numvnd)
1169		return (-1);
1170	sc = &vnd_softc[unit];
1171
1172	if ((sc->sc_flags & VNF_INITED) == 0)
1173		return (-1);
1174
1175	part = DISKPART(dev);
1176	omask = sc->sc_dkdev.dk_openmask & (1 << part);
1177	lp = sc->sc_dkdev.dk_label;
1178
1179	if (omask == 0 && vndopen(dev, 0, S_IFBLK, curproc))
1180		return (-1);
1181
1182	if (lp->d_partitions[part].p_fstype != FS_SWAP)
1183		size = -1;
1184	else
1185		size = lp->d_partitions[part].p_size *
1186		    (lp->d_secsize / DEV_BSIZE);
1187
1188	if (omask == 0 && vndclose(dev, 0, S_IFBLK, curproc))
1189		return (-1);
1190
1191	return (size);
1192}
1193
1194int
1195vnddump(dev, blkno, va, size)
1196	dev_t dev;
1197	daddr_t blkno;
1198	caddr_t va;
1199	size_t size;
1200{
1201
1202	/* Not implemented. */
1203	return ENXIO;
1204}
1205
1206void
1207vndgetdefaultlabel(sc, lp)
1208	struct vnd_softc *sc;
1209	struct disklabel *lp;
1210{
1211	struct vndgeom *vng = &sc->sc_geom;
1212	struct partition *pp;
1213
1214	memset(lp, 0, sizeof(*lp));
1215
1216	lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
1217	lp->d_secsize = vng->vng_secsize;
1218	lp->d_nsectors = vng->vng_nsectors;
1219	lp->d_ntracks = vng->vng_ntracks;
1220	lp->d_ncylinders = vng->vng_ncylinders;
1221	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1222
1223	strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1224	lp->d_type = DTYPE_VND;
1225	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1226	lp->d_rpm = 3600;
1227	lp->d_interleave = 1;
1228	lp->d_flags = 0;
1229
1230	pp = &lp->d_partitions[RAW_PART];
1231	pp->p_offset = 0;
1232	pp->p_size = lp->d_secperunit;
1233	pp->p_fstype = FS_UNUSED;
1234	lp->d_npartitions = RAW_PART + 1;
1235
1236	lp->d_magic = DISKMAGIC;
1237	lp->d_magic2 = DISKMAGIC;
1238	lp->d_checksum = dkcksum(lp);
1239}
1240
1241/*
1242 * Read the disklabel from a vnd.  If one is not present, create a fake one.
1243 */
1244void
1245vndgetdisklabel(dev)
1246	dev_t dev;
1247{
1248	struct vnd_softc *sc = &vnd_softc[vndunit(dev)];
1249	char *errstring;
1250	struct disklabel *lp = sc->sc_dkdev.dk_label;
1251	struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1252	int i;
1253
1254	memset(clp, 0, sizeof(*clp));
1255
1256	vndgetdefaultlabel(sc, lp);
1257
1258	/*
1259	 * Call the generic disklabel extraction routine.
1260	 */
1261	errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1262	if (errstring) {
1263		/*
1264		 * Lack of disklabel is common, but we print the warning
1265		 * anyway, since it might contain other useful information.
1266		 */
1267		printf("%s: %s\n", sc->sc_xname, errstring);
1268
1269		/*
1270		 * For historical reasons, if there's no disklabel
1271		 * present, all partitions must be FS_BSDFFS and
1272		 * occupy the entire disk.
1273		 */
1274		for (i = 0; i < MAXPARTITIONS; i++) {
1275			/*
1276			 * Don't wipe out port specific hack (such as
1277			 * dos partition hack of i386 port).
1278			 */
1279			if (lp->d_partitions[i].p_fstype != FS_UNUSED)
1280				continue;
1281
1282			lp->d_partitions[i].p_size = lp->d_secperunit;
1283			lp->d_partitions[i].p_offset = 0;
1284			lp->d_partitions[i].p_fstype = FS_BSDFFS;
1285		}
1286
1287		strncpy(lp->d_packname, "default label",
1288		    sizeof(lp->d_packname));
1289
1290		lp->d_checksum = dkcksum(lp);
1291	}
1292}
1293
1294/*
1295 * Wait interruptibly for an exclusive lock.
1296 *
1297 * XXX
1298 * Several drivers do this; it should be abstracted and made MP-safe.
1299 */
1300static int
1301vndlock(sc)
1302	struct vnd_softc *sc;
1303{
1304	int error;
1305
1306	while ((sc->sc_flags & VNF_LOCKED) != 0) {
1307		sc->sc_flags |= VNF_WANTED;
1308		if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1309			return (error);
1310	}
1311	sc->sc_flags |= VNF_LOCKED;
1312	return (0);
1313}
1314
1315/*
1316 * Unlock and wake up any waiters.
1317 */
1318static void
1319vndunlock(sc)
1320	struct vnd_softc *sc;
1321{
1322
1323	sc->sc_flags &= ~VNF_LOCKED;
1324	if ((sc->sc_flags & VNF_WANTED) != 0) {
1325		sc->sc_flags &= ~VNF_WANTED;
1326		wakeup(sc);
1327	}
1328}
1329