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