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