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