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