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