vnd.c revision 1.139 1 /* $NetBSD: vnd.c,v 1.139 2006/02/04 13:38:04 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.139 2006/02/04 13:38:04 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 if ((off = bn % bsize) != 0)
656 sz = bsize - off;
657 else
658 sz = (1 + nra) * bsize;
659 if (resid < sz)
660 sz = resid;
661 #ifdef DEBUG
662 if (vnddebug & VDB_IO)
663 printf("vndstrategy: vp %p/%p bn 0x%qx/0x%" PRIx64
664 " sz 0x%x\n",
665 vnd->sc_vp, vp, (long long)bn, nbn, sz);
666 #endif
667
668 nbp = getiobuf();
669 nestiobuf_setup(bp, nbp, offset, sz);
670 nbp->b_blkno = nbn + btodb(off);
671
672 #if 0 /* XXX #ifdef DEBUG */
673 if (vnddebug & VDB_IO)
674 printf("vndstart(%ld): bp %p vp %p blkno "
675 "0x%" PRIx64 " flags %x addr %p cnt 0x%x\n",
676 (long) (vnd-vnd_softc), &nbp->vb_buf,
677 nbp->vb_buf.b_vp, nbp->vb_buf.b_blkno,
678 nbp->vb_buf.b_flags, nbp->vb_buf.b_data,
679 nbp->vb_buf.b_bcount);
680 #endif
681 VOP_STRATEGY(vp, nbp);
682 bn += sz;
683 }
684 nestiobuf_done(bp, skipped, error);
685
686 if ((flags & B_READ) == 0)
687 vn_finished_write(mp, 0);
688
689 s = splbio();
690 continue;
691 done:
692 biodone(obp);
693 s = splbio();
694 }
695
696 vnd->sc_flags &= (~VNF_KTHREAD | VNF_VUNCONF);
697 wakeup(&vnd->sc_kthread);
698 splx(s);
699 kthread_exit(0);
700 }
701
702 static void
703 vndiodone(struct buf *bp)
704 {
705 struct vndxfer *vnx = VND_BUFTOXFER(bp);
706 struct vnd_softc *vnd = vnx->vx_vnd;
707 struct buf *obp = bp->b_private;
708
709 KASSERT(&vnx->vx_buf == bp);
710 KASSERT(vnd->sc_active > 0);
711 #ifdef DEBUG
712 if (vnddebug & VDB_IO) {
713 printf("vndiodone1: bp %p iodone: error %d\n",
714 bp, (bp->b_flags & B_ERROR) != 0 ? bp->b_error : 0);
715 }
716 #endif
717 disk_unbusy(&vnd->sc_dkdev, bp->b_bcount - bp->b_resid,
718 (bp->b_flags & B_READ));
719 vnd->sc_active--;
720 if (vnd->sc_active == 0) {
721 wakeup(&vnd->sc_tab);
722 }
723 obp->b_flags |= bp->b_flags & B_ERROR;
724 obp->b_error = bp->b_error;
725 obp->b_resid = bp->b_resid;
726 VND_PUTXFER(vnd, vnx);
727 biodone(obp);
728 }
729
730 /* ARGSUSED */
731 static int
732 vndread(dev_t dev, struct uio *uio, int flags)
733 {
734 int unit = vndunit(dev);
735 struct vnd_softc *sc;
736
737 #ifdef DEBUG
738 if (vnddebug & VDB_FOLLOW)
739 printf("vndread(0x%x, %p)\n", dev, uio);
740 #endif
741
742 sc = device_lookup(&vnd_cd, unit);
743 if (sc == NULL)
744 return ENXIO;
745
746 if ((sc->sc_flags & VNF_INITED) == 0)
747 return (ENXIO);
748
749 return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
750 }
751
752 /* ARGSUSED */
753 static int
754 vndwrite(dev_t dev, struct uio *uio, int flags)
755 {
756 int unit = vndunit(dev);
757 struct vnd_softc *sc;
758
759 #ifdef DEBUG
760 if (vnddebug & VDB_FOLLOW)
761 printf("vndwrite(0x%x, %p)\n", dev, uio);
762 #endif
763
764 sc = device_lookup(&vnd_cd, unit);
765 if (sc == NULL)
766 return ENXIO;
767
768 if ((sc->sc_flags & VNF_INITED) == 0)
769 return (ENXIO);
770
771 return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
772 }
773
774 static int
775 vnd_cget(struct lwp *l, int unit, int *un, struct vattr *va)
776 {
777 struct vnd_softc *vnd;
778
779 if (*un == -1)
780 *un = unit;
781 if (*un < 0)
782 return EINVAL;
783
784 vnd = device_lookup(&vnd_cd, *un);
785 if (vnd == NULL)
786 return (*un >= vnd_cd.cd_ndevs) ? ENXIO : -1;
787
788 if ((vnd->sc_flags & VNF_INITED) == 0)
789 return -1;
790
791 return VOP_GETATTR(vnd->sc_vp, va, l->l_proc->p_ucred, l);
792 }
793
794 /* ARGSUSED */
795 static int
796 vndioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
797 {
798 int unit = vndunit(dev);
799 struct vnd_softc *vnd;
800 struct vnd_ioctl *vio;
801 struct vattr vattr;
802 struct nameidata nd;
803 int error, part, pmask;
804 size_t geomsize;
805 struct proc *p = (l != NULL) ? l->l_proc : NULL;
806 int fflags;
807 #ifdef __HAVE_OLD_DISKLABEL
808 struct disklabel newlabel;
809 #endif
810
811 #ifdef DEBUG
812 if (vnddebug & VDB_FOLLOW)
813 printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
814 dev, cmd, data, flag, p, unit);
815 #endif
816 vnd = device_lookup(&vnd_cd, unit);
817 if (vnd == NULL &&
818 #ifdef COMPAT_30
819 cmd != VNDIOOCGET &&
820 #endif
821 cmd != VNDIOCGET)
822 return ENXIO;
823 vio = (struct vnd_ioctl *)data;
824
825 /* Must be open for writes for these commands... */
826 switch (cmd) {
827 case VNDIOCSET:
828 case VNDIOCCLR:
829 case DIOCSDINFO:
830 case DIOCWDINFO:
831 #ifdef __HAVE_OLD_DISKLABEL
832 case ODIOCSDINFO:
833 case ODIOCWDINFO:
834 #endif
835 case DIOCKLABEL:
836 case DIOCWLABEL:
837 if ((flag & FWRITE) == 0)
838 return (EBADF);
839 }
840
841 /* Must be initialized for these... */
842 switch (cmd) {
843 case VNDIOCCLR:
844 case DIOCGDINFO:
845 case DIOCSDINFO:
846 case DIOCWDINFO:
847 case DIOCGPART:
848 case DIOCKLABEL:
849 case DIOCWLABEL:
850 case DIOCGDEFLABEL:
851 #ifdef __HAVE_OLD_DISKLABEL
852 case ODIOCGDINFO:
853 case ODIOCSDINFO:
854 case ODIOCWDINFO:
855 case ODIOCGDEFLABEL:
856 #endif
857 if ((vnd->sc_flags & VNF_INITED) == 0)
858 return (ENXIO);
859 }
860
861 switch (cmd) {
862 case VNDIOCSET:
863 if (vnd->sc_flags & VNF_INITED)
864 return (EBUSY);
865
866 if ((error = vndlock(vnd)) != 0)
867 return (error);
868
869 fflags = FREAD;
870 if ((vio->vnd_flags & VNDIOF_READONLY) == 0)
871 fflags |= FWRITE;
872 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, l);
873 if ((error = vn_open(&nd, fflags, 0)) != 0)
874 goto unlock_and_exit;
875 error = VOP_GETATTR(nd.ni_vp, &vattr, l->l_proc->p_ucred, l);
876 if (!error && nd.ni_vp->v_type != VREG)
877 error = EOPNOTSUPP;
878 if (error) {
879 VOP_UNLOCK(nd.ni_vp, 0);
880 goto close_and_exit;
881 }
882
883 /* If using a compressed file, initialize its info */
884 /* (or abort with an error if kernel has no compression) */
885 if (vio->vnd_flags & VNF_COMP) {
886 #ifdef VND_COMPRESSION
887 struct vnd_comp_header *ch;
888 int i;
889 u_int32_t comp_size;
890 u_int32_t comp_maxsize;
891
892 /* allocate space for compresed file header */
893 ch = malloc(sizeof(struct vnd_comp_header),
894 M_TEMP, M_WAITOK);
895
896 /* read compressed file header */
897 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)ch,
898 sizeof(struct vnd_comp_header), 0, UIO_SYSSPACE,
899 IO_UNIT|IO_NODELOCKED, p->p_ucred, NULL, NULL);
900 if(error) {
901 free(ch, M_TEMP);
902 VOP_UNLOCK(nd.ni_vp, 0);
903 goto close_and_exit;
904 }
905
906 /* save some header info */
907 vnd->sc_comp_blksz = ntohl(ch->block_size);
908 /* note last offset is the file byte size */
909 vnd->sc_comp_numoffs = ntohl(ch->num_blocks)+1;
910 free(ch, M_TEMP);
911 if(vnd->sc_comp_blksz % DEV_BSIZE !=0) {
912 VOP_UNLOCK(nd.ni_vp, 0);
913 error = EINVAL;
914 goto close_and_exit;
915 }
916 if(sizeof(struct vnd_comp_header) +
917 sizeof(u_int64_t) * vnd->sc_comp_numoffs >
918 vattr.va_size) {
919 VOP_UNLOCK(nd.ni_vp, 0);
920 error = EINVAL;
921 goto close_and_exit;
922 }
923
924 /* set decompressed file size */
925 vattr.va_size =
926 (vnd->sc_comp_numoffs - 1) * vnd->sc_comp_blksz;
927
928 /* allocate space for all the compressed offsets */
929 vnd->sc_comp_offsets =
930 malloc(sizeof(u_int64_t) * vnd->sc_comp_numoffs,
931 M_DEVBUF, M_WAITOK);
932
933 /* read in the offsets */
934 error = vn_rdwr(UIO_READ, nd.ni_vp,
935 (caddr_t)vnd->sc_comp_offsets,
936 sizeof(u_int64_t) * vnd->sc_comp_numoffs,
937 sizeof(struct vnd_comp_header), UIO_SYSSPACE,
938 IO_UNIT|IO_NODELOCKED, p->p_ucred, NULL, NULL);
939 if(error) {
940 VOP_UNLOCK(nd.ni_vp, 0);
941 goto close_and_exit;
942 }
943 /*
944 * find largest block size (used for allocation limit).
945 * Also convert offset to native byte order.
946 */
947 comp_maxsize = 0;
948 for (i = 0; i < vnd->sc_comp_numoffs - 1; i++) {
949 vnd->sc_comp_offsets[i] =
950 be64toh(vnd->sc_comp_offsets[i]);
951 comp_size = be64toh(vnd->sc_comp_offsets[i + 1])
952 - vnd->sc_comp_offsets[i];
953 if (comp_size > comp_maxsize)
954 comp_maxsize = comp_size;
955 }
956 vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1] =
957 be64toh(vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1]);
958
959 /* create compressed data buffer */
960 vnd->sc_comp_buff = malloc(comp_maxsize,
961 M_DEVBUF, M_WAITOK);
962
963 /* create decompressed buffer */
964 vnd->sc_comp_decombuf = malloc(vnd->sc_comp_blksz,
965 M_DEVBUF, M_WAITOK);
966 vnd->sc_comp_buffblk = -1;
967
968 /* Initialize decompress stream */
969 bzero(&vnd->sc_comp_stream, sizeof(z_stream));
970 vnd->sc_comp_stream.zalloc = vnd_alloc;
971 vnd->sc_comp_stream.zfree = vnd_free;
972 error = inflateInit2(&vnd->sc_comp_stream, MAX_WBITS);
973 if(error) {
974 if(vnd->sc_comp_stream.msg)
975 printf("vnd%d: compressed file, %s\n",
976 unit, vnd->sc_comp_stream.msg);
977 VOP_UNLOCK(nd.ni_vp, 0);
978 error = EINVAL;
979 goto close_and_exit;
980 }
981
982 vnd->sc_flags |= VNF_COMP | VNF_READONLY;
983 #else /* !VND_COMPRESSION */
984 VOP_UNLOCK(nd.ni_vp, 0);
985 error = EOPNOTSUPP;
986 goto close_and_exit;
987 #endif /* VND_COMPRESSION */
988 }
989
990 VOP_UNLOCK(nd.ni_vp, 0);
991 vnd->sc_vp = nd.ni_vp;
992 vnd->sc_size = btodb(vattr.va_size); /* note truncation */
993
994 /*
995 * Use pseudo-geometry specified. If none was provided,
996 * use "standard" Adaptec fictitious geometry.
997 */
998 if (vio->vnd_flags & VNDIOF_HASGEOM) {
999
1000 memcpy(&vnd->sc_geom, &vio->vnd_geom,
1001 sizeof(vio->vnd_geom));
1002
1003 /*
1004 * Sanity-check the sector size.
1005 * XXX Don't allow secsize < DEV_BSIZE. Should
1006 * XXX we?
1007 */
1008 if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
1009 (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0 ||
1010 vnd->sc_geom.vng_ncylinders == 0 ||
1011 (vnd->sc_geom.vng_ntracks *
1012 vnd->sc_geom.vng_nsectors) == 0) {
1013 error = EINVAL;
1014 goto close_and_exit;
1015 }
1016
1017 /*
1018 * Compute the size (in DEV_BSIZE blocks) specified
1019 * by the geometry.
1020 */
1021 geomsize = (vnd->sc_geom.vng_nsectors *
1022 vnd->sc_geom.vng_ntracks *
1023 vnd->sc_geom.vng_ncylinders) *
1024 (vnd->sc_geom.vng_secsize / DEV_BSIZE);
1025
1026 /*
1027 * Sanity-check the size against the specified
1028 * geometry.
1029 */
1030 if (vnd->sc_size < geomsize) {
1031 error = EINVAL;
1032 goto close_and_exit;
1033 }
1034 } else if (vnd->sc_size >= (32 * 64)) {
1035 /*
1036 * Size must be at least 2048 DEV_BSIZE blocks
1037 * (1M) in order to use this geometry.
1038 */
1039 vnd->sc_geom.vng_secsize = DEV_BSIZE;
1040 vnd->sc_geom.vng_nsectors = 32;
1041 vnd->sc_geom.vng_ntracks = 64;
1042 vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
1043 } else {
1044 vnd->sc_geom.vng_secsize = DEV_BSIZE;
1045 vnd->sc_geom.vng_nsectors = 1;
1046 vnd->sc_geom.vng_ntracks = 1;
1047 vnd->sc_geom.vng_ncylinders = vnd->sc_size;
1048 }
1049
1050 if (vio->vnd_flags & VNDIOF_READONLY) {
1051 vnd->sc_flags |= VNF_READONLY;
1052 }
1053
1054 if ((error = vndsetcred(vnd, p->p_ucred)) != 0)
1055 goto close_and_exit;
1056
1057 vndthrottle(vnd, vnd->sc_vp);
1058 vio->vnd_size = dbtob(vnd->sc_size);
1059 vnd->sc_flags |= VNF_INITED;
1060
1061 /* create the kernel thread, wait for it to be up */
1062 error = kthread_create1(vndthread, vnd, &vnd->sc_kthread,
1063 vnd->sc_dev.dv_xname);
1064 if (error)
1065 goto close_and_exit;
1066 while ((vnd->sc_flags & VNF_KTHREAD) == 0) {
1067 tsleep(&vnd->sc_kthread, PRIBIO, "vndthr", 0);
1068 }
1069 #ifdef DEBUG
1070 if (vnddebug & VDB_INIT)
1071 printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
1072 vnd->sc_vp, (unsigned long) vnd->sc_size,
1073 vnd->sc_geom.vng_secsize,
1074 vnd->sc_geom.vng_nsectors,
1075 vnd->sc_geom.vng_ntracks,
1076 vnd->sc_geom.vng_ncylinders);
1077 #endif
1078
1079 /* Attach the disk. */
1080 vnd->sc_dkdev.dk_name = vnd->sc_dev.dv_xname;
1081 pseudo_disk_attach(&vnd->sc_dkdev);
1082
1083 /* Initialize the xfer and buffer pools. */
1084 pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
1085 0, 0, "vndxpl", NULL);
1086
1087 /* Try and read the disklabel. */
1088 vndgetdisklabel(dev, vnd);
1089
1090 vndunlock(vnd);
1091
1092 break;
1093
1094 close_and_exit:
1095 (void) vn_close(nd.ni_vp, fflags, p->p_ucred, l);
1096 unlock_and_exit:
1097 #ifdef VND_COMPRESSION
1098 /* free any allocated memory (for compressed file) */
1099 if(vnd->sc_comp_offsets) {
1100 free(vnd->sc_comp_offsets, M_DEVBUF);
1101 vnd->sc_comp_offsets = NULL;
1102 }
1103 if(vnd->sc_comp_buff) {
1104 free(vnd->sc_comp_buff, M_DEVBUF);
1105 vnd->sc_comp_buff = NULL;
1106 }
1107 if(vnd->sc_comp_decombuf) {
1108 free(vnd->sc_comp_decombuf, M_DEVBUF);
1109 vnd->sc_comp_decombuf = NULL;
1110 }
1111 #endif /* VND_COMPRESSION */
1112 vndunlock(vnd);
1113 return (error);
1114
1115 case VNDIOCCLR:
1116 if ((error = vndlock(vnd)) != 0)
1117 return (error);
1118
1119 /*
1120 * Don't unconfigure if any other partitions are open
1121 * or if both the character and block flavors of this
1122 * partition are open.
1123 */
1124 part = DISKPART(dev);
1125 pmask = (1 << part);
1126 if (((vnd->sc_dkdev.dk_openmask & ~pmask) ||
1127 ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
1128 (vnd->sc_dkdev.dk_copenmask & pmask))) &&
1129 !(vio->vnd_flags & VNDIOF_FORCE)) {
1130 vndunlock(vnd);
1131 return (EBUSY);
1132 }
1133
1134 /*
1135 * XXX vndclear() might call vndclose() implicitely;
1136 * release lock to avoid recursion
1137 */
1138 vndunlock(vnd);
1139 vndclear(vnd, minor(dev));
1140 #ifdef DEBUG
1141 if (vnddebug & VDB_INIT)
1142 printf("vndioctl: CLRed\n");
1143 #endif
1144
1145 /* Destroy the xfer and buffer pools. */
1146 pool_destroy(&vnd->sc_vxpool);
1147
1148 /* Detatch the disk. */
1149 pseudo_disk_detach(&vnd->sc_dkdev);
1150 break;
1151
1152 #ifdef COMPAT_30
1153 case VNDIOOCGET: {
1154 struct vnd_ouser *vnu;
1155 struct vattr va;
1156 vnu = (struct vnd_ouser *)data;
1157 switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
1158 case 0:
1159 vnu->vnu_dev = va.va_fsid;
1160 vnu->vnu_ino = va.va_fileid;
1161 break;
1162 case -1:
1163 /* unused is not an error */
1164 vnu->vnu_dev = 0;
1165 vnu->vnu_ino = 0;
1166 break;
1167 default:
1168 return error;
1169 }
1170 break;
1171 }
1172 #endif
1173 case VNDIOCGET: {
1174 struct vnd_user *vnu;
1175 struct vattr va;
1176 vnu = (struct vnd_user *)data;
1177 switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
1178 case 0:
1179 vnu->vnu_dev = va.va_fsid;
1180 vnu->vnu_ino = va.va_fileid;
1181 break;
1182 case -1:
1183 /* unused is not an error */
1184 vnu->vnu_dev = 0;
1185 vnu->vnu_ino = 0;
1186 break;
1187 default:
1188 return error;
1189 }
1190 break;
1191 }
1192
1193 case DIOCGDINFO:
1194 *(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);
1195 break;
1196
1197 #ifdef __HAVE_OLD_DISKLABEL
1198 case ODIOCGDINFO:
1199 newlabel = *(vnd->sc_dkdev.dk_label);
1200 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1201 return ENOTTY;
1202 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1203 break;
1204 #endif
1205
1206 case DIOCGPART:
1207 ((struct partinfo *)data)->disklab = vnd->sc_dkdev.dk_label;
1208 ((struct partinfo *)data)->part =
1209 &vnd->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
1210 break;
1211
1212 case DIOCWDINFO:
1213 case DIOCSDINFO:
1214 #ifdef __HAVE_OLD_DISKLABEL
1215 case ODIOCWDINFO:
1216 case ODIOCSDINFO:
1217 #endif
1218 {
1219 struct disklabel *lp;
1220
1221 if ((error = vndlock(vnd)) != 0)
1222 return (error);
1223
1224 vnd->sc_flags |= VNF_LABELLING;
1225
1226 #ifdef __HAVE_OLD_DISKLABEL
1227 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1228 memset(&newlabel, 0, sizeof newlabel);
1229 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1230 lp = &newlabel;
1231 } else
1232 #endif
1233 lp = (struct disklabel *)data;
1234
1235 error = setdisklabel(vnd->sc_dkdev.dk_label,
1236 lp, 0, vnd->sc_dkdev.dk_cpulabel);
1237 if (error == 0) {
1238 if (cmd == DIOCWDINFO
1239 #ifdef __HAVE_OLD_DISKLABEL
1240 || cmd == ODIOCWDINFO
1241 #endif
1242 )
1243 error = writedisklabel(VNDLABELDEV(dev),
1244 vndstrategy, vnd->sc_dkdev.dk_label,
1245 vnd->sc_dkdev.dk_cpulabel);
1246 }
1247
1248 vnd->sc_flags &= ~VNF_LABELLING;
1249
1250 vndunlock(vnd);
1251
1252 if (error)
1253 return (error);
1254 break;
1255 }
1256
1257 case DIOCKLABEL:
1258 if (*(int *)data != 0)
1259 vnd->sc_flags |= VNF_KLABEL;
1260 else
1261 vnd->sc_flags &= ~VNF_KLABEL;
1262 break;
1263
1264 case DIOCWLABEL:
1265 if (*(int *)data != 0)
1266 vnd->sc_flags |= VNF_WLABEL;
1267 else
1268 vnd->sc_flags &= ~VNF_WLABEL;
1269 break;
1270
1271 case DIOCGDEFLABEL:
1272 vndgetdefaultlabel(vnd, (struct disklabel *)data);
1273 break;
1274
1275 #ifdef __HAVE_OLD_DISKLABEL
1276 case ODIOCGDEFLABEL:
1277 vndgetdefaultlabel(vnd, &newlabel);
1278 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1279 return ENOTTY;
1280 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1281 break;
1282 #endif
1283
1284 default:
1285 return (ENOTTY);
1286 }
1287
1288 return (0);
1289 }
1290
1291 /*
1292 * Duplicate the current processes' credentials. Since we are called only
1293 * as the result of a SET ioctl and only root can do that, any future access
1294 * to this "disk" is essentially as root. Note that credentials may change
1295 * if some other uid can write directly to the mapped file (NFS).
1296 */
1297 static int
1298 vndsetcred(struct vnd_softc *vnd, struct ucred *cred)
1299 {
1300 struct uio auio;
1301 struct iovec aiov;
1302 char *tmpbuf;
1303 int error;
1304
1305 vnd->sc_cred = crdup(cred);
1306 tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1307
1308 /* XXX: Horrible kludge to establish credentials for NFS */
1309 aiov.iov_base = tmpbuf;
1310 aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
1311 auio.uio_iov = &aiov;
1312 auio.uio_iovcnt = 1;
1313 auio.uio_offset = 0;
1314 auio.uio_rw = UIO_READ;
1315 auio.uio_segflg = UIO_SYSSPACE;
1316 auio.uio_resid = aiov.iov_len;
1317 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1318 error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
1319 if (error == 0) {
1320 /*
1321 * Because vnd does all IO directly through the vnode
1322 * we need to flush (at least) the buffer from the above
1323 * VOP_READ from the buffer cache to prevent cache
1324 * incoherencies. Also, be careful to write dirty
1325 * buffers back to stable storage.
1326 */
1327 error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
1328 curlwp, 0, 0);
1329 }
1330 VOP_UNLOCK(vnd->sc_vp, 0);
1331
1332 free(tmpbuf, M_TEMP);
1333 return (error);
1334 }
1335
1336 /*
1337 * Set maxactive based on FS type
1338 */
1339 static void
1340 vndthrottle(struct vnd_softc *vnd, struct vnode *vp)
1341 {
1342 #ifdef NFS
1343 extern int (**nfsv2_vnodeop_p)(void *);
1344
1345 if (vp->v_op == nfsv2_vnodeop_p)
1346 vnd->sc_maxactive = 2;
1347 else
1348 #endif
1349 vnd->sc_maxactive = 8;
1350
1351 if (vnd->sc_maxactive < 1)
1352 vnd->sc_maxactive = 1;
1353 }
1354
1355 #if 0
1356 static void
1357 vndshutdown(void)
1358 {
1359 struct vnd_softc *vnd;
1360
1361 for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1362 if (vnd->sc_flags & VNF_INITED)
1363 vndclear(vnd);
1364 }
1365 #endif
1366
1367 static void
1368 vndclear(struct vnd_softc *vnd, int myminor)
1369 {
1370 struct vnode *vp = vnd->sc_vp;
1371 struct lwp *l = curlwp;
1372 int fflags = FREAD;
1373 int bmaj, cmaj, i, mn;
1374 int s;
1375
1376 #ifdef DEBUG
1377 if (vnddebug & VDB_FOLLOW)
1378 printf("vndclear(%p): vp %p\n", vnd, vp);
1379 #endif
1380 /* locate the major number */
1381 bmaj = bdevsw_lookup_major(&vnd_bdevsw);
1382 cmaj = cdevsw_lookup_major(&vnd_cdevsw);
1383
1384 /* Nuke the vnodes for any open instances */
1385 for (i = 0; i < MAXPARTITIONS; i++) {
1386 mn = DISKMINOR(vnd->sc_dev.dv_unit, i);
1387 vdevgone(bmaj, mn, mn, VBLK);
1388 if (mn != myminor) /* XXX avoid to kill own vnode */
1389 vdevgone(cmaj, mn, mn, VCHR);
1390 }
1391
1392 if ((vnd->sc_flags & VNF_READONLY) == 0)
1393 fflags |= FWRITE;
1394
1395 s = splbio();
1396 bufq_drain(vnd->sc_tab);
1397 splx(s);
1398
1399 vnd->sc_flags |= VNF_VUNCONF;
1400 wakeup(&vnd->sc_tab);
1401 while (vnd->sc_flags & VNF_KTHREAD)
1402 tsleep(&vnd->sc_kthread, PRIBIO, "vnthr", 0);
1403
1404 #ifdef VND_COMPRESSION
1405 /* free the compressed file buffers */
1406 if(vnd->sc_flags & VNF_COMP) {
1407 if(vnd->sc_comp_offsets) {
1408 free(vnd->sc_comp_offsets, M_DEVBUF);
1409 vnd->sc_comp_offsets = NULL;
1410 }
1411 if(vnd->sc_comp_buff) {
1412 free(vnd->sc_comp_buff, M_DEVBUF);
1413 vnd->sc_comp_buff = NULL;
1414 }
1415 if(vnd->sc_comp_decombuf) {
1416 free(vnd->sc_comp_decombuf, M_DEVBUF);
1417 vnd->sc_comp_decombuf = NULL;
1418 }
1419 }
1420 #endif /* VND_COMPRESSION */
1421 vnd->sc_flags &=
1422 ~(VNF_INITED | VNF_READONLY | VNF_VLABEL
1423 | VNF_VUNCONF | VNF_COMP);
1424 if (vp == (struct vnode *)0)
1425 panic("vndclear: null vp");
1426 (void) vn_close(vp, fflags, vnd->sc_cred, l);
1427 crfree(vnd->sc_cred);
1428 vnd->sc_vp = (struct vnode *)0;
1429 vnd->sc_cred = (struct ucred *)0;
1430 vnd->sc_size = 0;
1431 }
1432
1433 static int
1434 vndsize(dev_t dev)
1435 {
1436 struct vnd_softc *sc;
1437 struct disklabel *lp;
1438 int part, unit, omask;
1439 int size;
1440
1441 unit = vndunit(dev);
1442 sc = (struct vnd_softc *)device_lookup(&vnd_cd, unit);
1443 if (sc == NULL)
1444 return -1;
1445
1446 if ((sc->sc_flags & VNF_INITED) == 0)
1447 return (-1);
1448
1449 part = DISKPART(dev);
1450 omask = sc->sc_dkdev.dk_openmask & (1 << part);
1451 lp = sc->sc_dkdev.dk_label;
1452
1453 if (omask == 0 && vndopen(dev, 0, S_IFBLK, curlwp)) /* XXX */
1454 return (-1);
1455
1456 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1457 size = -1;
1458 else
1459 size = lp->d_partitions[part].p_size *
1460 (lp->d_secsize / DEV_BSIZE);
1461
1462 if (omask == 0 && vndclose(dev, 0, S_IFBLK, curlwp)) /* XXX */
1463 return (-1);
1464
1465 return (size);
1466 }
1467
1468 static int
1469 vnddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1470 {
1471
1472 /* Not implemented. */
1473 return ENXIO;
1474 }
1475
1476 static void
1477 vndgetdefaultlabel(struct vnd_softc *sc, struct disklabel *lp)
1478 {
1479 struct vndgeom *vng = &sc->sc_geom;
1480 struct partition *pp;
1481
1482 memset(lp, 0, sizeof(*lp));
1483
1484 lp->d_secperunit = sc->sc_size / (vng->vng_secsize / DEV_BSIZE);
1485 lp->d_secsize = vng->vng_secsize;
1486 lp->d_nsectors = vng->vng_nsectors;
1487 lp->d_ntracks = vng->vng_ntracks;
1488 lp->d_ncylinders = vng->vng_ncylinders;
1489 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1490
1491 strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1492 lp->d_type = DTYPE_VND;
1493 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1494 lp->d_rpm = 3600;
1495 lp->d_interleave = 1;
1496 lp->d_flags = 0;
1497
1498 pp = &lp->d_partitions[RAW_PART];
1499 pp->p_offset = 0;
1500 pp->p_size = lp->d_secperunit;
1501 pp->p_fstype = FS_UNUSED;
1502 lp->d_npartitions = RAW_PART + 1;
1503
1504 lp->d_magic = DISKMAGIC;
1505 lp->d_magic2 = DISKMAGIC;
1506 lp->d_checksum = dkcksum(lp);
1507 }
1508
1509 /*
1510 * Read the disklabel from a vnd. If one is not present, create a fake one.
1511 */
1512 static void
1513 vndgetdisklabel(dev_t dev, struct vnd_softc *sc)
1514 {
1515 const char *errstring;
1516 struct disklabel *lp = sc->sc_dkdev.dk_label;
1517 struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1518 int i;
1519
1520 memset(clp, 0, sizeof(*clp));
1521
1522 vndgetdefaultlabel(sc, lp);
1523
1524 /*
1525 * Call the generic disklabel extraction routine.
1526 */
1527 errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1528 if (errstring) {
1529 /*
1530 * Lack of disklabel is common, but we print the warning
1531 * anyway, since it might contain other useful information.
1532 */
1533 printf("%s: %s\n", sc->sc_dev.dv_xname, errstring);
1534
1535 /*
1536 * For historical reasons, if there's no disklabel
1537 * present, all partitions must be FS_BSDFFS and
1538 * occupy the entire disk.
1539 */
1540 for (i = 0; i < MAXPARTITIONS; i++) {
1541 /*
1542 * Don't wipe out port specific hack (such as
1543 * dos partition hack of i386 port).
1544 */
1545 if (lp->d_partitions[i].p_size != 0)
1546 continue;
1547
1548 lp->d_partitions[i].p_size = lp->d_secperunit;
1549 lp->d_partitions[i].p_offset = 0;
1550 lp->d_partitions[i].p_fstype = FS_BSDFFS;
1551 }
1552
1553 strncpy(lp->d_packname, "default label",
1554 sizeof(lp->d_packname));
1555
1556 lp->d_npartitions = MAXPARTITIONS;
1557 lp->d_checksum = dkcksum(lp);
1558 }
1559
1560 /* In-core label now valid. */
1561 sc->sc_flags |= VNF_VLABEL;
1562 }
1563
1564 /*
1565 * Wait interruptibly for an exclusive lock.
1566 *
1567 * XXX
1568 * Several drivers do this; it should be abstracted and made MP-safe.
1569 */
1570 static int
1571 vndlock(struct vnd_softc *sc)
1572 {
1573 int error;
1574
1575 while ((sc->sc_flags & VNF_LOCKED) != 0) {
1576 sc->sc_flags |= VNF_WANTED;
1577 if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1578 return (error);
1579 }
1580 sc->sc_flags |= VNF_LOCKED;
1581 return (0);
1582 }
1583
1584 /*
1585 * Unlock and wake up any waiters.
1586 */
1587 static void
1588 vndunlock(struct vnd_softc *sc)
1589 {
1590
1591 sc->sc_flags &= ~VNF_LOCKED;
1592 if ((sc->sc_flags & VNF_WANTED) != 0) {
1593 sc->sc_flags &= ~VNF_WANTED;
1594 wakeup(sc);
1595 }
1596 }
1597
1598 #ifdef VND_COMPRESSION
1599 /* compressed file read */
1600 static void
1601 compstrategy(struct buf *bp, off_t bn)
1602 {
1603 int error;
1604 int unit = vndunit(bp->b_dev);
1605 struct vnd_softc *vnd =
1606 (struct vnd_softc *)device_lookup(&vnd_cd, unit);
1607 u_int32_t comp_block;
1608 struct uio auio;
1609 caddr_t addr;
1610 int s;
1611
1612 /* set up constants for data move */
1613 auio.uio_rw = UIO_READ;
1614 auio.uio_segflg = UIO_SYSSPACE;
1615
1616 /* read, and transfer the data */
1617 addr = bp->b_data;
1618 s = splbio();
1619 while (bp->b_resid > 0) {
1620 unsigned length;
1621 size_t length_in_buffer;
1622 u_int32_t offset_in_buffer;
1623 struct iovec aiov;
1624
1625 /* calculate the compressed block number */
1626 comp_block = bn / (off_t)vnd->sc_comp_blksz;
1627
1628 /* check for good block number */
1629 if (comp_block >= vnd->sc_comp_numoffs) {
1630 bp->b_error = EINVAL;
1631 bp->b_flags |= B_ERROR;
1632 splx(s);
1633 return;
1634 }
1635
1636 /* read in the compressed block, if not in buffer */
1637 if (comp_block != vnd->sc_comp_buffblk) {
1638 length = vnd->sc_comp_offsets[comp_block + 1] -
1639 vnd->sc_comp_offsets[comp_block];
1640 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1641 error = vn_rdwr(UIO_READ, vnd->sc_vp, vnd->sc_comp_buff,
1642 length, vnd->sc_comp_offsets[comp_block],
1643 UIO_SYSSPACE, IO_UNIT, vnd->sc_cred, NULL, NULL);
1644 if (error) {
1645 bp->b_error = error;
1646 bp->b_flags |= B_ERROR;
1647 VOP_UNLOCK(vnd->sc_vp, 0);
1648 splx(s);
1649 return;
1650 }
1651 /* uncompress the buffer */
1652 vnd->sc_comp_stream.next_in = vnd->sc_comp_buff;
1653 vnd->sc_comp_stream.avail_in = length;
1654 vnd->sc_comp_stream.next_out = vnd->sc_comp_decombuf;
1655 vnd->sc_comp_stream.avail_out = vnd->sc_comp_blksz;
1656 inflateReset(&vnd->sc_comp_stream);
1657 error = inflate(&vnd->sc_comp_stream, Z_FINISH);
1658 if (error != Z_STREAM_END) {
1659 if (vnd->sc_comp_stream.msg)
1660 printf("%s: compressed file, %s\n",
1661 vnd->sc_dev.dv_xname,
1662 vnd->sc_comp_stream.msg);
1663 bp->b_error = EBADMSG;
1664 bp->b_flags |= B_ERROR;
1665 VOP_UNLOCK(vnd->sc_vp, 0);
1666 splx(s);
1667 return;
1668 }
1669 vnd->sc_comp_buffblk = comp_block;
1670 VOP_UNLOCK(vnd->sc_vp, 0);
1671 }
1672
1673 /* transfer the usable uncompressed data */
1674 offset_in_buffer = bn % (off_t)vnd->sc_comp_blksz;
1675 length_in_buffer = vnd->sc_comp_blksz - offset_in_buffer;
1676 if (length_in_buffer > bp->b_resid)
1677 length_in_buffer = bp->b_resid;
1678 auio.uio_iov = &aiov;
1679 auio.uio_iovcnt = 1;
1680 aiov.iov_base = addr;
1681 aiov.iov_len = length_in_buffer;
1682 auio.uio_resid = aiov.iov_len;
1683 auio.uio_offset = 0;
1684 error = uiomove(vnd->sc_comp_decombuf + offset_in_buffer,
1685 length_in_buffer, &auio);
1686 if (error) {
1687 bp->b_error = error;
1688 bp->b_flags |= B_ERROR;
1689 splx(s);
1690 return;
1691 }
1692
1693 bn += length_in_buffer;
1694 addr += length_in_buffer;
1695 bp->b_resid -= length_in_buffer;
1696 }
1697 splx(s);
1698 }
1699
1700 /* compression memory allocation routines */
1701 static void *
1702 vnd_alloc(void *aux, u_int items, u_int siz)
1703 {
1704 return malloc(items * siz, M_TEMP, M_NOWAIT);
1705 }
1706
1707 static void
1708 vnd_free(void *aux, void *ptr)
1709 {
1710 free(ptr, M_TEMP);
1711 }
1712 #endif /* VND_COMPRESSION */
1713