vnd.c revision 1.240 1 /* $NetBSD: vnd.c,v 1.240 2015/01/28 15:08:12 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998, 2008 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1988 University of Utah.
34 * Copyright (c) 1990, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This code is derived from software contributed to Berkeley by
38 * the Systems Programming Group of the University of Utah Computer
39 * Science Department.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * from: Utah $Hdr: vn.c 1.13 94/04/02$
66 *
67 * @(#)vn.c 8.9 (Berkeley) 5/14/95
68 */
69
70 /*
71 * Vnode disk driver.
72 *
73 * Block/character interface to a vnode. Allows one to treat a file
74 * as a disk (e.g. build a filesystem in it, mount it, etc.).
75 *
76 * NOTE 1: If the vnode supports the VOP_BMAP and VOP_STRATEGY operations,
77 * this uses them to avoid distorting the local buffer cache. If those
78 * block-level operations are not available, this falls back to the regular
79 * read and write calls. Using these may distort the cache in some cases
80 * but better have the driver working than preventing it to work on file
81 * systems where the block-level operations are not implemented for
82 * whatever reason.
83 *
84 * NOTE 2: There is a security issue involved with this driver.
85 * Once mounted all access to the contents of the "mapped" file via
86 * the special file is controlled by the permissions on the special
87 * file, the protection of the mapped file is ignored (effectively,
88 * by using root credentials in all transactions).
89 *
90 * NOTE 3: Doesn't interact with leases, should it?
91 */
92
93 #include <sys/cdefs.h>
94 __KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.240 2015/01/28 15:08:12 bouyer Exp $");
95
96 #if defined(_KERNEL_OPT)
97 #include "opt_vnd.h"
98 #include "opt_compat_netbsd.h"
99 #endif
100
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/namei.h>
104 #include <sys/proc.h>
105 #include <sys/kthread.h>
106 #include <sys/errno.h>
107 #include <sys/buf.h>
108 #include <sys/bufq.h>
109 #include <sys/malloc.h>
110 #include <sys/ioctl.h>
111 #include <sys/disklabel.h>
112 #include <sys/device.h>
113 #include <sys/disk.h>
114 #include <sys/stat.h>
115 #include <sys/mount.h>
116 #include <sys/vnode.h>
117 #include <sys/file.h>
118 #include <sys/uio.h>
119 #include <sys/conf.h>
120 #include <sys/kauth.h>
121
122 #include <net/zlib.h>
123
124 #include <miscfs/genfs/genfs.h>
125 #include <miscfs/specfs/specdev.h>
126
127 #include <dev/dkvar.h>
128 #include <dev/vndvar.h>
129
130 #if defined(VNDDEBUG) && !defined(DEBUG)
131 #define DEBUG
132 #endif
133
134 #ifdef DEBUG
135 int dovndcluster = 1;
136 #define VDB_FOLLOW 0x01
137 #define VDB_INIT 0x02
138 #define VDB_IO 0x04
139 #define VDB_LABEL 0x08
140 int vnddebug = 0x00;
141 #endif
142
143 #define vndunit(x) DISKUNIT(x)
144
145 struct vndxfer {
146 struct buf vx_buf;
147 struct vnd_softc *vx_vnd;
148 };
149 #define VND_BUFTOXFER(bp) ((struct vndxfer *)(void *)bp)
150
151 #define VND_GETXFER(vnd) pool_get(&(vnd)->sc_vxpool, PR_WAITOK)
152 #define VND_PUTXFER(vnd, vx) pool_put(&(vnd)->sc_vxpool, (vx))
153
154 #define VNDLABELDEV(dev) \
155 (MAKEDISKDEV(major((dev)), vndunit((dev)), RAW_PART))
156
157 #define VND_MAXPENDING(vnd) ((vnd)->sc_maxactive * 4)
158
159 /* called by main() at boot time */
160 void vndattach(int);
161
162 static void vndclear(struct vnd_softc *, int);
163 static int vnddoclear(struct vnd_softc *, int, int, bool);
164 static int vndsetcred(struct vnd_softc *, kauth_cred_t);
165 static void vndthrottle(struct vnd_softc *, struct vnode *);
166 static void vndiodone(struct buf *);
167 #if 0
168 static void vndshutdown(void);
169 #endif
170
171 static void vndgetdefaultlabel(struct vnd_softc *, struct disklabel *);
172 static void vndgetdisklabel(dev_t, struct vnd_softc *);
173
174 static int vndlock(struct vnd_softc *);
175 static void vndunlock(struct vnd_softc *);
176 #ifdef VND_COMPRESSION
177 static void compstrategy(struct buf *, off_t);
178 static void *vnd_alloc(void *, u_int, u_int);
179 static void vnd_free(void *, void *);
180 #endif /* VND_COMPRESSION */
181
182 static void vndthread(void *);
183 static bool vnode_has_op(const struct vnode *, int);
184 static void handle_with_rdwr(struct vnd_softc *, const struct buf *,
185 struct buf *);
186 static void handle_with_strategy(struct vnd_softc *, const struct buf *,
187 struct buf *);
188 static void vnd_set_geometry(struct vnd_softc *);
189
190 static dev_type_open(vndopen);
191 static dev_type_close(vndclose);
192 static dev_type_read(vndread);
193 static dev_type_write(vndwrite);
194 static dev_type_ioctl(vndioctl);
195 static dev_type_strategy(vndstrategy);
196 static dev_type_dump(vnddump);
197 static dev_type_size(vndsize);
198
199 const struct bdevsw vnd_bdevsw = {
200 .d_open = vndopen,
201 .d_close = vndclose,
202 .d_strategy = vndstrategy,
203 .d_ioctl = vndioctl,
204 .d_dump = vnddump,
205 .d_psize = vndsize,
206 .d_discard = nodiscard,
207 .d_flag = D_DISK
208 };
209
210 const struct cdevsw vnd_cdevsw = {
211 .d_open = vndopen,
212 .d_close = vndclose,
213 .d_read = vndread,
214 .d_write = vndwrite,
215 .d_ioctl = vndioctl,
216 .d_stop = nostop,
217 .d_tty = notty,
218 .d_poll = nopoll,
219 .d_mmap = nommap,
220 .d_kqfilter = nokqfilter,
221 .d_discard = nodiscard,
222 .d_flag = D_DISK
223 };
224
225 static int vnd_match(device_t, cfdata_t, void *);
226 static void vnd_attach(device_t, device_t, void *);
227 static int vnd_detach(device_t, int);
228
229 CFATTACH_DECL3_NEW(vnd, sizeof(struct vnd_softc),
230 vnd_match, vnd_attach, vnd_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
231 extern struct cfdriver vnd_cd;
232
233 static struct vnd_softc *vnd_spawn(int);
234 int vnd_destroy(device_t);
235
236 static struct dkdriver vnddkdriver = { vndstrategy, minphys };
237
238 void
239 vndattach(int num)
240 {
241 int error;
242
243 error = config_cfattach_attach(vnd_cd.cd_name, &vnd_ca);
244 if (error)
245 aprint_error("%s: unable to register cfattach\n",
246 vnd_cd.cd_name);
247 }
248
249 static int
250 vnd_match(device_t self, cfdata_t cfdata, void *aux)
251 {
252
253 return 1;
254 }
255
256 static void
257 vnd_attach(device_t parent, device_t self, void *aux)
258 {
259 struct vnd_softc *sc = device_private(self);
260
261 sc->sc_dev = self;
262 sc->sc_comp_offsets = NULL;
263 sc->sc_comp_buff = NULL;
264 sc->sc_comp_decombuf = NULL;
265 bufq_alloc(&sc->sc_tab, "disksort", BUFQ_SORT_RAWBLOCK);
266 disk_init(&sc->sc_dkdev, device_xname(self), &vnddkdriver);
267 if (!pmf_device_register(self, NULL, NULL))
268 aprint_error_dev(self, "couldn't establish power handler\n");
269 }
270
271 static int
272 vnd_detach(device_t self, int flags)
273 {
274 int error;
275 struct vnd_softc *sc = device_private(self);
276
277 if (sc->sc_flags & VNF_INITED) {
278 error = vnddoclear(sc, 0, -1, (flags & DETACH_FORCE) != 0);
279 if (error != 0)
280 return error;
281 }
282
283 pmf_device_deregister(self);
284 bufq_free(sc->sc_tab);
285 disk_destroy(&sc->sc_dkdev);
286
287 return 0;
288 }
289
290 static struct vnd_softc *
291 vnd_spawn(int unit)
292 {
293 cfdata_t cf;
294
295 cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
296 cf->cf_name = vnd_cd.cd_name;
297 cf->cf_atname = vnd_cd.cd_name;
298 cf->cf_unit = unit;
299 cf->cf_fstate = FSTATE_STAR;
300
301 return device_private(config_attach_pseudo(cf));
302 }
303
304 int
305 vnd_destroy(device_t dev)
306 {
307 int error;
308 cfdata_t cf;
309
310 cf = device_cfdata(dev);
311 error = config_detach(dev, DETACH_QUIET);
312 if (error)
313 return error;
314 free(cf, M_DEVBUF);
315 return 0;
316 }
317
318 static int
319 vndopen(dev_t dev, int flags, int mode, struct lwp *l)
320 {
321 int unit = vndunit(dev);
322 struct vnd_softc *sc;
323 int error = 0, part, pmask;
324 struct disklabel *lp;
325
326 #ifdef DEBUG
327 if (vnddebug & VDB_FOLLOW)
328 printf("vndopen(0x%"PRIx64", 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
329 #endif
330 sc = device_lookup_private(&vnd_cd, unit);
331 if (sc == NULL) {
332 sc = vnd_spawn(unit);
333 if (sc == NULL)
334 return ENOMEM;
335 }
336
337 if ((error = vndlock(sc)) != 0)
338 return error;
339
340 if ((sc->sc_flags & VNF_CLEARING) != 0) {
341 error = ENXIO;
342 goto done;
343 }
344
345 lp = sc->sc_dkdev.dk_label;
346
347 part = DISKPART(dev);
348 pmask = (1 << part);
349
350 if (sc->sc_dkdev.dk_nwedges != 0 && part != RAW_PART) {
351 error = EBUSY;
352 goto done;
353 }
354
355 if (sc->sc_flags & VNF_INITED) {
356 if ((sc->sc_dkdev.dk_openmask & ~(1<<RAW_PART)) != 0) {
357 /*
358 * If any non-raw partition is open, but the disk
359 * has been invalidated, disallow further opens.
360 */
361 if ((sc->sc_flags & VNF_VLABEL) == 0) {
362 error = EIO;
363 goto done;
364 }
365 } else {
366 /*
367 * Load the partition info if not already loaded.
368 */
369 if ((sc->sc_flags & VNF_VLABEL) == 0) {
370 sc->sc_flags |= VNF_VLABEL;
371 vndgetdisklabel(dev, sc);
372 }
373 }
374 }
375
376 /* Check that the partitions exists. */
377 if (part != RAW_PART) {
378 if (((sc->sc_flags & VNF_INITED) == 0) ||
379 ((part >= lp->d_npartitions) ||
380 (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
381 error = ENXIO;
382 goto done;
383 }
384 }
385
386 /* Prevent our unit from being unconfigured while open. */
387 switch (mode) {
388 case S_IFCHR:
389 sc->sc_dkdev.dk_copenmask |= pmask;
390 break;
391
392 case S_IFBLK:
393 sc->sc_dkdev.dk_bopenmask |= pmask;
394 break;
395 }
396 sc->sc_dkdev.dk_openmask =
397 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
398
399 done:
400 vndunlock(sc);
401 return error;
402 }
403
404 static int
405 vndclose(dev_t dev, int flags, int mode, struct lwp *l)
406 {
407 int unit = vndunit(dev);
408 struct vnd_softc *sc;
409 int error = 0, part;
410
411 #ifdef DEBUG
412 if (vnddebug & VDB_FOLLOW)
413 printf("vndclose(0x%"PRIx64", 0x%x, 0x%x, %p)\n", dev, flags, mode, l);
414 #endif
415 sc = device_lookup_private(&vnd_cd, unit);
416 if (sc == NULL)
417 return ENXIO;
418
419 if ((error = vndlock(sc)) != 0)
420 return error;
421
422 part = DISKPART(dev);
423
424 /* ...that much closer to allowing unconfiguration... */
425 switch (mode) {
426 case S_IFCHR:
427 sc->sc_dkdev.dk_copenmask &= ~(1 << part);
428 break;
429
430 case S_IFBLK:
431 sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
432 break;
433 }
434 sc->sc_dkdev.dk_openmask =
435 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
436
437 vndunlock(sc);
438
439 if ((sc->sc_flags & VNF_INITED) == 0) {
440 if ((error = vnd_destroy(sc->sc_dev)) != 0) {
441 aprint_error_dev(sc->sc_dev,
442 "unable to detach instance\n");
443 return error;
444 }
445 }
446
447 return 0;
448 }
449
450 /*
451 * Queue the request, and wakeup the kernel thread to handle it.
452 */
453 static void
454 vndstrategy(struct buf *bp)
455 {
456 int unit = vndunit(bp->b_dev);
457 struct vnd_softc *vnd =
458 device_lookup_private(&vnd_cd, unit);
459 struct disklabel *lp;
460 daddr_t blkno;
461 int s = splbio();
462
463 if (vnd == NULL) {
464 bp->b_error = ENXIO;
465 goto done;
466 }
467 lp = vnd->sc_dkdev.dk_label;
468
469 if ((vnd->sc_flags & VNF_INITED) == 0) {
470 bp->b_error = ENXIO;
471 goto done;
472 }
473
474 /*
475 * The transfer must be a whole number of blocks.
476 */
477 if ((bp->b_bcount % lp->d_secsize) != 0) {
478 bp->b_error = EINVAL;
479 goto done;
480 }
481
482 /*
483 * check if we're read-only.
484 */
485 if ((vnd->sc_flags & VNF_READONLY) && !(bp->b_flags & B_READ)) {
486 bp->b_error = EACCES;
487 goto done;
488 }
489
490 /* If it's a nil transfer, wake up the top half now. */
491 if (bp->b_bcount == 0) {
492 goto done;
493 }
494
495 /*
496 * Do bounds checking and adjust transfer. If there's an error,
497 * the bounds check will flag that for us.
498 */
499 if (DISKPART(bp->b_dev) == RAW_PART) {
500 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
501 vnd->sc_size) <= 0)
502 goto done;
503 } else {
504 if (bounds_check_with_label(&vnd->sc_dkdev,
505 bp, vnd->sc_flags & (VNF_WLABEL|VNF_LABELLING)) <= 0)
506 goto done;
507 }
508
509 /*
510 * Put the block number in terms of the logical blocksize
511 * of the "device".
512 */
513
514 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
515
516 /*
517 * Translate the partition-relative block number to an absolute.
518 */
519 if (DISKPART(bp->b_dev) != RAW_PART) {
520 struct partition *pp;
521
522 pp = &vnd->sc_dkdev.dk_label->d_partitions[
523 DISKPART(bp->b_dev)];
524 blkno += pp->p_offset;
525 }
526 bp->b_rawblkno = blkno;
527
528 #ifdef DEBUG
529 if (vnddebug & VDB_FOLLOW)
530 printf("vndstrategy(%p): unit %d\n", bp, unit);
531 #endif
532 if ((vnd->sc_flags & VNF_USE_VN_RDWR)) {
533 KASSERT(vnd->sc_pending >= 0 &&
534 vnd->sc_pending <= VND_MAXPENDING(vnd));
535 while (vnd->sc_pending == VND_MAXPENDING(vnd))
536 tsleep(&vnd->sc_pending, PRIBIO, "vndpc", 0);
537 vnd->sc_pending++;
538 }
539 bufq_put(vnd->sc_tab, bp);
540 wakeup(&vnd->sc_tab);
541 splx(s);
542 return;
543
544 done:
545 bp->b_resid = bp->b_bcount;
546 biodone(bp);
547 splx(s);
548 }
549
550 static bool
551 vnode_has_strategy(struct vnd_softc *vnd)
552 {
553 return vnode_has_op(vnd->sc_vp, VOFFSET(vop_bmap)) &&
554 vnode_has_op(vnd->sc_vp, VOFFSET(vop_strategy));
555 }
556
557 /* XXX this function needs a reliable check to detect
558 * sparse files. Otherwise, bmap/strategy may be used
559 * and fail on non-allocated blocks. VOP_READ/VOP_WRITE
560 * works on sparse files.
561 */
562 #if notyet
563 static bool
564 vnode_strategy_probe(struct vnd_softc *vnd)
565 {
566 int error;
567 daddr_t nbn;
568
569 if (!vnode_has_strategy(vnd))
570 return false;
571
572 /* Convert the first logical block number to its
573 * physical block number.
574 */
575 error = 0;
576 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
577 error = VOP_BMAP(vnd->sc_vp, 0, NULL, &nbn, NULL);
578 VOP_UNLOCK(vnd->sc_vp);
579
580 /* Test if that worked. */
581 if (error == 0 && (long)nbn == -1)
582 return false;
583
584 return true;
585 }
586 #endif
587
588 static void
589 vndthread(void *arg)
590 {
591 struct vnd_softc *vnd = arg;
592 int s;
593
594 /* Determine whether we can *use* VOP_BMAP and VOP_STRATEGY to
595 * directly access the backing vnode. If we can, use these two
596 * operations to avoid messing with the local buffer cache.
597 * Otherwise fall back to regular VOP_READ/VOP_WRITE operations
598 * which are guaranteed to work with any file system. */
599 if ((vnd->sc_flags & VNF_USE_VN_RDWR) == 0 &&
600 ! vnode_has_strategy(vnd))
601 vnd->sc_flags |= VNF_USE_VN_RDWR;
602
603 #ifdef DEBUG
604 if (vnddebug & VDB_INIT)
605 printf("vndthread: vp %p, %s\n", vnd->sc_vp,
606 (vnd->sc_flags & VNF_USE_VN_RDWR) == 0 ?
607 "using bmap/strategy operations" :
608 "using read/write operations");
609 #endif
610
611 s = splbio();
612 vnd->sc_flags |= VNF_KTHREAD;
613 wakeup(&vnd->sc_kthread);
614
615 /*
616 * Dequeue requests and serve them depending on the available
617 * vnode operations.
618 */
619 while ((vnd->sc_flags & VNF_VUNCONF) == 0) {
620 struct vndxfer *vnx;
621 struct buf *obp;
622 struct buf *bp;
623
624 obp = bufq_get(vnd->sc_tab);
625 if (obp == NULL) {
626 tsleep(&vnd->sc_tab, PRIBIO, "vndbp", 0);
627 continue;
628 };
629 if ((vnd->sc_flags & VNF_USE_VN_RDWR)) {
630 KASSERT(vnd->sc_pending > 0 &&
631 vnd->sc_pending <= VND_MAXPENDING(vnd));
632 if (vnd->sc_pending-- == VND_MAXPENDING(vnd))
633 wakeup(&vnd->sc_pending);
634 }
635 splx(s);
636 #ifdef DEBUG
637 if (vnddebug & VDB_FOLLOW)
638 printf("vndthread(%p)\n", obp);
639 #endif
640
641 if (vnd->sc_vp->v_mount == NULL) {
642 obp->b_error = ENXIO;
643 goto done;
644 }
645 #ifdef VND_COMPRESSION
646 /* handle a compressed read */
647 if ((obp->b_flags & B_READ) != 0 && (vnd->sc_flags & VNF_COMP)) {
648 off_t bn;
649
650 /* Convert to a byte offset within the file. */
651 bn = obp->b_rawblkno *
652 vnd->sc_dkdev.dk_label->d_secsize;
653
654 compstrategy(obp, bn);
655 goto done;
656 }
657 #endif /* VND_COMPRESSION */
658
659 /*
660 * Allocate a header for this transfer and link it to the
661 * buffer
662 */
663 s = splbio();
664 vnx = VND_GETXFER(vnd);
665 splx(s);
666 vnx->vx_vnd = vnd;
667
668 s = splbio();
669 while (vnd->sc_active >= vnd->sc_maxactive) {
670 tsleep(&vnd->sc_tab, PRIBIO, "vndac", 0);
671 }
672 vnd->sc_active++;
673 splx(s);
674
675 /* Instrumentation. */
676 disk_busy(&vnd->sc_dkdev);
677
678 bp = &vnx->vx_buf;
679 buf_init(bp);
680 bp->b_flags = (obp->b_flags & B_READ);
681 bp->b_oflags = obp->b_oflags;
682 bp->b_cflags = obp->b_cflags;
683 bp->b_iodone = vndiodone;
684 bp->b_private = obp;
685 bp->b_vp = vnd->sc_vp;
686 bp->b_objlock = bp->b_vp->v_interlock;
687 bp->b_data = obp->b_data;
688 bp->b_bcount = obp->b_bcount;
689 BIO_COPYPRIO(bp, obp);
690
691 /* Handle the request using the appropriate operations. */
692 if ((vnd->sc_flags & VNF_USE_VN_RDWR) == 0)
693 handle_with_strategy(vnd, obp, bp);
694 else
695 handle_with_rdwr(vnd, obp, bp);
696
697 s = splbio();
698 continue;
699
700 done:
701 biodone(obp);
702 s = splbio();
703 }
704
705 vnd->sc_flags &= (~VNF_KTHREAD | VNF_VUNCONF);
706 wakeup(&vnd->sc_kthread);
707 splx(s);
708 kthread_exit(0);
709 }
710
711 /*
712 * Checks if the given vnode supports the requested operation.
713 * The operation is specified the offset returned by VOFFSET.
714 *
715 * XXX The test below used to determine this is quite fragile
716 * because it relies on the file system to use genfs to specify
717 * unimplemented operations. There might be another way to do
718 * it more cleanly.
719 */
720 static bool
721 vnode_has_op(const struct vnode *vp, int opoffset)
722 {
723 int (*defaultp)(void *);
724 int (*opp)(void *);
725
726 defaultp = vp->v_op[VOFFSET(vop_default)];
727 opp = vp->v_op[opoffset];
728
729 return opp != defaultp && opp != genfs_eopnotsupp &&
730 opp != genfs_badop && opp != genfs_nullop;
731 }
732
733 /*
734 * Handes the read/write request given in 'bp' using the vnode's VOP_READ
735 * and VOP_WRITE operations.
736 *
737 * 'obp' is a pointer to the original request fed to the vnd device.
738 */
739 static void
740 handle_with_rdwr(struct vnd_softc *vnd, const struct buf *obp, struct buf *bp)
741 {
742 bool doread;
743 off_t offset;
744 size_t len, resid;
745 struct vnode *vp;
746
747 doread = bp->b_flags & B_READ;
748 offset = obp->b_rawblkno * vnd->sc_dkdev.dk_label->d_secsize;
749 len = bp->b_bcount;
750 vp = vnd->sc_vp;
751
752 #if defined(DEBUG)
753 if (vnddebug & VDB_IO)
754 printf("vnd (rdwr): vp %p, %s, rawblkno 0x%" PRIx64
755 ", secsize %d, offset %" PRIu64
756 ", bcount %d\n",
757 vp, doread ? "read" : "write", obp->b_rawblkno,
758 vnd->sc_dkdev.dk_label->d_secsize, offset,
759 bp->b_bcount);
760 #endif
761
762 /* Issue the read or write operation. */
763 bp->b_error =
764 vn_rdwr(doread ? UIO_READ : UIO_WRITE,
765 vp, bp->b_data, len, offset, UIO_SYSSPACE,
766 IO_ADV_ENCODE(POSIX_FADV_NOREUSE), vnd->sc_cred, &resid, NULL);
767 bp->b_resid = resid;
768
769 mutex_enter(vp->v_interlock);
770 (void) VOP_PUTPAGES(vp, 0, 0,
771 PGO_ALLPAGES | PGO_CLEANIT | PGO_FREE | PGO_SYNCIO);
772
773 /* We need to increase the number of outputs on the vnode if
774 * there was any write to it. */
775 if (!doread) {
776 mutex_enter(vp->v_interlock);
777 vp->v_numoutput++;
778 mutex_exit(vp->v_interlock);
779 }
780
781 biodone(bp);
782 }
783
784 /*
785 * Handes the read/write request given in 'bp' using the vnode's VOP_BMAP
786 * and VOP_STRATEGY operations.
787 *
788 * 'obp' is a pointer to the original request fed to the vnd device.
789 */
790 static void
791 handle_with_strategy(struct vnd_softc *vnd, const struct buf *obp,
792 struct buf *bp)
793 {
794 int bsize, error, flags, skipped;
795 size_t resid, sz;
796 off_t bn, offset;
797 struct vnode *vp;
798 struct buf *nbp = NULL;
799
800 flags = obp->b_flags;
801
802
803 /* convert to a byte offset within the file. */
804 bn = obp->b_rawblkno * vnd->sc_dkdev.dk_label->d_secsize;
805
806 bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
807 skipped = 0;
808
809 /*
810 * Break the request into bsize pieces and feed them
811 * sequentially using VOP_BMAP/VOP_STRATEGY.
812 * We do it this way to keep from flooding NFS servers if we
813 * are connected to an NFS file. This places the burden on
814 * the client rather than the server.
815 */
816 error = 0;
817 bp->b_resid = bp->b_bcount;
818 for (offset = 0, resid = bp->b_resid; /* true */;
819 resid -= sz, offset += sz) {
820 daddr_t nbn;
821 int off, nra;
822
823 nra = 0;
824 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
825 error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
826 VOP_UNLOCK(vnd->sc_vp);
827
828 if (error == 0 && (long)nbn == -1)
829 error = EIO;
830
831 /*
832 * If there was an error or a hole in the file...punt.
833 * Note that we may have to wait for any operations
834 * that we have already fired off before releasing
835 * the buffer.
836 *
837 * XXX we could deal with holes here but it would be
838 * a hassle (in the write case).
839 */
840 if (error) {
841 skipped += resid;
842 break;
843 }
844
845 #ifdef DEBUG
846 if (!dovndcluster)
847 nra = 0;
848 #endif
849
850 off = bn % bsize;
851 sz = MIN(((off_t)1 + nra) * bsize - off, resid);
852 #ifdef DEBUG
853 if (vnddebug & VDB_IO)
854 printf("vndstrategy: vp %p/%p bn 0x%qx/0x%" PRIx64
855 " sz 0x%zx\n", vnd->sc_vp, vp, (long long)bn,
856 nbn, sz);
857 #endif
858
859 nbp = getiobuf(vp, true);
860 nestiobuf_setup(bp, nbp, offset, sz);
861 nbp->b_blkno = nbn + btodb(off);
862
863 #if 0 /* XXX #ifdef DEBUG */
864 if (vnddebug & VDB_IO)
865 printf("vndstart(%ld): bp %p vp %p blkno "
866 "0x%" PRIx64 " flags %x addr %p cnt 0x%x\n",
867 (long) (vnd-vnd_softc), &nbp->vb_buf,
868 nbp->vb_buf.b_vp, nbp->vb_buf.b_blkno,
869 nbp->vb_buf.b_flags, nbp->vb_buf.b_data,
870 nbp->vb_buf.b_bcount);
871 #endif
872 if (resid == sz) {
873 break;
874 }
875 VOP_STRATEGY(vp, nbp);
876 bn += sz;
877 }
878 if (!(flags & B_READ)) {
879 struct vnode *w_vp;
880 /*
881 * this is the last nested buf, account for
882 * the parent buf write too.
883 * This has to be done last, so that
884 * fsync won't wait for this write which
885 * has to chance to complete before all nested bufs
886 * have been queued. But it has to be done
887 * before the last VOP_STRATEGY()
888 * or the call to nestiobuf_done().
889 */
890 w_vp = bp->b_vp;
891 mutex_enter(w_vp->v_interlock);
892 w_vp->v_numoutput++;
893 mutex_exit(w_vp->v_interlock);
894 }
895 KASSERT(skipped != 0 || nbp != NULL);
896 if (skipped)
897 nestiobuf_done(bp, skipped, error);
898 else
899 VOP_STRATEGY(vp, nbp);
900 }
901
902 static void
903 vndiodone(struct buf *bp)
904 {
905 struct vndxfer *vnx = VND_BUFTOXFER(bp);
906 struct vnd_softc *vnd = vnx->vx_vnd;
907 struct buf *obp = bp->b_private;
908 int s = splbio();
909
910 KASSERT(&vnx->vx_buf == bp);
911 KASSERT(vnd->sc_active > 0);
912 #ifdef DEBUG
913 if (vnddebug & VDB_IO) {
914 printf("vndiodone1: bp %p iodone: error %d\n",
915 bp, bp->b_error);
916 }
917 #endif
918 disk_unbusy(&vnd->sc_dkdev, bp->b_bcount - bp->b_resid,
919 (bp->b_flags & B_READ));
920 vnd->sc_active--;
921 if (vnd->sc_active == 0) {
922 wakeup(&vnd->sc_tab);
923 }
924 splx(s);
925 obp->b_error = bp->b_error;
926 obp->b_resid = bp->b_resid;
927 buf_destroy(bp);
928 VND_PUTXFER(vnd, vnx);
929 biodone(obp);
930 }
931
932 /* ARGSUSED */
933 static int
934 vndread(dev_t dev, struct uio *uio, int flags)
935 {
936 int unit = vndunit(dev);
937 struct vnd_softc *sc;
938
939 #ifdef DEBUG
940 if (vnddebug & VDB_FOLLOW)
941 printf("vndread(0x%"PRIx64", %p)\n", dev, uio);
942 #endif
943
944 sc = device_lookup_private(&vnd_cd, unit);
945 if (sc == NULL)
946 return ENXIO;
947
948 if ((sc->sc_flags & VNF_INITED) == 0)
949 return ENXIO;
950
951 return physio(vndstrategy, NULL, dev, B_READ, minphys, uio);
952 }
953
954 /* ARGSUSED */
955 static int
956 vndwrite(dev_t dev, struct uio *uio, int flags)
957 {
958 int unit = vndunit(dev);
959 struct vnd_softc *sc;
960
961 #ifdef DEBUG
962 if (vnddebug & VDB_FOLLOW)
963 printf("vndwrite(0x%"PRIx64", %p)\n", dev, uio);
964 #endif
965
966 sc = device_lookup_private(&vnd_cd, unit);
967 if (sc == NULL)
968 return ENXIO;
969
970 if ((sc->sc_flags & VNF_INITED) == 0)
971 return ENXIO;
972
973 return physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio);
974 }
975
976 static int
977 vnd_cget(struct lwp *l, int unit, int *un, struct vattr *va)
978 {
979 int error;
980 struct vnd_softc *vnd;
981
982 if (*un == -1)
983 *un = unit;
984 if (*un < 0)
985 return EINVAL;
986
987 vnd = device_lookup_private(&vnd_cd, *un);
988 if (vnd == NULL)
989 return -1;
990
991 if ((vnd->sc_flags & VNF_INITED) == 0)
992 return -1;
993
994 vn_lock(vnd->sc_vp, LK_SHARED | LK_RETRY);
995 error = VOP_GETATTR(vnd->sc_vp, va, l->l_cred);
996 VOP_UNLOCK(vnd->sc_vp);
997 return error;
998 }
999
1000 static int
1001 vnddoclear(struct vnd_softc *vnd, int pmask, int minor, bool force)
1002 {
1003 int error;
1004
1005 if ((error = vndlock(vnd)) != 0)
1006 return error;
1007
1008 /*
1009 * Don't unconfigure if any other partitions are open
1010 * or if both the character and block flavors of this
1011 * partition are open.
1012 */
1013 if (DK_BUSY(vnd, pmask) && !force) {
1014 vndunlock(vnd);
1015 return EBUSY;
1016 }
1017
1018 /* Delete all of our wedges */
1019 dkwedge_delall(&vnd->sc_dkdev);
1020
1021 /*
1022 * XXX vndclear() might call vndclose() implicitly;
1023 * release lock to avoid recursion
1024 *
1025 * Set VNF_CLEARING to prevent vndopen() from
1026 * sneaking in after we vndunlock().
1027 */
1028 vnd->sc_flags |= VNF_CLEARING;
1029 vndunlock(vnd);
1030 vndclear(vnd, minor);
1031 #ifdef DEBUG
1032 if (vnddebug & VDB_INIT)
1033 printf("vndioctl: CLRed\n");
1034 #endif
1035
1036 /* Destroy the xfer and buffer pools. */
1037 pool_destroy(&vnd->sc_vxpool);
1038
1039 /* Detach the disk. */
1040 disk_detach(&vnd->sc_dkdev);
1041
1042 return 0;
1043 }
1044
1045 /* ARGSUSED */
1046 static int
1047 vndioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1048 {
1049 bool force;
1050 int unit = vndunit(dev);
1051 struct vnd_softc *vnd;
1052 struct vnd_ioctl *vio;
1053 struct vattr vattr;
1054 struct pathbuf *pb;
1055 struct nameidata nd;
1056 int error, part, pmask;
1057 uint64_t geomsize;
1058 int fflags;
1059 #ifdef __HAVE_OLD_DISKLABEL
1060 struct disklabel newlabel;
1061 #endif
1062
1063 #ifdef DEBUG
1064 if (vnddebug & VDB_FOLLOW)
1065 printf("vndioctl(0x%"PRIx64", 0x%lx, %p, 0x%x, %p): unit %d\n",
1066 dev, cmd, data, flag, l->l_proc, unit);
1067 #endif
1068 vnd = device_lookup_private(&vnd_cd, unit);
1069 if (vnd == NULL &&
1070 #ifdef COMPAT_30
1071 cmd != VNDIOCGET30 &&
1072 #endif
1073 #ifdef COMPAT_50
1074 cmd != VNDIOCGET50 &&
1075 #endif
1076 cmd != VNDIOCGET)
1077 return ENXIO;
1078 vio = (struct vnd_ioctl *)data;
1079
1080 /* Must be open for writes for these commands... */
1081 switch (cmd) {
1082 case VNDIOCSET:
1083 case VNDIOCCLR:
1084 #ifdef COMPAT_50
1085 case VNDIOCSET50:
1086 case VNDIOCCLR50:
1087 #endif
1088 case DIOCSDINFO:
1089 case DIOCWDINFO:
1090 #ifdef __HAVE_OLD_DISKLABEL
1091 case ODIOCSDINFO:
1092 case ODIOCWDINFO:
1093 #endif
1094 case DIOCKLABEL:
1095 case DIOCWLABEL:
1096 if ((flag & FWRITE) == 0)
1097 return EBADF;
1098 }
1099
1100 /* Must be initialized for these... */
1101 switch (cmd) {
1102 case VNDIOCCLR:
1103 #ifdef VNDIOCCLR50
1104 case VNDIOCCLR50:
1105 #endif
1106 case DIOCGDINFO:
1107 case DIOCSDINFO:
1108 case DIOCWDINFO:
1109 case DIOCGPART:
1110 case DIOCKLABEL:
1111 case DIOCWLABEL:
1112 case DIOCGDEFLABEL:
1113 case DIOCCACHESYNC:
1114 #ifdef __HAVE_OLD_DISKLABEL
1115 case ODIOCGDINFO:
1116 case ODIOCSDINFO:
1117 case ODIOCWDINFO:
1118 case ODIOCGDEFLABEL:
1119 #endif
1120 if ((vnd->sc_flags & VNF_INITED) == 0)
1121 return ENXIO;
1122 }
1123
1124 error = disk_ioctl(&vnd->sc_dkdev, dev, cmd, data, flag, l);
1125 if (error != EPASSTHROUGH)
1126 return error;
1127
1128
1129 switch (cmd) {
1130 #ifdef VNDIOCSET50
1131 case VNDIOCSET50:
1132 #endif
1133 case VNDIOCSET:
1134 if (vnd->sc_flags & VNF_INITED)
1135 return EBUSY;
1136
1137 if ((error = vndlock(vnd)) != 0)
1138 return error;
1139
1140 fflags = FREAD;
1141 if ((vio->vnd_flags & VNDIOF_READONLY) == 0)
1142 fflags |= FWRITE;
1143 error = pathbuf_copyin(vio->vnd_file, &pb);
1144 if (error) {
1145 goto unlock_and_exit;
1146 }
1147 NDINIT(&nd, LOOKUP, FOLLOW, pb);
1148 if ((error = vn_open(&nd, fflags, 0)) != 0) {
1149 pathbuf_destroy(pb);
1150 goto unlock_and_exit;
1151 }
1152 KASSERT(l);
1153 error = VOP_GETATTR(nd.ni_vp, &vattr, l->l_cred);
1154 if (!error && nd.ni_vp->v_type != VREG)
1155 error = EOPNOTSUPP;
1156 if (!error && vattr.va_bytes < vattr.va_size)
1157 /* File is definitely sparse, use vn_rdwr() */
1158 vnd->sc_flags |= VNF_USE_VN_RDWR;
1159 if (error) {
1160 VOP_UNLOCK(nd.ni_vp);
1161 goto close_and_exit;
1162 }
1163
1164 /* If using a compressed file, initialize its info */
1165 /* (or abort with an error if kernel has no compression) */
1166 if (vio->vnd_flags & VNF_COMP) {
1167 #ifdef VND_COMPRESSION
1168 struct vnd_comp_header *ch;
1169 int i;
1170 u_int32_t comp_size;
1171 u_int32_t comp_maxsize;
1172
1173 /* allocate space for compresed file header */
1174 ch = malloc(sizeof(struct vnd_comp_header),
1175 M_TEMP, M_WAITOK);
1176
1177 /* read compressed file header */
1178 error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ch,
1179 sizeof(struct vnd_comp_header), 0, UIO_SYSSPACE,
1180 IO_UNIT|IO_NODELOCKED, l->l_cred, NULL, NULL);
1181 if (error) {
1182 free(ch, M_TEMP);
1183 VOP_UNLOCK(nd.ni_vp);
1184 goto close_and_exit;
1185 }
1186
1187 /* save some header info */
1188 vnd->sc_comp_blksz = ntohl(ch->block_size);
1189 /* note last offset is the file byte size */
1190 vnd->sc_comp_numoffs = ntohl(ch->num_blocks)+1;
1191 free(ch, M_TEMP);
1192 if (vnd->sc_comp_blksz == 0 ||
1193 vnd->sc_comp_blksz % DEV_BSIZE !=0) {
1194 VOP_UNLOCK(nd.ni_vp);
1195 error = EINVAL;
1196 goto close_and_exit;
1197 }
1198 if (sizeof(struct vnd_comp_header) +
1199 sizeof(u_int64_t) * vnd->sc_comp_numoffs >
1200 vattr.va_size) {
1201 VOP_UNLOCK(nd.ni_vp);
1202 error = EINVAL;
1203 goto close_and_exit;
1204 }
1205
1206 /* set decompressed file size */
1207 vattr.va_size =
1208 ((u_quad_t)vnd->sc_comp_numoffs - 1) *
1209 (u_quad_t)vnd->sc_comp_blksz;
1210
1211 /* allocate space for all the compressed offsets */
1212 vnd->sc_comp_offsets =
1213 malloc(sizeof(u_int64_t) * vnd->sc_comp_numoffs,
1214 M_DEVBUF, M_WAITOK);
1215
1216 /* read in the offsets */
1217 error = vn_rdwr(UIO_READ, nd.ni_vp,
1218 (void *)vnd->sc_comp_offsets,
1219 sizeof(u_int64_t) * vnd->sc_comp_numoffs,
1220 sizeof(struct vnd_comp_header), UIO_SYSSPACE,
1221 IO_UNIT|IO_NODELOCKED, l->l_cred, NULL, NULL);
1222 if (error) {
1223 VOP_UNLOCK(nd.ni_vp);
1224 goto close_and_exit;
1225 }
1226 /*
1227 * find largest block size (used for allocation limit).
1228 * Also convert offset to native byte order.
1229 */
1230 comp_maxsize = 0;
1231 for (i = 0; i < vnd->sc_comp_numoffs - 1; i++) {
1232 vnd->sc_comp_offsets[i] =
1233 be64toh(vnd->sc_comp_offsets[i]);
1234 comp_size = be64toh(vnd->sc_comp_offsets[i + 1])
1235 - vnd->sc_comp_offsets[i];
1236 if (comp_size > comp_maxsize)
1237 comp_maxsize = comp_size;
1238 }
1239 vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1] =
1240 be64toh(vnd->sc_comp_offsets[vnd->sc_comp_numoffs - 1]);
1241
1242 /* create compressed data buffer */
1243 vnd->sc_comp_buff = malloc(comp_maxsize,
1244 M_DEVBUF, M_WAITOK);
1245
1246 /* create decompressed buffer */
1247 vnd->sc_comp_decombuf = malloc(vnd->sc_comp_blksz,
1248 M_DEVBUF, M_WAITOK);
1249 vnd->sc_comp_buffblk = -1;
1250
1251 /* Initialize decompress stream */
1252 memset(&vnd->sc_comp_stream, 0, sizeof(z_stream));
1253 vnd->sc_comp_stream.zalloc = vnd_alloc;
1254 vnd->sc_comp_stream.zfree = vnd_free;
1255 error = inflateInit2(&vnd->sc_comp_stream, MAX_WBITS);
1256 if (error) {
1257 if (vnd->sc_comp_stream.msg)
1258 printf("vnd%d: compressed file, %s\n",
1259 unit, vnd->sc_comp_stream.msg);
1260 VOP_UNLOCK(nd.ni_vp);
1261 error = EINVAL;
1262 goto close_and_exit;
1263 }
1264
1265 vnd->sc_flags |= VNF_COMP | VNF_READONLY;
1266 #else /* !VND_COMPRESSION */
1267 VOP_UNLOCK(nd.ni_vp);
1268 error = EOPNOTSUPP;
1269 goto close_and_exit;
1270 #endif /* VND_COMPRESSION */
1271 }
1272
1273 VOP_UNLOCK(nd.ni_vp);
1274 vnd->sc_vp = nd.ni_vp;
1275 vnd->sc_size = btodb(vattr.va_size); /* note truncation */
1276
1277 /*
1278 * Use pseudo-geometry specified. If none was provided,
1279 * use "standard" Adaptec fictitious geometry.
1280 */
1281 if (vio->vnd_flags & VNDIOF_HASGEOM) {
1282
1283 memcpy(&vnd->sc_geom, &vio->vnd_geom,
1284 sizeof(vio->vnd_geom));
1285
1286 /*
1287 * Sanity-check the sector size.
1288 * XXX Don't allow secsize < DEV_BSIZE. Should
1289 * XXX we?
1290 */
1291 if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
1292 (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0 ||
1293 vnd->sc_geom.vng_ncylinders == 0 ||
1294 (vnd->sc_geom.vng_ntracks *
1295 vnd->sc_geom.vng_nsectors) == 0) {
1296 error = EINVAL;
1297 goto close_and_exit;
1298 }
1299
1300 /*
1301 * Compute the size (in DEV_BSIZE blocks) specified
1302 * by the geometry.
1303 */
1304 geomsize = (int64_t)vnd->sc_geom.vng_nsectors *
1305 vnd->sc_geom.vng_ntracks *
1306 vnd->sc_geom.vng_ncylinders *
1307 (vnd->sc_geom.vng_secsize / DEV_BSIZE);
1308
1309 /*
1310 * Sanity-check the size against the specified
1311 * geometry.
1312 */
1313 if (vnd->sc_size < geomsize) {
1314 error = EINVAL;
1315 goto close_and_exit;
1316 }
1317 } else if (vnd->sc_size >= (32 * 64)) {
1318 /*
1319 * Size must be at least 2048 DEV_BSIZE blocks
1320 * (1M) in order to use this geometry.
1321 */
1322 vnd->sc_geom.vng_secsize = DEV_BSIZE;
1323 vnd->sc_geom.vng_nsectors = 32;
1324 vnd->sc_geom.vng_ntracks = 64;
1325 vnd->sc_geom.vng_ncylinders = vnd->sc_size / (64 * 32);
1326 } else {
1327 vnd->sc_geom.vng_secsize = DEV_BSIZE;
1328 vnd->sc_geom.vng_nsectors = 1;
1329 vnd->sc_geom.vng_ntracks = 1;
1330 vnd->sc_geom.vng_ncylinders = vnd->sc_size;
1331 }
1332
1333 vnd_set_geometry(vnd);
1334
1335 if (vio->vnd_flags & VNDIOF_READONLY) {
1336 vnd->sc_flags |= VNF_READONLY;
1337 }
1338
1339 if ((error = vndsetcred(vnd, l->l_cred)) != 0)
1340 goto close_and_exit;
1341
1342 vndthrottle(vnd, vnd->sc_vp);
1343 vio->vnd_osize = dbtob(vnd->sc_size);
1344 #ifdef VNDIOCSET50
1345 if (cmd != VNDIOCSET50)
1346 #endif
1347 vio->vnd_size = dbtob(vnd->sc_size);
1348 vnd->sc_flags |= VNF_INITED;
1349
1350 /* create the kernel thread, wait for it to be up */
1351 error = kthread_create(PRI_NONE, 0, NULL, vndthread, vnd,
1352 &vnd->sc_kthread, "%s", device_xname(vnd->sc_dev));
1353 if (error)
1354 goto close_and_exit;
1355 while ((vnd->sc_flags & VNF_KTHREAD) == 0) {
1356 tsleep(&vnd->sc_kthread, PRIBIO, "vndthr", 0);
1357 }
1358 #ifdef DEBUG
1359 if (vnddebug & VDB_INIT)
1360 printf("vndioctl: SET vp %p size 0x%lx %d/%d/%d/%d\n",
1361 vnd->sc_vp, (unsigned long) vnd->sc_size,
1362 vnd->sc_geom.vng_secsize,
1363 vnd->sc_geom.vng_nsectors,
1364 vnd->sc_geom.vng_ntracks,
1365 vnd->sc_geom.vng_ncylinders);
1366 #endif
1367
1368 /* Attach the disk. */
1369 disk_attach(&vnd->sc_dkdev);
1370
1371 /* Initialize the xfer and buffer pools. */
1372 pool_init(&vnd->sc_vxpool, sizeof(struct vndxfer), 0,
1373 0, 0, "vndxpl", NULL, IPL_BIO);
1374
1375 vndunlock(vnd);
1376
1377 pathbuf_destroy(pb);
1378
1379 /* Discover wedges on this disk */
1380 dkwedge_discover(&vnd->sc_dkdev);
1381
1382 break;
1383
1384 close_and_exit:
1385 (void) vn_close(nd.ni_vp, fflags, l->l_cred);
1386 pathbuf_destroy(pb);
1387 unlock_and_exit:
1388 #ifdef VND_COMPRESSION
1389 /* free any allocated memory (for compressed file) */
1390 if (vnd->sc_comp_offsets) {
1391 free(vnd->sc_comp_offsets, M_DEVBUF);
1392 vnd->sc_comp_offsets = NULL;
1393 }
1394 if (vnd->sc_comp_buff) {
1395 free(vnd->sc_comp_buff, M_DEVBUF);
1396 vnd->sc_comp_buff = NULL;
1397 }
1398 if (vnd->sc_comp_decombuf) {
1399 free(vnd->sc_comp_decombuf, M_DEVBUF);
1400 vnd->sc_comp_decombuf = NULL;
1401 }
1402 #endif /* VND_COMPRESSION */
1403 vndunlock(vnd);
1404 return error;
1405
1406 #ifdef VNDIOCCLR50
1407 case VNDIOCCLR50:
1408 #endif
1409 case VNDIOCCLR:
1410 part = DISKPART(dev);
1411 pmask = (1 << part);
1412 force = (vio->vnd_flags & VNDIOF_FORCE) != 0;
1413
1414 if ((error = vnddoclear(vnd, pmask, minor(dev), force)) != 0)
1415 return error;
1416
1417 break;
1418
1419 #ifdef COMPAT_30
1420 case VNDIOCGET30: {
1421 struct vnd_user30 *vnu;
1422 struct vattr va;
1423 vnu = (struct vnd_user30 *)data;
1424 KASSERT(l);
1425 switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
1426 case 0:
1427 vnu->vnu_dev = va.va_fsid;
1428 vnu->vnu_ino = va.va_fileid;
1429 break;
1430 case -1:
1431 /* unused is not an error */
1432 vnu->vnu_dev = 0;
1433 vnu->vnu_ino = 0;
1434 break;
1435 default:
1436 return error;
1437 }
1438 break;
1439 }
1440 #endif
1441
1442 #ifdef COMPAT_50
1443 case VNDIOCGET50: {
1444 struct vnd_user50 *vnu;
1445 struct vattr va;
1446 vnu = (struct vnd_user50 *)data;
1447 KASSERT(l);
1448 switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
1449 case 0:
1450 vnu->vnu_dev = va.va_fsid;
1451 vnu->vnu_ino = va.va_fileid;
1452 break;
1453 case -1:
1454 /* unused is not an error */
1455 vnu->vnu_dev = 0;
1456 vnu->vnu_ino = 0;
1457 break;
1458 default:
1459 return error;
1460 }
1461 break;
1462 }
1463 #endif
1464
1465 case VNDIOCGET: {
1466 struct vnd_user *vnu;
1467 struct vattr va;
1468 vnu = (struct vnd_user *)data;
1469 KASSERT(l);
1470 switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
1471 case 0:
1472 vnu->vnu_dev = va.va_fsid;
1473 vnu->vnu_ino = va.va_fileid;
1474 break;
1475 case -1:
1476 /* unused is not an error */
1477 vnu->vnu_dev = 0;
1478 vnu->vnu_ino = 0;
1479 break;
1480 default:
1481 return error;
1482 }
1483 break;
1484 }
1485
1486 case DIOCWDINFO:
1487 case DIOCSDINFO:
1488 #ifdef __HAVE_OLD_DISKLABEL
1489 case ODIOCWDINFO:
1490 case ODIOCSDINFO:
1491 #endif
1492 {
1493 struct disklabel *lp;
1494
1495 if ((error = vndlock(vnd)) != 0)
1496 return error;
1497
1498 vnd->sc_flags |= VNF_LABELLING;
1499
1500 #ifdef __HAVE_OLD_DISKLABEL
1501 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1502 memset(&newlabel, 0, sizeof newlabel);
1503 memcpy(&newlabel, data, sizeof (struct olddisklabel));
1504 lp = &newlabel;
1505 } else
1506 #endif
1507 lp = (struct disklabel *)data;
1508
1509 error = setdisklabel(vnd->sc_dkdev.dk_label,
1510 lp, 0, vnd->sc_dkdev.dk_cpulabel);
1511 if (error == 0) {
1512 if (cmd == DIOCWDINFO
1513 #ifdef __HAVE_OLD_DISKLABEL
1514 || cmd == ODIOCWDINFO
1515 #endif
1516 )
1517 error = writedisklabel(VNDLABELDEV(dev),
1518 vndstrategy, vnd->sc_dkdev.dk_label,
1519 vnd->sc_dkdev.dk_cpulabel);
1520 }
1521
1522 vnd->sc_flags &= ~VNF_LABELLING;
1523
1524 vndunlock(vnd);
1525
1526 if (error)
1527 return error;
1528 break;
1529 }
1530
1531 case DIOCKLABEL:
1532 if (*(int *)data != 0)
1533 vnd->sc_flags |= VNF_KLABEL;
1534 else
1535 vnd->sc_flags &= ~VNF_KLABEL;
1536 break;
1537
1538 case DIOCWLABEL:
1539 if (*(int *)data != 0)
1540 vnd->sc_flags |= VNF_WLABEL;
1541 else
1542 vnd->sc_flags &= ~VNF_WLABEL;
1543 break;
1544
1545 case DIOCGDEFLABEL:
1546 vndgetdefaultlabel(vnd, (struct disklabel *)data);
1547 break;
1548
1549 #ifdef __HAVE_OLD_DISKLABEL
1550 case ODIOCGDEFLABEL:
1551 vndgetdefaultlabel(vnd, &newlabel);
1552 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1553 return ENOTTY;
1554 memcpy(data, &newlabel, sizeof (struct olddisklabel));
1555 break;
1556 #endif
1557
1558 case DIOCCACHESYNC:
1559 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1560 error = VOP_FSYNC(vnd->sc_vp, vnd->sc_cred,
1561 FSYNC_WAIT | FSYNC_DATAONLY | FSYNC_CACHE, 0, 0);
1562 VOP_UNLOCK(vnd->sc_vp);
1563 return error;
1564
1565 default:
1566 return ENOTTY;
1567 }
1568
1569 return 0;
1570 }
1571
1572 /*
1573 * Duplicate the current processes' credentials. Since we are called only
1574 * as the result of a SET ioctl and only root can do that, any future access
1575 * to this "disk" is essentially as root. Note that credentials may change
1576 * if some other uid can write directly to the mapped file (NFS).
1577 */
1578 static int
1579 vndsetcred(struct vnd_softc *vnd, kauth_cred_t cred)
1580 {
1581 struct uio auio;
1582 struct iovec aiov;
1583 char *tmpbuf;
1584 int error;
1585
1586 vnd->sc_cred = kauth_cred_dup(cred);
1587 tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1588
1589 /* XXX: Horrible kludge to establish credentials for NFS */
1590 aiov.iov_base = tmpbuf;
1591 aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
1592 auio.uio_iov = &aiov;
1593 auio.uio_iovcnt = 1;
1594 auio.uio_offset = 0;
1595 auio.uio_rw = UIO_READ;
1596 auio.uio_resid = aiov.iov_len;
1597 UIO_SETUP_SYSSPACE(&auio);
1598 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1599 error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
1600 if (error == 0) {
1601 /*
1602 * Because vnd does all IO directly through the vnode
1603 * we need to flush (at least) the buffer from the above
1604 * VOP_READ from the buffer cache to prevent cache
1605 * incoherencies. Also, be careful to write dirty
1606 * buffers back to stable storage.
1607 */
1608 error = vinvalbuf(vnd->sc_vp, V_SAVE, vnd->sc_cred,
1609 curlwp, 0, 0);
1610 }
1611 VOP_UNLOCK(vnd->sc_vp);
1612
1613 free(tmpbuf, M_TEMP);
1614 return error;
1615 }
1616
1617 /*
1618 * Set maxactive based on FS type
1619 */
1620 static void
1621 vndthrottle(struct vnd_softc *vnd, struct vnode *vp)
1622 {
1623
1624 if (vp->v_tag == VT_NFS)
1625 vnd->sc_maxactive = 2;
1626 else
1627 vnd->sc_maxactive = 8;
1628
1629 if (vnd->sc_maxactive < 1)
1630 vnd->sc_maxactive = 1;
1631 }
1632
1633 #if 0
1634 static void
1635 vndshutdown(void)
1636 {
1637 struct vnd_softc *vnd;
1638
1639 for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
1640 if (vnd->sc_flags & VNF_INITED)
1641 vndclear(vnd);
1642 }
1643 #endif
1644
1645 static void
1646 vndclear(struct vnd_softc *vnd, int myminor)
1647 {
1648 struct vnode *vp = vnd->sc_vp;
1649 int fflags = FREAD;
1650 int bmaj, cmaj, i, mn;
1651 int s;
1652
1653 #ifdef DEBUG
1654 if (vnddebug & VDB_FOLLOW)
1655 printf("vndclear(%p): vp %p\n", vnd, vp);
1656 #endif
1657 /* locate the major number */
1658 bmaj = bdevsw_lookup_major(&vnd_bdevsw);
1659 cmaj = cdevsw_lookup_major(&vnd_cdevsw);
1660
1661 /* Nuke the vnodes for any open instances */
1662 for (i = 0; i < MAXPARTITIONS; i++) {
1663 mn = DISKMINOR(device_unit(vnd->sc_dev), i);
1664 vdevgone(bmaj, mn, mn, VBLK);
1665 if (mn != myminor) /* XXX avoid to kill own vnode */
1666 vdevgone(cmaj, mn, mn, VCHR);
1667 }
1668
1669 if ((vnd->sc_flags & VNF_READONLY) == 0)
1670 fflags |= FWRITE;
1671
1672 s = splbio();
1673 bufq_drain(vnd->sc_tab);
1674 splx(s);
1675
1676 vnd->sc_flags |= VNF_VUNCONF;
1677 wakeup(&vnd->sc_tab);
1678 while (vnd->sc_flags & VNF_KTHREAD)
1679 tsleep(&vnd->sc_kthread, PRIBIO, "vnthr", 0);
1680
1681 #ifdef VND_COMPRESSION
1682 /* free the compressed file buffers */
1683 if (vnd->sc_flags & VNF_COMP) {
1684 if (vnd->sc_comp_offsets) {
1685 free(vnd->sc_comp_offsets, M_DEVBUF);
1686 vnd->sc_comp_offsets = NULL;
1687 }
1688 if (vnd->sc_comp_buff) {
1689 free(vnd->sc_comp_buff, M_DEVBUF);
1690 vnd->sc_comp_buff = NULL;
1691 }
1692 if (vnd->sc_comp_decombuf) {
1693 free(vnd->sc_comp_decombuf, M_DEVBUF);
1694 vnd->sc_comp_decombuf = NULL;
1695 }
1696 }
1697 #endif /* VND_COMPRESSION */
1698 vnd->sc_flags &=
1699 ~(VNF_INITED | VNF_READONLY | VNF_VLABEL
1700 | VNF_VUNCONF | VNF_COMP | VNF_CLEARING);
1701 if (vp == NULL)
1702 panic("vndclear: null vp");
1703 (void) vn_close(vp, fflags, vnd->sc_cred);
1704 kauth_cred_free(vnd->sc_cred);
1705 vnd->sc_vp = NULL;
1706 vnd->sc_cred = NULL;
1707 vnd->sc_size = 0;
1708 }
1709
1710 static int
1711 vndsize(dev_t dev)
1712 {
1713 struct vnd_softc *sc;
1714 struct disklabel *lp;
1715 int part, unit, omask;
1716 int size;
1717
1718 unit = vndunit(dev);
1719 sc = device_lookup_private(&vnd_cd, unit);
1720 if (sc == NULL)
1721 return -1;
1722
1723 if ((sc->sc_flags & VNF_INITED) == 0)
1724 return -1;
1725
1726 part = DISKPART(dev);
1727 omask = sc->sc_dkdev.dk_openmask & (1 << part);
1728 lp = sc->sc_dkdev.dk_label;
1729
1730 if (omask == 0 && vndopen(dev, 0, S_IFBLK, curlwp)) /* XXX */
1731 return -1;
1732
1733 if (lp->d_partitions[part].p_fstype != FS_SWAP)
1734 size = -1;
1735 else
1736 size = lp->d_partitions[part].p_size *
1737 (lp->d_secsize / DEV_BSIZE);
1738
1739 if (omask == 0 && vndclose(dev, 0, S_IFBLK, curlwp)) /* XXX */
1740 return -1;
1741
1742 return size;
1743 }
1744
1745 static int
1746 vnddump(dev_t dev, daddr_t blkno, void *va,
1747 size_t size)
1748 {
1749
1750 /* Not implemented. */
1751 return ENXIO;
1752 }
1753
1754 static void
1755 vndgetdefaultlabel(struct vnd_softc *sc, struct disklabel *lp)
1756 {
1757 struct vndgeom *vng = &sc->sc_geom;
1758 struct partition *pp;
1759 unsigned spb;
1760
1761 memset(lp, 0, sizeof(*lp));
1762
1763 spb = vng->vng_secsize / DEV_BSIZE;
1764 if (sc->sc_size / spb > UINT32_MAX)
1765 lp->d_secperunit = UINT32_MAX;
1766 else
1767 lp->d_secperunit = sc->sc_size / spb;
1768 lp->d_secsize = vng->vng_secsize;
1769 lp->d_nsectors = vng->vng_nsectors;
1770 lp->d_ntracks = vng->vng_ntracks;
1771 lp->d_ncylinders = vng->vng_ncylinders;
1772 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1773
1774 strncpy(lp->d_typename, "vnd", sizeof(lp->d_typename));
1775 lp->d_type = DKTYPE_VND;
1776 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1777 lp->d_rpm = 3600;
1778 lp->d_interleave = 1;
1779 lp->d_flags = 0;
1780
1781 pp = &lp->d_partitions[RAW_PART];
1782 pp->p_offset = 0;
1783 pp->p_size = lp->d_secperunit;
1784 pp->p_fstype = FS_UNUSED;
1785 lp->d_npartitions = RAW_PART + 1;
1786
1787 lp->d_magic = DISKMAGIC;
1788 lp->d_magic2 = DISKMAGIC;
1789 lp->d_checksum = dkcksum(lp);
1790 }
1791
1792 /*
1793 * Read the disklabel from a vnd. If one is not present, create a fake one.
1794 */
1795 static void
1796 vndgetdisklabel(dev_t dev, struct vnd_softc *sc)
1797 {
1798 const char *errstring;
1799 struct disklabel *lp = sc->sc_dkdev.dk_label;
1800 struct cpu_disklabel *clp = sc->sc_dkdev.dk_cpulabel;
1801 int i;
1802
1803 memset(clp, 0, sizeof(*clp));
1804
1805 vndgetdefaultlabel(sc, lp);
1806
1807 /*
1808 * Call the generic disklabel extraction routine.
1809 */
1810 errstring = readdisklabel(VNDLABELDEV(dev), vndstrategy, lp, clp);
1811 if (errstring) {
1812 /*
1813 * Lack of disklabel is common, but we print the warning
1814 * anyway, since it might contain other useful information.
1815 */
1816 aprint_normal_dev(sc->sc_dev, "%s\n", errstring);
1817
1818 /*
1819 * For historical reasons, if there's no disklabel
1820 * present, all partitions must be FS_BSDFFS and
1821 * occupy the entire disk.
1822 */
1823 for (i = 0; i < MAXPARTITIONS; i++) {
1824 /*
1825 * Don't wipe out port specific hack (such as
1826 * dos partition hack of i386 port).
1827 */
1828 if (lp->d_partitions[i].p_size != 0)
1829 continue;
1830
1831 lp->d_partitions[i].p_size = lp->d_secperunit;
1832 lp->d_partitions[i].p_offset = 0;
1833 lp->d_partitions[i].p_fstype = FS_BSDFFS;
1834 }
1835
1836 strncpy(lp->d_packname, "default label",
1837 sizeof(lp->d_packname));
1838
1839 lp->d_npartitions = MAXPARTITIONS;
1840 lp->d_checksum = dkcksum(lp);
1841 }
1842 }
1843
1844 /*
1845 * Wait interruptibly for an exclusive lock.
1846 *
1847 * XXX
1848 * Several drivers do this; it should be abstracted and made MP-safe.
1849 */
1850 static int
1851 vndlock(struct vnd_softc *sc)
1852 {
1853 int error;
1854
1855 while ((sc->sc_flags & VNF_LOCKED) != 0) {
1856 sc->sc_flags |= VNF_WANTED;
1857 if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
1858 return error;
1859 }
1860 sc->sc_flags |= VNF_LOCKED;
1861 return 0;
1862 }
1863
1864 /*
1865 * Unlock and wake up any waiters.
1866 */
1867 static void
1868 vndunlock(struct vnd_softc *sc)
1869 {
1870
1871 sc->sc_flags &= ~VNF_LOCKED;
1872 if ((sc->sc_flags & VNF_WANTED) != 0) {
1873 sc->sc_flags &= ~VNF_WANTED;
1874 wakeup(sc);
1875 }
1876 }
1877
1878 #ifdef VND_COMPRESSION
1879 /* compressed file read */
1880 static void
1881 compstrategy(struct buf *bp, off_t bn)
1882 {
1883 int error;
1884 int unit = vndunit(bp->b_dev);
1885 struct vnd_softc *vnd =
1886 device_lookup_private(&vnd_cd, unit);
1887 u_int32_t comp_block;
1888 struct uio auio;
1889 char *addr;
1890 int s;
1891
1892 /* set up constants for data move */
1893 auio.uio_rw = UIO_READ;
1894 UIO_SETUP_SYSSPACE(&auio);
1895
1896 /* read, and transfer the data */
1897 addr = bp->b_data;
1898 bp->b_resid = bp->b_bcount;
1899 s = splbio();
1900 while (bp->b_resid > 0) {
1901 unsigned length;
1902 size_t length_in_buffer;
1903 u_int32_t offset_in_buffer;
1904 struct iovec aiov;
1905
1906 /* calculate the compressed block number */
1907 comp_block = bn / (off_t)vnd->sc_comp_blksz;
1908
1909 /* check for good block number */
1910 if (comp_block >= vnd->sc_comp_numoffs) {
1911 bp->b_error = EINVAL;
1912 splx(s);
1913 return;
1914 }
1915
1916 /* read in the compressed block, if not in buffer */
1917 if (comp_block != vnd->sc_comp_buffblk) {
1918 length = vnd->sc_comp_offsets[comp_block + 1] -
1919 vnd->sc_comp_offsets[comp_block];
1920 vn_lock(vnd->sc_vp, LK_EXCLUSIVE | LK_RETRY);
1921 error = vn_rdwr(UIO_READ, vnd->sc_vp, vnd->sc_comp_buff,
1922 length, vnd->sc_comp_offsets[comp_block],
1923 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, vnd->sc_cred,
1924 NULL, NULL);
1925 if (error) {
1926 bp->b_error = error;
1927 VOP_UNLOCK(vnd->sc_vp);
1928 splx(s);
1929 return;
1930 }
1931 /* uncompress the buffer */
1932 vnd->sc_comp_stream.next_in = vnd->sc_comp_buff;
1933 vnd->sc_comp_stream.avail_in = length;
1934 vnd->sc_comp_stream.next_out = vnd->sc_comp_decombuf;
1935 vnd->sc_comp_stream.avail_out = vnd->sc_comp_blksz;
1936 inflateReset(&vnd->sc_comp_stream);
1937 error = inflate(&vnd->sc_comp_stream, Z_FINISH);
1938 if (error != Z_STREAM_END) {
1939 if (vnd->sc_comp_stream.msg)
1940 aprint_normal_dev(vnd->sc_dev,
1941 "compressed file, %s\n",
1942 vnd->sc_comp_stream.msg);
1943 bp->b_error = EBADMSG;
1944 VOP_UNLOCK(vnd->sc_vp);
1945 splx(s);
1946 return;
1947 }
1948 vnd->sc_comp_buffblk = comp_block;
1949 VOP_UNLOCK(vnd->sc_vp);
1950 }
1951
1952 /* transfer the usable uncompressed data */
1953 offset_in_buffer = bn % (off_t)vnd->sc_comp_blksz;
1954 length_in_buffer = vnd->sc_comp_blksz - offset_in_buffer;
1955 if (length_in_buffer > bp->b_resid)
1956 length_in_buffer = bp->b_resid;
1957 auio.uio_iov = &aiov;
1958 auio.uio_iovcnt = 1;
1959 aiov.iov_base = addr;
1960 aiov.iov_len = length_in_buffer;
1961 auio.uio_resid = aiov.iov_len;
1962 auio.uio_offset = 0;
1963 error = uiomove(vnd->sc_comp_decombuf + offset_in_buffer,
1964 length_in_buffer, &auio);
1965 if (error) {
1966 bp->b_error = error;
1967 splx(s);
1968 return;
1969 }
1970
1971 bn += length_in_buffer;
1972 addr += length_in_buffer;
1973 bp->b_resid -= length_in_buffer;
1974 }
1975 splx(s);
1976 }
1977
1978 /* compression memory allocation routines */
1979 static void *
1980 vnd_alloc(void *aux, u_int items, u_int siz)
1981 {
1982 return malloc(items * siz, M_TEMP, M_NOWAIT);
1983 }
1984
1985 static void
1986 vnd_free(void *aux, void *ptr)
1987 {
1988 free(ptr, M_TEMP);
1989 }
1990 #endif /* VND_COMPRESSION */
1991
1992 static void
1993 vnd_set_geometry(struct vnd_softc *vnd)
1994 {
1995 struct disk_geom *dg = &vnd->sc_dkdev.dk_geom;
1996
1997 memset(dg, 0, sizeof(*dg));
1998
1999 dg->dg_secperunit = (int64_t)vnd->sc_geom.vng_nsectors *
2000 vnd->sc_geom.vng_ntracks * vnd->sc_geom.vng_ncylinders;
2001 dg->dg_secsize = vnd->sc_geom.vng_secsize;
2002 dg->dg_nsectors = vnd->sc_geom.vng_nsectors;
2003 dg->dg_ntracks = vnd->sc_geom.vng_ntracks;
2004 dg->dg_ncylinders = vnd->sc_geom.vng_ncylinders;
2005
2006 #ifdef DEBUG
2007 if (vnddebug & VDB_LABEL) {
2008 printf("dg->dg_secperunit: %" PRId64 "\n", dg->dg_secperunit);
2009 printf("dg->dg_ncylinders: %u\n", dg->dg_ncylinders);
2010 }
2011 #endif
2012 disk_set_info(vnd->sc_dev, &vnd->sc_dkdev, NULL);
2013 }
2014
2015 #ifdef _MODULE
2016
2017 #include <sys/module.h>
2018
2019 #ifdef VND_COMPRESSION
2020 #define VND_DEPENDS "zlib"
2021 #else
2022 #define VND_DEPENDS NULL
2023 #endif
2024
2025 MODULE(MODULE_CLASS_DRIVER, vnd, VND_DEPENDS);
2026 CFDRIVER_DECL(vnd, DV_DISK, NULL);
2027
2028 static int
2029 vnd_modcmd(modcmd_t cmd, void *arg)
2030 {
2031 int bmajor = -1, cmajor = -1, error = 0;
2032
2033 switch (cmd) {
2034 case MODULE_CMD_INIT:
2035 error = config_cfdriver_attach(&vnd_cd);
2036 if (error)
2037 break;
2038
2039 error = config_cfattach_attach(vnd_cd.cd_name, &vnd_ca);
2040 if (error) {
2041 config_cfdriver_detach(&vnd_cd);
2042 aprint_error("%s: unable to register cfattach\n",
2043 vnd_cd.cd_name);
2044 break;
2045 }
2046
2047 error = devsw_attach("vnd", &vnd_bdevsw, &bmajor,
2048 &vnd_cdevsw, &cmajor);
2049 if (error) {
2050 config_cfattach_detach(vnd_cd.cd_name, &vnd_ca);
2051 config_cfdriver_detach(&vnd_cd);
2052 break;
2053 }
2054
2055 break;
2056
2057 case MODULE_CMD_FINI:
2058 error = config_cfattach_detach(vnd_cd.cd_name, &vnd_ca);
2059 if (error)
2060 break;
2061 config_cfdriver_detach(&vnd_cd);
2062 devsw_detach(&vnd_bdevsw, &vnd_cdevsw);
2063 break;
2064
2065 case MODULE_CMD_STAT:
2066 return ENOTTY;
2067
2068 default:
2069 return ENOTTY;
2070 }
2071
2072 return error;
2073 }
2074
2075 #endif
2076