vnd.c revision 1.103 1 /* $NetBSD: vnd.c,v 1.103 2003/10/15 11:28:59 hannken 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.103 2003/10/15 11:28:59 hannken 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 error = EINVAL;
885 goto close_and_exit;
886 }
887
888 /*
889 * Compute the size (in DEV_BSIZE blocks) specified
890 * by the geometry.
891 */
892 geomsize = (vnd->sc_geom.vng_nsectors *
893 vnd->sc_geom.vng_ntracks *
894 vnd->sc_geom.vng_ncylinders) *
895 (vnd->sc_geom.vng_secsize / DEV_BSIZE);
896
897 /*
898 * Sanity-check the size against the specified
899 * geometry.
900 */
901 if (vnd->sc_size < geomsize) {
902 error = EINVAL;
903 goto close_and_exit;
904 }
905 } else {
906 /*
907 * Size must be at least 2048 DEV_BSIZE blocks
908 * (1M) in order to use this geometry.
909 */
910 if (vnd->sc_size < (32 * 64)) {
911 error = EINVAL;
912 goto close_and_exit;
913 }
914
915 vnd->sc_geom.vng_secsize = DEV_BSIZE;
916 vnd->sc_geom.vng_nsectors = 32;
917 vnd->sc_geom.vng_ntracks = 64;
918 vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
919 }
920
921 if (vio->vnd_flags & VNDIOF_READONLY) {
922 vnd->sc_flags |= VNF_READONLY;
923 }
924
925 if ((error = vndsetcred(vnd, p->p_ucred)) != 0)
926 goto close_and_exit;
927 vndthrottle(vnd, vnd->sc_vp);
928 vio->vnd_size = dbtob(vnd->sc_size);
929 vnd->sc_flags |= VNF_INITED;
930 #ifdef DEBUG
931 if (vnddebug & VDB_INIT)
932 printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
933 vnd->sc_vp, (unsigned long) vnd->sc_size,
934 vnd->sc_geom.vng_secsize,
935 vnd->sc_geom.vng_nsectors,
936 vnd->sc_geom.vng_ntracks,
937 vnd->sc_geom.vng_ncylinders);
938 #endif
939
940 /* Attach the disk. */
941 memset(vnd->sc_xname, 0, sizeof(vnd->sc_xname)); /* XXX */
942 sprintf(vnd->sc_xname, "vnd%d", unit); /* XXX */
943 vnd->sc_dkdev.dk_name = vnd->sc_xname;
944 disk_attach(&vnd->sc_dkdev);
945
946 /* Initialize the xfer and buffer pools. */
947 pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
948 0, 0, "vndxpl", NULL);
949 pool_init(&vnd->sc_vbpool, sizeof(struct vndbuf), 0,
950 0, 0, "vndbpl", NULL);
951
952 /* Try and read the disklabel. */
953 vndgetdisklabel(dev);
954
955 vndunlock(vnd);
956
957 break;
958
959 close_and_exit:
960 (void) vn_close(nd.ni_vp, fflags, p->p_ucred, p);
961 unlock_and_exit:
962 vndunlock(vnd);
963 return (error);
964
965 case VNDIOCCLR:
966 if ((error = vndlock(vnd)) != 0)
967 return (error);
968
969 /*
970 * Don't unconfigure if any other partitions are open
971 * or if both the character and block flavors of this
972 * partition are open.
973 */
974 part = DISKPART(dev);
975 pmask = (1 << part);
976 if (((vnd->sc_dkdev.dk_openmask & ~pmask) ||
977 ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
978 (vnd->sc_dkdev.dk_copenmask & pmask))) &&
979 !(vio->vnd_flags & VNDIOF_FORCE)) {
980 vndunlock(vnd);
981 return (EBUSY);
982 }
983
984 /*
985 * XXX vndclear() might call vndclose() implicitely;
986 * release lock to avoid recursion
987 */
988 vndunlock(vnd);
989 vndclear(vnd, minor(dev));
990 #ifdef DEBUG
991 if (vnddebug & VDB_INIT)
992 printf("vndioctl: CLRed\n");
993 #endif
994
995 /* Destroy the xfer and buffer pools. */
996 pool_destroy(&vnd->sc_vxpool);
997 pool_destroy(&vnd->sc_vbpool);
998
999 /* Detatch the disk. */
1000 disk_detach(&vnd->sc_dkdev);
1001
1002 break;
1003
1004 case VNDIOCGET: {
1005 struct vnd_user *vnu;
1006 struct vattr va;
1007
1008 vnu = (struct vnd_user *)data;
1009
1010 if (vnu->vnu_unit == -1)
1011 vnu->vnu_unit = unit;
1012 if (vnu->vnu_unit >= numvnd)
1013 return (ENXIO);
1014 if (vnu->vnu_unit < 0)
1015 return (EINVAL);
1016
1017 vnd = &vnd_softc[vnu->vnu_unit];
1018
1019 if (vnd->sc_flags & VNF_INITED) {
1020 error = VOP_GETATTR(vnd->sc_vp, &va, p->p_ucred, p);
1021 if (error)
1022 return (error);
1023 vnu->vnu_dev = va.va_fsid;
1024 vnu->vnu_ino = va.va_fileid;
1025 }
1026 else {
1027 /* unused is not an error */
1028 vnu->vnu_dev = 0;
1029 vnu->vnu_ino = 0;
1030 }
1031
1032 break;
1033 }
1034
1035 case DIOCGDINFO:
1036 *(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
1037 break;
1038
1039 #ifdef __HAVE_OLD_DISKLABEL
1040 case ODIOCGDINFO:
1041 newlabel = *(vnd->sc_dkdev.dk_label);
1042 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1043 return ENOTTY;
1044 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1045 break;
1046 #endif
1047
1048 case DIOCGPART:
1049 ((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
1050 ((struct partinfo *)data)->part =
1051 &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1052 break;
1053
1054 case DIOCWDINFO:
1055 case DIOCSDINFO:
1056 #ifdef __HAVE_OLD_DISKLABEL
1057 case ODIOCWDINFO:
1058 case ODIOCSDINFO:
1059 #endif
1060 {
1061 struct disklabel *lp;
1062
1063 if ((error = vndlock(vnd)) != 0)
1064 return (error);
1065
1066 vnd->sc_flags |= VNF_LABELLING;
1067
1068 #ifdef __HAVE_OLD_DISKLABEL
1069 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1070 memset(&newlabel, 0, sizeof newlabel);
1071 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1072 lp = &newlabel;
1073 } else
1074 #endif
1075 lp = (struct disklabel *)data;
1076
1077 error = setdisklabel(vnd->sc_dkdev.dk_label,
1078 lp, 0, vnd->sc_dkdev.dk_cpulabel);
1079 if (error == 0) {
1080 if (cmd == DIOCWDINFO
1081 #ifdef __HAVE_OLD_DISKLABEL
1082 || cmd == ODIOCWDINFO
1083 #endif
1084 )
1085 error = writedisklabel(VNDLABELDEV(dev),
1086 vndstrategy, vnd->sc_dkdev.dk_label,
1087 vnd->sc_dkdev.dk_cpulabel);
1088 }
1089
1090 vnd->sc_flags &= ~VNF_LABELLING;
1091
1092 vndunlock(vnd);
1093
1094 if (error)
1095 return (error);
1096 break;
1097 }
1098
1099 case DIOCKLABEL:
1100 if (*(int *)data != 0)
1101 vnd->sc_flags |= VNF_KLABEL;
1102 else
1103 vnd->sc_flags &= ~VNF_KLABEL;
1104 break;
1105
1106 case DIOCWLABEL:
1107 if (*(int *)data != 0)
1108 vnd->sc_flags |= VNF_WLABEL;
1109 else
1110 vnd->sc_flags &= ~VNF_WLABEL;
1111 break;
1112
1113 case DIOCGDEFLABEL:
1114 vndgetdefaultlabel(vnd, (struct disklabel *)data);
1115 break;
1116
1117 #ifdef __HAVE_OLD_DISKLABEL
1118 case ODIOCGDEFLABEL:
1119 vndgetdefaultlabel(vnd, &newlabel);
1120 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1121 return ENOTTY;
1122 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1123 break;
1124 #endif
1125
1126 default:
1127 return (ENOTTY);
1128 }
1129
1130 return (0);
1131 }
1132
1133 /*
1134 * Duplicate the current processes' credentials. Since we are called only
1135 * as the result of a SET ioctl and only root can do that, any future access
1136 * to this "disk" is essentially as root. Note that credentials may change
1137 * if some other uid can write directly to the mapped file (NFS).
1138 */
1139 int
1140 vndsetcred(vnd, cred)
1141 struct vnd_softc *vnd;
1142 struct ucred *cred;
1143 {
1144 struct uio auio;
1145 struct iovec aiov;
1146 char *tmpbuf;
1147 int error;
1148
1149 vnd->sc_cred = crdup(cred);
1150 tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1151
1152 /* XXX: Horrible kludge to establish credentials for NFS */
1153 aiov.iov_base = tmpbuf;
1154 aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
1155 auio.uio_iov = &aiov;
1156 auio.uio_iovcnt = 1;
1157 auio.uio_offset = 0;
1158 auio.uio_rw = UIO_READ;
1159 auio.uio_segflg = UIO_SYSSPACE;
1160 auio.uio_resid = aiov.iov_len;
1161 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1162 error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
1163 if (error == 0) {
1164 /*
1165 * Because vnd does all IO directly through the vnode
1166 * we need to flush (at least) the buffer from the above
1167 * VOP_READ from the buffer cache to prevent cache
1168 * incoherencies. Also, be careful to write dirty
1169 * buffers back to stable storage.
1170 */
1171 error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
1172 curproc, 0, 0);
1173 }
1174 VOP_UNLOCK(vnd->sc_vp, 0);
1175
1176 free(tmpbuf, M_TEMP);
1177 return (error);
1178 }
1179
1180 /*
1181 * Set maxactive based on FS type
1182 */
1183 void
1184 vndthrottle(vnd, vp)
1185 struct vnd_softc *vnd;
1186 struct vnode *vp;
1187 {
1188 #ifdef NFS
1189 extern int (**nfsv2_vnodeop_p) __P((void *));
1190
1191 if (vp->v_op == nfsv2_vnodeop_p)
1192 vnd->sc_maxactive = 2;
1193 else
1194 #endif
1195 vnd->sc_maxactive = 8;
1196
1197 if (vnd->sc_maxactive < 1)
1198 vnd->sc_maxactive = 1;
1199 }
1200
1201 #if 0
1202 void
1203 vndshutdown()
1204 {
1205 struct vnd_softc *vnd;
1206
1207 for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1208 if (vnd->sc_flags & VNF_INITED)
1209 vndclear(vnd);
1210 }
1211 #endif
1212
1213 void
1214 vndclear(vnd, myminor)
1215 struct vnd_softc *vnd;
1216 int myminor;
1217 {
1218 struct vnode *vp = vnd->sc_vp;
1219 struct proc *p = curproc; /* XXX */
1220 int fflags = FREAD;
1221 int bmaj, cmaj, i, mn;
1222
1223 #ifdef DEBUG
1224 if (vnddebug & VDB_FOLLOW)
1225 printf("vndclear(%p): vp %p\n", vnd, vp);
1226 #endif
1227 /* locate the major number */
1228 bmaj = bdevsw_lookup_major(&vnd_bdevsw);
1229 cmaj = cdevsw_lookup_major(&vnd_cdevsw);
1230
1231 /* Nuke the vnodes for any open instances */
1232 for (i = 0; i < MAXPARTITIONS; i++) {
1233 mn = DISKMINOR(vnd->sc_unit, i);
1234 vdevgone(bmaj, mn, mn, VBLK);
1235 if (mn != myminor) /* XXX avoid to kill own vnode */
1236 vdevgone(cmaj, mn, mn, VCHR);
1237 }
1238
1239 if ((vnd->sc_flags & VNF_READONLY) == 0)
1240 fflags |= FWRITE;
1241 vnd->sc_flags &= ~(VNF_INITED | VNF_READONLY | VNF_VLABEL);
1242 if (vp == (struct vnode *)0)
1243 panic("vndioctl: null vp");
1244 (void) vn_close(vp, fflags, vnd->sc_cred, p);
1245 crfree(vnd->sc_cred);
1246 vnd->sc_vp = (struct vnode *)0;
1247 vnd->sc_cred = (struct ucred *)0;
1248 vnd->sc_size = 0;
1249 }
1250
1251 int
1252 vndsize(dev)
1253 dev_t dev;
1254 {
1255 struct vnd_softc *sc;
1256 struct disklabel *lp;
1257 int part, unit, omask;
1258 int size;
1259
1260 unit = vndunit(dev);
1261 if (unit >= numvnd)
1262 return (-1);
1263 sc = &vnd_softc[unit];
1264
1265 if ((sc->sc_flags & VNF_INITED) == 0)
1266 return (-1);
1267
1268 part = DISKPART(dev);
1269 omask = sc->sc_dkdev.dk_openmask & (1 << part);
1270 lp = sc->sc_dkdev.dk_label;
1271
1272 if (omask == 0 && vndopen(dev, 0, S_IFBLK, curproc))
1273 return (-1);
1274
1275 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1276 size = -1;
1277 else
1278 size = lp->d_partitions[part].p_size *
1279 (lp->d_secsize / DEV_BSIZE);
1280
1281 if (omask == 0 && vndclose(dev, 0, S_IFBLK, curproc))
1282 return (-1);
1283
1284 return (size);
1285 }
1286
1287 int
1288 vnddump(dev, blkno, va, size)
1289 dev_t dev;
1290 daddr_t blkno;
1291 caddr_t va;
1292 size_t size;
1293 {
1294
1295 /* Not implemented. */
1296 return ENXIO;
1297 }
1298
1299 void
1300 vndgetdefaultlabel(sc, lp)
1301 struct vnd_softc *sc;
1302 struct disklabel *lp;
1303 {
1304 struct vndgeom *vng = &sc->sc_geom;
1305 struct partition *pp;
1306
1307 memset(lp, 0, sizeof(*lp));
1308
1309 lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
1310 lp->d_secsize = vng->vng_secsize;
1311 lp->d_nsectors = vng->vng_nsectors;
1312 lp->d_ntracks = vng->vng_ntracks;
1313 lp->d_ncylinders = vng->vng_ncylinders;
1314 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1315
1316 strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1317 lp->d_type = DTYPE_VND;
1318 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1319 lp->d_rpm = 3600;
1320 lp->d_interleave = 1;
1321 lp->d_flags = 0;
1322
1323 pp = &lp->d_partitions[RAW_PART];
1324 pp->p_offset = 0;
1325 pp->p_size = lp->d_secperunit;
1326 pp->p_fstype = FS_UNUSED;
1327 lp->d_npartitions = RAW_PART + 1;
1328
1329 lp->d_magic = DISKMAGIC;
1330 lp->d_magic2 = DISKMAGIC;
1331 lp->d_checksum = dkcksum(lp);
1332 }
1333
1334 /*
1335 * Read the disklabel from a vnd. If one is not present, create a fake one.
1336 */
1337 void
1338 vndgetdisklabel(dev)
1339 dev_t dev;
1340 {
1341 struct vnd_softc *sc = &vnd_softc[vndunit(dev)];
1342 const char *errstring;
1343 struct disklabel *lp = sc->sc_dkdev.dk_label;
1344 struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1345 int i;
1346
1347 memset(clp, 0, sizeof(*clp));
1348
1349 vndgetdefaultlabel(sc, lp);
1350
1351 /*
1352 * Call the generic disklabel extraction routine.
1353 */
1354 errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1355 if (errstring) {
1356 /*
1357 * Lack of disklabel is common, but we print the warning
1358 * anyway, since it might contain other useful information.
1359 */
1360 printf("%s: %s\n", sc->sc_xname, errstring);
1361
1362 /*
1363 * For historical reasons, if there's no disklabel
1364 * present, all partitions must be FS_BSDFFS and
1365 * occupy the entire disk.
1366 */
1367 for (i = 0; i < MAXPARTITIONS; i++) {
1368 /*
1369 * Don't wipe out port specific hack (such as
1370 * dos partition hack of i386 port).
1371 */
1372 if (lp->d_partitions[i].p_size != 0)
1373 continue;
1374
1375 lp->d_partitions[i].p_size = lp->d_secperunit;
1376 lp->d_partitions[i].p_offset = 0;
1377 lp->d_partitions[i].p_fstype = FS_BSDFFS;
1378 }
1379
1380 strncpy(lp->d_packname, "default label",
1381 sizeof(lp->d_packname));
1382
1383 lp->d_npartitions = MAXPARTITIONS;
1384 lp->d_checksum = dkcksum(lp);
1385 }
1386
1387 /* In-core label now valid. */
1388 sc->sc_flags |= VNF_VLABEL;
1389 }
1390
1391 /*
1392 * Wait interruptibly for an exclusive lock.
1393 *
1394 * XXX
1395 * Several drivers do this; it should be abstracted and made MP-safe.
1396 */
1397 static int
1398 vndlock(sc)
1399 struct vnd_softc *sc;
1400 {
1401 int error;
1402
1403 while ((sc->sc_flags & VNF_LOCKED) != 0) {
1404 sc->sc_flags |= VNF_WANTED;
1405 if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1406 return (error);
1407 }
1408 sc->sc_flags |= VNF_LOCKED;
1409 return (0);
1410 }
1411
1412 /*
1413 * Unlock and wake up any waiters.
1414 */
1415 static void
1416 vndunlock(sc)
1417 struct vnd_softc *sc;
1418 {
1419
1420 sc->sc_flags &= ~VNF_LOCKED;
1421 if ((sc->sc_flags & VNF_WANTED) != 0) {
1422 sc->sc_flags &= ~VNF_WANTED;
1423 wakeup(sc);
1424 }
1425 }
1426