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