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