vnd.c revision 1.136 1 /* $NetBSD: vnd.c,v 1.136 2006/02/04 10:48:32 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1990, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * the Systems Programming Group of the University of Utah Computer
45 * Science Department.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * from: Utah $Hdr: vn.c 1.13 94/04/02$
72 *
73 * @(#)vn.c 8.9 (Berkeley) 5/14/95
74 */
75
76 /*
77 * Copyright (c) 1988 University of Utah.
78 *
79 * This code is derived from software contributed to Berkeley by
80 * the Systems Programming Group of the University of Utah Computer
81 * Science Department.
82 *
83 * Redistribution and use in source and binary forms, with or without
84 * modification, are permitted provided that the following conditions
85 * are met:
86 * 1. Redistributions of source code must retain the above copyright
87 * notice, this list of conditions and the following disclaimer.
88 * 2. Redistributions in binary form must reproduce the above copyright
89 * notice, this list of conditions and the following disclaimer in the
90 * documentation and/or other materials provided with the distribution.
91 * 3. All advertising materials mentioning features or use of this software
92 * must display the following acknowledgement:
93 * This product includes software developed by the University of
94 * California, Berkeley and its contributors.
95 * 4. Neither the name of the University nor the names of its contributors
96 * may be used to endorse or promote products derived from this software
97 * without specific prior written permission.
98 *
99 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
100 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
102 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
103 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
104 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
105 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
106 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
107 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
108 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
109 * SUCH DAMAGE.
110 *
111 * from: Utah $Hdr: vn.c 1.13 94/04/02$
112 *
113 * @(#)vn.c 8.9 (Berkeley) 5/14/95
114 */
115
116 /*
117 * Vnode disk driver.
118 *
119 * Block/character interface to a vnode. Allows one to treat a file
120 * as a disk (e.g. build a filesystem in it, mount it, etc.).
121 *
122 * NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode
123 * instead of a simple VOP_RDWR. We do this to avoid distorting the
124 * local buffer cache.
125 *
126 * NOTE 2: There is a security issue involved with this driver.
127 * Once mounted all access to the contents of the "mapped" file via
128 * the special file is controlled by the permissions on the special
129 * file, the protection of the mapped file is ignored (effectively,
130 * by using root credentials in all transactions).
131 *
132 * NOTE 3: Doesn't interact with leases, should it?
133 */
134
135 #include <sys/cdefs.h>
136 __KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.136 2006/02/04 10:48:32 yamt Exp $");
137
138 #if defined(_KERNEL_OPT)
139 #include "fs_nfs.h"
140 #include "opt_vnd.h"
141 #endif
142
143 #include <sys/param.h>
144 #include <sys/systm.h>
145 #include <sys/namei.h>
146 #include <sys/proc.h>
147 #include <sys/kthread.h>
148 #include <sys/errno.h>
149 #include <sys/buf.h>
150 #include <sys/bufq.h>
151 #include <sys/malloc.h>
152 #include <sys/ioctl.h>
153 #include <sys/disklabel.h>
154 #include <sys/device.h>
155 #include <sys/disk.h>
156 #include <sys/stat.h>
157 #include <sys/mount.h>
158 #include <sys/vnode.h>
159 #include <sys/file.h>
160 #include <sys/uio.h>
161 #include <sys/conf.h>
162 #include <net/zlib.h>
163
164 #include <miscfs/specfs/specdev.h>
165
166 #include <dev/vndvar.h>
167
168 #if defined(VNDDEBUG) && !defined(DEBUG)
169 #define DEBUG
170 #endif
171
172 #ifdef DEBUG
173 int dovndcluster = 1;
174 #define VDB_FOLLOW 0x01
175 #define VDB_INIT 0x02
176 #define VDB_IO 0x04
177 #define VDB_LABEL 0x08
178 int vnddebug = 0x00;
179 #endif
180
181 #define vndunit(x) DISKUNIT(x)
182
183 struct vndxfer {
184 struct buf vx_buf;
185 struct vnd_softc *vx_vnd;
186 };
187 #define VND_BUFTOXFER(bp) ((struct vndxfer *)(void *)bp)
188
189 #define VND_GETXFER(vnd) pool_get(&(vnd)->sc_vxpool, PR_WAITOK)
190 #define VND_PUTXFER(vnd, vx) pool_put(&(vnd)->sc_vxpool, (vx))
191
192 #define VNDLABELDEV(dev) \
193 (MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
194
195 /* called by main() at boot time (XXX: and the LKM driver) */
196 void vndattach(int);
197
198 static void vndclear(struct vnd_softc *, int);
199 static int vndsetcred(struct vnd_softc *, struct ucred *);
200 static void vndthrottle(struct vnd_softc *, struct vnode *);
201 static void vndiodone(struct buf *);
202 #if 0
203 static void vndshutdown(void);
204 #endif
205
206 static void vndgetdefaultlabel(struct vnd_softc *, struct disklabel *);
207 static void vndgetdisklabel(dev_t, struct vnd_softc *);
208
209 static int vndlock(struct vnd_softc *);
210 static void vndunlock(struct vnd_softc *);
211 #ifdef VND_COMPRESSION
212 static void compstrategy(struct buf *, off_t);
213 static void *vnd_alloc(void *, u_int, u_int);
214 static void vnd_free(void *, void *);
215 #endif /* VND_COMPRESSION */
216
217 static void vndthread(void *);
218
219 static dev_type_open(vndopen);
220 static dev_type_close(vndclose);
221 static dev_type_read(vndread);
222 static dev_type_write(vndwrite);
223 static dev_type_ioctl(vndioctl);
224 static dev_type_strategy(vndstrategy);
225 static dev_type_dump(vnddump);
226 static dev_type_size(vndsize);
227
228 const struct bdevsw vnd_bdevsw = {
229 vndopen, vndclose, vndstrategy, vndioctl, vnddump, vndsize, D_DISK
230 };
231
232 const struct cdevsw vnd_cdevsw = {
233 vndopen, vndclose, vndread, vndwrite, vndioctl,
234 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
235 };
236
237 static int vnd_match(struct device *, struct cfdata *, void *);
238 static void vnd_attach(struct device *, struct device *, void *);
239 static int vnd_detach(struct device *, int);
240
241 CFATTACH_DECL(vnd, sizeof(struct vnd_softc),
242 vnd_match, vnd_attach, vnd_detach, NULL);
243 extern struct cfdriver vnd_cd;
244
245 static struct vnd_softc *vnd_spawn(int);
246 int vnd_destroy(struct device *);
247
248 void
249 vndattach(int num)
250 {
251 int error;
252
253 error = config_cfattach_attach(vnd_cd.cd_name, &vnd_ca);
254 if (error)
255 aprint_error("%s: unable to register cfattach\n",
256 vnd_cd.cd_name);
257 }
258
259 static int
260 vnd_match(struct device *self, struct cfdata *cfdata, void *aux)
261 {
262 return 1;
263 }
264
265 static void
266 vnd_attach(struct device *parent, struct device *self, void *aux)
267 {
268 struct vnd_softc *sc = (struct vnd_softc *)self;
269
270 sc->sc_comp_offsets = NULL;
271 sc->sc_comp_buff = NULL;
272 sc->sc_comp_decombuf = NULL;
273 bufq_alloc(&sc->sc_tab, "disksort", BUFQ_SORT_RAWBLOCK);
274 pseudo_disk_init(&sc->sc_dkdev);
275
276 aprint_normal("%s: vnode disk driver\n", self->dv_xname);
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 = dev->dv_cfdata;
312 error = config_detach(dev, 0);
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_label(&vnd->sc_dkdev,
475 bp, vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING)) <= 0)
476 goto done;
477 }
478
479 /* If it's a nil transfer, wake up the top half now. */
480 if (bp->b_bcount == 0)
481 goto done;
482
483 /*
484 * Put the block number in terms of the logical blocksize
485 * of the "device".
486 */
487
488 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
489
490 /*
491 * Translate the partition-relative block number to an absolute.
492 */
493 if (DISKPART(bp->b_dev) != RAW_PART) {
494 struct partition *pp;
495
496 pp = &vnd->sc_dkdev.dk_label->d_partitions[
497 DISKPART(bp->b_dev)];
498 blkno += pp->p_offset;
499 }
500 bp->b_rawblkno = blkno;
501
502 #ifdef DEBUG
503 if (vnddebug & VDB_FOLLOW)
504 printf("vndstrategy(%p): unit %d\n", bp, unit);
505 #endif
506 BUFQ_PUT(vnd->sc_tab, bp);
507 wakeup(&vnd->sc_tab);
508 splx(s);
509 return;
510 done:
511 biodone(bp);
512 splx(s);
513 }
514
515 static void
516 vndthread(void *arg)
517 {
518 struct vnd_softc *vnd = arg;
519 struct mount *mp;
520 int s, bsize;
521 int sz, error;
522 struct disklabel *lp;
523
524 s = splbio();
525 vnd->sc_flags |= VNF_KTHREAD;
526 wakeup(&vnd->sc_kthread);
527
528 /*
529 * Dequeue requests, break them into bsize pieces and submit using
530 * VOP_BMAP/VOP_STRATEGY.
531 */
532 while ((vnd->sc_flags & VNF_VUNCONF) == 0) {
533 struct vndxfer *vnx;
534 off_t offset;
535 int resid;
536 int skipped = 0;
537 off_t bn;
538 int flags;
539 struct buf *obp;
540 struct buf *bp;
541
542 obp = BUFQ_GET(vnd->sc_tab);
543 if (obp == NULL) {
544 tsleep(&vnd->sc_tab, PRIBIO, "vndbp", 0);
545 continue;
546 };
547 splx(s);
548 flags = obp->b_flags;
549 #ifdef DEBUG
550 if (vnddebug & VDB_FOLLOW)
551 printf("vndthread(%p\n", obp);
552 #endif
553 lp = vnd->sc_dkdev.dk_label;
554
555 /* convert to a byte offset within the file. */
556 bn = obp->b_rawblkno * lp->d_secsize;
557
558 if (vnd->sc_vp->v_mount == NULL) {
559 obp->b_error = ENXIO;
560 obp->b_flags |= B_ERROR;
561 goto done;
562 }
563 #ifdef VND_COMPRESSION
564 /* handle a compressed read */
565 if ((flags & B_READ) != 0 && (vnd->sc_flags & VNF_COMP)) {
566 compstrategy(obp, bn);
567 goto done;
568 }
569 #endif /* VND_COMPRESSION */
570
571 bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
572
573 /*
574 * Allocate a header for this transfer and link it to the
575 * buffer
576 */
577 s = splbio();
578 vnx = VND_GETXFER(vnd);
579 splx(s);
580 vnx->vx_vnd = vnd;
581
582 bp = &vnx->vx_buf;
583 BUF_INIT(bp);
584 bp->b_flags = (obp->b_flags & B_READ) | B_CALL;
585 bp->b_iodone = vndiodone;
586 bp->b_private = obp;
587 bp->b_vp = NULL;
588 bp->b_data = obp->b_data;
589 bp->b_bcount = bp->b_resid = obp->b_bcount;
590 BIO_COPYPRIO(bp, obp);
591
592 s = splbio();
593 while (vnd->sc_active >= vnd->sc_maxactive) {
594 tsleep(&vnd->sc_tab, PRIBIO, "vndac", 0);
595 }
596 vnd->sc_active++;
597 splx(s);
598
599 if ((flags & B_READ) == 0)
600 vn_start_write(vnd->sc_vp, &mp, V_WAIT);
601
602 /* Instrumentation. */
603 disk_busy(&vnd->sc_dkdev);
604
605 /*
606 * Feed requests sequentially.
607 * We do it this way to keep from flooding NFS servers if we
608 * are connected to an NFS file. This places the burden on
609 * the client rather than the server.
610 */
611 error = 0;
612 for (offset = 0, resid = bp->b_resid; resid;
613 resid -= sz, offset += sz) {
614 struct buf *nbp;
615 struct vnode *vp;
616 daddr_t nbn;
617 int off, nra;
618
619 nra = 0;
620 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
621 error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
622 VOP_UNLOCK(vnd->sc_vp, 0);
623
624 if (error == 0 && (long)nbn == -1)
625 error = EIO;
626
627 /*
628 * If there was an error or a hole in the file...punt.
629 * Note that we may have to wait for any operations
630 * that we have already fired off before releasing
631 * the buffer.
632 *
633 * XXX we could deal with holes here but it would be
634 * a hassle (in the write case).
635 */
636 if (error) {
637 skipped += resid;
638 break;
639 }
640
641 #ifdef DEBUG
642 if (!dovndcluster)
643 nra = 0;
644 #endif
645
646 if ((off = bn % bsize) != 0)
647 sz = bsize - off;
648 else
649 sz = (1 + nra) * bsize;
650 if (resid < sz)
651 sz = resid;
652 #ifdef DEBUG
653 if (vnddebug & VDB_IO)
654 printf("vndstrategy: vp %p/%p bn 0x%qx/0x%" PRIx64
655 " sz 0x%x\n",
656 vnd->sc_vp, vp, (long long)bn, nbn, sz);
657 #endif
658
659 nbp = getiobuf();
660 nestiobuf_setup(bp, nbp, offset, sz);
661 nbp->b_blkno = nbn + btodb(off);
662
663 #if 0 /* XXX #ifdef DEBUG */
664 if (vnddebug & VDB_IO)
665 printf("vndstart(%ld): bp %p vp %p blkno "
666 "0x%" PRIx64 " flags %x addr %p cnt 0x%x\n",
667 (long) (vnd-vnd_softc), &nbp->vb_buf,
668 nbp->vb_buf.b_vp, nbp->vb_buf.b_blkno,
669 nbp->vb_buf.b_flags, nbp->vb_buf.b_data,
670 nbp->vb_buf.b_bcount);
671 #endif
672 VOP_STRATEGY(vp, nbp);
673 bn += sz;
674 }
675 nestiobuf_done(bp, skipped, error);
676
677 if ((flags & B_READ) == 0)
678 vn_finished_write(mp, 0);
679
680 s = splbio();
681 continue;
682 done:
683 biodone(obp);
684 s = splbio();
685 }
686
687 vnd->sc_flags &= (~VNF_KTHREAD | VNF_VUNCONF);
688 wakeup(&vnd->sc_kthread);
689 splx(s);
690 kthread_exit(0);
691 }
692
693 static void
694 vndiodone(struct buf *bp)
695 {
696 struct vndxfer *vnx = VND_BUFTOXFER(bp);
697 struct vnd_softc *vnd = vnx->vx_vnd;
698 struct buf *obp = bp->b_private;
699
700 KASSERT(&vnx->vx_buf == bp);
701 KASSERT(vnd->sc_active > 0);
702 #ifdef DEBUG
703 if (vnddebug & VDB_IO) {
704 printf("vndiodone1: bp %p iodone: error %d\n",
705 bp, (bp->b_flags & B_ERROR) != 0 ? bp->b_error : 0);
706 }
707 #endif
708 disk_unbusy(&vnd->sc_dkdev, bp->b_bcount - bp->b_resid,
709 (bp->b_flags & B_READ));
710 vnd->sc_active--;
711 if (vnd->sc_active == 0) {
712 wakeup(&vnd->sc_tab);
713 }
714 obp->b_flags |= bp->b_flags & B_ERROR;
715 obp->b_error = bp->b_error;
716 obp->b_resid = bp->b_resid;
717 VND_PUTXFER(vnd, vnx);
718 biodone(obp);
719 }
720
721 /* ARGSUSED */
722 static int
723 vndread(dev_t dev, struct uio *uio, int flags)
724 {
725 int unit = vndunit(dev);
726 struct vnd_softc *sc;
727
728 #ifdef DEBUG
729 if (vnddebug & VDB_FOLLOW)
730 printf("vndread(0x%x, %p)\n", dev, uio);
731 #endif
732
733 sc = device_lookup(&vnd_cd, unit);
734 if (sc == NULL)
735 return ENXIO;
736
737 if ((sc->sc_flags & VNF_INITED) == 0)
738 return (ENXIO);
739
740 return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
741 }
742
743 /* ARGSUSED */
744 static int
745 vndwrite(dev_t dev, struct uio *uio, int flags)
746 {
747 int unit = vndunit(dev);
748 struct vnd_softc *sc;
749
750 #ifdef DEBUG
751 if (vnddebug & VDB_FOLLOW)
752 printf("vndwrite(0x%x, %p)\n", dev, uio);
753 #endif
754
755 sc = device_lookup(&vnd_cd, unit);
756 if (sc == NULL)
757 return ENXIO;
758
759 if ((sc->sc_flags & VNF_INITED) == 0)
760 return (ENXIO);
761
762 return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
763 }
764
765 static int
766 vnd_cget(struct lwp *l, int unit, int *un, struct vattr *va)
767 {
768 struct vnd_softc *vnd;
769
770 if (*un == -1)
771 *un = unit;
772 if (*un < 0)
773 return EINVAL;
774
775 vnd = device_lookup(&vnd_cd, *un);
776 if (vnd == NULL)
777 /*
778 * vnconfig(8) has weird expectations to list the
779 * devices.
780 * It will stop as soon as it gets ENXIO, but
781 * will continue if it gets something else...
782 */
783 return (*un >= vnd_cd.cd_ndevs) ? ENXIO : -1;
784
785 if ((vnd->sc_flags & VNF_INITED) == 0)
786 return -1;
787
788 return VOP_GETATTR(vnd->sc_vp, va, l->l_proc->p_ucred, l);
789 }
790
791 /* ARGSUSED */
792 static int
793 vndioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
794 {
795 int unit = vndunit(dev);
796 struct vnd_softc *vnd;
797 struct vnd_ioctl *vio;
798 struct vattr vattr;
799 struct nameidata nd;
800 int error, part, pmask;
801 size_t geomsize;
802 struct proc *p = (l != NULL) ? l->l_proc : NULL;
803 int fflags;
804 #ifdef __HAVE_OLD_DISKLABEL
805 struct disklabel newlabel;
806 #endif
807
808 #ifdef DEBUG
809 if (vnddebug & VDB_FOLLOW)
810 printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
811 dev, cmd, data, flag, p, unit);
812 #endif
813 vnd = device_lookup(&vnd_cd, unit);
814 if (vnd == NULL &&
815 #ifdef COMPAT_30
816 cmd != VNDIOOCGET &&
817 #endif
818 cmd != VNDIOCGET)
819 return ENXIO;
820 vio = (struct vnd_ioctl *)data;
821
822 /* Must be open for writes for these commands... */
823 switch (cmd) {
824 case VNDIOCSET:
825 case VNDIOCCLR:
826 case DIOCSDINFO:
827 case DIOCWDINFO:
828 #ifdef __HAVE_OLD_DISKLABEL
829 case ODIOCSDINFO:
830 case ODIOCWDINFO:
831 #endif
832 case DIOCKLABEL:
833 case DIOCWLABEL:
834 if ((flag & FWRITE) == 0)
835 return (EBADF);
836 }
837
838 /* Must be initialized for these... */
839 switch (cmd) {
840 case VNDIOCCLR:
841 case DIOCGDINFO:
842 case DIOCSDINFO:
843 case DIOCWDINFO:
844 case DIOCGPART:
845 case DIOCKLABEL:
846 case DIOCWLABEL:
847 case DIOCGDEFLABEL:
848 #ifdef __HAVE_OLD_DISKLABEL
849 case ODIOCGDINFO:
850 case ODIOCSDINFO:
851 case ODIOCWDINFO:
852 case ODIOCGDEFLABEL:
853 #endif
854 if ((vnd->sc_flags & VNF_INITED) == 0)
855 return (ENXIO);
856 }
857
858 switch (cmd) {
859 case VNDIOCSET:
860 if (vnd->sc_flags & VNF_INITED)
861 return (EBUSY);
862
863 if ((error = vndlock(vnd)) != 0)
864 return (error);
865
866 fflags = FREAD;
867 if ((vio->vnd_flags & VNDIOF_READONLY) == 0)
868 fflags |= FWRITE;
869 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, l);
870 if ((error = vn_open(&nd, fflags, 0)) != 0)
871 goto unlock_and_exit;
872 error = VOP_GETATTR(nd.ni_vp, &vattr, l->l_proc->p_ucred, l);
873 if (!error && nd.ni_vp->v_type != VREG)
874 error = EOPNOTSUPP;
875 if (error) {
876 VOP_UNLOCK(nd.ni_vp, 0);
877 goto close_and_exit;
878 }
879
880 /* If using a compressed file, initialize its info */
881 /* (or abort with an error if kernel has no compression) */
882 if (vio->vnd_flags & VNF_COMP) {
883 #ifdef VND_COMPRESSION
884 struct vnd_comp_header *ch;
885 int i;
886 u_int32_t comp_size;
887 u_int32_t comp_maxsize;
888
889 /* allocate space for compresed file header */
890 ch = malloc(sizeof(struct vnd_comp_header),
891 M_TEMP, M_WAITOK);
892
893 /* read compressed file header */
894 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)ch,
895 sizeof(struct vnd_comp_header), 0, UIO_SYSSPACE,
896 IO_UNIT|IO_NODELOCKED, p->p_ucred, NULL, NULL);
897 if(error) {
898 free(ch, M_TEMP);
899 VOP_UNLOCK(nd.ni_vp, 0);
900 goto close_and_exit;
901 }
902
903 /* save some header info */
904 vnd->sc_comp_blksz = ntohl(ch->block_size);
905 /* note last offset is the file byte size */
906 vnd->sc_comp_numoffs = ntohl(ch->num_blocks)+1;
907 free(ch, M_TEMP);
908 if(vnd->sc_comp_blksz % DEV_BSIZE !=0) {
909 VOP_UNLOCK(nd.ni_vp, 0);
910 error = EINVAL;
911 goto close_and_exit;
912 }
913 if(sizeof(struct vnd_comp_header) +
914 sizeof(u_int64_t) * vnd->sc_comp_numoffs >
915 vattr.va_size) {
916 VOP_UNLOCK(nd.ni_vp, 0);
917 error = EINVAL;
918 goto close_and_exit;
919 }
920
921 /* set decompressed file size */
922 vattr.va_size =
923 (vnd->sc_comp_numoffs - 1) * vnd->sc_comp_blksz;
924
925 /* allocate space for all the compressed offsets */
926 vnd->sc_comp_offsets =
927 malloc(sizeof(u_int64_t) * vnd->sc_comp_numoffs,
928 M_DEVBUF, M_WAITOK);
929
930 /* read in the offsets */
931 error = vn_rdwr(UIO_READ, nd.ni_vp,
932 (caddr_t)vnd->sc_comp_offsets,
933 sizeof(u_int64_t) * vnd->sc_comp_numoffs,
934 sizeof(struct vnd_comp_header), UIO_SYSSPACE,
935 IO_UNIT|IO_NODELOCKED, p->p_ucred, NULL, NULL);
936 if(error) {
937 VOP_UNLOCK(nd.ni_vp, 0);
938 goto close_and_exit;
939 }
940 /*
941 * find largest block size (used for allocation limit).
942 * Also convert offset to native byte order.
943 */
944 comp_maxsize = 0;
945 for (i = 0; i < vnd->sc_comp_numoffs - 1; i++) {
946 vnd->sc_comp_offsets[i] =
947 be64toh(vnd->sc_comp_offsets[i]);
948 comp_size = be64toh(vnd->sc_comp_offsets[i + 1])
949 - vnd->sc_comp_offsets[i];
950 if (comp_size > comp_maxsize)
951 comp_maxsize = comp_size;
952 }
953 vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1] =
954 be64toh(vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1]);
955
956 /* create compressed data buffer */
957 vnd->sc_comp_buff = malloc(comp_maxsize,
958 M_DEVBUF, M_WAITOK);
959
960 /* create decompressed buffer */
961 vnd->sc_comp_decombuf = malloc(vnd->sc_comp_blksz,
962 M_DEVBUF, M_WAITOK);
963 vnd->sc_comp_buffblk = -1;
964
965 /* Initialize decompress stream */
966 bzero(&vnd->sc_comp_stream, sizeof(z_stream));
967 vnd->sc_comp_stream.zalloc = vnd_alloc;
968 vnd->sc_comp_stream.zfree = vnd_free;
969 error = inflateInit2(&vnd->sc_comp_stream, MAX_WBITS);
970 if(error) {
971 if(vnd->sc_comp_stream.msg)
972 printf("vnd%d: compressed file, %s\n",
973 unit, vnd->sc_comp_stream.msg);
974 VOP_UNLOCK(nd.ni_vp, 0);
975 error = EINVAL;
976 goto close_and_exit;
977 }
978
979 vnd->sc_flags |= VNF_COMP | VNF_READONLY;
980 #else /* !VND_COMPRESSION */
981 VOP_UNLOCK(nd.ni_vp, 0);
982 error = EOPNOTSUPP;
983 goto close_and_exit;
984 #endif /* VND_COMPRESSION */
985 }
986
987 VOP_UNLOCK(nd.ni_vp, 0);
988 vnd->sc_vp = nd.ni_vp;
989 vnd->sc_size = btodb(vattr.va_size); /* note truncation */
990
991 /*
992 * Use pseudo-geometry specified. If none was provided,
993 * use "standard" Adaptec fictitious geometry.
994 */
995 if (vio->vnd_flags & VNDIOF_HASGEOM) {
996
997 memcpy(&vnd->sc_geom, &vio->vnd_geom,
998 sizeof(vio->vnd_geom));
999
1000 /*
1001 * Sanity-check the sector size.
1002 * XXX Don't allow secsize < DEV_BSIZE. Should
1003 * XXX we?
1004 */
1005 if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
1006 (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0 ||
1007 vnd->sc_geom.vng_ncylinders == 0 ||
1008 (vnd->sc_geom.vng_ntracks *
1009 vnd->sc_geom.vng_nsectors) == 0) {
1010 error = EINVAL;
1011 goto close_and_exit;
1012 }
1013
1014 /*
1015 * Compute the size (in DEV_BSIZE blocks) specified
1016 * by the geometry.
1017 */
1018 geomsize = (vnd->sc_geom.vng_nsectors *
1019 vnd->sc_geom.vng_ntracks *
1020 vnd->sc_geom.vng_ncylinders) *
1021 (vnd->sc_geom.vng_secsize / DEV_BSIZE);
1022
1023 /*
1024 * Sanity-check the size against the specified
1025 * geometry.
1026 */
1027 if (vnd->sc_size < geomsize) {
1028 error = EINVAL;
1029 goto close_and_exit;
1030 }
1031 } else if (vnd->sc_size >= (32 * 64)) {
1032 /*
1033 * Size must be at least 2048 DEV_BSIZE blocks
1034 * (1M) in order to use this geometry.
1035 */
1036 vnd->sc_geom.vng_secsize = DEV_BSIZE;
1037 vnd->sc_geom.vng_nsectors = 32;
1038 vnd->sc_geom.vng_ntracks = 64;
1039 vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
1040 } else {
1041 vnd->sc_geom.vng_secsize = DEV_BSIZE;
1042 vnd->sc_geom.vng_nsectors = 1;
1043 vnd->sc_geom.vng_ntracks = 1;
1044 vnd->sc_geom.vng_ncylinders = vnd->sc_size;
1045 }
1046
1047 if (vio->vnd_flags & VNDIOF_READONLY) {
1048 vnd->sc_flags |= VNF_READONLY;
1049 }
1050
1051 if ((error = vndsetcred(vnd, p->p_ucred)) != 0)
1052 goto close_and_exit;
1053
1054 vndthrottle(vnd, vnd->sc_vp);
1055 vio->vnd_size = dbtob(vnd->sc_size);
1056 vnd->sc_flags |= VNF_INITED;
1057
1058 /* create the kernel thread, wait for it to be up */
1059 error = kthread_create1(vndthread, vnd, &vnd->sc_kthread,
1060 vnd->sc_dev.dv_xname);
1061 if (error)
1062 goto close_and_exit;
1063 while ((vnd->sc_flags & VNF_KTHREAD) == 0) {
1064 tsleep(&vnd->sc_kthread, PRIBIO, "vndthr", 0);
1065 }
1066 #ifdef DEBUG
1067 if (vnddebug & VDB_INIT)
1068 printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
1069 vnd->sc_vp, (unsigned long) vnd->sc_size,
1070 vnd->sc_geom.vng_secsize,
1071 vnd->sc_geom.vng_nsectors,
1072 vnd->sc_geom.vng_ntracks,
1073 vnd->sc_geom.vng_ncylinders);
1074 #endif
1075
1076 /* Attach the disk. */
1077 vnd->sc_dkdev.dk_name = vnd->sc_dev.dv_xname;
1078 pseudo_disk_attach(&vnd->sc_dkdev);
1079
1080 /* Initialize the xfer and buffer pools. */
1081 pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
1082 0, 0, "vndxpl", NULL);
1083
1084 /* Try and read the disklabel. */
1085 vndgetdisklabel(dev, vnd);
1086
1087 vndunlock(vnd);
1088
1089 break;
1090
1091 close_and_exit:
1092 (void) vn_close(nd.ni_vp, fflags, p->p_ucred, l);
1093 unlock_and_exit:
1094 #ifdef VND_COMPRESSION
1095 /* free any allocated memory (for compressed file) */
1096 if(vnd->sc_comp_offsets) {
1097 free(vnd->sc_comp_offsets, M_DEVBUF);
1098 vnd->sc_comp_offsets = NULL;
1099 }
1100 if(vnd->sc_comp_buff) {
1101 free(vnd->sc_comp_buff, M_DEVBUF);
1102 vnd->sc_comp_buff = NULL;
1103 }
1104 if(vnd->sc_comp_decombuf) {
1105 free(vnd->sc_comp_decombuf, M_DEVBUF);
1106 vnd->sc_comp_decombuf = NULL;
1107 }
1108 #endif /* VND_COMPRESSION */
1109 vndunlock(vnd);
1110 return (error);
1111
1112 case VNDIOCCLR:
1113 if ((error = vndlock(vnd)) != 0)
1114 return (error);
1115
1116 /*
1117 * Don't unconfigure if any other partitions are open
1118 * or if both the character and block flavors of this
1119 * partition are open.
1120 */
1121 part = DISKPART(dev);
1122 pmask = (1 << part);
1123 if (((vnd->sc_dkdev.dk_openmask & ~pmask) ||
1124 ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
1125 (vnd->sc_dkdev.dk_copenmask & pmask))) &&
1126 !(vio->vnd_flags & VNDIOF_FORCE)) {
1127 vndunlock(vnd);
1128 return (EBUSY);
1129 }
1130
1131 /*
1132 * XXX vndclear() might call vndclose() implicitely;
1133 * release lock to avoid recursion
1134 */
1135 vndunlock(vnd);
1136 vndclear(vnd, minor(dev));
1137 #ifdef DEBUG
1138 if (vnddebug & VDB_INIT)
1139 printf("vndioctl: CLRed\n");
1140 #endif
1141
1142 /* Destroy the xfer and buffer pools. */
1143 pool_destroy(&vnd->sc_vxpool);
1144
1145 /* Detatch the disk. */
1146 pseudo_disk_detach(&vnd->sc_dkdev);
1147 break;
1148
1149 #ifdef COMPAT_30
1150 case VNDIOOCGET: {
1151 struct vnd_ouser *vnu;
1152 struct vattr va;
1153 vnu = (struct vnd_ouser *)data;
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 switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
1175 case 0:
1176 vnu->vnu_dev = va.va_fsid;
1177 vnu->vnu_ino = va.va_fileid;
1178 break;
1179 case -1:
1180 /* unused is not an error */
1181 vnu->vnu_dev = 0;
1182 vnu->vnu_ino = 0;
1183 break;
1184 default:
1185 return error;
1186 }
1187 break;
1188 }
1189
1190 case DIOCGDINFO:
1191 *(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
1192 break;
1193
1194 #ifdef __HAVE_OLD_DISKLABEL
1195 case ODIOCGDINFO:
1196 newlabel = *(vnd->sc_dkdev.dk_label);
1197 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1198 return ENOTTY;
1199 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1200 break;
1201 #endif
1202
1203 case DIOCGPART:
1204 ((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
1205 ((struct partinfo *)data)->part =
1206 &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1207 break;
1208
1209 case DIOCWDINFO:
1210 case DIOCSDINFO:
1211 #ifdef __HAVE_OLD_DISKLABEL
1212 case ODIOCWDINFO:
1213 case ODIOCSDINFO:
1214 #endif
1215 {
1216 struct disklabel *lp;
1217
1218 if ((error = vndlock(vnd)) != 0)
1219 return (error);
1220
1221 vnd->sc_flags |= VNF_LABELLING;
1222
1223 #ifdef __HAVE_OLD_DISKLABEL
1224 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1225 memset(&newlabel, 0, sizeof newlabel);
1226 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1227 lp = &newlabel;
1228 } else
1229 #endif
1230 lp = (struct disklabel *)data;
1231
1232 error = setdisklabel(vnd->sc_dkdev.dk_label,
1233 lp, 0, vnd->sc_dkdev.dk_cpulabel);
1234 if (error == 0) {
1235 if (cmd == DIOCWDINFO
1236 #ifdef __HAVE_OLD_DISKLABEL
1237 || cmd == ODIOCWDINFO
1238 #endif
1239 )
1240 error = writedisklabel(VNDLABELDEV(dev),
1241 vndstrategy, vnd->sc_dkdev.dk_label,
1242 vnd->sc_dkdev.dk_cpulabel);
1243 }
1244
1245 vnd->sc_flags &= ~VNF_LABELLING;
1246
1247 vndunlock(vnd);
1248
1249 if (error)
1250 return (error);
1251 break;
1252 }
1253
1254 case DIOCKLABEL:
1255 if (*(int *)data != 0)
1256 vnd->sc_flags |= VNF_KLABEL;
1257 else
1258 vnd->sc_flags &= ~VNF_KLABEL;
1259 break;
1260
1261 case DIOCWLABEL:
1262 if (*(int *)data != 0)
1263 vnd->sc_flags |= VNF_WLABEL;
1264 else
1265 vnd->sc_flags &= ~VNF_WLABEL;
1266 break;
1267
1268 case DIOCGDEFLABEL:
1269 vndgetdefaultlabel(vnd, (struct disklabel *)data);
1270 break;
1271
1272 #ifdef __HAVE_OLD_DISKLABEL
1273 case ODIOCGDEFLABEL:
1274 vndgetdefaultlabel(vnd, &newlabel);
1275 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1276 return ENOTTY;
1277 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1278 break;
1279 #endif
1280
1281 default:
1282 return (ENOTTY);
1283 }
1284
1285 return (0);
1286 }
1287
1288 /*
1289 * Duplicate the current processes' credentials. Since we are called only
1290 * as the result of a SET ioctl and only root can do that, any future access
1291 * to this "disk" is essentially as root. Note that credentials may change
1292 * if some other uid can write directly to the mapped file (NFS).
1293 */
1294 static int
1295 vndsetcred(struct vnd_softc *vnd, struct ucred *cred)
1296 {
1297 struct uio auio;
1298 struct iovec aiov;
1299 char *tmpbuf;
1300 int error;
1301
1302 vnd->sc_cred = crdup(cred);
1303 tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1304
1305 /* XXX: Horrible kludge to establish credentials for NFS */
1306 aiov.iov_base = tmpbuf;
1307 aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
1308 auio.uio_iov = &aiov;
1309 auio.uio_iovcnt = 1;
1310 auio.uio_offset = 0;
1311 auio.uio_rw = UIO_READ;
1312 auio.uio_segflg = UIO_SYSSPACE;
1313 auio.uio_resid = aiov.iov_len;
1314 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1315 error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
1316 if (error == 0) {
1317 /*
1318 * Because vnd does all IO directly through the vnode
1319 * we need to flush (at least) the buffer from the above
1320 * VOP_READ from the buffer cache to prevent cache
1321 * incoherencies. Also, be careful to write dirty
1322 * buffers back to stable storage.
1323 */
1324 error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
1325 curlwp, 0, 0);
1326 }
1327 VOP_UNLOCK(vnd->sc_vp, 0);
1328
1329 free(tmpbuf, M_TEMP);
1330 return (error);
1331 }
1332
1333 /*
1334 * Set maxactive based on FS type
1335 */
1336 static void
1337 vndthrottle(struct vnd_softc *vnd, struct vnode *vp)
1338 {
1339 #ifdef NFS
1340 extern int (**nfsv2_vnodeop_p)(void *);
1341
1342 if (vp->v_op == nfsv2_vnodeop_p)
1343 vnd->sc_maxactive = 2;
1344 else
1345 #endif
1346 vnd->sc_maxactive = 8;
1347
1348 if (vnd->sc_maxactive < 1)
1349 vnd->sc_maxactive = 1;
1350 }
1351
1352 #if 0
1353 static void
1354 vndshutdown(void)
1355 {
1356 struct vnd_softc *vnd;
1357
1358 for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1359 if (vnd->sc_flags & VNF_INITED)
1360 vndclear(vnd);
1361 }
1362 #endif
1363
1364 static void
1365 vndclear(struct vnd_softc *vnd, int myminor)
1366 {
1367 struct vnode *vp = vnd->sc_vp;
1368 struct lwp *l = curlwp;
1369 int fflags = FREAD;
1370 int bmaj, cmaj, i, mn;
1371 int s;
1372
1373 #ifdef DEBUG
1374 if (vnddebug & VDB_FOLLOW)
1375 printf("vndclear(%p): vp %p\n", vnd, vp);
1376 #endif
1377 /* locate the major number */
1378 bmaj = bdevsw_lookup_major(&vnd_bdevsw);
1379 cmaj = cdevsw_lookup_major(&vnd_cdevsw);
1380
1381 /* Nuke the vnodes for any open instances */
1382 for (i = 0; i < MAXPARTITIONS; i++) {
1383 mn = DISKMINOR(vnd->sc_dev.dv_unit, i);
1384 vdevgone(bmaj, mn, mn, VBLK);
1385 if (mn != myminor) /* XXX avoid to kill own vnode */
1386 vdevgone(cmaj, mn, mn, VCHR);
1387 }
1388
1389 if ((vnd->sc_flags & VNF_READONLY) == 0)
1390 fflags |= FWRITE;
1391
1392 s = splbio();
1393 bufq_drain(vnd->sc_tab);
1394 splx(s);
1395
1396 vnd->sc_flags |= VNF_VUNCONF;
1397 wakeup(&vnd->sc_tab);
1398 while (vnd->sc_flags & VNF_KTHREAD)
1399 tsleep(&vnd->sc_kthread, PRIBIO, "vnthr", 0);
1400
1401 #ifdef VND_COMPRESSION
1402 /* free the compressed file buffers */
1403 if(vnd->sc_flags & VNF_COMP) {
1404 if(vnd->sc_comp_offsets) {
1405 free(vnd->sc_comp_offsets, M_DEVBUF);
1406 vnd->sc_comp_offsets = NULL;
1407 }
1408 if(vnd->sc_comp_buff) {
1409 free(vnd->sc_comp_buff, M_DEVBUF);
1410 vnd->sc_comp_buff = NULL;
1411 }
1412 if(vnd->sc_comp_decombuf) {
1413 free(vnd->sc_comp_decombuf, M_DEVBUF);
1414 vnd->sc_comp_decombuf = NULL;
1415 }
1416 }
1417 #endif /* VND_COMPRESSION */
1418 vnd->sc_flags &=
1419 ~(VNF_INITED | VNF_READONLY | VNF_VLABEL
1420 | VNF_VUNCONF | VNF_COMP);
1421 if (vp == (struct vnode *)0)
1422 panic("vndclear: null vp");
1423 (void) vn_close(vp, fflags, vnd->sc_cred, l);
1424 crfree(vnd->sc_cred);
1425 vnd->sc_vp = (struct vnode *)0;
1426 vnd->sc_cred = (struct ucred *)0;
1427 vnd->sc_size = 0;
1428 }
1429
1430 static int
1431 vndsize(dev_t dev)
1432 {
1433 struct vnd_softc *sc;
1434 struct disklabel *lp;
1435 int part, unit, omask;
1436 int size;
1437
1438 unit = vndunit(dev);
1439 sc = (struct vnd_softc *)device_lookup(&vnd_cd, unit);
1440 if (sc == NULL)
1441 return -1;
1442
1443 if ((sc->sc_flags & VNF_INITED) == 0)
1444 return (-1);
1445
1446 part = DISKPART(dev);
1447 omask = sc->sc_dkdev.dk_openmask & (1 << part);
1448 lp = sc->sc_dkdev.dk_label;
1449
1450 if (omask == 0 && vndopen(dev, 0, S_IFBLK, curlwp)) /* XXX */
1451 return (-1);
1452
1453 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1454 size = -1;
1455 else
1456 size = lp->d_partitions[part].p_size *
1457 (lp->d_secsize / DEV_BSIZE);
1458
1459 if (omask == 0 && vndclose(dev, 0, S_IFBLK, curlwp)) /* XXX */
1460 return (-1);
1461
1462 return (size);
1463 }
1464
1465 static int
1466 vnddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1467 {
1468
1469 /* Not implemented. */
1470 return ENXIO;
1471 }
1472
1473 static void
1474 vndgetdefaultlabel(struct vnd_softc *sc, struct disklabel *lp)
1475 {
1476 struct vndgeom *vng = &sc->sc_geom;
1477 struct partition *pp;
1478
1479 memset(lp, 0, sizeof(*lp));
1480
1481 lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
1482 lp->d_secsize = vng->vng_secsize;
1483 lp->d_nsectors = vng->vng_nsectors;
1484 lp->d_ntracks = vng->vng_ntracks;
1485 lp->d_ncylinders = vng->vng_ncylinders;
1486 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1487
1488 strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1489 lp->d_type = DTYPE_VND;
1490 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1491 lp->d_rpm = 3600;
1492 lp->d_interleave = 1;
1493 lp->d_flags = 0;
1494
1495 pp = &lp->d_partitions[RAW_PART];
1496 pp->p_offset = 0;
1497 pp->p_size = lp->d_secperunit;
1498 pp->p_fstype = FS_UNUSED;
1499 lp->d_npartitions = RAW_PART + 1;
1500
1501 lp->d_magic = DISKMAGIC;
1502 lp->d_magic2 = DISKMAGIC;
1503 lp->d_checksum = dkcksum(lp);
1504 }
1505
1506 /*
1507 * Read the disklabel from a vnd. If one is not present, create a fake one.
1508 */
1509 static void
1510 vndgetdisklabel(dev_t dev, struct vnd_softc *sc)
1511 {
1512 const char *errstring;
1513 struct disklabel *lp = sc->sc_dkdev.dk_label;
1514 struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1515 int i;
1516
1517 memset(clp, 0, sizeof(*clp));
1518
1519 vndgetdefaultlabel(sc, lp);
1520
1521 /*
1522 * Call the generic disklabel extraction routine.
1523 */
1524 errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1525 if (errstring) {
1526 /*
1527 * Lack of disklabel is common, but we print the warning
1528 * anyway, since it might contain other useful information.
1529 */
1530 printf("%s: %s\n", sc->sc_dev.dv_xname, errstring);
1531
1532 /*
1533 * For historical reasons, if there's no disklabel
1534 * present, all partitions must be FS_BSDFFS and
1535 * occupy the entire disk.
1536 */
1537 for (i = 0; i < MAXPARTITIONS; i++) {
1538 /*
1539 * Don't wipe out port specific hack (such as
1540 * dos partition hack of i386 port).
1541 */
1542 if (lp->d_partitions[i].p_size != 0)
1543 continue;
1544
1545 lp->d_partitions[i].p_size = lp->d_secperunit;
1546 lp->d_partitions[i].p_offset = 0;
1547 lp->d_partitions[i].p_fstype = FS_BSDFFS;
1548 }
1549
1550 strncpy(lp->d_packname, "default label",
1551 sizeof(lp->d_packname));
1552
1553 lp->d_npartitions = MAXPARTITIONS;
1554 lp->d_checksum = dkcksum(lp);
1555 }
1556
1557 /* In-core label now valid. */
1558 sc->sc_flags |= VNF_VLABEL;
1559 }
1560
1561 /*
1562 * Wait interruptibly for an exclusive lock.
1563 *
1564 * XXX
1565 * Several drivers do this; it should be abstracted and made MP-safe.
1566 */
1567 static int
1568 vndlock(struct vnd_softc *sc)
1569 {
1570 int error;
1571
1572 while ((sc->sc_flags & VNF_LOCKED) != 0) {
1573 sc->sc_flags |= VNF_WANTED;
1574 if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1575 return (error);
1576 }
1577 sc->sc_flags |= VNF_LOCKED;
1578 return (0);
1579 }
1580
1581 /*
1582 * Unlock and wake up any waiters.
1583 */
1584 static void
1585 vndunlock(struct vnd_softc *sc)
1586 {
1587
1588 sc->sc_flags &= ~VNF_LOCKED;
1589 if ((sc->sc_flags & VNF_WANTED) != 0) {
1590 sc->sc_flags &= ~VNF_WANTED;
1591 wakeup(sc);
1592 }
1593 }
1594
1595 #ifdef VND_COMPRESSION
1596 /* compressed file read */
1597 static void
1598 compstrategy(struct buf *bp, off_t bn)
1599 {
1600 int error;
1601 int unit = vndunit(bp->b_dev);
1602 struct vnd_softc *vnd =
1603 (struct vnd_softc *)device_lookup(&vnd_cd, unit);
1604 u_int32_t comp_block;
1605 struct uio auio;
1606 caddr_t addr;
1607 int s;
1608
1609 /* set up constants for data move */
1610 auio.uio_rw = UIO_READ;
1611 auio.uio_segflg = UIO_SYSSPACE;
1612
1613 /* read, and transfer the data */
1614 addr = bp->b_data;
1615 s = splbio();
1616 while (bp->b_resid > 0) {
1617 unsigned length;
1618 size_t length_in_buffer;
1619 u_int32_t offset_in_buffer;
1620 struct iovec aiov;
1621
1622 /* calculate the compressed block number */
1623 comp_block = bn / (off_t)vnd->sc_comp_blksz;
1624
1625 /* check for good block number */
1626 if (comp_block >= vnd->sc_comp_numoffs) {
1627 bp->b_error = EINVAL;
1628 bp->b_flags |= B_ERROR;
1629 splx(s);
1630 return;
1631 }
1632
1633 /* read in the compressed block, if not in buffer */
1634 if (comp_block != vnd->sc_comp_buffblk) {
1635 length = vnd->sc_comp_offsets[comp_block + 1] -
1636 vnd->sc_comp_offsets[comp_block];
1637 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1638 error = vn_rdwr(UIO_READ, vnd->sc_vp, vnd->sc_comp_buff,
1639 length, vnd->sc_comp_offsets[comp_block],
1640 UIO_SYSSPACE, IO_UNIT, vnd->sc_cred, NULL, NULL);
1641 if (error) {
1642 bp->b_error = error;
1643 bp->b_flags |= B_ERROR;
1644 VOP_UNLOCK(vnd->sc_vp, 0);
1645 splx(s);
1646 return;
1647 }
1648 /* uncompress the buffer */
1649 vnd->sc_comp_stream.next_in = vnd->sc_comp_buff;
1650 vnd->sc_comp_stream.avail_in = length;
1651 vnd->sc_comp_stream.next_out = vnd->sc_comp_decombuf;
1652 vnd->sc_comp_stream.avail_out = vnd->sc_comp_blksz;
1653 inflateReset(&vnd->sc_comp_stream);
1654 error = inflate(&vnd->sc_comp_stream, Z_FINISH);
1655 if (error != Z_STREAM_END) {
1656 if (vnd->sc_comp_stream.msg)
1657 printf("%s: compressed file, %s\n",
1658 vnd->sc_dev.dv_xname,
1659 vnd->sc_comp_stream.msg);
1660 bp->b_error = EBADMSG;
1661 bp->b_flags |= B_ERROR;
1662 VOP_UNLOCK(vnd->sc_vp, 0);
1663 splx(s);
1664 return;
1665 }
1666 vnd->sc_comp_buffblk = comp_block;
1667 VOP_UNLOCK(vnd->sc_vp, 0);
1668 }
1669
1670 /* transfer the usable uncompressed data */
1671 offset_in_buffer = bn % (off_t)vnd->sc_comp_blksz;
1672 length_in_buffer = vnd->sc_comp_blksz - offset_in_buffer;
1673 if (length_in_buffer > bp->b_resid)
1674 length_in_buffer = bp->b_resid;
1675 auio.uio_iov = &aiov;
1676 auio.uio_iovcnt = 1;
1677 aiov.iov_base = addr;
1678 aiov.iov_len = length_in_buffer;
1679 auio.uio_resid = aiov.iov_len;
1680 auio.uio_offset = 0;
1681 error = uiomove(vnd->sc_comp_decombuf + offset_in_buffer,
1682 length_in_buffer, &auio);
1683 if (error) {
1684 bp->b_error = error;
1685 bp->b_flags |= B_ERROR;
1686 splx(s);
1687 return;
1688 }
1689
1690 bn += length_in_buffer;
1691 addr += length_in_buffer;
1692 bp->b_resid -= length_in_buffer;
1693 }
1694 splx(s);
1695 }
1696
1697 /* compression memory allocation routines */
1698 static void *
1699 vnd_alloc(void *aux, u_int items, u_int siz)
1700 {
1701 return malloc(items * siz, M_TEMP, M_NOWAIT);
1702 }
1703
1704 static void
1705 vnd_free(void *aux, void *ptr)
1706 {
1707 free(ptr, M_TEMP);
1708 }
1709 #endif /* VND_COMPRESSION */
1710