vnd.c revision 1.112 1 /* $NetBSD: vnd.c,v 1.112 2005/03/30 19:23:08 bouyer 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) 1990, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * the Systems Programming Group of the University of Utah Computer
45 * Science Department.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * from: Utah $Hdr: vn.c 1.13 94/04/02$
72 *
73 * @(#)vn.c 8.9 (Berkeley) 5/14/95
74 */
75
76 /*
77 * Copyright (c) 1988 University of Utah.
78 *
79 * This code is derived from software contributed to Berkeley by
80 * the Systems Programming Group of the University of Utah Computer
81 * Science Department.
82 *
83 * Redistribution and use in source and binary forms, with or without
84 * modification, are permitted provided that the following conditions
85 * are met:
86 * 1. Redistributions of source code must retain the above copyright
87 * notice, this list of conditions and the following disclaimer.
88 * 2. Redistributions in binary form must reproduce the above copyright
89 * notice, this list of conditions and the following disclaimer in the
90 * documentation and/or other materials provided with the distribution.
91 * 3. All advertising materials mentioning features or use of this software
92 * must display the following acknowledgement:
93 * This product includes software developed by the University of
94 * California, Berkeley and its contributors.
95 * 4. Neither the name of the University nor the names of its contributors
96 * may be used to endorse or promote products derived from this software
97 * without specific prior written permission.
98 *
99 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
100 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
102 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
103 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
104 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
105 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
106 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
107 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
108 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
109 * SUCH DAMAGE.
110 *
111 * from: Utah $Hdr: vn.c 1.13 94/04/02$
112 *
113 * @(#)vn.c 8.9 (Berkeley) 5/14/95
114 */
115
116 /*
117 * Vnode disk driver.
118 *
119 * Block/character interface to a vnode. Allows one to treat a file
120 * as a disk (e.g. build a filesystem in it, mount it, etc.).
121 *
122 * NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode
123 * instead of a simple VOP_RDWR. We do this to avoid distorting the
124 * local buffer cache.
125 *
126 * NOTE 2: There is a security issue involved with this driver.
127 * Once mounted all access to the contents of the "mapped" file via
128 * the special file is controlled by the permissions on the special
129 * file, the protection of the mapped file is ignored (effectively,
130 * by using root credentials in all transactions).
131 *
132 * NOTE 3: Doesn't interact with leases, should it?
133 */
134
135 #include <sys/cdefs.h>
136 __KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.112 2005/03/30 19:23:08 bouyer Exp $");
137
138 #if defined(_KERNEL_OPT)
139 #include "fs_nfs.h"
140 #endif
141
142 #include <sys/param.h>
143 #include <sys/systm.h>
144 #include <sys/namei.h>
145 #include <sys/proc.h>
146 #include <sys/kthread.h>
147 #include <sys/errno.h>
148 #include <sys/buf.h>
149 #include <sys/bufq.h>
150 #include <sys/malloc.h>
151 #include <sys/ioctl.h>
152 #include <sys/disklabel.h>
153 #include <sys/device.h>
154 #include <sys/disk.h>
155 #include <sys/stat.h>
156 #include <sys/mount.h>
157 #include <sys/vnode.h>
158 #include <sys/file.h>
159 #include <sys/uio.h>
160 #include <sys/conf.h>
161
162 #include <miscfs/specfs/specdev.h>
163
164 #include <dev/vndvar.h>
165
166 #if defined(VNDDEBUG) && !defined(DEBUG)
167 #define DEBUG
168 #endif
169
170 #ifdef DEBUG
171 int dovndcluster = 1;
172 #define VDB_FOLLOW 0x01
173 #define VDB_INIT 0x02
174 #define VDB_IO 0x04
175 #define VDB_LABEL 0x08
176 int vnddebug = 0x00;
177 #endif
178
179 #define vndunit(x) DISKUNIT(x)
180
181 struct vndxfer {
182 struct buf *vx_bp; /* Pointer to parent buffer */
183 int vx_error;
184 int vx_pending; /* # of pending aux buffers */
185 int vx_flags;
186 #define VX_BUSY 1
187 };
188
189 struct vndbuf {
190 struct buf vb_buf;
191 struct vndxfer *vb_xfer;
192 };
193
194 #define VND_GETXFER(vnd) pool_get(&(vnd)->sc_vxpool, PR_WAITOK)
195 #define VND_PUTXFER(vnd, vx) pool_put(&(vnd)->sc_vxpool, (vx))
196
197 #define VND_GETBUF(vnd) pool_get(&(vnd)->sc_vbpool, PR_WAITOK)
198 #define VND_PUTBUF(vnd, vb) pool_put(&(vnd)->sc_vbpool, (vb))
199
200 struct vnd_softc *vnd_softc;
201 int numvnd = 0;
202
203 #define VNDLABELDEV(dev) \
204 (MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
205
206 /* called by main() at boot time (XXX: and the LKM driver) */
207 void vndattach(int);
208 int vnddetach(void);
209
210 static void vndclear(struct vnd_softc *, int);
211 static int vndsetcred(struct vnd_softc *, struct ucred *);
212 static void vndthrottle(struct vnd_softc *, struct vnode *);
213 static void vndiodone(struct buf *);
214 #if 0
215 static void vndshutdown(void);
216 #endif
217
218 static void vndgetdefaultlabel(struct vnd_softc *, struct disklabel *);
219 static void vndgetdisklabel(dev_t);
220
221 static int vndlock(struct vnd_softc *);
222 static void vndunlock(struct vnd_softc *);
223
224 void vndthread(void *);
225
226 static dev_type_open(vndopen);
227 static dev_type_close(vndclose);
228 static dev_type_read(vndread);
229 static dev_type_write(vndwrite);
230 static dev_type_ioctl(vndioctl);
231 static dev_type_strategy(vndstrategy);
232 static dev_type_dump(vnddump);
233 static dev_type_size(vndsize);
234
235 const struct bdevsw vnd_bdevsw = {
236 vndopen, vndclose, vndstrategy, vndioctl, vnddump, vndsize, D_DISK
237 };
238
239 const struct cdevsw vnd_cdevsw = {
240 vndopen, vndclose, vndread, vndwrite, vndioctl,
241 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
242 };
243
244 static int vndattached;
245
246 void
247 vndattach(int num)
248 {
249 int i;
250 char *mem;
251
252 if (vndattached)
253 return;
254 vndattached = 1;
255 if (num <= 0)
256 return;
257 i = num * sizeof(struct vnd_softc);
258 mem = malloc(i, M_DEVBUF, M_NOWAIT|M_ZERO);
259 if (mem == NULL) {
260 printf("WARNING: no memory for vnode disks\n");
261 return;
262 }
263 vnd_softc = (struct vnd_softc *)mem;
264 numvnd = num;
265
266 for (i = 0; i < numvnd; i++) {
267 vnd_softc[i].sc_unit = i;
268 bufq_alloc(&vnd_softc[i].sc_tab,
269 BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
270 }
271 }
272
273 int
274 vnddetach(void)
275 {
276 int i;
277
278 /* First check we aren't in use. */
279 for (i = 0; i < numvnd; i++)
280 if (vnd_softc[i].sc_flags & VNF_INITED)
281 return (EBUSY);
282
283 for (i = 0; i < numvnd; i++)
284 bufq_free(&vnd_softc[i].sc_tab);
285
286 free(vnd_softc, M_DEVBUF);
287 vndattached = 0;
288
289 return (0);
290 }
291
292 static int
293 vndopen(dev_t dev, int flags, int mode, struct proc *p)
294 {
295 int unit = vndunit(dev);
296 struct vnd_softc *sc;
297 int error = 0, part, pmask;
298 struct disklabel *lp;
299
300 #ifdef DEBUG
301 if (vnddebug & VDB_FOLLOW)
302 printf("vndopen(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, p);
303 #endif
304 if (unit >= numvnd)
305 return (ENXIO);
306 sc = &vnd_softc[unit];
307
308 if ((error = vndlock(sc)) != 0)
309 return (error);
310
311 lp = sc->sc_dkdev.dk_label;
312
313 part = DISKPART(dev);
314 pmask = (1 << part);
315
316 /*
317 * If we're initialized, check to see if there are any other
318 * open partitions. If not, then it's safe to update the
319 * in-core disklabel. Only read the disklabel if it is
320 * not realdy valid.
321 */
322 if ((sc->sc_flags & (VNF_INITED|VNF_VLABEL)) == VNF_INITED &&
323 sc->sc_dkdev.dk_openmask == 0)
324 vndgetdisklabel(dev);
325
326 /* Check that the partitions exists. */
327 if (part != RAW_PART) {
328 if (((sc->sc_flags & VNF_INITED) == 0) ||
329 ((part >= lp->d_npartitions) ||
330 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
331 error = ENXIO;
332 goto done;
333 }
334 }
335
336 /* Prevent our unit from being unconfigured while open. */
337 switch (mode) {
338 case S_IFCHR:
339 sc->sc_dkdev.dk_copenmask |= pmask;
340 break;
341
342 case S_IFBLK:
343 sc->sc_dkdev.dk_bopenmask |= pmask;
344 break;
345 }
346 sc->sc_dkdev.dk_openmask =
347 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
348
349 done:
350 vndunlock(sc);
351 return (error);
352 }
353
354 static int
355 vndclose(dev_t dev, int flags, int mode, struct proc *p)
356 {
357 int unit = vndunit(dev);
358 struct vnd_softc *sc;
359 int error = 0, part;
360
361 #ifdef DEBUG
362 if (vnddebug & VDB_FOLLOW)
363 printf("vndclose(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, p);
364 #endif
365
366 if (unit >= numvnd)
367 return (ENXIO);
368 sc = &vnd_softc[unit];
369
370 if ((error = vndlock(sc)) != 0)
371 return (error);
372
373 part = DISKPART(dev);
374
375 /* ...that much closer to allowing unconfiguration... */
376 switch (mode) {
377 case S_IFCHR:
378 sc->sc_dkdev.dk_copenmask &= ~(1 << part);
379 break;
380
381 case S_IFBLK:
382 sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
383 break;
384 }
385 sc->sc_dkdev.dk_openmask =
386 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
387
388 if (sc->sc_dkdev.dk_openmask == 0) {
389 if ((sc->sc_flags & VNF_KLABEL) == 0)
390 sc->sc_flags &= ~VNF_VLABEL;
391 }
392
393 vndunlock(sc);
394 return (0);
395 }
396
397 /*
398 * Qeue the request, and wakeup the kernel thread to handle it.
399 */
400 static void
401 vndstrategy(struct buf *bp)
402 {
403 int unit = vndunit(bp->b_dev);
404 struct vnd_softc *vnd = &vnd_softc[unit];
405 struct disklabel *lp = vnd->sc_dkdev.dk_label;
406 int s = splbio();
407
408 bp->b_resid = bp->b_bcount;
409
410 if ((vnd->sc_flags & VNF_INITED) == 0) {
411 bp->b_error = ENXIO;
412 bp->b_flags |= B_ERROR;
413 goto done;
414 }
415
416 /*
417 * The transfer must be a whole number of blocks.
418 */
419 if ((bp->b_bcount % lp->d_secsize) != 0) {
420 bp->b_error = EINVAL;
421 bp->b_flags |= B_ERROR;
422 goto done;
423 }
424
425 /*
426 * check if we're read-only.
427 */
428 if ((vnd->sc_flags & VNF_READONLY) && !(bp->b_flags & B_READ)) {
429 bp->b_error = EACCES;
430 bp->b_flags |= B_ERROR;
431 goto done;
432 }
433
434 /*
435 * Do bounds checking and adjust transfer. If there's an error,
436 * the bounds check will flag that for us.
437 */
438 if (DISKPART(bp->b_dev) != RAW_PART) {
439 if (bounds_check_with_label(&vnd->sc_dkdev,
440 bp, vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING)) <= 0)
441 goto done;
442 }
443
444 /* If it's a nil transfer, wake up the top half now. */
445 if (bp->b_bcount == 0)
446 goto done;
447 #ifdef DEBUG
448 if (vnddebug & VDB_FOLLOW)
449 printf("vndstrategy(%p): unit %d\n", bp, unit);
450 #endif
451 BUFQ_PUT(&vnd->sc_tab, bp);
452 wakeup(&vnd->sc_tab);
453 splx(s);
454 return;
455 done:
456 biodone(bp);
457 splx(s);
458 }
459
460 void
461 vndthread(void *arg)
462 {
463 struct vnd_softc *vnd = arg;
464 struct buf *bp;
465 struct vndxfer *vnx;
466 struct mount *mp;
467 int s, bsize, resid;
468 off_t bn;
469 caddr_t addr;
470 int sz, flags, error;
471 struct disklabel *lp;
472 struct partition *pp;
473
474 s = splbio();
475 vnd->sc_flags |= VNF_KTHREAD;
476 wakeup(&vnd->sc_kthread);
477
478 /*
479 * Dequeue requests, break them into bsize pieces and submit using
480 * VOP_BMAP/VOP_STRATEGY.
481 */
482 while ((vnd->sc_flags & VNF_VUNCONF) == 0) {
483 bp = BUFQ_GET(&vnd->sc_tab);
484 if (bp == NULL) {
485 tsleep(&vnd->sc_tab, PRIBIO, "vndbp", 0);
486 continue;
487 };
488 splx(s);
489
490 #ifdef DEBUG
491 if (vnddebug & VDB_FOLLOW)
492 printf("vndthread(%p\n", bp);
493 #endif
494 lp = vnd->sc_dkdev.dk_label;
495 bp->b_resid = bp->b_bcount;
496
497 /*
498 * Put the block number in terms of the logical blocksize
499 * of the "device".
500 */
501 bn = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
502
503 /*
504 * Translate the partition-relative block number to an absolute.
505 */
506 if (DISKPART(bp->b_dev) != RAW_PART) {
507 pp = &vnd->sc_dkdev.dk_label->d_partitions[
508 DISKPART(bp->b_dev)];
509 bn += pp->p_offset;
510 }
511
512 /* ...and convert to a byte offset within the file. */
513 bn *= lp->d_secsize;
514
515 if (vnd->sc_vp->v_mount == NULL) {
516 bp->b_error = ENXIO;
517 bp->b_flags |= B_ERROR;
518 goto done;
519 }
520 bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
521 addr = bp->b_data;
522 flags = (bp->b_flags & (B_READ|B_ASYNC)) | B_CALL;
523
524 /*
525 * Allocate a header for this transfer and link it to the
526 * buffer
527 */
528 s = splbio();
529 vnx = VND_GETXFER(vnd);
530 splx(s);
531 vnx->vx_flags = VX_BUSY;
532 vnx->vx_error = 0;
533 vnx->vx_pending = 0;
534 vnx->vx_bp = bp;
535
536 if ((flags & B_READ) == 0)
537 vn_start_write(vnd->sc_vp, &mp, V_WAIT);
538
539 /*
540 * Feed requests sequentially.
541 * We do it this way to keep from flooding NFS servers if we
542 * are connected to an NFS file. This places the burden on
543 * the client rather than the server.
544 */
545 for (resid = bp->b_resid; resid; resid -= sz) {
546 struct vndbuf *nbp;
547 struct vnode *vp;
548 daddr_t nbn;
549 int off, nra;
550
551 nra = 0;
552 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
553 error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
554 VOP_UNLOCK(vnd->sc_vp, 0);
555
556 if (error == 0 && (long)nbn == -1)
557 error = EIO;
558
559 /*
560 * If there was an error or a hole in the file...punt.
561 * Note that we may have to wait for any operations
562 * that we have already fired off before releasing
563 * the buffer.
564 *
565 * XXX we could deal with holes here but it would be
566 * a hassle (in the write case).
567 */
568 if (error) {
569 s = splbio();
570 vnx->vx_error = error;
571 goto out;
572 }
573
574 #ifdef DEBUG
575 if (!dovndcluster)
576 nra = 0;
577 #endif
578
579 if ((off = bn % bsize) != 0)
580 sz = bsize - off;
581 else
582 sz = (1 + nra) * bsize;
583 if (resid < sz)
584 sz = resid;
585 #ifdef DEBUG
586 if (vnddebug & VDB_IO)
587 printf("vndstrategy: vp %p/%p bn 0x%qx/0x%" PRIx64
588 " sz 0x%x\n",
589 vnd->sc_vp, vp, (long long)bn, nbn, sz);
590 #endif
591
592 s = splbio();
593 while (vnd->sc_active >= vnd->sc_maxactive) {
594 tsleep(&vnd->sc_tab, PRIBIO, "vndac", 0);
595 if (vnd->sc_flags & VNF_VUNCONF)
596 goto kthread_end;
597 }
598 vnd->sc_active++;
599 nbp = VND_GETBUF(vnd);
600 splx(s);
601 BUF_INIT(&nbp->vb_buf);
602 nbp->vb_buf.b_flags = flags;
603 nbp->vb_buf.b_bcount = sz;
604 nbp->vb_buf.b_bufsize = round_page((ulong)addr + sz)
605 - trunc_page((ulong) addr);
606 nbp->vb_buf.b_error = 0;
607 nbp->vb_buf.b_data = addr;
608 nbp->vb_buf.b_blkno = nbp->vb_buf.b_rawblkno = nbn + btodb(off);
609 nbp->vb_buf.b_proc = bp->b_proc;
610 nbp->vb_buf.b_iodone = vndiodone;
611 nbp->vb_buf.b_vp = vp;
612
613 nbp->vb_xfer = vnx;
614
615 BIO_COPYPRIO(&nbp->vb_buf, bp);
616
617 /*
618 * Just sort by block number
619 */
620 s = splbio();
621 if (vnx->vx_error != 0) {
622 VND_PUTBUF(vnd, nbp);
623 goto out;
624 }
625 vnx->vx_pending++;
626 #ifdef DEBUG
627 if (vnddebug & VDB_IO)
628 printf("vndstart(%ld): bp %p vp %p blkno "
629 "0x%" PRIx64 " flags %x addr %p cnt 0x%x\n",
630 (long) (vnd-vnd_softc), &nbp->vb_buf,
631 nbp->vb_buf.b_vp, nbp->vb_buf.b_blkno,
632 nbp->vb_buf.b_flags, nbp->vb_buf.b_data,
633 nbp->vb_buf.b_bcount);
634 #endif
635
636 /* Instrumentation. */
637 disk_busy(&vnd->sc_dkdev);
638
639 if ((nbp->vb_buf.b_flags & B_READ) == 0)
640 vp->v_numoutput++;
641 VOP_STRATEGY(vp, &nbp->vb_buf);
642
643 splx(s);
644 bn += sz;
645 addr += sz;
646 }
647
648 s = splbio();
649
650 out: /* Arrive here at splbio */
651 if ((flags & B_READ) == 0)
652 vn_finished_write(mp, 0);
653 vnx->vx_flags &= ~VX_BUSY;
654 if (vnx->vx_pending == 0) {
655 if (vnx->vx_error != 0) {
656 bp->b_error = vnx->vx_error;
657 bp->b_flags |= B_ERROR;
658 }
659 VND_PUTXFER(vnd, vnx);
660 biodone(bp);
661 }
662 continue;
663 done:
664 biodone(bp);
665 s = splbio();
666 continue;
667 }
668
669 kthread_end:
670 vnd->sc_flags &= (~VNF_KTHREAD | VNF_VUNCONF);
671 wakeup(&vnd->sc_kthread);
672 splx(s);
673 kthread_exit(0);
674 }
675
676
677 static void
678 vndiodone(struct buf *bp)
679 {
680 struct vndbuf *vbp = (struct vndbuf *) bp;
681 struct vndxfer *vnx = (struct vndxfer *)vbp->vb_xfer;
682 struct buf *pbp = vnx->vx_bp;
683 struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
684 int s, resid;
685
686 s = splbio();
687 #ifdef DEBUG
688 if (vnddebug & VDB_IO)
689 printf("vndiodone(%ld): vbp %p vp %p blkno 0x%" PRIx64
690 " addr %p cnt 0x%x\n",
691 (long) (vnd-vnd_softc), vbp, vbp->vb_buf.b_vp,
692 vbp->vb_buf.b_blkno, vbp->vb_buf.b_data,
693 vbp->vb_buf.b_bcount);
694 #endif
695
696 resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
697 pbp->b_resid -= resid;
698 disk_unbusy(&vnd->sc_dkdev, resid, (pbp->b_flags & B_READ));
699 vnx->vx_pending--;
700
701 if (vbp->vb_buf.b_error) {
702 #ifdef DEBUG
703 if (vnddebug & VDB_IO)
704 printf("vndiodone: vbp %p error %d\n", vbp,
705 vbp->vb_buf.b_error);
706 #endif
707 vnx->vx_error = vbp->vb_buf.b_error;
708 }
709
710 VND_PUTBUF(vnd, vbp);
711
712 /*
713 * Wrap up this transaction if it has run to completion or, in
714 * case of an error, when all auxiliary buffers have returned.
715 */
716 if (vnx->vx_error != 0) {
717 pbp->b_flags |= B_ERROR;
718 pbp->b_error = vnx->vx_error;
719 if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
720
721 #ifdef DEBUG
722 if (vnddebug & VDB_IO)
723 printf("vndiodone: pbp %p iodone: error %d\n",
724 pbp, vnx->vx_error);
725 #endif
726 VND_PUTXFER(vnd, vnx);
727 biodone(pbp);
728 }
729 } else if (pbp->b_resid == 0) {
730
731 #ifdef DIAGNOSTIC
732 if (vnx->vx_pending != 0)
733 panic("vndiodone: vnx pending: %d", vnx->vx_pending);
734 #endif
735
736 if ((vnx->vx_flags & VX_BUSY) == 0) {
737 #ifdef DEBUG
738 if (vnddebug & VDB_IO)
739 printf("vndiodone: pbp %p iodone\n", pbp);
740 #endif
741 VND_PUTXFER(vnd, vnx);
742 biodone(pbp);
743 }
744 }
745
746 vnd->sc_active--;
747 wakeup(&vnd->sc_tab);
748 splx(s);
749 }
750
751 /* ARGSUSED */
752 static int
753 vndread(dev_t dev, struct uio *uio, int flags)
754 {
755 int unit = vndunit(dev);
756 struct vnd_softc *sc;
757
758 #ifdef DEBUG
759 if (vnddebug & VDB_FOLLOW)
760 printf("vndread(0x%x, %p)\n", dev, uio);
761 #endif
762
763 if (unit >= numvnd)
764 return (ENXIO);
765 sc = &vnd_softc[unit];
766
767 if ((sc->sc_flags & VNF_INITED) == 0)
768 return (ENXIO);
769
770 return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
771 }
772
773 /* ARGSUSED */
774 static int
775 vndwrite(dev_t dev, struct uio *uio, int flags)
776 {
777 int unit = vndunit(dev);
778 struct vnd_softc *sc;
779
780 #ifdef DEBUG
781 if (vnddebug & VDB_FOLLOW)
782 printf("vndwrite(0x%x, %p)\n", dev, uio);
783 #endif
784
785 if (unit >= numvnd)
786 return (ENXIO);
787 sc = &vnd_softc[unit];
788
789 if ((sc->sc_flags & VNF_INITED) == 0)
790 return (ENXIO);
791
792 return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
793 }
794
795 /* ARGSUSED */
796 static int
797 vndioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
798 {
799 int unit = vndunit(dev);
800 struct vnd_softc *vnd;
801 struct vnd_ioctl *vio;
802 struct vattr vattr;
803 struct nameidata nd;
804 int error, part, pmask;
805 size_t geomsize;
806 int fflags;
807 #ifdef __HAVE_OLD_DISKLABEL
808 struct disklabel newlabel;
809 #endif
810
811 #ifdef DEBUG
812 if (vnddebug & VDB_FOLLOW)
813 printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
814 dev, cmd, data, flag, p, unit);
815 #endif
816 if (unit >= numvnd)
817 return (ENXIO);
818
819 vnd = &vnd_softc[unit];
820 vio = (struct vnd_ioctl *)data;
821
822 /* Must be open for writes for these commands... */
823 switch (cmd) {
824 case VNDIOCSET:
825 case VNDIOCCLR:
826 case DIOCSDINFO:
827 case DIOCWDINFO:
828 #ifdef __HAVE_OLD_DISKLABEL
829 case ODIOCSDINFO:
830 case ODIOCWDINFO:
831 #endif
832 case DIOCKLABEL:
833 case DIOCWLABEL:
834 if ((flag & FWRITE) == 0)
835 return (EBADF);
836 }
837
838 /* Must be initialized for these... */
839 switch (cmd) {
840 case VNDIOCCLR:
841 case DIOCGDINFO:
842 case DIOCSDINFO:
843 case DIOCWDINFO:
844 case DIOCGPART:
845 case DIOCKLABEL:
846 case DIOCWLABEL:
847 case DIOCGDEFLABEL:
848 #ifdef __HAVE_OLD_DISKLABEL
849 case ODIOCGDINFO:
850 case ODIOCSDINFO:
851 case ODIOCWDINFO:
852 case ODIOCGDEFLABEL:
853 #endif
854 if ((vnd->sc_flags & VNF_INITED) == 0)
855 return (ENXIO);
856 }
857
858 switch (cmd) {
859 case VNDIOCSET:
860 if (vnd->sc_flags & VNF_INITED)
861 return (EBUSY);
862
863 if ((error = vndlock(vnd)) != 0)
864 return (error);
865
866 fflags = FREAD;
867 if ((vio->vnd_flags & VNDIOF_READONLY) == 0)
868 fflags |= FWRITE;
869 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
870 if ((error = vn_open(&nd, fflags, 0)) != 0)
871 goto unlock_and_exit;
872 error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p);
873 VOP_UNLOCK(nd.ni_vp, 0);
874 if (!error && nd.ni_vp->v_type != VREG)
875 error = EOPNOTSUPP;
876 if (error)
877 goto close_and_exit;
878 vnd->sc_vp = nd.ni_vp;
879 vnd->sc_size = btodb(vattr.va_size); /* note truncation */
880
881 /*
882 * Use pseudo-geometry specified. If none was provided,
883 * use "standard" Adaptec fictitious geometry.
884 */
885 if (vio->vnd_flags & VNDIOF_HASGEOM) {
886
887 memcpy(&vnd->sc_geom, &vio->vnd_geom,
888 sizeof(vio->vnd_geom));
889
890 /*
891 * Sanity-check the sector size.
892 * XXX Don't allow secsize < DEV_BSIZE. Should
893 * XXX we?
894 */
895 if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
896 (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0 ||
897 vnd->sc_geom.vng_ncylinders == 0 ||
898 (vnd->sc_geom.vng_ntracks *
899 vnd->sc_geom.vng_nsectors) == 0) {
900 error = EINVAL;
901 goto close_and_exit;
902 }
903
904 /*
905 * Compute the size (in DEV_BSIZE blocks) specified
906 * by the geometry.
907 */
908 geomsize = (vnd->sc_geom.vng_nsectors *
909 vnd->sc_geom.vng_ntracks *
910 vnd->sc_geom.vng_ncylinders) *
911 (vnd->sc_geom.vng_secsize / DEV_BSIZE);
912
913 /*
914 * Sanity-check the size against the specified
915 * geometry.
916 */
917 if (vnd->sc_size < geomsize) {
918 error = EINVAL;
919 goto close_and_exit;
920 }
921 } else {
922 /*
923 * Size must be at least 2048 DEV_BSIZE blocks
924 * (1M) in order to use this geometry.
925 */
926 if (vnd->sc_size < (32 * 64)) {
927 error = EINVAL;
928 goto close_and_exit;
929 }
930
931 vnd->sc_geom.vng_secsize = DEV_BSIZE;
932 vnd->sc_geom.vng_nsectors = 32;
933 vnd->sc_geom.vng_ntracks = 64;
934 vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
935 }
936
937 if (vio->vnd_flags & VNDIOF_READONLY) {
938 vnd->sc_flags |= VNF_READONLY;
939 }
940
941 if ((error = vndsetcred(vnd, p->p_ucred)) != 0)
942 goto close_and_exit;
943
944 memset(vnd->sc_xname, 0, sizeof(vnd->sc_xname)); /* XXX */
945 snprintf(vnd->sc_xname, sizeof(vnd->sc_xname), "vnd%d", unit);
946
947
948 vndthrottle(vnd, vnd->sc_vp);
949 vio->vnd_size = dbtob(vnd->sc_size);
950 vnd->sc_flags |= VNF_INITED;
951
952 /* create the kernel thread, wait for it to be up */
953 error = kthread_create1(vndthread, vnd, &vnd->sc_kthread,
954 vnd->sc_xname);
955 if (error)
956 goto close_and_exit;
957 while ((vnd->sc_flags & VNF_KTHREAD) == 0) {
958 tsleep(&vnd->sc_kthread, PRIBIO, "vndthr", 0);
959 }
960 #ifdef DEBUG
961 if (vnddebug & VDB_INIT)
962 printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
963 vnd->sc_vp, (unsigned long) vnd->sc_size,
964 vnd->sc_geom.vng_secsize,
965 vnd->sc_geom.vng_nsectors,
966 vnd->sc_geom.vng_ntracks,
967 vnd->sc_geom.vng_ncylinders);
968 #endif
969
970 /* Attach the disk. */
971 vnd->sc_dkdev.dk_name = vnd->sc_xname;
972 disk_attach(&vnd->sc_dkdev);
973
974 /* Initialize the xfer and buffer pools. */
975 pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
976 0, 0, "vndxpl", NULL);
977 pool_init(&vnd->sc_vbpool, sizeof(struct vndbuf), 0,
978 0, 0, "vndbpl", NULL);
979
980 /* Try and read the disklabel. */
981 vndgetdisklabel(dev);
982
983 vndunlock(vnd);
984
985 break;
986
987 close_and_exit:
988 (void) vn_close(nd.ni_vp, fflags, p->p_ucred, p);
989 unlock_and_exit:
990 vndunlock(vnd);
991 return (error);
992
993 case VNDIOCCLR:
994 if ((error = vndlock(vnd)) != 0)
995 return (error);
996
997 /*
998 * Don't unconfigure if any other partitions are open
999 * or if both the character and block flavors of this
1000 * partition are open.
1001 */
1002 part = DISKPART(dev);
1003 pmask = (1 << part);
1004 if (((vnd->sc_dkdev.dk_openmask & ~pmask) ||
1005 ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
1006 (vnd->sc_dkdev.dk_copenmask & pmask))) &&
1007 !(vio->vnd_flags & VNDIOF_FORCE)) {
1008 vndunlock(vnd);
1009 return (EBUSY);
1010 }
1011
1012 /*
1013 * XXX vndclear() might call vndclose() implicitely;
1014 * release lock to avoid recursion
1015 */
1016 vndunlock(vnd);
1017 vndclear(vnd, minor(dev));
1018 #ifdef DEBUG
1019 if (vnddebug & VDB_INIT)
1020 printf("vndioctl: CLRed\n");
1021 #endif
1022
1023 /* Destroy the xfer and buffer pools. */
1024 pool_destroy(&vnd->sc_vxpool);
1025 pool_destroy(&vnd->sc_vbpool);
1026
1027 /* Detatch the disk. */
1028 disk_detach(&vnd->sc_dkdev);
1029
1030 break;
1031
1032 case VNDIOCGET: {
1033 struct vnd_user *vnu;
1034 struct vattr va;
1035
1036 vnu = (struct vnd_user *)data;
1037
1038 if (vnu->vnu_unit == -1)
1039 vnu->vnu_unit = unit;
1040 if (vnu->vnu_unit >= numvnd)
1041 return (ENXIO);
1042 if (vnu->vnu_unit < 0)
1043 return (EINVAL);
1044
1045 vnd = &vnd_softc[vnu->vnu_unit];
1046
1047 if (vnd->sc_flags & VNF_INITED) {
1048 error = VOP_GETATTR(vnd->sc_vp, &va, p->p_ucred, p);
1049 if (error)
1050 return (error);
1051 vnu->vnu_dev = va.va_fsid;
1052 vnu->vnu_ino = va.va_fileid;
1053 }
1054 else {
1055 /* unused is not an error */
1056 vnu->vnu_dev = 0;
1057 vnu->vnu_ino = 0;
1058 }
1059
1060 break;
1061 }
1062
1063 case DIOCGDINFO:
1064 *(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
1065 break;
1066
1067 #ifdef __HAVE_OLD_DISKLABEL
1068 case ODIOCGDINFO:
1069 newlabel = *(vnd->sc_dkdev.dk_label);
1070 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1071 return ENOTTY;
1072 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1073 break;
1074 #endif
1075
1076 case DIOCGPART:
1077 ((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
1078 ((struct partinfo *)data)->part =
1079 &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1080 break;
1081
1082 case DIOCWDINFO:
1083 case DIOCSDINFO:
1084 #ifdef __HAVE_OLD_DISKLABEL
1085 case ODIOCWDINFO:
1086 case ODIOCSDINFO:
1087 #endif
1088 {
1089 struct disklabel *lp;
1090
1091 if ((error = vndlock(vnd)) != 0)
1092 return (error);
1093
1094 vnd->sc_flags |= VNF_LABELLING;
1095
1096 #ifdef __HAVE_OLD_DISKLABEL
1097 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1098 memset(&newlabel, 0, sizeof newlabel);
1099 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1100 lp = &newlabel;
1101 } else
1102 #endif
1103 lp = (struct disklabel *)data;
1104
1105 error = setdisklabel(vnd->sc_dkdev.dk_label,
1106 lp, 0, vnd->sc_dkdev.dk_cpulabel);
1107 if (error == 0) {
1108 if (cmd == DIOCWDINFO
1109 #ifdef __HAVE_OLD_DISKLABEL
1110 || cmd == ODIOCWDINFO
1111 #endif
1112 )
1113 error = writedisklabel(VNDLABELDEV(dev),
1114 vndstrategy, vnd->sc_dkdev.dk_label,
1115 vnd->sc_dkdev.dk_cpulabel);
1116 }
1117
1118 vnd->sc_flags &= ~VNF_LABELLING;
1119
1120 vndunlock(vnd);
1121
1122 if (error)
1123 return (error);
1124 break;
1125 }
1126
1127 case DIOCKLABEL:
1128 if (*(int *)data != 0)
1129 vnd->sc_flags |= VNF_KLABEL;
1130 else
1131 vnd->sc_flags &= ~VNF_KLABEL;
1132 break;
1133
1134 case DIOCWLABEL:
1135 if (*(int *)data != 0)
1136 vnd->sc_flags |= VNF_WLABEL;
1137 else
1138 vnd->sc_flags &= ~VNF_WLABEL;
1139 break;
1140
1141 case DIOCGDEFLABEL:
1142 vndgetdefaultlabel(vnd, (struct disklabel *)data);
1143 break;
1144
1145 #ifdef __HAVE_OLD_DISKLABEL
1146 case ODIOCGDEFLABEL:
1147 vndgetdefaultlabel(vnd, &newlabel);
1148 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1149 return ENOTTY;
1150 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1151 break;
1152 #endif
1153
1154 default:
1155 return (ENOTTY);
1156 }
1157
1158 return (0);
1159 }
1160
1161 /*
1162 * Duplicate the current processes' credentials. Since we are called only
1163 * as the result of a SET ioctl and only root can do that, any future access
1164 * to this "disk" is essentially as root. Note that credentials may change
1165 * if some other uid can write directly to the mapped file (NFS).
1166 */
1167 static int
1168 vndsetcred(struct vnd_softc *vnd, struct ucred *cred)
1169 {
1170 struct uio auio;
1171 struct iovec aiov;
1172 char *tmpbuf;
1173 int error;
1174
1175 vnd->sc_cred = crdup(cred);
1176 tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1177
1178 /* XXX: Horrible kludge to establish credentials for NFS */
1179 aiov.iov_base = tmpbuf;
1180 aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
1181 auio.uio_iov = &aiov;
1182 auio.uio_iovcnt = 1;
1183 auio.uio_offset = 0;
1184 auio.uio_rw = UIO_READ;
1185 auio.uio_segflg = UIO_SYSSPACE;
1186 auio.uio_resid = aiov.iov_len;
1187 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1188 error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
1189 if (error == 0) {
1190 /*
1191 * Because vnd does all IO directly through the vnode
1192 * we need to flush (at least) the buffer from the above
1193 * VOP_READ from the buffer cache to prevent cache
1194 * incoherencies. Also, be careful to write dirty
1195 * buffers back to stable storage.
1196 */
1197 error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
1198 curproc, 0, 0);
1199 }
1200 VOP_UNLOCK(vnd->sc_vp, 0);
1201
1202 free(tmpbuf, M_TEMP);
1203 return (error);
1204 }
1205
1206 /*
1207 * Set maxactive based on FS type
1208 */
1209 static void
1210 vndthrottle(struct vnd_softc *vnd, struct vnode *vp)
1211 {
1212 #ifdef NFS
1213 extern int (**nfsv2_vnodeop_p)(void *);
1214
1215 if (vp->v_op == nfsv2_vnodeop_p)
1216 vnd->sc_maxactive = 2;
1217 else
1218 #endif
1219 vnd->sc_maxactive = 8;
1220
1221 if (vnd->sc_maxactive < 1)
1222 vnd->sc_maxactive = 1;
1223 }
1224
1225 #if 0
1226 static void
1227 vndshutdown(void)
1228 {
1229 struct vnd_softc *vnd;
1230
1231 for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1232 if (vnd->sc_flags & VNF_INITED)
1233 vndclear(vnd);
1234 }
1235 #endif
1236
1237 static void
1238 vndclear(struct vnd_softc *vnd, int myminor)
1239 {
1240 struct vnode *vp = vnd->sc_vp;
1241 struct proc *p = curproc; /* XXX */
1242 int fflags = FREAD;
1243 int bmaj, cmaj, i, mn;
1244
1245 #ifdef DEBUG
1246 if (vnddebug & VDB_FOLLOW)
1247 printf("vndclear(%p): vp %p\n", vnd, vp);
1248 #endif
1249 /* locate the major number */
1250 bmaj = bdevsw_lookup_major(&vnd_bdevsw);
1251 cmaj = cdevsw_lookup_major(&vnd_cdevsw);
1252
1253 /* Nuke the vnodes for any open instances */
1254 for (i = 0; i < MAXPARTITIONS; i++) {
1255 mn = DISKMINOR(vnd->sc_unit, i);
1256 vdevgone(bmaj, mn, mn, VBLK);
1257 if (mn != myminor) /* XXX avoid to kill own vnode */
1258 vdevgone(cmaj, mn, mn, VCHR);
1259 }
1260
1261 if ((vnd->sc_flags & VNF_READONLY) == 0)
1262 fflags |= FWRITE;
1263
1264 vnd->sc_flags |= VNF_VUNCONF;
1265 wakeup(&vnd->sc_tab);
1266 while (vnd->sc_flags & VNF_KTHREAD)
1267 tsleep(&vnd->sc_kthread, PRIBIO, "vnthr", 0);
1268
1269 vnd->sc_flags &=
1270 ~(VNF_INITED | VNF_READONLY | VNF_VLABEL | VNF_VUNCONF);
1271 if (vp == (struct vnode *)0)
1272 panic("vndclear: null vp");
1273 (void) vn_close(vp, fflags, vnd->sc_cred, p);
1274 crfree(vnd->sc_cred);
1275 vnd->sc_vp = (struct vnode *)0;
1276 vnd->sc_cred = (struct ucred *)0;
1277 vnd->sc_size = 0;
1278 }
1279
1280 static int
1281 vndsize(dev_t dev)
1282 {
1283 struct vnd_softc *sc;
1284 struct disklabel *lp;
1285 int part, unit, omask;
1286 int size;
1287
1288 unit = vndunit(dev);
1289 if (unit >= numvnd)
1290 return (-1);
1291 sc = &vnd_softc[unit];
1292
1293 if ((sc->sc_flags & VNF_INITED) == 0)
1294 return (-1);
1295
1296 part = DISKPART(dev);
1297 omask = sc->sc_dkdev.dk_openmask & (1 << part);
1298 lp = sc->sc_dkdev.dk_label;
1299
1300 if (omask == 0 && vndopen(dev, 0, S_IFBLK, curproc))
1301 return (-1);
1302
1303 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1304 size = -1;
1305 else
1306 size = lp->d_partitions[part].p_size *
1307 (lp->d_secsize / DEV_BSIZE);
1308
1309 if (omask == 0 && vndclose(dev, 0, S_IFBLK, curproc))
1310 return (-1);
1311
1312 return (size);
1313 }
1314
1315 static int
1316 vnddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1317 {
1318
1319 /* Not implemented. */
1320 return ENXIO;
1321 }
1322
1323 static void
1324 vndgetdefaultlabel(struct vnd_softc *sc, struct disklabel *lp)
1325 {
1326 struct vndgeom *vng = &sc->sc_geom;
1327 struct partition *pp;
1328
1329 memset(lp, 0, sizeof(*lp));
1330
1331 lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
1332 lp->d_secsize = vng->vng_secsize;
1333 lp->d_nsectors = vng->vng_nsectors;
1334 lp->d_ntracks = vng->vng_ntracks;
1335 lp->d_ncylinders = vng->vng_ncylinders;
1336 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1337
1338 strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1339 lp->d_type = DTYPE_VND;
1340 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1341 lp->d_rpm = 3600;
1342 lp->d_interleave = 1;
1343 lp->d_flags = 0;
1344
1345 pp = &lp->d_partitions[RAW_PART];
1346 pp->p_offset = 0;
1347 pp->p_size = lp->d_secperunit;
1348 pp->p_fstype = FS_UNUSED;
1349 lp->d_npartitions = RAW_PART + 1;
1350
1351 lp->d_magic = DISKMAGIC;
1352 lp->d_magic2 = DISKMAGIC;
1353 lp->d_checksum = dkcksum(lp);
1354 }
1355
1356 /*
1357 * Read the disklabel from a vnd. If one is not present, create a fake one.
1358 */
1359 static void
1360 vndgetdisklabel(dev_t dev)
1361 {
1362 struct vnd_softc *sc = &vnd_softc[vndunit(dev)];
1363 const char *errstring;
1364 struct disklabel *lp = sc->sc_dkdev.dk_label;
1365 struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1366 int i;
1367
1368 memset(clp, 0, sizeof(*clp));
1369
1370 vndgetdefaultlabel(sc, lp);
1371
1372 /*
1373 * Call the generic disklabel extraction routine.
1374 */
1375 errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1376 if (errstring) {
1377 /*
1378 * Lack of disklabel is common, but we print the warning
1379 * anyway, since it might contain other useful information.
1380 */
1381 printf("%s: %s\n", sc->sc_xname, errstring);
1382
1383 /*
1384 * For historical reasons, if there's no disklabel
1385 * present, all partitions must be FS_BSDFFS and
1386 * occupy the entire disk.
1387 */
1388 for (i = 0; i < MAXPARTITIONS; i++) {
1389 /*
1390 * Don't wipe out port specific hack (such as
1391 * dos partition hack of i386 port).
1392 */
1393 if (lp->d_partitions[i].p_size != 0)
1394 continue;
1395
1396 lp->d_partitions[i].p_size = lp->d_secperunit;
1397 lp->d_partitions[i].p_offset = 0;
1398 lp->d_partitions[i].p_fstype = FS_BSDFFS;
1399 }
1400
1401 strncpy(lp->d_packname, "default label",
1402 sizeof(lp->d_packname));
1403
1404 lp->d_npartitions = MAXPARTITIONS;
1405 lp->d_checksum = dkcksum(lp);
1406 }
1407
1408 /* In-core label now valid. */
1409 sc->sc_flags |= VNF_VLABEL;
1410 }
1411
1412 /*
1413 * Wait interruptibly for an exclusive lock.
1414 *
1415 * XXX
1416 * Several drivers do this; it should be abstracted and made MP-safe.
1417 */
1418 static int
1419 vndlock(struct vnd_softc *sc)
1420 {
1421 int error;
1422
1423 while ((sc->sc_flags & VNF_LOCKED) != 0) {
1424 sc->sc_flags |= VNF_WANTED;
1425 if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1426 return (error);
1427 }
1428 sc->sc_flags |= VNF_LOCKED;
1429 return (0);
1430 }
1431
1432 /*
1433 * Unlock and wake up any waiters.
1434 */
1435 static void
1436 vndunlock(struct vnd_softc *sc)
1437 {
1438
1439 sc->sc_flags &= ~VNF_LOCKED;
1440 if ((sc->sc_flags & VNF_WANTED) != 0) {
1441 sc->sc_flags &= ~VNF_WANTED;
1442 wakeup(sc);
1443 }
1444 }
1445