vm: fix sanity checks on arm
[minix.git] / sbin / fsck / partutil.c
blob88cf59d9e36dbea2398cc1ecc57045b8132fcea7
1 /* $NetBSD: partutil.c,v 1.10 2010/03/06 00:30:54 christos Exp $ */
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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.
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.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: partutil.c,v 1.10 2010/03/06 00:30:54 christos Exp $");
35 #include <sys/types.h>
36 #include <sys/disklabel.h>
37 #include <sys/disk.h>
38 #include <sys/ioctl.h>
39 #include <sys/stat.h>
42 #include <disktab.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <util.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include <prop/proplib.h>
53 #include "partutil.h"
56 * Convert disklabel geometry info to disk_geom.
58 static void
59 label2geom(struct disk_geom *geo, const struct disklabel *lp)
61 geo->dg_secperunit = lp->d_secperunit;
62 geo->dg_secsize = lp->d_secsize;
63 geo->dg_nsectors = lp->d_nsectors;
64 geo->dg_ntracks = lp->d_ntracks;
65 geo->dg_ncylinders = lp->d_ncylinders;
66 geo->dg_secpercyl = lp->d_secpercyl;
67 geo->dg_pcylinders = lp->d_ncylinders;
68 geo->dg_sparespertrack = lp->d_sparespertrack;
69 geo->dg_sparespercyl = lp->d_sparespercyl;
70 geo->dg_acylinders = lp->d_acylinders;
74 * Set what we need to know about disk geometry.
76 static void
77 dict2geom(struct disk_geom *geo, prop_dictionary_t dict)
79 (void)memset(geo, 0, sizeof(struct disk_geom));
80 prop_dictionary_get_int64(dict, "sectors-per-unit",
81 &geo->dg_secperunit);
82 prop_dictionary_get_uint32(dict, "sector-size", &geo->dg_secsize);
83 prop_dictionary_get_uint32(dict, "sectors-per-track",
84 &geo->dg_nsectors);
85 prop_dictionary_get_uint32(dict, "tracks-per-cylinder",
86 &geo->dg_ntracks);
87 prop_dictionary_get_uint32(dict, "cylinders-per-unit",
88 &geo->dg_ncylinders);
92 static void
93 part2wedge(struct dkwedge_info *dkw, const struct disklabel *lp, const char *s)
95 #ifdef __minix
96 errx(1, "minix doesn't know about wedges");
97 #else
98 struct stat sb;
99 const struct partition *pp;
100 int ptn;
102 (void)memset(dkw, 0, sizeof(*dkw));
103 if (stat(s, &sb) == -1)
104 return;
106 ptn = strchr(s, '\0')[-1] - 'a';
107 if ((unsigned)ptn >= lp->d_npartitions ||
108 (devminor_t)ptn != DISKPART(sb.st_rdev))
109 return;
111 pp = &lp->d_partitions[ptn];
112 dkw->dkw_offset = pp->p_offset;
113 dkw->dkw_size = pp->p_size;
114 dkw->dkw_parent[0] = '*';
115 switch (pp->p_fstype) {
116 default:
117 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNKNOWN);
118 break;
119 case FS_UNUSED:
120 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_UNUSED);
121 break;
122 case FS_SWAP:
123 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_SWAP);
124 break;
125 case FS_BSDFFS:
126 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS);
127 break;
128 case FS_BSDLFS:
129 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_LFS);
130 break;
131 case FS_EX2FS:
132 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_EXT2FS);
133 break;
134 case FS_ISO9660:
135 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_ISO9660);
136 break;
137 case FS_ADOS:
138 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_AMIGADOS);
139 break;
140 case FS_HFS:
141 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEHFS);
142 break;
143 case FS_MSDOS:
144 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FAT);
145 break;
146 case FS_FILECORE:
147 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_FILECORE);
148 break;
149 case FS_APPLEUFS:
150 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_APPLEUFS);
151 break;
152 case FS_NTFS:
153 (void)strcpy(dkw->dkw_ptype, DKW_PTYPE_NTFS);
154 break;
156 #endif
160 getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo,
161 struct dkwedge_info *dkw)
163 struct disklabel lab;
164 struct disklabel *lp = &lab;
165 prop_dictionary_t disk_dict, geom_dict;
167 if (dt) {
168 #ifdef __minix
169 errx(1, "minix doesn't know about disk types (%s)", dt);
170 #else
171 lp = getdiskbyname(dt);
172 if (lp == NULL)
173 errx(1, "unknown disk type `%s'", dt);
174 #endif
177 /* Get disk description dictionary */
178 #ifndef __minix
179 if (prop_dictionary_recv_ioctl(fd, DIOCGDISKINFO, &disk_dict)) {
180 #else
181 if (-1) {
182 #endif
184 * Ask for disklabel if DIOCGDISKINFO failed. This is
185 * compatibility call and can be removed when all devices
186 * will support DIOCGDISKINFO.
187 * cgd, ccd pseudo disk drives doesn't support DIOCGDDISKINFO
189 if (ioctl(fd, DIOCGDINFO, lp) == -1) {
190 warn("DIOCGDINFO on %s failed", s);
191 return -1;
193 label2geom(geo, lp);
194 } else {
195 geom_dict = prop_dictionary_get(disk_dict, "geometry");
196 dict2geom(geo, geom_dict);
199 /* Get info about partition/wedge */
200 if (ioctl(fd, DIOCGWEDGEINFO, dkw) == -1) {
201 if (ioctl(fd, DIOCGDINFO, lp) == -1)
202 err(1, "Please implement DIOCGWEDGEINFO or "
203 "DIOCGDINFO for disk device %s", s);
205 part2wedge(dkw, lp, s);
208 return 0;