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