vnd.c revision 1.140 1 /* $NetBSD: vnd.c,v 1.140 2006/02/04 13:40:38 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.140 2006/02/04 13:40:38 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_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_proc->p_ucred, 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 struct proc *p = (l != NULL) ? l->l_proc : NULL;
802 int fflags;
803 #ifdef __HAVE_OLD_DISKLABEL
804 struct disklabel newlabel;
805 #endif
806
807 #ifdef DEBUG
808 if (vnddebug & VDB_FOLLOW)
809 printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
810 dev, cmd, data, flag, p, unit);
811 #endif
812 vnd = device_lookup(&vnd_cd, unit);
813 if (vnd == NULL &&
814 #ifdef COMPAT_30
815 cmd != VNDIOOCGET &&
816 #endif
817 cmd != VNDIOCGET)
818 return ENXIO;
819 vio = (struct vnd_ioctl *)data;
820
821 /* Must be open for writes for these commands... */
822 switch (cmd) {
823 case VNDIOCSET:
824 case VNDIOCCLR:
825 case DIOCSDINFO:
826 case DIOCWDINFO:
827 #ifdef __HAVE_OLD_DISKLABEL
828 case ODIOCSDINFO:
829 case ODIOCWDINFO:
830 #endif
831 case DIOCKLABEL:
832 case DIOCWLABEL:
833 if ((flag & FWRITE) == 0)
834 return (EBADF);
835 }
836
837 /* Must be initialized for these... */
838 switch (cmd) {
839 case VNDIOCCLR:
840 case DIOCGDINFO:
841 case DIOCSDINFO:
842 case DIOCWDINFO:
843 case DIOCGPART:
844 case DIOCKLABEL:
845 case DIOCWLABEL:
846 case DIOCGDEFLABEL:
847 #ifdef __HAVE_OLD_DISKLABEL
848 case ODIOCGDINFO:
849 case ODIOCSDINFO:
850 case ODIOCWDINFO:
851 case ODIOCGDEFLABEL:
852 #endif
853 if ((vnd->sc_flags & VNF_INITED) == 0)
854 return (ENXIO);
855 }
856
857 switch (cmd) {
858 case VNDIOCSET:
859 if (vnd->sc_flags & VNF_INITED)
860 return (EBUSY);
861
862 if ((error = vndlock(vnd)) != 0)
863 return (error);
864
865 fflags = FREAD;
866 if ((vio->vnd_flags & VNDIOF_READONLY) == 0)
867 fflags |= FWRITE;
868 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, l);
869 if ((error = vn_open(&nd, fflags, 0)) != 0)
870 goto unlock_and_exit;
871 error = VOP_GETATTR(nd.ni_vp, &vattr, l->l_proc->p_ucred, 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, p->p_ucred, 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, p->p_ucred, 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, p->p_ucred)) != 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, p->p_ucred, 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 switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
1154 case 0:
1155 vnu->vnu_dev = va.va_fsid;
1156 vnu->vnu_ino = va.va_fileid;
1157 break;
1158 case -1:
1159 /* unused is not an error */
1160 vnu->vnu_dev = 0;
1161 vnu->vnu_ino = 0;
1162 break;
1163 default:
1164 return error;
1165 }
1166 break;
1167 }
1168 #endif
1169 case VNDIOCGET: {
1170 struct vnd_user *vnu;
1171 struct vattr va;
1172 vnu = (struct vnd_user *)data;
1173 switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
1174 case 0:
1175 vnu->vnu_dev = va.va_fsid;
1176 vnu->vnu_ino = va.va_fileid;
1177 break;
1178 case -1:
1179 /* unused is not an error */
1180 vnu->vnu_dev = 0;
1181 vnu->vnu_ino = 0;
1182 break;
1183 default:
1184 return error;
1185 }
1186 break;
1187 }
1188
1189 case DIOCGDINFO:
1190 *(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
1191 break;
1192
1193 #ifdef __HAVE_OLD_DISKLABEL
1194 case ODIOCGDINFO:
1195 newlabel = *(vnd->sc_dkdev.dk_label);
1196 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1197 return ENOTTY;
1198 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1199 break;
1200 #endif
1201
1202 case DIOCGPART:
1203 ((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
1204 ((struct partinfo *)data)->part =
1205 &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1206 break;
1207
1208 case DIOCWDINFO:
1209 case DIOCSDINFO:
1210 #ifdef __HAVE_OLD_DISKLABEL
1211 case ODIOCWDINFO:
1212 case ODIOCSDINFO:
1213 #endif
1214 {
1215 struct disklabel *lp;
1216
1217 if ((error = vndlock(vnd)) != 0)
1218 return (error);
1219
1220 vnd->sc_flags |= VNF_LABELLING;
1221
1222 #ifdef __HAVE_OLD_DISKLABEL
1223 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1224 memset(&newlabel, 0, sizeof newlabel);
1225 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1226 lp = &newlabel;
1227 } else
1228 #endif
1229 lp = (struct disklabel *)data;
1230
1231 error = setdisklabel(vnd->sc_dkdev.dk_label,
1232 lp, 0, vnd->sc_dkdev.dk_cpulabel);
1233 if (error == 0) {
1234 if (cmd == DIOCWDINFO
1235 #ifdef __HAVE_OLD_DISKLABEL
1236 || cmd == ODIOCWDINFO
1237 #endif
1238 )
1239 error = writedisklabel(VNDLABELDEV(dev),
1240 vndstrategy, vnd->sc_dkdev.dk_label,
1241 vnd->sc_dkdev.dk_cpulabel);
1242 }
1243
1244 vnd->sc_flags &= ~VNF_LABELLING;
1245
1246 vndunlock(vnd);
1247
1248 if (error)
1249 return (error);
1250 break;
1251 }
1252
1253 case DIOCKLABEL:
1254 if (*(int *)data != 0)
1255 vnd->sc_flags |= VNF_KLABEL;
1256 else
1257 vnd->sc_flags &= ~VNF_KLABEL;
1258 break;
1259
1260 case DIOCWLABEL:
1261 if (*(int *)data != 0)
1262 vnd->sc_flags |= VNF_WLABEL;
1263 else
1264 vnd->sc_flags &= ~VNF_WLABEL;
1265 break;
1266
1267 case DIOCGDEFLABEL:
1268 vndgetdefaultlabel(vnd, (struct disklabel *)data);
1269 break;
1270
1271 #ifdef __HAVE_OLD_DISKLABEL
1272 case ODIOCGDEFLABEL:
1273 vndgetdefaultlabel(vnd, &newlabel);
1274 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1275 return ENOTTY;
1276 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1277 break;
1278 #endif
1279
1280 default:
1281 return (ENOTTY);
1282 }
1283
1284 return (0);
1285 }
1286
1287 /*
1288 * Duplicate the current processes' credentials. Since we are called only
1289 * as the result of a SET ioctl and only root can do that, any future access
1290 * to this "disk" is essentially as root. Note that credentials may change
1291 * if some other uid can write directly to the mapped file (NFS).
1292 */
1293 static int
1294 vndsetcred(struct vnd_softc *vnd, struct ucred *cred)
1295 {
1296 struct uio auio;
1297 struct iovec aiov;
1298 char *tmpbuf;
1299 int error;
1300
1301 vnd->sc_cred = crdup(cred);
1302 tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1303
1304 /* XXX: Horrible kludge to establish credentials for NFS */
1305 aiov.iov_base = tmpbuf;
1306 aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
1307 auio.uio_iov = &aiov;
1308 auio.uio_iovcnt = 1;
1309 auio.uio_offset = 0;
1310 auio.uio_rw = UIO_READ;
1311 auio.uio_segflg = UIO_SYSSPACE;
1312 auio.uio_resid = aiov.iov_len;
1313 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1314 error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
1315 if (error == 0) {
1316 /*
1317 * Because vnd does all IO directly through the vnode
1318 * we need to flush (at least) the buffer from the above
1319 * VOP_READ from the buffer cache to prevent cache
1320 * incoherencies. Also, be careful to write dirty
1321 * buffers back to stable storage.
1322 */
1323 error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
1324 curlwp, 0, 0);
1325 }
1326 VOP_UNLOCK(vnd->sc_vp, 0);
1327
1328 free(tmpbuf, M_TEMP);
1329 return (error);
1330 }
1331
1332 /*
1333 * Set maxactive based on FS type
1334 */
1335 static void
1336 vndthrottle(struct vnd_softc *vnd, struct vnode *vp)
1337 {
1338 #ifdef NFS
1339 extern int (**nfsv2_vnodeop_p)(void *);
1340
1341 if (vp->v_op == nfsv2_vnodeop_p)
1342 vnd->sc_maxactive = 2;
1343 else
1344 #endif
1345 vnd->sc_maxactive = 8;
1346
1347 if (vnd->sc_maxactive < 1)
1348 vnd->sc_maxactive = 1;
1349 }
1350
1351 #if 0
1352 static void
1353 vndshutdown(void)
1354 {
1355 struct vnd_softc *vnd;
1356
1357 for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1358 if (vnd->sc_flags & VNF_INITED)
1359 vndclear(vnd);
1360 }
1361 #endif
1362
1363 static void
1364 vndclear(struct vnd_softc *vnd, int myminor)
1365 {
1366 struct vnode *vp = vnd->sc_vp;
1367 struct lwp *l = curlwp;
1368 int fflags = FREAD;
1369 int bmaj, cmaj, i, mn;
1370 int s;
1371
1372 #ifdef DEBUG
1373 if (vnddebug & VDB_FOLLOW)
1374 printf("vndclear(%p): vp %p\n", vnd, vp);
1375 #endif
1376 /* locate the major number */
1377 bmaj = bdevsw_lookup_major(&vnd_bdevsw);
1378 cmaj = cdevsw_lookup_major(&vnd_cdevsw);
1379
1380 /* Nuke the vnodes for any open instances */
1381 for (i = 0; i < MAXPARTITIONS; i++) {
1382 mn = DISKMINOR(vnd->sc_dev.dv_unit, i);
1383 vdevgone(bmaj, mn, mn, VBLK);
1384 if (mn != myminor) /* XXX avoid to kill own vnode */
1385 vdevgone(cmaj, mn, mn, VCHR);
1386 }
1387
1388 if ((vnd->sc_flags & VNF_READONLY) == 0)
1389 fflags |= FWRITE;
1390
1391 s = splbio();
1392 bufq_drain(vnd->sc_tab);
1393 splx(s);
1394
1395 vnd->sc_flags |= VNF_VUNCONF;
1396 wakeup(&vnd->sc_tab);
1397 while (vnd->sc_flags & VNF_KTHREAD)
1398 tsleep(&vnd->sc_kthread, PRIBIO, "vnthr", 0);
1399
1400 #ifdef VND_COMPRESSION
1401 /* free the compressed file buffers */
1402 if(vnd->sc_flags & VNF_COMP) {
1403 if(vnd->sc_comp_offsets) {
1404 free(vnd->sc_comp_offsets, M_DEVBUF);
1405 vnd->sc_comp_offsets = NULL;
1406 }
1407 if(vnd->sc_comp_buff) {
1408 free(vnd->sc_comp_buff, M_DEVBUF);
1409 vnd->sc_comp_buff = NULL;
1410 }
1411 if(vnd->sc_comp_decombuf) {
1412 free(vnd->sc_comp_decombuf, M_DEVBUF);
1413 vnd->sc_comp_decombuf = NULL;
1414 }
1415 }
1416 #endif /* VND_COMPRESSION */
1417 vnd->sc_flags &=
1418 ~(VNF_INITED | VNF_READONLY | VNF_VLABEL
1419 | VNF_VUNCONF | VNF_COMP);
1420 if (vp == (struct vnode *)0)
1421 panic("vndclear: null vp");
1422 (void) vn_close(vp, fflags, vnd->sc_cred, l);
1423 crfree(vnd->sc_cred);
1424 vnd->sc_vp = (struct vnode *)0;
1425 vnd->sc_cred = (struct ucred *)0;
1426 vnd->sc_size = 0;
1427 }
1428
1429 static int
1430 vndsize(dev_t dev)
1431 {
1432 struct vnd_softc *sc;
1433 struct disklabel *lp;
1434 int part, unit, omask;
1435 int size;
1436
1437 unit = vndunit(dev);
1438 sc = (struct vnd_softc *)device_lookup(&vnd_cd, unit);
1439 if (sc == NULL)
1440 return -1;
1441
1442 if ((sc->sc_flags & VNF_INITED) == 0)
1443 return (-1);
1444
1445 part = DISKPART(dev);
1446 omask = sc->sc_dkdev.dk_openmask & (1 << part);
1447 lp = sc->sc_dkdev.dk_label;
1448
1449 if (omask == 0 && vndopen(dev, 0, S_IFBLK, curlwp)) /* XXX */
1450 return (-1);
1451
1452 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1453 size = -1;
1454 else
1455 size = lp->d_partitions[part].p_size *
1456 (lp->d_secsize / DEV_BSIZE);
1457
1458 if (omask == 0 && vndclose(dev, 0, S_IFBLK, curlwp)) /* XXX */
1459 return (-1);
1460
1461 return (size);
1462 }
1463
1464 static int
1465 vnddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1466 {
1467
1468 /* Not implemented. */
1469 return ENXIO;
1470 }
1471
1472 static void
1473 vndgetdefaultlabel(struct vnd_softc *sc, struct disklabel *lp)
1474 {
1475 struct vndgeom *vng = &sc->sc_geom;
1476 struct partition *pp;
1477
1478 memset(lp, 0, sizeof(*lp));
1479
1480 lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
1481 lp->d_secsize = vng->vng_secsize;
1482 lp->d_nsectors = vng->vng_nsectors;
1483 lp->d_ntracks = vng->vng_ntracks;
1484 lp->d_ncylinders = vng->vng_ncylinders;
1485 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1486
1487 strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1488 lp->d_type = DTYPE_VND;
1489 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1490 lp->d_rpm = 3600;
1491 lp->d_interleave = 1;
1492 lp->d_flags = 0;
1493
1494 pp = &lp->d_partitions[RAW_PART];
1495 pp->p_offset = 0;
1496 pp->p_size = lp->d_secperunit;
1497 pp->p_fstype = FS_UNUSED;
1498 lp->d_npartitions = RAW_PART + 1;
1499
1500 lp->d_magic = DISKMAGIC;
1501 lp->d_magic2 = DISKMAGIC;
1502 lp->d_checksum = dkcksum(lp);
1503 }
1504
1505 /*
1506 * Read the disklabel from a vnd. If one is not present, create a fake one.
1507 */
1508 static void
1509 vndgetdisklabel(dev_t dev, struct vnd_softc *sc)
1510 {
1511 const char *errstring;
1512 struct disklabel *lp = sc->sc_dkdev.dk_label;
1513 struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1514 int i;
1515
1516 memset(clp, 0, sizeof(*clp));
1517
1518 vndgetdefaultlabel(sc, lp);
1519
1520 /*
1521 * Call the generic disklabel extraction routine.
1522 */
1523 errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1524 if (errstring) {
1525 /*
1526 * Lack of disklabel is common, but we print the warning
1527 * anyway, since it might contain other useful information.
1528 */
1529 printf("%s: %s\n", sc->sc_dev.dv_xname, errstring);
1530
1531 /*
1532 * For historical reasons, if there's no disklabel
1533 * present, all partitions must be FS_BSDFFS and
1534 * occupy the entire disk.
1535 */
1536 for (i = 0; i < MAXPARTITIONS; i++) {
1537 /*
1538 * Don't wipe out port specific hack (such as
1539 * dos partition hack of i386 port).
1540 */
1541 if (lp->d_partitions[i].p_size != 0)
1542 continue;
1543
1544 lp->d_partitions[i].p_size = lp->d_secperunit;
1545 lp->d_partitions[i].p_offset = 0;
1546 lp->d_partitions[i].p_fstype = FS_BSDFFS;
1547 }
1548
1549 strncpy(lp->d_packname, "default label",
1550 sizeof(lp->d_packname));
1551
1552 lp->d_npartitions = MAXPARTITIONS;
1553 lp->d_checksum = dkcksum(lp);
1554 }
1555
1556 /* In-core label now valid. */
1557 sc->sc_flags |= VNF_VLABEL;
1558 }
1559
1560 /*
1561 * Wait interruptibly for an exclusive lock.
1562 *
1563 * XXX
1564 * Several drivers do this; it should be abstracted and made MP-safe.
1565 */
1566 static int
1567 vndlock(struct vnd_softc *sc)
1568 {
1569 int error;
1570
1571 while ((sc->sc_flags & VNF_LOCKED) != 0) {
1572 sc->sc_flags |= VNF_WANTED;
1573 if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1574 return (error);
1575 }
1576 sc->sc_flags |= VNF_LOCKED;
1577 return (0);
1578 }
1579
1580 /*
1581 * Unlock and wake up any waiters.
1582 */
1583 static void
1584 vndunlock(struct vnd_softc *sc)
1585 {
1586
1587 sc->sc_flags &= ~VNF_LOCKED;
1588 if ((sc->sc_flags & VNF_WANTED) != 0) {
1589 sc->sc_flags &= ~VNF_WANTED;
1590 wakeup(sc);
1591 }
1592 }
1593
1594 #ifdef VND_COMPRESSION
1595 /* compressed file read */
1596 static void
1597 compstrategy(struct buf *bp, off_t bn)
1598 {
1599 int error;
1600 int unit = vndunit(bp->b_dev);
1601 struct vnd_softc *vnd =
1602 (struct vnd_softc *)device_lookup(&vnd_cd, unit);
1603 u_int32_t comp_block;
1604 struct uio auio;
1605 caddr_t addr;
1606 int s;
1607
1608 /* set up constants for data move */
1609 auio.uio_rw = UIO_READ;
1610 auio.uio_segflg = UIO_SYSSPACE;
1611
1612 /* read, and transfer the data */
1613 addr = bp->b_data;
1614 s = splbio();
1615 while (bp->b_resid > 0) {
1616 unsigned length;
1617 size_t length_in_buffer;
1618 u_int32_t offset_in_buffer;
1619 struct iovec aiov;
1620
1621 /* calculate the compressed block number */
1622 comp_block = bn / (off_t)vnd->sc_comp_blksz;
1623
1624 /* check for good block number */
1625 if (comp_block >= vnd->sc_comp_numoffs) {
1626 bp->b_error = EINVAL;
1627 bp->b_flags |= B_ERROR;
1628 splx(s);
1629 return;
1630 }
1631
1632 /* read in the compressed block, if not in buffer */
1633 if (comp_block != vnd->sc_comp_buffblk) {
1634 length = vnd->sc_comp_offsets[comp_block + 1] -
1635 vnd->sc_comp_offsets[comp_block];
1636 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1637 error = vn_rdwr(UIO_READ, vnd->sc_vp, vnd->sc_comp_buff,
1638 length, vnd->sc_comp_offsets[comp_block],
1639 UIO_SYSSPACE, IO_UNIT, vnd->sc_cred, NULL, NULL);
1640 if (error) {
1641 bp->b_error = error;
1642 bp->b_flags |= B_ERROR;
1643 VOP_UNLOCK(vnd->sc_vp, 0);
1644 splx(s);
1645 return;
1646 }
1647 /* uncompress the buffer */
1648 vnd->sc_comp_stream.next_in = vnd->sc_comp_buff;
1649 vnd->sc_comp_stream.avail_in = length;
1650 vnd->sc_comp_stream.next_out = vnd->sc_comp_decombuf;
1651 vnd->sc_comp_stream.avail_out = vnd->sc_comp_blksz;
1652 inflateReset(&vnd->sc_comp_stream);
1653 error = inflate(&vnd->sc_comp_stream, Z_FINISH);
1654 if (error != Z_STREAM_END) {
1655 if (vnd->sc_comp_stream.msg)
1656 printf("%s: compressed file, %s\n",
1657 vnd->sc_dev.dv_xname,
1658 vnd->sc_comp_stream.msg);
1659 bp->b_error = EBADMSG;
1660 bp->b_flags |= B_ERROR;
1661 VOP_UNLOCK(vnd->sc_vp, 0);
1662 splx(s);
1663 return;
1664 }
1665 vnd->sc_comp_buffblk = comp_block;
1666 VOP_UNLOCK(vnd->sc_vp, 0);
1667 }
1668
1669 /* transfer the usable uncompressed data */
1670 offset_in_buffer = bn % (off_t)vnd->sc_comp_blksz;
1671 length_in_buffer = vnd->sc_comp_blksz - offset_in_buffer;
1672 if (length_in_buffer > bp->b_resid)
1673 length_in_buffer = bp->b_resid;
1674 auio.uio_iov = &aiov;
1675 auio.uio_iovcnt = 1;
1676 aiov.iov_base = addr;
1677 aiov.iov_len = length_in_buffer;
1678 auio.uio_resid = aiov.iov_len;
1679 auio.uio_offset = 0;
1680 error = uiomove(vnd->sc_comp_decombuf + offset_in_buffer,
1681 length_in_buffer, &auio);
1682 if (error) {
1683 bp->b_error = error;
1684 bp->b_flags |= B_ERROR;
1685 splx(s);
1686 return;
1687 }
1688
1689 bn += length_in_buffer;
1690 addr += length_in_buffer;
1691 bp->b_resid -= length_in_buffer;
1692 }
1693 splx(s);
1694 }
1695
1696 /* compression memory allocation routines */
1697 static void *
1698 vnd_alloc(void *aux, u_int items, u_int siz)
1699 {
1700 return malloc(items * siz, M_TEMP, M_NOWAIT);
1701 }
1702
1703 static void
1704 vnd_free(void *aux, void *ptr)
1705 {
1706 free(ptr, M_TEMP);
1707 }
1708 #endif /* VND_COMPRESSION */
1709