1 /*- 2 * Copyright (c) 2007 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/bio.h> 32 #include <sys/disklabel.h> 33 #include <sys/endian.h> 34 #include <sys/kernel.h> 35 #include <sys/kobj.h> 36 #include <sys/limits.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/mutex.h> 40 #include <sys/queue.h> 41 #include <sys/sbuf.h> 42 #include <sys/systm.h> 43 #include <geom/geom.h> 44 #include <geom/part/g_part.h> 45 46 #include "g_part_if.h" 47 48 struct g_part_bsd_table { 49 struct g_part_table base; 50 u_char *label; 51 uint32_t offset; 52 }; 53 54 struct g_part_bsd_entry { 55 struct g_part_entry base; 56 struct partition part; 57 }; 58 59 static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *, 60 struct g_part_parms *); 61 static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *); 62 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *); 63 static int g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *, 64 struct sbuf *, const char *); 65 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *); 66 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *, 67 struct g_part_parms *); 68 static char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *, 69 char *, size_t); 70 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *); 71 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *); 72 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *, 73 char *, size_t); 74 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *); 75 76 static kobj_method_t g_part_bsd_methods[] = { 77 KOBJMETHOD(g_part_add, g_part_bsd_add), 78 KOBJMETHOD(g_part_create, g_part_bsd_create), 79 KOBJMETHOD(g_part_destroy, g_part_bsd_destroy), 80 KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf), 81 KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto), 82 KOBJMETHOD(g_part_modify, g_part_bsd_modify), 83 KOBJMETHOD(g_part_name, g_part_bsd_name), 84 KOBJMETHOD(g_part_probe, g_part_bsd_probe), 85 KOBJMETHOD(g_part_read, g_part_bsd_read), 86 KOBJMETHOD(g_part_type, g_part_bsd_type), 87 KOBJMETHOD(g_part_write, g_part_bsd_write), 88 { 0, 0 } 89 }; 90 91 static struct g_part_scheme g_part_bsd_scheme = { 92 "BSD", 93 g_part_bsd_methods, 94 sizeof(struct g_part_bsd_table), 95 .gps_entrysz = sizeof(struct g_part_bsd_entry), 96 .gps_minent = 8, 97 .gps_maxent = 20, 98 }; 99 G_PART_SCHEME_DECLARE(g_part_bsd); 100 101 static int 102 bsd_parse_type(const char *type, uint8_t *fstype) 103 { 104 const char *alias; 105 char *endp; 106 long lt; 107 108 if (type[0] == '!') { 109 lt = strtol(type + 1, &endp, 0); 110 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256) 111 return (EINVAL); 112 *fstype = (u_int)lt; 113 return (0); 114 } 115 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP); 116 if (!strcasecmp(type, alias)) { 117 *fstype = FS_SWAP; 118 return (0); 119 } 120 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS); 121 if (!strcasecmp(type, alias)) { 122 *fstype = FS_BSDFFS; 123 return (0); 124 } 125 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM); 126 if (!strcasecmp(type, alias)) { 127 *fstype = FS_VINUM; 128 return (0); 129 } 130 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS); 131 if (!strcasecmp(type, alias)) { 132 *fstype = FS_ZFS; 133 return (0); 134 } 135 return (EINVAL); 136 } 137 138 static int 139 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 140 struct g_part_parms *gpp) 141 { 142 struct g_part_bsd_entry *entry; 143 struct g_part_bsd_table *table; 144 145 if (gpp->gpp_parms & G_PART_PARM_LABEL) 146 return (EINVAL); 147 148 entry = (struct g_part_bsd_entry *)baseentry; 149 table = (struct g_part_bsd_table *)basetable; 150 151 entry->part.p_size = gpp->gpp_size; 152 entry->part.p_offset = gpp->gpp_start + table->offset; 153 entry->part.p_fsize = 0; 154 entry->part.p_frag = 0; 155 entry->part.p_cpg = 0; 156 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype)); 157 } 158 159 static int 160 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp) 161 { 162 struct g_consumer *cp; 163 struct g_provider *pp; 164 struct g_part_entry *baseentry; 165 struct g_part_bsd_entry *entry; 166 struct g_part_bsd_table *table; 167 u_char *ptr; 168 uint64_t msize; 169 uint32_t ncyls, secpercyl; 170 171 pp = gpp->gpp_provider; 172 cp = LIST_FIRST(&pp->consumers); 173 174 if (pp->sectorsize < sizeof(struct disklabel)) 175 return (ENOSPC); 176 177 msize = pp->mediasize / pp->sectorsize; 178 secpercyl = basetable->gpt_sectors * basetable->gpt_heads; 179 ncyls = msize / secpercyl; 180 181 table = (struct g_part_bsd_table *)basetable; 182 ptr = table->label = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 183 184 le32enc(ptr + 0, DISKMAGIC); /* d_magic */ 185 le32enc(ptr + 40, pp->sectorsize); /* d_secsize */ 186 le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */ 187 le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */ 188 le32enc(ptr + 52, ncyls); /* d_ncylinders */ 189 le32enc(ptr + 56, secpercyl); /* d_secpercyl */ 190 le32enc(ptr + 60, msize); /* d_secperunit */ 191 le16enc(ptr + 72, 3600); /* d_rpm */ 192 le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */ 193 le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */ 194 le32enc(ptr + 140, BBSIZE); /* d_bbsize */ 195 196 basetable->gpt_first = 0; 197 basetable->gpt_last = msize - 1; 198 basetable->gpt_isleaf = 1; 199 200 baseentry = g_part_new_entry(basetable, RAW_PART + 1, 201 basetable->gpt_first, basetable->gpt_last); 202 baseentry->gpe_internal = 1; 203 entry = (struct g_part_bsd_entry *)baseentry; 204 entry->part.p_size = basetable->gpt_last + 1; 205 entry->part.p_offset = table->offset; 206 207 return (0); 208 } 209 210 static int 211 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 212 { 213 214 /* Wipe the second sector to clear the partitioning. */ 215 basetable->gpt_smhead |= 2; 216 return (0); 217 } 218 219 static int 220 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 221 struct sbuf *sb, const char *indent) 222 { 223 struct g_part_bsd_entry *entry; 224 225 entry = (struct g_part_bsd_entry *)baseentry; 226 if (indent == NULL) { 227 /* conftxt: libdisk compatibility */ 228 sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype); 229 } else if (entry != NULL) { 230 /* confxml: partition entry information */ 231 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent, 232 entry->part.p_fstype); 233 } else { 234 /* confxml: scheme information */ 235 } 236 return (0); 237 } 238 239 static int 240 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 241 { 242 struct g_part_bsd_entry *entry; 243 244 /* Allow dumping to a swap partition only. */ 245 entry = (struct g_part_bsd_entry *)baseentry; 246 return ((entry->part.p_fstype == FS_SWAP) ? 1 : 0); 247 } 248 249 static int 250 g_part_bsd_modify(struct g_part_table *basetable, 251 struct g_part_entry *baseentry, struct g_part_parms *gpp) 252 { 253 struct g_part_bsd_entry *entry; 254 255 if (gpp->gpp_parms & G_PART_PARM_LABEL) 256 return (EINVAL); 257 258 entry = (struct g_part_bsd_entry *)baseentry; 259 if (gpp->gpp_parms & G_PART_PARM_TYPE) 260 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype)); 261 return (0); 262 } 263 264 static char * 265 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry, 266 char *buf, size_t bufsz) 267 { 268 269 snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1); 270 return (buf); 271 } 272 273 static int 274 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp) 275 { 276 struct g_provider *pp; 277 u_char *buf; 278 uint32_t magic1, magic2; 279 int error; 280 281 pp = cp->provider; 282 283 /* Sanity-check the provider. */ 284 if (pp->sectorsize < sizeof(struct disklabel) || 285 pp->mediasize < BBSIZE) 286 return (ENOSPC); 287 288 /* Check that there's a disklabel. */ 289 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 290 if (buf == NULL) 291 return (error); 292 magic1 = le32dec(buf + 0); 293 magic2 = le32dec(buf + 132); 294 g_free(buf); 295 return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC) 296 ? G_PART_PROBE_PRI_HIGH : ENXIO); 297 } 298 299 static int 300 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp) 301 { 302 struct g_provider *pp; 303 struct g_part_bsd_table *table; 304 struct g_part_entry *baseentry; 305 struct g_part_bsd_entry *entry; 306 struct partition part; 307 u_char *buf, *p; 308 off_t chs, msize; 309 u_int sectors, heads; 310 int error, index; 311 312 pp = cp->provider; 313 table = (struct g_part_bsd_table *)basetable; 314 msize = pp->mediasize / pp->sectorsize; 315 316 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 317 if (buf == NULL) 318 return (error); 319 320 table->label = buf; 321 322 if (le32dec(buf + 40) != pp->sectorsize) 323 goto invalid_label; 324 sectors = le32dec(buf + 44); 325 if (sectors < 1 || sectors > 255) 326 goto invalid_label; 327 if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) { 328 g_part_geometry_heads(msize, sectors, &chs, &heads); 329 if (chs != 0) { 330 basetable->gpt_sectors = sectors; 331 basetable->gpt_heads = heads; 332 } 333 } 334 heads = le32dec(buf + 48); 335 if (heads < 1 || heads > 255) 336 goto invalid_label; 337 if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom) 338 basetable->gpt_heads = heads; 339 if (sectors != basetable->gpt_sectors || 340 heads != basetable->gpt_heads) 341 printf("GEOM: %s: geometry does not match label.\n", pp->name); 342 343 chs = le32dec(buf + 60); 344 if (chs < 1) 345 goto invalid_label; 346 /* Fix-up a sysinstall bug. */ 347 if (chs > msize) { 348 chs = msize; 349 le32enc(buf + 60, msize); 350 } 351 if (chs != msize) 352 printf("GEOM: %s: media size does not match label.\n", 353 pp->name); 354 355 basetable->gpt_first = 0; 356 basetable->gpt_last = msize - 1; 357 basetable->gpt_isleaf = 1; 358 359 basetable->gpt_entries = le16dec(buf + 138); 360 if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent || 361 basetable->gpt_entries > g_part_bsd_scheme.gps_maxent) 362 goto invalid_label; 363 364 table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4); 365 for (index = basetable->gpt_entries - 1; index >= 0; index--) { 366 p = buf + 148 + index * 16; 367 part.p_size = le32dec(p + 0); 368 part.p_offset = le32dec(p + 4); 369 part.p_fsize = le32dec(p + 8); 370 part.p_fstype = p[12]; 371 part.p_frag = p[13]; 372 part.p_cpg = le16dec(p + 14); 373 if (part.p_size == 0) 374 continue; 375 if (part.p_fstype == FS_UNUSED && index != RAW_PART) 376 continue; 377 if (part.p_offset < table->offset) 378 continue; 379 baseentry = g_part_new_entry(basetable, index + 1, 380 part.p_offset - table->offset, 381 part.p_offset - table->offset + part.p_size - 1); 382 entry = (struct g_part_bsd_entry *)baseentry; 383 entry->part = part; 384 if (part.p_fstype == FS_UNUSED) 385 baseentry->gpe_internal = 1; 386 } 387 388 return (0); 389 390 invalid_label: 391 printf("GEOM: %s: invalid disklabel.\n", pp->name); 392 g_free(table->label); 393 return (EINVAL); 394 } 395 396 static const char * 397 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 398 char *buf, size_t bufsz) 399 { 400 struct g_part_bsd_entry *entry; 401 int type; 402 403 entry = (struct g_part_bsd_entry *)baseentry; 404 type = entry->part.p_fstype; 405 if (type == FS_SWAP) 406 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP)); 407 if (type == FS_BSDFFS) 408 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS)); 409 if (type == FS_VINUM) 410 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM)); 411 if (type == FS_ZFS) 412 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS)); 413 snprintf(buf, bufsz, "!%d", type); 414 return (buf); 415 } 416 417 static int 418 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp) 419 { 420 struct g_provider *pp; 421 struct g_part_entry *baseentry; 422 struct g_part_bsd_entry *entry; 423 struct g_part_bsd_table *table; 424 uint16_t sum; 425 u_char *p, *pe; 426 int error, index; 427 428 pp = cp->provider; 429 table = (struct g_part_bsd_table *)basetable; 430 baseentry = LIST_FIRST(&basetable->gpt_entry); 431 for (index = 1; index <= basetable->gpt_entries; index++) { 432 p = table->label + 148 + (index - 1) * 16; 433 entry = (baseentry != NULL && index == baseentry->gpe_index) 434 ? (struct g_part_bsd_entry *)baseentry : NULL; 435 if (entry != NULL && !baseentry->gpe_deleted) { 436 le32enc(p + 0, entry->part.p_size); 437 le32enc(p + 4, entry->part.p_offset); 438 le32enc(p + 8, entry->part.p_fsize); 439 p[12] = entry->part.p_fstype; 440 p[13] = entry->part.p_frag; 441 le16enc(p + 14, entry->part.p_cpg); 442 } else 443 bzero(p, 16); 444 445 if (entry != NULL) 446 baseentry = LIST_NEXT(baseentry, gpe_entry); 447 } 448 449 /* Calculate checksum. */ 450 le16enc(table->label + 136, 0); 451 pe = table->label + 148 + basetable->gpt_entries * 16; 452 sum = 0; 453 for (p = table->label; p < pe; p += 2) 454 sum ^= le16dec(p); 455 le16enc(table->label + 136, sum); 456 457 error = g_write_data(cp, pp->sectorsize, table->label, pp->sectorsize); 458 return (error); 459 } 460