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