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