vnd.c revision 1.153 1 /* $NetBSD: vnd.c,v 1.153 2006/09/25 13:47:26 cube 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.153 2006/09/25 13:47:26 cube Exp $");
137
138 #if defined(_KERNEL_OPT)
139 #include "fs_nfs.h"
140 #include "opt_vnd.h"
141 #endif
142
143 #include <sys/param.h>
144 #include <sys/systm.h>
145 #include <sys/namei.h>
146 #include <sys/proc.h>
147 #include <sys/kthread.h>
148 #include <sys/errno.h>
149 #include <sys/buf.h>
150 #include <sys/bufq.h>
151 #include <sys/malloc.h>
152 #include <sys/ioctl.h>
153 #include <sys/disklabel.h>
154 #include <sys/device.h>
155 #include <sys/disk.h>
156 #include <sys/stat.h>
157 #include <sys/mount.h>
158 #include <sys/vnode.h>
159 #include <sys/file.h>
160 #include <sys/uio.h>
161 #include <sys/conf.h>
162 #include <sys/kauth.h>
163
164 #include <net/zlib.h>
165
166 #include <miscfs/specfs/specdev.h>
167
168 #include <dev/vndvar.h>
169
170 #if defined(VNDDEBUG) && !defined(DEBUG)
171 #define DEBUG
172 #endif
173
174 #ifdef DEBUG
175 int dovndcluster = 1;
176 #define VDB_FOLLOW 0x01
177 #define VDB_INIT 0x02
178 #define VDB_IO 0x04
179 #define VDB_LABEL 0x08
180 int vnddebug = 0x00;
181 #endif
182
183 #define vndunit(x) DISKUNIT(x)
184
185 struct vndxfer {
186 struct buf vx_buf;
187 struct vnd_softc *vx_vnd;
188 };
189 #define VND_BUFTOXFER(bp) ((struct vndxfer *)(void *)bp)
190
191 #define VND_GETXFER(vnd) pool_get(&(vnd)->sc_vxpool, PR_WAITOK)
192 #define VND_PUTXFER(vnd, vx) pool_put(&(vnd)->sc_vxpool, (vx))
193
194 #define VNDLABELDEV(dev) \
195 (MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
196
197 /* called by main() at boot time (XXX: and the LKM driver) */
198 void vndattach(int);
199
200 static void vndclear(struct vnd_softc *, int);
201 static int vndsetcred(struct vnd_softc *, kauth_cred_t);
202 static void vndthrottle(struct vnd_softc *, struct vnode *);
203 static void vndiodone(struct buf *);
204 #if 0
205 static void vndshutdown(void);
206 #endif
207
208 static void vndgetdefaultlabel(struct vnd_softc *, struct disklabel *);
209 static void vndgetdisklabel(dev_t, struct vnd_softc *);
210
211 static int vndlock(struct vnd_softc *);
212 static void vndunlock(struct vnd_softc *);
213 #ifdef VND_COMPRESSION
214 static void compstrategy(struct buf *, off_t);
215 static void *vnd_alloc(void *, u_int, u_int);
216 static void vnd_free(void *, void *);
217 #endif /* VND_COMPRESSION */
218
219 static void vndthread(void *);
220
221 static dev_type_open(vndopen);
222 static dev_type_close(vndclose);
223 static dev_type_read(vndread);
224 static dev_type_write(vndwrite);
225 static dev_type_ioctl(vndioctl);
226 static dev_type_strategy(vndstrategy);
227 static dev_type_dump(vnddump);
228 static dev_type_size(vndsize);
229
230 const struct bdevsw vnd_bdevsw = {
231 vndopen, vndclose, vndstrategy, vndioctl, vnddump, vndsize, D_DISK
232 };
233
234 const struct cdevsw vnd_cdevsw = {
235 vndopen, vndclose, vndread, vndwrite, vndioctl,
236 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
237 };
238
239 static int vnd_match(struct device *, struct cfdata *, void *);
240 static void vnd_attach(struct device *, struct device *, void *);
241 static int vnd_detach(struct device *, int);
242
243 CFATTACH_DECL(vnd, sizeof(struct vnd_softc),
244 vnd_match, vnd_attach, vnd_detach, NULL);
245 extern struct cfdriver vnd_cd;
246
247 static struct vnd_softc *vnd_spawn(int);
248 int vnd_destroy(struct device *);
249
250 void
251 vndattach(int num)
252 {
253 int error;
254
255 error = config_cfattach_attach(vnd_cd.cd_name, &vnd_ca);
256 if (error)
257 aprint_error("%s: unable to register cfattach\n",
258 vnd_cd.cd_name);
259 }
260
261 static int
262 vnd_match(struct device *self, struct cfdata *cfdata, void *aux)
263 {
264 return 1;
265 }
266
267 static void
268 vnd_attach(struct device *parent, struct device *self, void *aux)
269 {
270 struct vnd_softc *sc = (struct vnd_softc *)self;
271
272 sc->sc_comp_offsets = NULL;
273 sc->sc_comp_buff = NULL;
274 sc->sc_comp_decombuf = NULL;
275 bufq_alloc(&sc->sc_tab, "disksort", BUFQ_SORT_RAWBLOCK);
276 pseudo_disk_init(&sc->sc_dkdev);
277 }
278
279 static int
280 vnd_detach(struct device *self, int flags)
281 {
282 struct vnd_softc *sc = (struct vnd_softc *)self;
283 if (sc->sc_flags & VNF_INITED)
284 return EBUSY;
285
286 bufq_free(sc->sc_tab);
287
288 return 0;
289 }
290
291 static struct vnd_softc *
292 vnd_spawn(int unit)
293 {
294 struct cfdata *cf;
295
296 cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
297 cf->cf_name = vnd_cd.cd_name;
298 cf->cf_atname = vnd_cd.cd_name;
299 cf->cf_unit = unit;
300 cf->cf_fstate = FSTATE_STAR;
301
302 return (struct vnd_softc *)config_attach_pseudo(cf);
303 }
304
305 int
306 vnd_destroy(struct device *dev)
307 {
308 int error;
309 struct cfdata *cf;
310
311 cf = device_cfdata(dev);
312 error = config_detach(dev, DETACH_QUIET);
313 if (error)
314 return error;
315 free(cf, M_DEVBUF);
316 return 0;
317 }
318
319 static int
320 vndopen(dev_t dev, int flags, int mode, struct lwp *l)
321 {
322 int unit = vndunit(dev);
323 struct vnd_softc *sc;
324 int error = 0, part, pmask;
325 struct disklabel *lp;
326
327 #ifdef DEBUG
328 if (vnddebug & VDB_FOLLOW)
329 printf("vndopen(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
330 #endif
331 sc = device_lookup(&vnd_cd, unit);
332 if (sc == NULL) {
333 sc = vnd_spawn(unit);
334 if (sc == NULL)
335 return ENOMEM;
336 }
337
338 if ((error = vndlock(sc)) != 0)
339 return (error);
340
341 lp = sc->sc_dkdev.dk_label;
342
343 part = DISKPART(dev);
344 pmask = (1 << part);
345
346 /*
347 * If we're initialized, check to see if there are any other
348 * open partitions. If not, then it's safe to update the
349 * in-core disklabel. Only read the disklabel if it is
350 * not already valid.
351 */
352 if ((sc->sc_flags & (VNF_INITED|VNF_VLABEL)) == VNF_INITED &&
353 sc->sc_dkdev.dk_openmask == 0)
354 vndgetdisklabel(dev, sc);
355
356 /* Check that the partitions exists. */
357 if (part != RAW_PART) {
358 if (((sc->sc_flags & VNF_INITED) == 0) ||
359 ((part >= lp->d_npartitions) ||
360 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
361 error = ENXIO;
362 goto done;
363 }
364 }
365
366 /* Prevent our unit from being unconfigured while open. */
367 switch (mode) {
368 case S_IFCHR:
369 sc->sc_dkdev.dk_copenmask |= pmask;
370 break;
371
372 case S_IFBLK:
373 sc->sc_dkdev.dk_bopenmask |= pmask;
374 break;
375 }
376 sc->sc_dkdev.dk_openmask =
377 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
378
379 done:
380 vndunlock(sc);
381 return (error);
382 }
383
384 static int
385 vndclose(dev_t dev, int flags, int mode, struct lwp *l)
386 {
387 int unit = vndunit(dev);
388 struct vnd_softc *sc;
389 int error = 0, part;
390
391 #ifdef DEBUG
392 if (vnddebug & VDB_FOLLOW)
393 printf("vndclose(0x%x, 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
394 #endif
395 sc = device_lookup(&vnd_cd, unit);
396 if (sc == NULL)
397 return ENXIO;
398
399 if ((error = vndlock(sc)) != 0)
400 return (error);
401
402 part = DISKPART(dev);
403
404 /* ...that much closer to allowing unconfiguration... */
405 switch (mode) {
406 case S_IFCHR:
407 sc->sc_dkdev.dk_copenmask &= ~(1 << part);
408 break;
409
410 case S_IFBLK:
411 sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
412 break;
413 }
414 sc->sc_dkdev.dk_openmask =
415 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
416
417 vndunlock(sc);
418
419 if ((sc->sc_flags & VNF_INITED) == 0) {
420 if ((error = vnd_destroy((struct device *)sc)) != 0) {
421 aprint_error("%s: unable to detach instance\n",
422 sc->sc_dev.dv_xname);
423 return error;
424 }
425 }
426
427 return (0);
428 }
429
430 /*
431 * Queue the request, and wakeup the kernel thread to handle it.
432 */
433 static void
434 vndstrategy(struct buf *bp)
435 {
436 int unit = vndunit(bp->b_dev);
437 struct vnd_softc *vnd =
438 (struct vnd_softc *)device_lookup(&vnd_cd, unit);
439 struct disklabel *lp = vnd->sc_dkdev.dk_label;
440 daddr_t blkno;
441 int s = splbio();
442
443 bp->b_resid = bp->b_bcount;
444
445 if ((vnd->sc_flags & VNF_INITED) == 0) {
446 bp->b_error = ENXIO;
447 bp->b_flags |= B_ERROR;
448 goto done;
449 }
450
451 /*
452 * The transfer must be a whole number of blocks.
453 */
454 if ((bp->b_bcount % lp->d_secsize) != 0) {
455 bp->b_error = EINVAL;
456 bp->b_flags |= B_ERROR;
457 goto done;
458 }
459
460 /*
461 * check if we're read-only.
462 */
463 if ((vnd->sc_flags & VNF_READONLY) && !(bp->b_flags & B_READ)) {
464 bp->b_error = EACCES;
465 bp->b_flags |= B_ERROR;
466 goto done;
467 }
468
469 /*
470 * Do bounds checking and adjust transfer. If there's an error,
471 * the bounds check will flag that for us.
472 */
473 if (DISKPART(bp->b_dev) == RAW_PART) {
474 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
475 vnd->sc_size) <= 0)
476 goto done;
477 } else {
478 if (bounds_check_with_label(&vnd->sc_dkdev,
479 bp, vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING)) <= 0)
480 goto done;
481 }
482
483 /* If it's a nil transfer, wake up the top half now. */
484 if (bp->b_bcount == 0)
485 goto done;
486
487 /*
488 * Put the block number in terms of the logical blocksize
489 * of the "device".
490 */
491
492 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
493
494 /*
495 * Translate the partition-relative block number to an absolute.
496 */
497 if (DISKPART(bp->b_dev) != RAW_PART) {
498 struct partition *pp;
499
500 pp = &vnd->sc_dkdev.dk_label->d_partitions[
501 DISKPART(bp->b_dev)];
502 blkno += pp->p_offset;
503 }
504 bp->b_rawblkno = blkno;
505
506 #ifdef DEBUG
507 if (vnddebug & VDB_FOLLOW)
508 printf("vndstrategy(%p): unit %d\n", bp, unit);
509 #endif
510 BUFQ_PUT(vnd->sc_tab, bp);
511 wakeup(&vnd->sc_tab);
512 splx(s);
513 return;
514 done:
515 biodone(bp);
516 splx(s);
517 }
518
519 static void
520 vndthread(void *arg)
521 {
522 struct vnd_softc *vnd = arg;
523 struct mount *mp;
524 int s, bsize;
525 int sz, error;
526 struct disklabel *lp;
527
528 s = splbio();
529 vnd->sc_flags |= VNF_KTHREAD;
530 wakeup(&vnd->sc_kthread);
531
532 /*
533 * Dequeue requests, break them into bsize pieces and submit using
534 * VOP_BMAP/VOP_STRATEGY.
535 */
536 while ((vnd->sc_flags & VNF_VUNCONF) == 0) {
537 struct vndxfer *vnx;
538 off_t offset;
539 int resid;
540 int skipped = 0;
541 off_t bn;
542 int flags;
543 struct buf *obp;
544 struct buf *bp;
545
546 obp = BUFQ_GET(vnd->sc_tab);
547 if (obp == NULL) {
548 tsleep(&vnd->sc_tab, PRIBIO, "vndbp", 0);
549 continue;
550 };
551 splx(s);
552 flags = obp->b_flags;
553 #ifdef DEBUG
554 if (vnddebug & VDB_FOLLOW)
555 printf("vndthread(%p\n", obp);
556 #endif
557 lp = vnd->sc_dkdev.dk_label;
558
559 /* convert to a byte offset within the file. */
560 bn = obp->b_rawblkno * lp->d_secsize;
561
562 if (vnd->sc_vp->v_mount == NULL) {
563 obp->b_error = ENXIO;
564 obp->b_flags |= B_ERROR;
565 goto done;
566 }
567 #ifdef VND_COMPRESSION
568 /* handle a compressed read */
569 if ((flags & B_READ) != 0 && (vnd->sc_flags & VNF_COMP)) {
570 compstrategy(obp, bn);
571 goto done;
572 }
573 #endif /* VND_COMPRESSION */
574
575 bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
576
577 /*
578 * Allocate a header for this transfer and link it to the
579 * buffer
580 */
581 s = splbio();
582 vnx = VND_GETXFER(vnd);
583 splx(s);
584 vnx->vx_vnd = vnd;
585
586 bp = &vnx->vx_buf;
587 BUF_INIT(bp);
588 bp->b_flags = (obp->b_flags & B_READ) | B_CALL;
589 bp->b_iodone = vndiodone;
590 bp->b_private = obp;
591 bp->b_vp = vnd->sc_vp;
592 bp->b_data = obp->b_data;
593 bp->b_bcount = bp->b_resid = obp->b_bcount;
594 BIO_COPYPRIO(bp, obp);
595
596 s = splbio();
597 while (vnd->sc_active >= vnd->sc_maxactive) {
598 tsleep(&vnd->sc_tab, PRIBIO, "vndac", 0);
599 }
600 vnd->sc_active++;
601 splx(s);
602
603 if ((flags & B_READ) == 0) {
604 s = splbio();
605 V_INCR_NUMOUTPUT(bp->b_vp);
606 splx(s);
607
608 vn_start_write(vnd->sc_vp, &mp, V_WAIT);
609 }
610
611 /* Instrumentation. */
612 disk_busy(&vnd->sc_dkdev);
613
614 /*
615 * Feed requests sequentially.
616 * We do it this way to keep from flooding NFS servers if we
617 * are connected to an NFS file. This places the burden on
618 * the client rather than the server.
619 */
620 error = 0;
621 for (offset = 0, resid = bp->b_resid; resid;
622 resid -= sz, offset += sz) {
623 struct buf *nbp;
624 struct vnode *vp;
625 daddr_t nbn;
626 int off, nra;
627
628 nra = 0;
629 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
630 error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
631 VOP_UNLOCK(vnd->sc_vp, 0);
632
633 if (error == 0 && (long)nbn == -1)
634 error = EIO;
635
636 /*
637 * If there was an error or a hole in the file...punt.
638 * Note that we may have to wait for any operations
639 * that we have already fired off before releasing
640 * the buffer.
641 *
642 * XXX we could deal with holes here but it would be
643 * a hassle (in the write case).
644 */
645 if (error) {
646 skipped += resid;
647 break;
648 }
649
650 #ifdef DEBUG
651 if (!dovndcluster)
652 nra = 0;
653 #endif
654
655 off = bn % bsize;
656 sz = MIN(((off_t)1 + nra) * bsize - off, resid);
657 #ifdef DEBUG
658 if (vnddebug & VDB_IO)
659 printf("vndstrategy: vp %p/%p bn 0x%qx/0x%" PRIx64
660 " sz 0x%x\n",
661 vnd->sc_vp, vp, (long long)bn, nbn, sz);
662 #endif
663
664 nbp = getiobuf();
665 nestiobuf_setup(bp, nbp, offset, sz);
666 nbp->b_blkno = nbn + btodb(off);
667
668 #if 0 /* XXX #ifdef DEBUG */
669 if (vnddebug & VDB_IO)
670 printf("vndstart(%ld): bp %p vp %p blkno "
671 "0x%" PRIx64 " flags %x addr %p cnt 0x%x\n",
672 (long) (vnd-vnd_softc), &nbp->vb_buf,
673 nbp->vb_buf.b_vp, nbp->vb_buf.b_blkno,
674 nbp->vb_buf.b_flags, nbp->vb_buf.b_data,
675 nbp->vb_buf.b_bcount);
676 #endif
677 VOP_STRATEGY(vp, nbp);
678 bn += sz;
679 }
680 nestiobuf_done(bp, skipped, error);
681
682 if ((flags & B_READ) == 0)
683 vn_finished_write(mp, 0);
684
685 s = splbio();
686 continue;
687 done:
688 biodone(obp);
689 s = splbio();
690 }
691
692 vnd->sc_flags &= (~VNF_KTHREAD | VNF_VUNCONF);
693 wakeup(&vnd->sc_kthread);
694 splx(s);
695 kthread_exit(0);
696 }
697
698 static void
699 vndiodone(struct buf *bp)
700 {
701 struct vndxfer *vnx = VND_BUFTOXFER(bp);
702 struct vnd_softc *vnd = vnx->vx_vnd;
703 struct buf *obp = bp->b_private;
704
705 KASSERT(&vnx->vx_buf == bp);
706 KASSERT(vnd->sc_active > 0);
707 #ifdef DEBUG
708 if (vnddebug & VDB_IO) {
709 printf("vndiodone1: bp %p iodone: error %d\n",
710 bp, (bp->b_flags & B_ERROR) != 0 ? bp->b_error : 0);
711 }
712 #endif
713 disk_unbusy(&vnd->sc_dkdev, bp->b_bcount - bp->b_resid,
714 (bp->b_flags & B_READ));
715 vnd->sc_active--;
716 if (vnd->sc_active == 0) {
717 wakeup(&vnd->sc_tab);
718 }
719 obp->b_flags |= bp->b_flags & B_ERROR;
720 obp->b_error = bp->b_error;
721 obp->b_resid = bp->b_resid;
722 VND_PUTXFER(vnd, vnx);
723 biodone(obp);
724 }
725
726 /* ARGSUSED */
727 static int
728 vndread(dev_t dev, struct uio *uio, int flags)
729 {
730 int unit = vndunit(dev);
731 struct vnd_softc *sc;
732
733 #ifdef DEBUG
734 if (vnddebug & VDB_FOLLOW)
735 printf("vndread(0x%x, %p)\n", dev, uio);
736 #endif
737
738 sc = device_lookup(&vnd_cd, unit);
739 if (sc == NULL)
740 return ENXIO;
741
742 if ((sc->sc_flags & VNF_INITED) == 0)
743 return (ENXIO);
744
745 return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
746 }
747
748 /* ARGSUSED */
749 static int
750 vndwrite(dev_t dev, struct uio *uio, int flags)
751 {
752 int unit = vndunit(dev);
753 struct vnd_softc *sc;
754
755 #ifdef DEBUG
756 if (vnddebug & VDB_FOLLOW)
757 printf("vndwrite(0x%x, %p)\n", dev, uio);
758 #endif
759
760 sc = device_lookup(&vnd_cd, unit);
761 if (sc == NULL)
762 return ENXIO;
763
764 if ((sc->sc_flags & VNF_INITED) == 0)
765 return (ENXIO);
766
767 return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
768 }
769
770 static int
771 vnd_cget(struct lwp *l, int unit, int *un, struct vattr *va)
772 {
773 struct vnd_softc *vnd;
774
775 if (*un == -1)
776 *un = unit;
777 if (*un < 0)
778 return EINVAL;
779
780 vnd = device_lookup(&vnd_cd, *un);
781 if (vnd == NULL)
782 return (*un >= vnd_cd.cd_ndevs) ? ENXIO : -1;
783
784 if ((vnd->sc_flags & VNF_INITED) == 0)
785 return -1;
786
787 return VOP_GETATTR(vnd->sc_vp, va, l->l_cred, l);
788 }
789
790 /* ARGSUSED */
791 static int
792 vndioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
793 {
794 int unit = vndunit(dev);
795 struct vnd_softc *vnd;
796 struct vnd_ioctl *vio;
797 struct vattr vattr;
798 struct nameidata nd;
799 int error, part, pmask;
800 size_t geomsize;
801 int fflags;
802 #ifdef __HAVE_OLD_DISKLABEL
803 struct disklabel newlabel;
804 #endif
805
806 #ifdef DEBUG
807 if (vnddebug & VDB_FOLLOW)
808 printf("vndioctl(0x%x, 0x%lx, %p, 0x%x, %p): unit %d\n",
809 dev, cmd, data, flag, l->l_proc, unit);
810 #endif
811 vnd = device_lookup(&vnd_cd, unit);
812 if (vnd == NULL &&
813 #ifdef COMPAT_30
814 cmd != VNDIOOCGET &&
815 #endif
816 cmd != VNDIOCGET)
817 return ENXIO;
818 vio = (struct vnd_ioctl *)data;
819
820 /* Must be open for writes for these commands... */
821 switch (cmd) {
822 case VNDIOCSET:
823 case VNDIOCCLR:
824 case DIOCSDINFO:
825 case DIOCWDINFO:
826 #ifdef __HAVE_OLD_DISKLABEL
827 case ODIOCSDINFO:
828 case ODIOCWDINFO:
829 #endif
830 case DIOCKLABEL:
831 case DIOCWLABEL:
832 if ((flag & FWRITE) == 0)
833 return (EBADF);
834 }
835
836 /* Must be initialized for these... */
837 switch (cmd) {
838 case VNDIOCCLR:
839 case DIOCGDINFO:
840 case DIOCSDINFO:
841 case DIOCWDINFO:
842 case DIOCGPART:
843 case DIOCKLABEL:
844 case DIOCWLABEL:
845 case DIOCGDEFLABEL:
846 #ifdef __HAVE_OLD_DISKLABEL
847 case ODIOCGDINFO:
848 case ODIOCSDINFO:
849 case ODIOCWDINFO:
850 case ODIOCGDEFLABEL:
851 #endif
852 if ((vnd->sc_flags & VNF_INITED) == 0)
853 return (ENXIO);
854 }
855
856 switch (cmd) {
857 case VNDIOCSET:
858 if (vnd->sc_flags & VNF_INITED)
859 return (EBUSY);
860
861 if ((error = vndlock(vnd)) != 0)
862 return (error);
863
864 fflags = FREAD;
865 if ((vio->vnd_flags & VNDIOF_READONLY) == 0)
866 fflags |= FWRITE;
867 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, l);
868 if ((error = vn_open(&nd, fflags, 0)) != 0)
869 goto unlock_and_exit;
870 KASSERT(l);
871 error = VOP_GETATTR(nd.ni_vp, &vattr, l->l_cred, l);
872 if (!error && nd.ni_vp->v_type != VREG)
873 error = EOPNOTSUPP;
874 if (error) {
875 VOP_UNLOCK(nd.ni_vp, 0);
876 goto close_and_exit;
877 }
878
879 /* If using a compressed file, initialize its info */
880 /* (or abort with an error if kernel has no compression) */
881 if (vio->vnd_flags & VNF_COMP) {
882 #ifdef VND_COMPRESSION
883 struct vnd_comp_header *ch;
884 int i;
885 u_int32_t comp_size;
886 u_int32_t comp_maxsize;
887
888 /* allocate space for compresed file header */
889 ch = malloc(sizeof(struct vnd_comp_header),
890 M_TEMP, M_WAITOK);
891
892 /* read compressed file header */
893 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)ch,
894 sizeof(struct vnd_comp_header), 0, UIO_SYSSPACE,
895 IO_UNIT|IO_NODELOCKED, l->l_cred, NULL, NULL);
896 if(error) {
897 free(ch, M_TEMP);
898 VOP_UNLOCK(nd.ni_vp, 0);
899 goto close_and_exit;
900 }
901
902 /* save some header info */
903 vnd->sc_comp_blksz = ntohl(ch->block_size);
904 /* note last offset is the file byte size */
905 vnd->sc_comp_numoffs = ntohl(ch->num_blocks)+1;
906 free(ch, M_TEMP);
907 if (vnd->sc_comp_blksz == 0 ||
908 vnd->sc_comp_blksz % DEV_BSIZE !=0) {
909 VOP_UNLOCK(nd.ni_vp, 0);
910 error = EINVAL;
911 goto close_and_exit;
912 }
913 if(sizeof(struct vnd_comp_header) +
914 sizeof(u_int64_t) * vnd->sc_comp_numoffs >
915 vattr.va_size) {
916 VOP_UNLOCK(nd.ni_vp, 0);
917 error = EINVAL;
918 goto close_and_exit;
919 }
920
921 /* set decompressed file size */
922 vattr.va_size =
923 ((u_quad_t)vnd->sc_comp_numoffs - 1) *
924 (u_quad_t)vnd->sc_comp_blksz;
925
926 /* allocate space for all the compressed offsets */
927 vnd->sc_comp_offsets =
928 malloc(sizeof(u_int64_t) * vnd->sc_comp_numoffs,
929 M_DEVBUF, M_WAITOK);
930
931 /* read in the offsets */
932 error = vn_rdwr(UIO_READ, nd.ni_vp,
933 (caddr_t)vnd->sc_comp_offsets,
934 sizeof(u_int64_t) * vnd->sc_comp_numoffs,
935 sizeof(struct vnd_comp_header), UIO_SYSSPACE,
936 IO_UNIT|IO_NODELOCKED, l->l_cred, NULL, NULL);
937 if(error) {
938 VOP_UNLOCK(nd.ni_vp, 0);
939 goto close_and_exit;
940 }
941 /*
942 * find largest block size (used for allocation limit).
943 * Also convert offset to native byte order.
944 */
945 comp_maxsize = 0;
946 for (i = 0; i < vnd->sc_comp_numoffs - 1; i++) {
947 vnd->sc_comp_offsets[i] =
948 be64toh(vnd->sc_comp_offsets[i]);
949 comp_size = be64toh(vnd->sc_comp_offsets[i + 1])
950 - vnd->sc_comp_offsets[i];
951 if (comp_size > comp_maxsize)
952 comp_maxsize = comp_size;
953 }
954 vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1] =
955 be64toh(vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1]);
956
957 /* create compressed data buffer */
958 vnd->sc_comp_buff = malloc(comp_maxsize,
959 M_DEVBUF, M_WAITOK);
960
961 /* create decompressed buffer */
962 vnd->sc_comp_decombuf = malloc(vnd->sc_comp_blksz,
963 M_DEVBUF, M_WAITOK);
964 vnd->sc_comp_buffblk = -1;
965
966 /* Initialize decompress stream */
967 bzero(&vnd->sc_comp_stream, sizeof(z_stream));
968 vnd->sc_comp_stream.zalloc = vnd_alloc;
969 vnd->sc_comp_stream.zfree = vnd_free;
970 error = inflateInit2(&vnd->sc_comp_stream, MAX_WBITS);
971 if(error) {
972 if(vnd->sc_comp_stream.msg)
973 printf("vnd%d: compressed file, %s\n",
974 unit, vnd->sc_comp_stream.msg);
975 VOP_UNLOCK(nd.ni_vp, 0);
976 error = EINVAL;
977 goto close_and_exit;
978 }
979
980 vnd->sc_flags |= VNF_COMP | VNF_READONLY;
981 #else /* !VND_COMPRESSION */
982 VOP_UNLOCK(nd.ni_vp, 0);
983 error = EOPNOTSUPP;
984 goto close_and_exit;
985 #endif /* VND_COMPRESSION */
986 }
987
988 VOP_UNLOCK(nd.ni_vp, 0);
989 vnd->sc_vp = nd.ni_vp;
990 vnd->sc_size = btodb(vattr.va_size); /* note truncation */
991
992 /*
993 * Use pseudo-geometry specified. If none was provided,
994 * use "standard" Adaptec fictitious geometry.
995 */
996 if (vio->vnd_flags & VNDIOF_HASGEOM) {
997
998 memcpy(&vnd->sc_geom, &vio->vnd_geom,
999 sizeof(vio->vnd_geom));
1000
1001 /*
1002 * Sanity-check the sector size.
1003 * XXX Don't allow secsize < DEV_BSIZE. Should
1004 * XXX we?
1005 */
1006 if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
1007 (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0 ||
1008 vnd->sc_geom.vng_ncylinders == 0 ||
1009 (vnd->sc_geom.vng_ntracks *
1010 vnd->sc_geom.vng_nsectors) == 0) {
1011 error = EINVAL;
1012 goto close_and_exit;
1013 }
1014
1015 /*
1016 * Compute the size (in DEV_BSIZE blocks) specified
1017 * by the geometry.
1018 */
1019 geomsize = (vnd->sc_geom.vng_nsectors *
1020 vnd->sc_geom.vng_ntracks *
1021 vnd->sc_geom.vng_ncylinders) *
1022 (vnd->sc_geom.vng_secsize / DEV_BSIZE);
1023
1024 /*
1025 * Sanity-check the size against the specified
1026 * geometry.
1027 */
1028 if (vnd->sc_size < geomsize) {
1029 error = EINVAL;
1030 goto close_and_exit;
1031 }
1032 } else if (vnd->sc_size >= (32 * 64)) {
1033 /*
1034 * Size must be at least 2048 DEV_BSIZE blocks
1035 * (1M) in order to use this geometry.
1036 */
1037 vnd->sc_geom.vng_secsize = DEV_BSIZE;
1038 vnd->sc_geom.vng_nsectors = 32;
1039 vnd->sc_geom.vng_ntracks = 64;
1040 vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
1041 } else {
1042 vnd->sc_geom.vng_secsize = DEV_BSIZE;
1043 vnd->sc_geom.vng_nsectors = 1;
1044 vnd->sc_geom.vng_ntracks = 1;
1045 vnd->sc_geom.vng_ncylinders = vnd->sc_size;
1046 }
1047
1048 if (vio->vnd_flags & VNDIOF_READONLY) {
1049 vnd->sc_flags |= VNF_READONLY;
1050 }
1051
1052 if ((error = vndsetcred(vnd, l->l_cred)) != 0)
1053 goto close_and_exit;
1054
1055 vndthrottle(vnd, vnd->sc_vp);
1056 vio->vnd_size = dbtob(vnd->sc_size);
1057 vnd->sc_flags |= VNF_INITED;
1058
1059 /* create the kernel thread, wait for it to be up */
1060 error = kthread_create1(vndthread, vnd, &vnd->sc_kthread,
1061 vnd->sc_dev.dv_xname);
1062 if (error)
1063 goto close_and_exit;
1064 while ((vnd->sc_flags & VNF_KTHREAD) == 0) {
1065 tsleep(&vnd->sc_kthread, PRIBIO, "vndthr", 0);
1066 }
1067 #ifdef DEBUG
1068 if (vnddebug & VDB_INIT)
1069 printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
1070 vnd->sc_vp, (unsigned long) vnd->sc_size,
1071 vnd->sc_geom.vng_secsize,
1072 vnd->sc_geom.vng_nsectors,
1073 vnd->sc_geom.vng_ntracks,
1074 vnd->sc_geom.vng_ncylinders);
1075 #endif
1076
1077 /* Attach the disk. */
1078 vnd->sc_dkdev.dk_name = vnd->sc_dev.dv_xname;
1079 pseudo_disk_attach(&vnd->sc_dkdev);
1080
1081 /* Initialize the xfer and buffer pools. */
1082 pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
1083 0, 0, "vndxpl", NULL);
1084
1085 /* Try and read the disklabel. */
1086 vndgetdisklabel(dev, vnd);
1087
1088 vndunlock(vnd);
1089
1090 break;
1091
1092 close_and_exit:
1093 (void) vn_close(nd.ni_vp, fflags, l->l_cred, l);
1094 unlock_and_exit:
1095 #ifdef VND_COMPRESSION
1096 /* free any allocated memory (for compressed file) */
1097 if(vnd->sc_comp_offsets) {
1098 free(vnd->sc_comp_offsets, M_DEVBUF);
1099 vnd->sc_comp_offsets = NULL;
1100 }
1101 if(vnd->sc_comp_buff) {
1102 free(vnd->sc_comp_buff, M_DEVBUF);
1103 vnd->sc_comp_buff = NULL;
1104 }
1105 if(vnd->sc_comp_decombuf) {
1106 free(vnd->sc_comp_decombuf, M_DEVBUF);
1107 vnd->sc_comp_decombuf = NULL;
1108 }
1109 #endif /* VND_COMPRESSION */
1110 vndunlock(vnd);
1111 return (error);
1112
1113 case VNDIOCCLR:
1114 if ((error = vndlock(vnd)) != 0)
1115 return (error);
1116
1117 /*
1118 * Don't unconfigure if any other partitions are open
1119 * or if both the character and block flavors of this
1120 * partition are open.
1121 */
1122 part = DISKPART(dev);
1123 pmask = (1 << part);
1124 if (((vnd->sc_dkdev.dk_openmask & ~pmask) ||
1125 ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
1126 (vnd->sc_dkdev.dk_copenmask & pmask))) &&
1127 !(vio->vnd_flags & VNDIOF_FORCE)) {
1128 vndunlock(vnd);
1129 return (EBUSY);
1130 }
1131
1132 /*
1133 * XXX vndclear() might call vndclose() implicitely;
1134 * release lock to avoid recursion
1135 */
1136 vndunlock(vnd);
1137 vndclear(vnd, minor(dev));
1138 #ifdef DEBUG
1139 if (vnddebug & VDB_INIT)
1140 printf("vndioctl: CLRed\n");
1141 #endif
1142
1143 /* Destroy the xfer and buffer pools. */
1144 pool_destroy(&vnd->sc_vxpool);
1145
1146 /* Detatch the disk. */
1147 pseudo_disk_detach(&vnd->sc_dkdev);
1148 break;
1149
1150 #ifdef COMPAT_30
1151 case VNDIOOCGET: {
1152 struct vnd_ouser *vnu;
1153 struct vattr va;
1154 vnu = (struct vnd_ouser *)data;
1155 KASSERT(l);
1156 switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
1157 case 0:
1158 vnu->vnu_dev = va.va_fsid;
1159 vnu->vnu_ino = va.va_fileid;
1160 break;
1161 case -1:
1162 /* unused is not an error */
1163 vnu->vnu_dev = 0;
1164 vnu->vnu_ino = 0;
1165 break;
1166 default:
1167 return error;
1168 }
1169 break;
1170 }
1171 #endif
1172 case VNDIOCGET: {
1173 struct vnd_user *vnu;
1174 struct vattr va;
1175 vnu = (struct vnd_user *)data;
1176 KASSERT(l);
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, kauth_cred_t cred)
1299 {
1300 struct uio auio;
1301 struct iovec aiov;
1302 char *tmpbuf;
1303 int error;
1304
1305 vnd->sc_cred = kauth_cred_dup(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_resid = aiov.iov_len;
1316 UIO_SETUP_SYSSPACE(&auio);
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(device_unit(&vnd->sc_dev), 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 kauth_cred_free(vnd->sc_cred);
1428 vnd->sc_vp = (struct vnode *)0;
1429 vnd->sc_cred = (kauth_cred_t)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 UIO_SETUP_SYSSPACE(&auio);
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