subr_disk_open.c revision 1.1.2.3 1 1.1.2.3 yamt /* $NetBSD: subr_disk_open.c,v 1.1.2.3 2010/03/11 15:04:18 yamt Exp $ */
2 1.1.2.2 yamt
3 1.1.2.2 yamt /*-
4 1.1.2.2 yamt * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1.2.2 yamt * All rights reserved.
6 1.1.2.2 yamt *
7 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 yamt * modification, are permitted provided that the following conditions
9 1.1.2.2 yamt * are met:
10 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 yamt * documentation and/or other materials provided with the distribution.
15 1.1.2.2 yamt *
16 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.2.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.2.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.2.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
27 1.1.2.2 yamt */
28 1.1.2.2 yamt
29 1.1.2.2 yamt #include <sys/cdefs.h>
30 1.1.2.3 yamt __KERNEL_RCSID(0, "$NetBSD: subr_disk_open.c,v 1.1.2.3 2010/03/11 15:04:18 yamt Exp $");
31 1.1.2.2 yamt
32 1.1.2.2 yamt #include <sys/param.h>
33 1.1.2.2 yamt #include <sys/conf.h>
34 1.1.2.2 yamt #include <sys/device.h>
35 1.1.2.2 yamt #include <sys/disk.h>
36 1.1.2.2 yamt #include <sys/disklabel.h>
37 1.1.2.2 yamt #include <sys/fcntl.h>
38 1.1.2.2 yamt #include <sys/kauth.h>
39 1.1.2.2 yamt #include <sys/vnode.h>
40 1.1.2.2 yamt
41 1.1.2.2 yamt struct vnode *
42 1.1.2.2 yamt opendisk(struct device *dv)
43 1.1.2.2 yamt {
44 1.1.2.2 yamt int bmajor, bminor;
45 1.1.2.2 yamt struct vnode *tmpvn;
46 1.1.2.2 yamt int error;
47 1.1.2.2 yamt dev_t dev;
48 1.1.2.2 yamt
49 1.1.2.2 yamt /*
50 1.1.2.2 yamt * Lookup major number for disk block device.
51 1.1.2.2 yamt */
52 1.1.2.2 yamt bmajor = devsw_name2blk(device_xname(dv), NULL, 0);
53 1.1.2.2 yamt if (bmajor == -1)
54 1.1.2.2 yamt return NULL;
55 1.1.2.2 yamt
56 1.1.2.2 yamt bminor = minor(device_unit(dv));
57 1.1.2.2 yamt /*
58 1.1.2.2 yamt * Fake a temporary vnode for the disk, open it, and read
59 1.1.2.2 yamt * and hash the sectors.
60 1.1.2.2 yamt */
61 1.1.2.2 yamt dev = device_is_a(dv, "dk") ? makedev(bmajor, bminor) :
62 1.1.2.2 yamt MAKEDISKDEV(bmajor, bminor, RAW_PART);
63 1.1.2.2 yamt if (bdevvp(dev, &tmpvn))
64 1.1.2.2 yamt panic("%s: can't alloc vnode for %s", __func__,
65 1.1.2.2 yamt device_xname(dv));
66 1.1.2.2 yamt error = VOP_OPEN(tmpvn, FREAD, NOCRED);
67 1.1.2.2 yamt if (error) {
68 1.1.2.2 yamt #ifndef DEBUG
69 1.1.2.2 yamt /*
70 1.1.2.2 yamt * Ignore errors caused by missing device, partition,
71 1.1.2.2 yamt * or medium.
72 1.1.2.2 yamt */
73 1.1.2.2 yamt if (error != ENXIO && error != ENODEV)
74 1.1.2.2 yamt #endif
75 1.1.2.2 yamt printf("%s: can't open dev %s (%d)\n",
76 1.1.2.2 yamt __func__, device_xname(dv), error);
77 1.1.2.2 yamt vput(tmpvn);
78 1.1.2.2 yamt return NULL;
79 1.1.2.2 yamt }
80 1.1.2.2 yamt
81 1.1.2.2 yamt return tmpvn;
82 1.1.2.2 yamt }
83 1.1.2.3 yamt
84 1.1.2.3 yamt int
85 1.1.2.3 yamt getdisksize(struct vnode *vp, uint64_t *numsecp, unsigned *secsizep)
86 1.1.2.3 yamt {
87 1.1.2.3 yamt struct partinfo dpart;
88 1.1.2.3 yamt struct dkwedge_info dkw;
89 1.1.2.3 yamt struct disk *pdk;
90 1.1.2.3 yamt int error;
91 1.1.2.3 yamt
92 1.1.2.3 yamt error = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, NOCRED);
93 1.1.2.3 yamt if (error == 0) {
94 1.1.2.3 yamt *secsizep = dpart.disklab->d_secsize;
95 1.1.2.3 yamt *numsecp = dpart.part->p_size;
96 1.1.2.3 yamt return 0;
97 1.1.2.3 yamt }
98 1.1.2.3 yamt
99 1.1.2.3 yamt error = VOP_IOCTL(vp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED);
100 1.1.2.3 yamt if (error == 0) {
101 1.1.2.3 yamt pdk = disk_find(dkw.dkw_parent);
102 1.1.2.3 yamt if (pdk != NULL) {
103 1.1.2.3 yamt *secsizep = DEV_BSIZE << pdk->dk_blkshift;
104 1.1.2.3 yamt *numsecp = dkw.dkw_size;
105 1.1.2.3 yamt } else
106 1.1.2.3 yamt error = ENODEV;
107 1.1.2.3 yamt }
108 1.1.2.3 yamt
109 1.1.2.3 yamt return error;
110 1.1.2.3 yamt }
111