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