vnd.c revision 1.127 1 /* $NetBSD: vnd.c,v 1.127 2006/01/08 09:09:00 yamt 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.127 2006/01/08 09:09:00 yamt Exp $");
137
138 #if defined(_KERNEL_OPT)
139 #include "fs_nfs.h"
140 #include "opt_vnd.h"
141 #endif
142
143 #include <sys/param.h>
144 #include <sys/systm.h>
145 #include <sys/namei.h>
146 #include <sys/proc.h>
147 #include <sys/kthread.h>
148 #include <sys/errno.h>
149 #include <sys/buf.h>
150 #include <sys/bufq.h>
151 #include <sys/malloc.h>
152 #include <sys/ioctl.h>
153 #include <sys/disklabel.h>
154 #include <sys/device.h>
155 #include <sys/disk.h>
156 #include <sys/stat.h>
157 #include <sys/mount.h>
158 #include <sys/vnode.h>
159 #include <sys/file.h>
160 #include <sys/uio.h>
161 #include <sys/conf.h>
162 #include <net/zlib.h>
163
164 #include <miscfs/specfs/specdev.h>
165
166 #include <dev/vndvar.h>
167
168 #if defined(VNDDEBUG) && !defined(DEBUG)
169 #define DEBUG
170 #endif
171
172 #ifdef DEBUG
173 int dovndcluster = 1;
174 #define VDB_FOLLOW 0x01
175 #define VDB_INIT 0x02
176 #define VDB_IO 0x04
177 #define VDB_LABEL 0x08
178 int vnddebug = 0x00;
179 #endif
180
181 #define vndunit(x) DISKUNIT(x)
182
183 struct vndxfer {
184 struct buf *vx_bp; /* Pointer to parent buffer */
185 int vx_error;
186 int vx_pending; /* # of pending aux buffers */
187 int vx_flags;
188 #define VX_BUSY 1
189 };
190
191 struct vndbuf {
192 struct buf vb_buf;
193 struct vndxfer *vb_xfer;
194 };
195
196 #define VND_GETXFER(vnd) pool_get(&(vnd)->sc_vxpool, PR_WAITOK)
197 #define VND_PUTXFER(vnd, vx) pool_put(&(vnd)->sc_vxpool, (vx))
198
199 #define VND_GETBUF(vnd) pool_get(&(vnd)->sc_vbpool, PR_WAITOK)
200 #define VND_PUTBUF(vnd, vb) pool_put(&(vnd)->sc_vbpool, (vb))
201
202 struct vnd_softc *vnd_softc;
203 int numvnd = 0;
204
205 #define VNDLABELDEV(dev) \
206 (MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
207
208 /* called by main() at boot time (XXX: and the LKM driver) */
209 void vndattach(int);
210 int vnddetach(void);
211
212 static void vndclear(struct vnd_softc *, int);
213 static int vndsetcred(struct vnd_softc *, struct ucred *);
214 static void vndthrottle(struct vnd_softc *, struct vnode *);
215 static void vndiodone(struct buf *);
216 #if 0
217 static void vndshutdown(void);
218 #endif
219
220 static void vndgetdefaultlabel(struct vnd_softc *, struct disklabel *);
221 static void vndgetdisklabel(dev_t);
222
223 static int vndlock(struct vnd_softc *);
224 static void vndunlock(struct vnd_softc *);
225 #ifdef VND_COMPRESSION
226 static void compstrategy(struct buf *, off_t);
227 static void *vnd_alloc(void *, u_int, u_int);
228 static void vnd_free(void *, void *);
229 #endif /* VND_COMPRESSION */
230
231 static void vndthread(void *);
232 static void vndiodone1(struct vnd_softc *, struct vndxfer *);
233
234 static dev_type_open(vndopen);
235 static dev_type_close(vndclose);
236 static dev_type_read(vndread);
237 static dev_type_write(vndwrite);
238 static dev_type_ioctl(vndioctl);
239 static dev_type_strategy(vndstrategy);
240 static dev_type_dump(vnddump);
241 static dev_type_size(vndsize);
242
243 const struct bdevsw vnd_bdevsw = {
244 vndopen, vndclose, vndstrategy, vndioctl, vnddump, vndsize, D_DISK
245 };
246
247 const struct cdevsw vnd_cdevsw = {
248 vndopen, vndclose, vndread, vndwrite, vndioctl,
249 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
250 };
251
252 static int vndattached;
253
254 void
255 vndattach(int num)
256 {
257 int i;
258 char *mem;
259
260 if (vndattached)
261 return;
262 vndattached = 1;
263 if (num <= 0)
264 return;
265 i = num * sizeof(struct vnd_softc);
266 mem = malloc(i, M_DEVBUF, M_NOWAIT|M_ZERO);
267 if (mem == NULL) {
268 printf("WARNING: no memory for vnode disks\n");
269 return;
270 }
271 vnd_softc = (struct vnd_softc *)mem;
272 numvnd = num;
273
274 for (i = 0; i < numvnd; i++) {
275 vnd_softc[i].sc_unit = i;
276 vnd_softc[i].sc_comp_offsets = NULL;
277 vnd_softc[i].sc_comp_buff = NULL;
278 vnd_softc[i].sc_comp_decombuf = NULL;
279 bufq_alloc(&vnd_softc[i].sc_tab,
280 "disksort", BUFQ_SORT_RAWBLOCK);
281 pseudo_disk_init(&vnd_softc[i].sc_dkdev);
282 }
283 }
284
285 int
286 vnddetach(void)
287 {
288 int i;
289
290 /* First check we aren't in use. */
291 for (i = 0; i < numvnd; i++)
292 if (vnd_softc[i].sc_flags & VNF_INITED)
293 return (EBUSY);
294
295 for (i = 0; i < numvnd; i++)
296 bufq_free(vnd_softc[i].sc_tab);
297
298 free(vnd_softc, M_DEVBUF);
299 vndattached = 0;
300
301 return (0);
302 }
303
304 static int
305 vndopen(dev_t dev, int flags, int mode, struct lwp *l)
306 {
307 int unit = vndunit(dev);
308 struct vnd_softc *sc;
309 int error = 0, part, pmask;
310 struct disklabel *lp;
311
312 #ifdef DEBUG
313 if (vnddebug & VDB_FOLLOW)
314 printf("vndopen(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
315 #endif
316 if (unit >= numvnd)
317 return (ENXIO);
318 sc = &vnd_softc[unit];
319
320 if ((error = vndlock(sc)) != 0)
321 return (error);
322
323 lp = sc->sc_dkdev.dk_label;
324
325 part = DISKPART(dev);
326 pmask = (1 << part);
327
328 /*
329 * If we're initialized, check to see if there are any other
330 * open partitions. If not, then it's safe to update the
331 * in-core disklabel. Only read the disklabel if it is
332 * not already valid.
333 */
334 if ((sc->sc_flags & (VNF_INITED|VNF_VLABEL)) == VNF_INITED &&
335 sc->sc_dkdev.dk_openmask == 0)
336 vndgetdisklabel(dev);
337
338 /* Check that the partitions exists. */
339 if (part != RAW_PART) {
340 if (((sc->sc_flags & VNF_INITED) == 0) ||
341 ((part >= lp->d_npartitions) ||
342 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
343 error = ENXIO;
344 goto done;
345 }
346 }
347
348 /* Prevent our unit from being unconfigured while open. */
349 switch (mode) {
350 case S_IFCHR:
351 sc->sc_dkdev.dk_copenmask |= pmask;
352 break;
353
354 case S_IFBLK:
355 sc->sc_dkdev.dk_bopenmask |= pmask;
356 break;
357 }
358 sc->sc_dkdev.dk_openmask =
359 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
360
361 done:
362 vndunlock(sc);
363 return (error);
364 }
365
366 static int
367 vndclose(dev_t dev, int flags, int mode, struct lwp *l)
368 {
369 int unit = vndunit(dev);
370 struct vnd_softc *sc;
371 int error = 0, part;
372
373 #ifdef DEBUG
374 if (vnddebug & VDB_FOLLOW)
375 printf("vndclose(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
376 #endif
377
378 if (unit >= numvnd)
379 return (ENXIO);
380 sc = &vnd_softc[unit];
381
382 if ((error = vndlock(sc)) != 0)
383 return (error);
384
385 part = DISKPART(dev);
386
387 /* ...that much closer to allowing unconfiguration... */
388 switch (mode) {
389 case S_IFCHR:
390 sc->sc_dkdev.dk_copenmask &= ~(1 << part);
391 break;
392
393 case S_IFBLK:
394 sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
395 break;
396 }
397 sc->sc_dkdev.dk_openmask =
398 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
399
400 vndunlock(sc);
401 return (0);
402 }
403
404 /*
405 * Qeue the request, and wakeup the kernel thread to handle it.
406 */
407 static void
408 vndstrategy(struct buf *bp)
409 {
410 int unit = vndunit(bp->b_dev);
411 struct vnd_softc *vnd = &vnd_softc[unit];
412 struct disklabel *lp = vnd->sc_dkdev.dk_label;
413 daddr_t blkno;
414 int s = splbio();
415
416 bp->b_resid = bp->b_bcount;
417
418 if ((vnd->sc_flags & VNF_INITED) == 0) {
419 bp->b_error = ENXIO;
420 bp->b_flags |= B_ERROR;
421 goto done;
422 }
423
424 /*
425 * The transfer must be a whole number of blocks.
426 */
427 if ((bp->b_bcount % lp->d_secsize) != 0) {
428 bp->b_error = EINVAL;
429 bp->b_flags |= B_ERROR;
430 goto done;
431 }
432
433 /*
434 * check if we're read-only.
435 */
436 if ((vnd->sc_flags & VNF_READONLY) && !(bp->b_flags & B_READ)) {
437 bp->b_error = EACCES;
438 bp->b_flags |= B_ERROR;
439 goto done;
440 }
441
442 /*
443 * Do bounds checking and adjust transfer. If there's an error,
444 * the bounds check will flag that for us.
445 */
446 if (DISKPART(bp->b_dev) != RAW_PART) {
447 if (bounds_check_with_label(&vnd->sc_dkdev,
448 bp, vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING)) <= 0)
449 goto done;
450 }
451
452 /* If it's a nil transfer, wake up the top half now. */
453 if (bp->b_bcount == 0)
454 goto done;
455
456 /*
457 * Put the block number in terms of the logical blocksize
458 * of the "device".
459 */
460
461 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
462
463 /*
464 * Translate the partition-relative block number to an absolute.
465 */
466 if (DISKPART(bp->b_dev) != RAW_PART) {
467 struct partition *pp;
468
469 pp = &vnd->sc_dkdev.dk_label->d_partitions[
470 DISKPART(bp->b_dev)];
471 blkno += pp->p_offset;
472 }
473 bp->b_rawblkno = blkno;
474
475 #ifdef DEBUG
476 if (vnddebug & VDB_FOLLOW)
477 printf("vndstrategy(%p): unit %d\n", bp, unit);
478 #endif
479 BUFQ_PUT(vnd->sc_tab, bp);
480 wakeup(&vnd->sc_tab);
481 splx(s);
482 return;
483 done:
484 biodone(bp);
485 splx(s);
486 }
487
488 static void
489 vndthread(void *arg)
490 {
491 struct vnd_softc *vnd = arg;
492 struct buf *bp;
493 struct vndxfer *vnx;
494 struct mount *mp;
495 int s, bsize, resid;
496 off_t bn;
497 caddr_t addr;
498 int sz, flags, error;
499 struct disklabel *lp;
500
501 s = splbio();
502 vnd->sc_flags |= VNF_KTHREAD;
503 wakeup(&vnd->sc_kthread);
504
505 /*
506 * Dequeue requests, break them into bsize pieces and submit using
507 * VOP_BMAP/VOP_STRATEGY.
508 */
509 while ((vnd->sc_flags & VNF_VUNCONF) == 0) {
510 bp = BUFQ_GET(vnd->sc_tab);
511 if (bp == NULL) {
512 tsleep(&vnd->sc_tab, PRIBIO, "vndbp", 0);
513 continue;
514 };
515 splx(s);
516
517 #ifdef DEBUG
518 if (vnddebug & VDB_FOLLOW)
519 printf("vndthread(%p\n", bp);
520 #endif
521 lp = vnd->sc_dkdev.dk_label;
522 bp->b_resid = bp->b_bcount;
523
524 /* convert to a byte offset within the file. */
525 bn = bp->b_rawblkno * lp->d_secsize;
526
527 if (vnd->sc_vp->v_mount == NULL) {
528 bp->b_error = ENXIO;
529 bp->b_flags |= B_ERROR;
530 goto done;
531 }
532 #ifdef VND_COMPRESSION
533 /* handle a compressed read */
534 if ((bp->b_flags & B_READ) && (vnd->sc_flags & VNF_COMP)) {
535 compstrategy(bp, bn);
536 goto done;
537 }
538 #endif /* VND_COMPRESSION */
539
540 bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
541 addr = bp->b_data;
542 flags = (bp->b_flags & (B_READ|B_ASYNC)) | B_CALL;
543
544 /*
545 * Allocate a header for this transfer and link it to the
546 * buffer
547 */
548 s = splbio();
549 vnx = VND_GETXFER(vnd);
550 splx(s);
551 vnx->vx_flags = VX_BUSY;
552 vnx->vx_error = 0;
553 vnx->vx_pending = 0;
554 vnx->vx_bp = bp;
555
556 if ((flags & B_READ) == 0)
557 vn_start_write(vnd->sc_vp, &mp, V_WAIT);
558
559 /* Instrumentation. */
560 disk_busy(&vnd->sc_dkdev);
561
562 /*
563 * Feed requests sequentially.
564 * We do it this way to keep from flooding NFS servers if we
565 * are connected to an NFS file. This places the burden on
566 * the client rather than the server.
567 */
568 for (resid = bp->b_resid; resid; resid -= sz) {
569 struct vndbuf *nbp;
570 struct vnode *vp;
571 daddr_t nbn;
572 int off, nra;
573
574 nra = 0;
575 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
576 error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
577 VOP_UNLOCK(vnd->sc_vp, 0);
578
579 if (error == 0 && (long)nbn == -1)
580 error = EIO;
581
582 /*
583 * If there was an error or a hole in the file...punt.
584 * Note that we may have to wait for any operations
585 * that we have already fired off before releasing
586 * the buffer.
587 *
588 * XXX we could deal with holes here but it would be
589 * a hassle (in the write case).
590 */
591 if (error) {
592 s = splbio();
593 vnx->vx_error = error;
594 goto out;
595 }
596
597 #ifdef DEBUG
598 if (!dovndcluster)
599 nra = 0;
600 #endif
601
602 if ((off = bn % bsize) != 0)
603 sz = bsize - off;
604 else
605 sz = (1 + nra) * bsize;
606 if (resid < sz)
607 sz = resid;
608 #ifdef DEBUG
609 if (vnddebug & VDB_IO)
610 printf("vndstrategy: vp %p/%p bn 0x%qx/0x%" PRIx64
611 " sz 0x%x\n",
612 vnd->sc_vp, vp, (long long)bn, nbn, sz);
613 #endif
614
615 s = splbio();
616 while (vnd->sc_active >= vnd->sc_maxactive) {
617 tsleep(&vnd->sc_tab, PRIBIO, "vndac", 0);
618 }
619 vnd->sc_active++;
620 nbp = VND_GETBUF(vnd);
621 splx(s);
622 BUF_INIT(&nbp->vb_buf);
623 nbp->vb_buf.b_flags = flags;
624 nbp->vb_buf.b_bcount = sz;
625 nbp->vb_buf.b_bufsize = round_page((ulong)addr + sz)
626 - trunc_page((ulong) addr);
627 nbp->vb_buf.b_error = 0;
628 nbp->vb_buf.b_data = addr;
629 nbp->vb_buf.b_blkno = nbp->vb_buf.b_rawblkno = nbn + btodb(off);
630 nbp->vb_buf.b_proc = bp->b_proc;
631 nbp->vb_buf.b_iodone = vndiodone;
632 nbp->vb_buf.b_vp = vp;
633
634 nbp->vb_xfer = vnx;
635
636 BIO_COPYPRIO(&nbp->vb_buf, bp);
637
638 /*
639 * Just sort by block number
640 */
641 s = splbio();
642 if (vnx->vx_error != 0) {
643 VND_PUTBUF(vnd, nbp);
644 goto out;
645 }
646 vnx->vx_pending++;
647 #ifdef DEBUG
648 if (vnddebug & VDB_IO)
649 printf("vndstart(%ld): bp %p vp %p blkno "
650 "0x%" PRIx64 " flags %x addr %p cnt 0x%x\n",
651 (long) (vnd-vnd_softc), &nbp->vb_buf,
652 nbp->vb_buf.b_vp, nbp->vb_buf.b_blkno,
653 nbp->vb_buf.b_flags, nbp->vb_buf.b_data,
654 nbp->vb_buf.b_bcount);
655 #endif
656
657 if ((nbp->vb_buf.b_flags & B_READ) == 0)
658 vp->v_numoutput++;
659 VOP_STRATEGY(vp, &nbp->vb_buf);
660
661 splx(s);
662 bn += sz;
663 addr += sz;
664 }
665
666 s = splbio();
667
668 out: /* Arrive here at splbio */
669 if ((flags & B_READ) == 0)
670 vn_finished_write(mp, 0);
671 vnx->vx_flags &= ~VX_BUSY;
672 if (vnx->vx_pending == 0) {
673 vndiodone1(vnd, vnx);
674 }
675 continue;
676 done:
677 biodone(bp);
678 s = splbio();
679 }
680
681 vnd->sc_flags &= (~VNF_KTHREAD | VNF_VUNCONF);
682 wakeup(&vnd->sc_kthread);
683 splx(s);
684 kthread_exit(0);
685 }
686
687 static void
688 vndiodone1(struct vnd_softc *vnd, struct vndxfer *vnx)
689 {
690 struct buf *bp = vnx->vx_bp;
691 int error;
692
693 if ((vnx->vx_flags & VX_BUSY) != 0) {
694 return;
695 }
696 KASSERT(bp->b_resid != 0 || vnx->vx_pending == 0);
697 if (vnx->vx_pending > 0) {
698 return;
699 }
700 #ifdef DEBUG
701 if (vnddebug & VDB_IO) {
702 printf("vndiodone1: bp %p iodone: error %d\n",
703 bp, vnx->vx_error);
704 }
705 #endif
706 error = vnx->vx_error;
707 if (error != 0) {
708 bp->b_flags |= B_ERROR;
709 bp->b_error = error;
710
711 /* we don't know which sub-buf failed */
712 bp->b_resid = bp->b_bcount;
713 }
714 VND_PUTXFER(vnd, vnx);
715 disk_unbusy(&vnd->sc_dkdev, bp->b_bcount - bp->b_resid,
716 (bp->b_flags & B_READ));
717 biodone(bp);
718 }
719
720 static void
721 vndiodone(struct buf *bp)
722 {
723 struct vndbuf *vbp = (struct vndbuf *) bp;
724 struct vndxfer *vnx = (struct vndxfer *)vbp->vb_xfer;
725 struct buf *pbp = vnx->vx_bp;
726 struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
727 int s, done;
728
729 s = splbio();
730 #ifdef DEBUG
731 if (vnddebug & VDB_IO)
732 printf("vndiodone(%ld): vbp %p vp %p blkno 0x%" PRIx64
733 " addr %p cnt 0x%x\n",
734 (long) (vnd-vnd_softc), vbp, vbp->vb_buf.b_vp,
735 vbp->vb_buf.b_blkno, vbp->vb_buf.b_data,
736 vbp->vb_buf.b_bcount);
737 #endif
738
739 done = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
740 pbp->b_resid -= done;
741 vnx->vx_pending--;
742 if (vbp->vb_buf.b_error) {
743 #ifdef DEBUG
744 if (vnddebug & VDB_IO)
745 printf("vndiodone: vbp %p error %d\n", vbp,
746 vbp->vb_buf.b_error);
747 #endif
748 vnx->vx_error = vbp->vb_buf.b_error;
749 }
750
751 VND_PUTBUF(vnd, vbp);
752
753 /*
754 * Wrap up this transaction if it has run to completion or, in
755 * case of an error, when all auxiliary buffers have returned.
756 */
757 if (vnx->vx_error != 0 || pbp->b_resid == 0) {
758 vndiodone1(vnd, vnx);
759 }
760
761 vnd->sc_active--;
762 wakeup(&vnd->sc_tab);
763 splx(s);
764 }
765
766 /* ARGSUSED */
767 static int
768 vndread(dev_t dev, struct uio *uio, int flags)
769 {
770 int unit = vndunit(dev);
771 struct vnd_softc *sc;
772
773 #ifdef DEBUG
774 if (vnddebug & VDB_FOLLOW)
775 printf("vndread(0x%x, %p)\n", dev, uio);
776 #endif
777
778 if (unit >= numvnd)
779 return (ENXIO);
780 sc = &vnd_softc[unit];
781
782 if ((sc->sc_flags & VNF_INITED) == 0)
783 return (ENXIO);
784
785 return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
786 }
787
788 /* ARGSUSED */
789 static int
790 vndwrite(dev_t dev, struct uio *uio, int flags)
791 {
792 int unit = vndunit(dev);
793 struct vnd_softc *sc;
794
795 #ifdef DEBUG
796 if (vnddebug & VDB_FOLLOW)
797 printf("vndwrite(0x%x, %p)\n", dev, uio);
798 #endif
799
800 if (unit >= numvnd)
801 return (ENXIO);
802 sc = &vnd_softc[unit];
803
804 if ((sc->sc_flags & VNF_INITED) == 0)
805 return (ENXIO);
806
807 return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
808 }
809
810 static int
811 vnd_cget(struct lwp *l, int unit, int *un, struct vattr *va)
812 {
813 struct vnd_softc *vnd;
814
815 if (*un == -1)
816 *un = unit;
817 if (*un >= numvnd)
818 return ENXIO;
819 if (*un < 0)
820 return EINVAL;
821
822 vnd = &vnd_softc[*un];
823
824 if ((vnd->sc_flags & VNF_INITED) == 0)
825 return -1;
826
827 return VOP_GETATTR(vnd->sc_vp, va, l->l_proc->p_ucred, l);
828 }
829
830 /* ARGSUSED */
831 static int
832 vndioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
833 {
834 int unit = vndunit(dev);
835 struct vnd_softc *vnd;
836 struct vnd_ioctl *vio;
837 struct vattr vattr;
838 struct nameidata nd;
839 int error, part, pmask;
840 size_t geomsize;
841 struct proc *p = (l != NULL) ? l->l_proc : NULL;
842 int fflags;
843 #ifdef __HAVE_OLD_DISKLABEL
844 struct disklabel newlabel;
845 #endif
846
847 #ifdef DEBUG
848 if (vnddebug & VDB_FOLLOW)
849 printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
850 dev, cmd, data, flag, p, unit);
851 #endif
852 if (unit >= numvnd)
853 return (ENXIO);
854
855 vnd = &vnd_softc[unit];
856 vio = (struct vnd_ioctl *)data;
857
858 /* Must be open for writes for these commands... */
859 switch (cmd) {
860 case VNDIOCSET:
861 case VNDIOCCLR:
862 case DIOCSDINFO:
863 case DIOCWDINFO:
864 #ifdef __HAVE_OLD_DISKLABEL
865 case ODIOCSDINFO:
866 case ODIOCWDINFO:
867 #endif
868 case DIOCKLABEL:
869 case DIOCWLABEL:
870 if ((flag & FWRITE) == 0)
871 return (EBADF);
872 }
873
874 /* Must be initialized for these... */
875 switch (cmd) {
876 case VNDIOCCLR:
877 case DIOCGDINFO:
878 case DIOCSDINFO:
879 case DIOCWDINFO:
880 case DIOCGPART:
881 case DIOCKLABEL:
882 case DIOCWLABEL:
883 case DIOCGDEFLABEL:
884 #ifdef __HAVE_OLD_DISKLABEL
885 case ODIOCGDINFO:
886 case ODIOCSDINFO:
887 case ODIOCWDINFO:
888 case ODIOCGDEFLABEL:
889 #endif
890 if ((vnd->sc_flags & VNF_INITED) == 0)
891 return (ENXIO);
892 }
893
894 switch (cmd) {
895 case VNDIOCSET:
896 if (vnd->sc_flags & VNF_INITED)
897 return (EBUSY);
898
899 if ((error = vndlock(vnd)) != 0)
900 return (error);
901
902 fflags = FREAD;
903 if ((vio->vnd_flags & VNDIOF_READONLY) == 0)
904 fflags |= FWRITE;
905 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, l);
906 if ((error = vn_open(&nd, fflags, 0)) != 0)
907 goto unlock_and_exit;
908 error = VOP_GETATTR(nd.ni_vp, &vattr, l->l_proc->p_ucred, l);
909 if (!error && nd.ni_vp->v_type != VREG)
910 error = EOPNOTSUPP;
911 if (error) {
912 VOP_UNLOCK(nd.ni_vp, 0);
913 goto close_and_exit;
914 }
915
916 /* If using a compressed file, initialize its info */
917 /* (or abort with an error if kernel has no compression) */
918 if (vio->vnd_flags & VNF_COMP) {
919 #ifdef VND_COMPRESSION
920 struct vnd_comp_header *ch;
921 int i;
922 u_int32_t comp_size;
923 u_int32_t comp_maxsize;
924
925 /* allocate space for compresed file header */
926 ch = malloc(sizeof(struct vnd_comp_header),
927 M_TEMP, M_WAITOK);
928
929 /* read compressed file header */
930 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)ch,
931 sizeof(struct vnd_comp_header), 0, UIO_SYSSPACE,
932 IO_UNIT|IO_NODELOCKED, p->p_ucred, NULL, NULL);
933 if(error) {
934 free(ch, M_TEMP);
935 VOP_UNLOCK(nd.ni_vp, 0);
936 goto close_and_exit;
937 }
938
939 /* save some header info */
940 vnd->sc_comp_blksz = ntohl(ch->block_size);
941 /* note last offset is the file byte size */
942 vnd->sc_comp_numoffs = ntohl(ch->num_blocks)+1;
943 free(ch, M_TEMP);
944 if(vnd->sc_comp_blksz % DEV_BSIZE !=0) {
945 VOP_UNLOCK(nd.ni_vp, 0);
946 error = EINVAL;
947 goto close_and_exit;
948 }
949 if(sizeof(struct vnd_comp_header) +
950 sizeof(u_int64_t) * vnd->sc_comp_numoffs >
951 vattr.va_size) {
952 VOP_UNLOCK(nd.ni_vp, 0);
953 error = EINVAL;
954 goto close_and_exit;
955 }
956
957 /* set decompressed file size */
958 vattr.va_size =
959 (vnd->sc_comp_numoffs - 1) * vnd->sc_comp_blksz;
960
961 /* allocate space for all the compressed offsets */
962 vnd->sc_comp_offsets =
963 malloc(sizeof(u_int64_t) * vnd->sc_comp_numoffs,
964 M_DEVBUF, M_WAITOK);
965
966 /* read in the offsets */
967 error = vn_rdwr(UIO_READ, nd.ni_vp,
968 (caddr_t)vnd->sc_comp_offsets,
969 sizeof(u_int64_t) * vnd->sc_comp_numoffs,
970 sizeof(struct vnd_comp_header), UIO_SYSSPACE,
971 IO_UNIT|IO_NODELOCKED, p->p_ucred, NULL, NULL);
972 if(error) {
973 VOP_UNLOCK(nd.ni_vp, 0);
974 goto close_and_exit;
975 }
976 /*
977 * find largest block size (used for allocation limit).
978 * Also convert offset to native byte order.
979 */
980 comp_maxsize = 0;
981 for (i = 0; i < vnd->sc_comp_numoffs - 1; i++) {
982 vnd->sc_comp_offsets[i] =
983 be64toh(vnd->sc_comp_offsets[i]);
984 comp_size = be64toh(vnd->sc_comp_offsets[i + 1])
985 - vnd->sc_comp_offsets[i];
986 if (comp_size > comp_maxsize)
987 comp_maxsize = comp_size;
988 }
989 vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1] =
990 be64toh(vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1]);
991
992 /* create compressed data buffer */
993 vnd->sc_comp_buff = malloc(comp_maxsize,
994 M_DEVBUF, M_WAITOK);
995
996 /* create decompressed buffer */
997 vnd->sc_comp_decombuf = malloc(vnd->sc_comp_blksz,
998 M_DEVBUF, M_WAITOK);
999 vnd->sc_comp_buffblk = -1;
1000
1001 /* Initialize decompress stream */
1002 bzero(&vnd->sc_comp_stream, sizeof(z_stream));
1003 vnd->sc_comp_stream.zalloc = vnd_alloc;
1004 vnd->sc_comp_stream.zfree = vnd_free;
1005 error = inflateInit2(&vnd->sc_comp_stream, MAX_WBITS);
1006 if(error) {
1007 if(vnd->sc_comp_stream.msg)
1008 printf("vnd%d: compressed file, %s\n",
1009 unit, vnd->sc_comp_stream.msg);
1010 VOP_UNLOCK(nd.ni_vp, 0);
1011 error = EINVAL;
1012 goto close_and_exit;
1013 }
1014
1015 vnd->sc_flags |= VNF_COMP | VNF_READONLY;
1016 #else /* !VND_COMPRESSION */
1017 VOP_UNLOCK(nd.ni_vp, 0);
1018 error = EOPNOTSUPP;
1019 goto close_and_exit;
1020 #endif /* VND_COMPRESSION */
1021 }
1022
1023 VOP_UNLOCK(nd.ni_vp, 0);
1024 vnd->sc_vp = nd.ni_vp;
1025 vnd->sc_size = btodb(vattr.va_size); /* note truncation */
1026
1027 /*
1028 * Use pseudo-geometry specified. If none was provided,
1029 * use "standard" Adaptec fictitious geometry.
1030 */
1031 if (vio->vnd_flags & VNDIOF_HASGEOM) {
1032
1033 memcpy(&vnd->sc_geom, &vio->vnd_geom,
1034 sizeof(vio->vnd_geom));
1035
1036 /*
1037 * Sanity-check the sector size.
1038 * XXX Don't allow secsize < DEV_BSIZE. Should
1039 * XXX we?
1040 */
1041 if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
1042 (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0 ||
1043 vnd->sc_geom.vng_ncylinders == 0 ||
1044 (vnd->sc_geom.vng_ntracks *
1045 vnd->sc_geom.vng_nsectors) == 0) {
1046 error = EINVAL;
1047 goto close_and_exit;
1048 }
1049
1050 /*
1051 * Compute the size (in DEV_BSIZE blocks) specified
1052 * by the geometry.
1053 */
1054 geomsize = (vnd->sc_geom.vng_nsectors *
1055 vnd->sc_geom.vng_ntracks *
1056 vnd->sc_geom.vng_ncylinders) *
1057 (vnd->sc_geom.vng_secsize / DEV_BSIZE);
1058
1059 /*
1060 * Sanity-check the size against the specified
1061 * geometry.
1062 */
1063 if (vnd->sc_size < geomsize) {
1064 error = EINVAL;
1065 goto close_and_exit;
1066 }
1067 } else if (vnd->sc_size >= (32 * 64)) {
1068 /*
1069 * Size must be at least 2048 DEV_BSIZE blocks
1070 * (1M) in order to use this geometry.
1071 */
1072 vnd->sc_geom.vng_secsize = DEV_BSIZE;
1073 vnd->sc_geom.vng_nsectors = 32;
1074 vnd->sc_geom.vng_ntracks = 64;
1075 vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
1076 } else {
1077 vnd->sc_geom.vng_secsize = DEV_BSIZE;
1078 vnd->sc_geom.vng_nsectors = 1;
1079 vnd->sc_geom.vng_ntracks = 1;
1080 vnd->sc_geom.vng_ncylinders = vnd->sc_size;
1081 }
1082
1083 if (vio->vnd_flags & VNDIOF_READONLY) {
1084 vnd->sc_flags |= VNF_READONLY;
1085 }
1086
1087 if ((error = vndsetcred(vnd, p->p_ucred)) != 0)
1088 goto close_and_exit;
1089
1090 memset(vnd->sc_xname, 0, sizeof(vnd->sc_xname)); /* XXX */
1091 snprintf(vnd->sc_xname, sizeof(vnd->sc_xname), "vnd%d", unit);
1092
1093
1094 vndthrottle(vnd, vnd->sc_vp);
1095 vio->vnd_size = dbtob(vnd->sc_size);
1096 vnd->sc_flags |= VNF_INITED;
1097
1098 /* create the kernel thread, wait for it to be up */
1099 error = kthread_create1(vndthread, vnd, &vnd->sc_kthread,
1100 vnd->sc_xname);
1101 if (error)
1102 goto close_and_exit;
1103 while ((vnd->sc_flags & VNF_KTHREAD) == 0) {
1104 tsleep(&vnd->sc_kthread, PRIBIO, "vndthr", 0);
1105 }
1106 #ifdef DEBUG
1107 if (vnddebug & VDB_INIT)
1108 printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
1109 vnd->sc_vp, (unsigned long) vnd->sc_size,
1110 vnd->sc_geom.vng_secsize,
1111 vnd->sc_geom.vng_nsectors,
1112 vnd->sc_geom.vng_ntracks,
1113 vnd->sc_geom.vng_ncylinders);
1114 #endif
1115
1116 /* Attach the disk. */
1117 vnd->sc_dkdev.dk_name = vnd->sc_xname;
1118 pseudo_disk_attach(&vnd->sc_dkdev);
1119
1120 /* Initialize the xfer and buffer pools. */
1121 pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
1122 0, 0, "vndxpl", NULL);
1123 pool_init(&vnd->sc_vbpool, sizeof(struct vndbuf), 0,
1124 0, 0, "vndbpl", NULL);
1125
1126 /* Try and read the disklabel. */
1127 vndgetdisklabel(dev);
1128
1129 vndunlock(vnd);
1130
1131 break;
1132
1133 close_and_exit:
1134 (void) vn_close(nd.ni_vp, fflags, p->p_ucred, l);
1135 unlock_and_exit:
1136 #ifdef VND_COMPRESSION
1137 /* free any allocated memory (for compressed file) */
1138 if(vnd->sc_comp_offsets) {
1139 free(vnd->sc_comp_offsets, M_DEVBUF);
1140 vnd->sc_comp_offsets = NULL;
1141 }
1142 if(vnd->sc_comp_buff) {
1143 free(vnd->sc_comp_buff, M_DEVBUF);
1144 vnd->sc_comp_buff = NULL;
1145 }
1146 if(vnd->sc_comp_decombuf) {
1147 free(vnd->sc_comp_decombuf, M_DEVBUF);
1148 vnd->sc_comp_decombuf = NULL;
1149 }
1150 #endif /* VND_COMPRESSION */
1151 vndunlock(vnd);
1152 return (error);
1153
1154 case VNDIOCCLR:
1155 if ((error = vndlock(vnd)) != 0)
1156 return (error);
1157
1158 /*
1159 * Don't unconfigure if any other partitions are open
1160 * or if both the character and block flavors of this
1161 * partition are open.
1162 */
1163 part = DISKPART(dev);
1164 pmask = (1 << part);
1165 if (((vnd->sc_dkdev.dk_openmask & ~pmask) ||
1166 ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
1167 (vnd->sc_dkdev.dk_copenmask & pmask))) &&
1168 !(vio->vnd_flags & VNDIOF_FORCE)) {
1169 vndunlock(vnd);
1170 return (EBUSY);
1171 }
1172
1173 /*
1174 * XXX vndclear() might call vndclose() implicitely;
1175 * release lock to avoid recursion
1176 */
1177 vndunlock(vnd);
1178 vndclear(vnd, minor(dev));
1179 #ifdef DEBUG
1180 if (vnddebug & VDB_INIT)
1181 printf("vndioctl: CLRed\n");
1182 #endif
1183
1184 /* Destroy the xfer and buffer pools. */
1185 pool_destroy(&vnd->sc_vxpool);
1186 pool_destroy(&vnd->sc_vbpool);
1187
1188 /* Detatch the disk. */
1189 pseudo_disk_detach(&vnd->sc_dkdev);
1190
1191 break;
1192
1193 #ifdef COMPAT_30
1194 case VNDIOOCGET: {
1195 struct vnd_ouser *vnu;
1196 struct vattr va;
1197 vnu = (struct vnd_ouser *)data;
1198 switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
1199 case 0:
1200 vnu->vnu_dev = va.va_fsid;
1201 vnu->vnu_ino = va.va_fileid;
1202 break;
1203 case -1:
1204 /* unused is not an error */
1205 vnu->vnu_dev = 0;
1206 vnu->vnu_ino = 0;
1207 break;
1208 default:
1209 return error;
1210 }
1211 break;
1212 }
1213 #endif
1214 case VNDIOCGET: {
1215 struct vnd_user *vnu;
1216 struct vattr va;
1217 vnu = (struct vnd_user *)data;
1218 switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
1219 case 0:
1220 vnu->vnu_dev = va.va_fsid;
1221 vnu->vnu_ino = va.va_fileid;
1222 break;
1223 case -1:
1224 /* unused is not an error */
1225 vnu->vnu_dev = 0;
1226 vnu->vnu_ino = 0;
1227 break;
1228 default:
1229 return error;
1230 }
1231 break;
1232 }
1233
1234 case DIOCGDINFO:
1235 *(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
1236 break;
1237
1238 #ifdef __HAVE_OLD_DISKLABEL
1239 case ODIOCGDINFO:
1240 newlabel = *(vnd->sc_dkdev.dk_label);
1241 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1242 return ENOTTY;
1243 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1244 break;
1245 #endif
1246
1247 case DIOCGPART:
1248 ((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
1249 ((struct partinfo *)data)->part =
1250 &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1251 break;
1252
1253 case DIOCWDINFO:
1254 case DIOCSDINFO:
1255 #ifdef __HAVE_OLD_DISKLABEL
1256 case ODIOCWDINFO:
1257 case ODIOCSDINFO:
1258 #endif
1259 {
1260 struct disklabel *lp;
1261
1262 if ((error = vndlock(vnd)) != 0)
1263 return (error);
1264
1265 vnd->sc_flags |= VNF_LABELLING;
1266
1267 #ifdef __HAVE_OLD_DISKLABEL
1268 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1269 memset(&newlabel, 0, sizeof newlabel);
1270 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1271 lp = &newlabel;
1272 } else
1273 #endif
1274 lp = (struct disklabel *)data;
1275
1276 error = setdisklabel(vnd->sc_dkdev.dk_label,
1277 lp, 0, vnd->sc_dkdev.dk_cpulabel);
1278 if (error == 0) {
1279 if (cmd == DIOCWDINFO
1280 #ifdef __HAVE_OLD_DISKLABEL
1281 || cmd == ODIOCWDINFO
1282 #endif
1283 )
1284 error = writedisklabel(VNDLABELDEV(dev),
1285 vndstrategy, vnd->sc_dkdev.dk_label,
1286 vnd->sc_dkdev.dk_cpulabel);
1287 }
1288
1289 vnd->sc_flags &= ~VNF_LABELLING;
1290
1291 vndunlock(vnd);
1292
1293 if (error)
1294 return (error);
1295 break;
1296 }
1297
1298 case DIOCKLABEL:
1299 if (*(int *)data != 0)
1300 vnd->sc_flags |= VNF_KLABEL;
1301 else
1302 vnd->sc_flags &= ~VNF_KLABEL;
1303 break;
1304
1305 case DIOCWLABEL:
1306 if (*(int *)data != 0)
1307 vnd->sc_flags |= VNF_WLABEL;
1308 else
1309 vnd->sc_flags &= ~VNF_WLABEL;
1310 break;
1311
1312 case DIOCGDEFLABEL:
1313 vndgetdefaultlabel(vnd, (struct disklabel *)data);
1314 break;
1315
1316 #ifdef __HAVE_OLD_DISKLABEL
1317 case ODIOCGDEFLABEL:
1318 vndgetdefaultlabel(vnd, &newlabel);
1319 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1320 return ENOTTY;
1321 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1322 break;
1323 #endif
1324
1325 default:
1326 return (ENOTTY);
1327 }
1328
1329 return (0);
1330 }
1331
1332 /*
1333 * Duplicate the current processes' credentials. Since we are called only
1334 * as the result of a SET ioctl and only root can do that, any future access
1335 * to this "disk" is essentially as root. Note that credentials may change
1336 * if some other uid can write directly to the mapped file (NFS).
1337 */
1338 static int
1339 vndsetcred(struct vnd_softc *vnd, struct ucred *cred)
1340 {
1341 struct uio auio;
1342 struct iovec aiov;
1343 char *tmpbuf;
1344 int error;
1345
1346 vnd->sc_cred = crdup(cred);
1347 tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1348
1349 /* XXX: Horrible kludge to establish credentials for NFS */
1350 aiov.iov_base = tmpbuf;
1351 aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
1352 auio.uio_iov = &aiov;
1353 auio.uio_iovcnt = 1;
1354 auio.uio_offset = 0;
1355 auio.uio_rw = UIO_READ;
1356 auio.uio_segflg = UIO_SYSSPACE;
1357 auio.uio_resid = aiov.iov_len;
1358 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1359 error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
1360 if (error == 0) {
1361 /*
1362 * Because vnd does all IO directly through the vnode
1363 * we need to flush (at least) the buffer from the above
1364 * VOP_READ from the buffer cache to prevent cache
1365 * incoherencies. Also, be careful to write dirty
1366 * buffers back to stable storage.
1367 */
1368 error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
1369 curlwp, 0, 0);
1370 }
1371 VOP_UNLOCK(vnd->sc_vp, 0);
1372
1373 free(tmpbuf, M_TEMP);
1374 return (error);
1375 }
1376
1377 /*
1378 * Set maxactive based on FS type
1379 */
1380 static void
1381 vndthrottle(struct vnd_softc *vnd, struct vnode *vp)
1382 {
1383 #ifdef NFS
1384 extern int (**nfsv2_vnodeop_p)(void *);
1385
1386 if (vp->v_op == nfsv2_vnodeop_p)
1387 vnd->sc_maxactive = 2;
1388 else
1389 #endif
1390 vnd->sc_maxactive = 8;
1391
1392 if (vnd->sc_maxactive < 1)
1393 vnd->sc_maxactive = 1;
1394 }
1395
1396 #if 0
1397 static void
1398 vndshutdown(void)
1399 {
1400 struct vnd_softc *vnd;
1401
1402 for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1403 if (vnd->sc_flags & VNF_INITED)
1404 vndclear(vnd);
1405 }
1406 #endif
1407
1408 static void
1409 vndclear(struct vnd_softc *vnd, int myminor)
1410 {
1411 struct vnode *vp = vnd->sc_vp;
1412 struct lwp *l = curlwp;
1413 int fflags = FREAD;
1414 int bmaj, cmaj, i, mn;
1415 int s;
1416
1417 #ifdef DEBUG
1418 if (vnddebug & VDB_FOLLOW)
1419 printf("vndclear(%p): vp %p\n", vnd, vp);
1420 #endif
1421 /* locate the major number */
1422 bmaj = bdevsw_lookup_major(&vnd_bdevsw);
1423 cmaj = cdevsw_lookup_major(&vnd_cdevsw);
1424
1425 /* Nuke the vnodes for any open instances */
1426 for (i = 0; i < MAXPARTITIONS; i++) {
1427 mn = DISKMINOR(vnd->sc_unit, i);
1428 vdevgone(bmaj, mn, mn, VBLK);
1429 if (mn != myminor) /* XXX avoid to kill own vnode */
1430 vdevgone(cmaj, mn, mn, VCHR);
1431 }
1432
1433 if ((vnd->sc_flags & VNF_READONLY) == 0)
1434 fflags |= FWRITE;
1435
1436 s = splbio();
1437 bufq_drain(vnd->sc_tab);
1438 splx(s);
1439
1440 vnd->sc_flags |= VNF_VUNCONF;
1441 wakeup(&vnd->sc_tab);
1442 while (vnd->sc_flags & VNF_KTHREAD)
1443 tsleep(&vnd->sc_kthread, PRIBIO, "vnthr", 0);
1444
1445 #ifdef VND_COMPRESSION
1446 /* free the compressed file buffers */
1447 if(vnd->sc_flags & VNF_COMP) {
1448 if(vnd->sc_comp_offsets) {
1449 free(vnd->sc_comp_offsets, M_DEVBUF);
1450 vnd->sc_comp_offsets = NULL;
1451 }
1452 if(vnd->sc_comp_buff) {
1453 free(vnd->sc_comp_buff, M_DEVBUF);
1454 vnd->sc_comp_buff = NULL;
1455 }
1456 if(vnd->sc_comp_decombuf) {
1457 free(vnd->sc_comp_decombuf, M_DEVBUF);
1458 vnd->sc_comp_decombuf = NULL;
1459 }
1460 }
1461 #endif /* VND_COMPRESSION */
1462 vnd->sc_flags &=
1463 ~(VNF_INITED | VNF_READONLY | VNF_VLABEL
1464 | VNF_VUNCONF | VNF_COMP);
1465 if (vp == (struct vnode *)0)
1466 panic("vndclear: null vp");
1467 (void) vn_close(vp, fflags, vnd->sc_cred, l);
1468 crfree(vnd->sc_cred);
1469 vnd->sc_vp = (struct vnode *)0;
1470 vnd->sc_cred = (struct ucred *)0;
1471 vnd->sc_size = 0;
1472 }
1473
1474 static int
1475 vndsize(dev_t dev)
1476 {
1477 struct vnd_softc *sc;
1478 struct disklabel *lp;
1479 int part, unit, omask;
1480 int size;
1481
1482 unit = vndunit(dev);
1483 if (unit >= numvnd)
1484 return (-1);
1485 sc = &vnd_softc[unit];
1486
1487 if ((sc->sc_flags & VNF_INITED) == 0)
1488 return (-1);
1489
1490 part = DISKPART(dev);
1491 omask = sc->sc_dkdev.dk_openmask & (1 << part);
1492 lp = sc->sc_dkdev.dk_label;
1493
1494 if (omask == 0 && vndopen(dev, 0, S_IFBLK, curlwp)) /* XXX */
1495 return (-1);
1496
1497 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1498 size = -1;
1499 else
1500 size = lp->d_partitions[part].p_size *
1501 (lp->d_secsize / DEV_BSIZE);
1502
1503 if (omask == 0 && vndclose(dev, 0, S_IFBLK, curlwp)) /* XXX */
1504 return (-1);
1505
1506 return (size);
1507 }
1508
1509 static int
1510 vnddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1511 {
1512
1513 /* Not implemented. */
1514 return ENXIO;
1515 }
1516
1517 static void
1518 vndgetdefaultlabel(struct vnd_softc *sc, struct disklabel *lp)
1519 {
1520 struct vndgeom *vng = &sc->sc_geom;
1521 struct partition *pp;
1522
1523 memset(lp, 0, sizeof(*lp));
1524
1525 lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
1526 lp->d_secsize = vng->vng_secsize;
1527 lp->d_nsectors = vng->vng_nsectors;
1528 lp->d_ntracks = vng->vng_ntracks;
1529 lp->d_ncylinders = vng->vng_ncylinders;
1530 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1531
1532 strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1533 lp->d_type = DTYPE_VND;
1534 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1535 lp->d_rpm = 3600;
1536 lp->d_interleave = 1;
1537 lp->d_flags = 0;
1538
1539 pp = &lp->d_partitions[RAW_PART];
1540 pp->p_offset = 0;
1541 pp->p_size = lp->d_secperunit;
1542 pp->p_fstype = FS_UNUSED;
1543 lp->d_npartitions = RAW_PART + 1;
1544
1545 lp->d_magic = DISKMAGIC;
1546 lp->d_magic2 = DISKMAGIC;
1547 lp->d_checksum = dkcksum(lp);
1548 }
1549
1550 /*
1551 * Read the disklabel from a vnd. If one is not present, create a fake one.
1552 */
1553 static void
1554 vndgetdisklabel(dev_t dev)
1555 {
1556 struct vnd_softc *sc = &vnd_softc[vndunit(dev)];
1557 const char *errstring;
1558 struct disklabel *lp = sc->sc_dkdev.dk_label;
1559 struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1560 int i;
1561
1562 memset(clp, 0, sizeof(*clp));
1563
1564 vndgetdefaultlabel(sc, lp);
1565
1566 /*
1567 * Call the generic disklabel extraction routine.
1568 */
1569 errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1570 if (errstring) {
1571 /*
1572 * Lack of disklabel is common, but we print the warning
1573 * anyway, since it might contain other useful information.
1574 */
1575 printf("%s: %s\n", sc->sc_xname, errstring);
1576
1577 /*
1578 * For historical reasons, if there's no disklabel
1579 * present, all partitions must be FS_BSDFFS and
1580 * occupy the entire disk.
1581 */
1582 for (i = 0; i < MAXPARTITIONS; i++) {
1583 /*
1584 * Don't wipe out port specific hack (such as
1585 * dos partition hack of i386 port).
1586 */
1587 if (lp->d_partitions[i].p_size != 0)
1588 continue;
1589
1590 lp->d_partitions[i].p_size = lp->d_secperunit;
1591 lp->d_partitions[i].p_offset = 0;
1592 lp->d_partitions[i].p_fstype = FS_BSDFFS;
1593 }
1594
1595 strncpy(lp->d_packname, "default label",
1596 sizeof(lp->d_packname));
1597
1598 lp->d_npartitions = MAXPARTITIONS;
1599 lp->d_checksum = dkcksum(lp);
1600 }
1601
1602 /* In-core label now valid. */
1603 sc->sc_flags |= VNF_VLABEL;
1604 }
1605
1606 /*
1607 * Wait interruptibly for an exclusive lock.
1608 *
1609 * XXX
1610 * Several drivers do this; it should be abstracted and made MP-safe.
1611 */
1612 static int
1613 vndlock(struct vnd_softc *sc)
1614 {
1615 int error;
1616
1617 while ((sc->sc_flags & VNF_LOCKED) != 0) {
1618 sc->sc_flags |= VNF_WANTED;
1619 if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1620 return (error);
1621 }
1622 sc->sc_flags |= VNF_LOCKED;
1623 return (0);
1624 }
1625
1626 /*
1627 * Unlock and wake up any waiters.
1628 */
1629 static void
1630 vndunlock(struct vnd_softc *sc)
1631 {
1632
1633 sc->sc_flags &= ~VNF_LOCKED;
1634 if ((sc->sc_flags & VNF_WANTED) != 0) {
1635 sc->sc_flags &= ~VNF_WANTED;
1636 wakeup(sc);
1637 }
1638 }
1639
1640 #ifdef VND_COMPRESSION
1641 /* compressed file read */
1642 static void
1643 compstrategy(struct buf *bp, off_t bn)
1644 {
1645 int error;
1646 int unit = vndunit(bp->b_dev);
1647 struct vnd_softc *vnd = &vnd_softc[unit];
1648 u_int32_t comp_block;
1649 struct uio auio;
1650 caddr_t addr;
1651 int s;
1652
1653 /* set up constants for data move */
1654 auio.uio_rw = UIO_READ;
1655 auio.uio_segflg = bp->b_flags & B_PHYS ? UIO_USERSPACE : UIO_SYSSPACE;
1656 auio.uio_lwp = LIST_FIRST(&bp->b_proc->p_lwps);
1657
1658 /* read, and transfer the data */
1659 addr = bp->b_data;
1660 s = splbio();
1661 while (bp->b_resid > 0) {
1662 unsigned length;
1663 size_t length_in_buffer;
1664 u_int32_t offset_in_buffer;
1665 struct iovec aiov;
1666
1667 /* calculate the compressed block number */
1668 comp_block = bn / (off_t)vnd->sc_comp_blksz;
1669
1670 /* check for good block number */
1671 if (comp_block >= vnd->sc_comp_numoffs) {
1672 bp->b_error = EINVAL;
1673 bp->b_flags |= B_ERROR;
1674 splx(s);
1675 return;
1676 }
1677
1678 /* read in the compressed block, if not in buffer */
1679 if (comp_block != vnd->sc_comp_buffblk) {
1680 length = vnd->sc_comp_offsets[comp_block + 1] -
1681 vnd->sc_comp_offsets[comp_block];
1682 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1683 error = vn_rdwr(UIO_READ, vnd->sc_vp, vnd->sc_comp_buff,
1684 length, vnd->sc_comp_offsets[comp_block],
1685 UIO_SYSSPACE, IO_UNIT, vnd->sc_cred, NULL, NULL);
1686 if (error) {
1687 bp->b_error = error;
1688 bp->b_flags |= B_ERROR;
1689 VOP_UNLOCK(vnd->sc_vp, 0);
1690 splx(s);
1691 return;
1692 }
1693 /* uncompress the buffer */
1694 vnd->sc_comp_stream.next_in = vnd->sc_comp_buff;
1695 vnd->sc_comp_stream.avail_in = length;
1696 vnd->sc_comp_stream.next_out = vnd->sc_comp_decombuf;
1697 vnd->sc_comp_stream.avail_out = vnd->sc_comp_blksz;
1698 inflateReset(&vnd->sc_comp_stream);
1699 error = inflate(&vnd->sc_comp_stream, Z_FINISH);
1700 if (error != Z_STREAM_END) {
1701 if (vnd->sc_comp_stream.msg)
1702 printf("%s: compressed file, %s\n",
1703 vnd->sc_xname,
1704 vnd->sc_comp_stream.msg);
1705 bp->b_error = EBADMSG;
1706 bp->b_flags |= B_ERROR;
1707 VOP_UNLOCK(vnd->sc_vp, 0);
1708 splx(s);
1709 return;
1710 }
1711 vnd->sc_comp_buffblk = comp_block;
1712 VOP_UNLOCK(vnd->sc_vp, 0);
1713 }
1714
1715 /* transfer the usable uncompressed data */
1716 offset_in_buffer = bn % (off_t)vnd->sc_comp_blksz;
1717 length_in_buffer = vnd->sc_comp_blksz - offset_in_buffer;
1718 if (length_in_buffer > bp->b_resid)
1719 length_in_buffer = bp->b_resid;
1720 auio.uio_iov = &aiov;
1721 auio.uio_iovcnt = 1;
1722 aiov.iov_base = addr;
1723 aiov.iov_len = length_in_buffer;
1724 auio.uio_resid = aiov.iov_len;
1725 auio.uio_offset = 0;
1726 error = uiomove(vnd->sc_comp_decombuf + offset_in_buffer,
1727 length_in_buffer, &auio);
1728 if (error) {
1729 bp->b_error = error;
1730 bp->b_flags |= B_ERROR;
1731 splx(s);
1732 return;
1733 }
1734
1735 bn += length_in_buffer;
1736 addr += length_in_buffer;
1737 bp->b_resid -= length_in_buffer;
1738 }
1739 splx(s);
1740 }
1741
1742 /* compression memory allocation routines */
1743 static void *
1744 vnd_alloc(void *aux, u_int items, u_int siz)
1745 {
1746 return malloc(items * siz, M_TEMP, M_NOWAIT);
1747 }
1748
1749 static void
1750 vnd_free(void *aux, void *ptr)
1751 {
1752 free(ptr, M_TEMP);
1753 }
1754 #endif /* VND_COMPRESSION */
1755