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