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