g_part_bsd.c revision 207094
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: head/sys/geom/part/g_part_bsd.c 207094 2010-04-23 03:11:39Z marcel $"); 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 48struct g_part_bsd_table { 49 struct g_part_table base; 50 u_char *bbarea; 51 uint32_t offset; 52}; 53 54struct g_part_bsd_entry { 55 struct g_part_entry base; 56 struct partition part; 57}; 58 59static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *, 60 struct g_part_parms *); 61static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *); 62static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *); 63static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *); 64static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *, 65 struct sbuf *, const char *); 66static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *); 67static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *, 68 struct g_part_parms *); 69static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *, 70 char *, size_t); 71static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *); 72static int g_part_bsd_read(struct g_part_table *, struct g_consumer *); 73static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *, 74 char *, size_t); 75static int g_part_bsd_write(struct g_part_table *, struct g_consumer *); 76static int g_part_bsd_resize(struct g_part_table *, struct g_part_entry *, 77 struct g_part_parms *); 78 79static kobj_method_t g_part_bsd_methods[] = { 80 KOBJMETHOD(g_part_add, g_part_bsd_add), 81 KOBJMETHOD(g_part_bootcode, g_part_bsd_bootcode), 82 KOBJMETHOD(g_part_create, g_part_bsd_create), 83 KOBJMETHOD(g_part_destroy, g_part_bsd_destroy), 84 KOBJMETHOD(g_part_dumpconf, g_part_bsd_dumpconf), 85 KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto), 86 KOBJMETHOD(g_part_modify, g_part_bsd_modify), 87 KOBJMETHOD(g_part_resize, g_part_bsd_resize), 88 KOBJMETHOD(g_part_name, g_part_bsd_name), 89 KOBJMETHOD(g_part_probe, g_part_bsd_probe), 90 KOBJMETHOD(g_part_read, g_part_bsd_read), 91 KOBJMETHOD(g_part_type, g_part_bsd_type), 92 KOBJMETHOD(g_part_write, g_part_bsd_write), 93 { 0, 0 } 94}; 95 96static struct g_part_scheme g_part_bsd_scheme = { 97 "BSD", 98 g_part_bsd_methods, 99 sizeof(struct g_part_bsd_table), 100 .gps_entrysz = sizeof(struct g_part_bsd_entry), 101 .gps_minent = 8, 102 .gps_maxent = 20, 103 .gps_bootcodesz = BBSIZE, 104}; 105G_PART_SCHEME_DECLARE(g_part_bsd); 106 107static int 108bsd_parse_type(const char *type, uint8_t *fstype) 109{ 110 const char *alias; 111 char *endp; 112 long lt; 113 114 if (type[0] == '!') { 115 lt = strtol(type + 1, &endp, 0); 116 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256) 117 return (EINVAL); 118 *fstype = (u_int)lt; 119 return (0); 120 } 121 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP); 122 if (!strcasecmp(type, alias)) { 123 *fstype = FS_SWAP; 124 return (0); 125 } 126 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS); 127 if (!strcasecmp(type, alias)) { 128 *fstype = FS_BSDFFS; 129 return (0); 130 } 131 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM); 132 if (!strcasecmp(type, alias)) { 133 *fstype = FS_VINUM; 134 return (0); 135 } 136 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS); 137 if (!strcasecmp(type, alias)) { 138 *fstype = FS_ZFS; 139 return (0); 140 } 141 return (EINVAL); 142} 143 144static int 145g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 146 struct g_part_parms *gpp) 147{ 148 struct g_part_bsd_entry *entry; 149 struct g_part_bsd_table *table; 150 151 if (gpp->gpp_parms & G_PART_PARM_LABEL) 152 return (EINVAL); 153 154 entry = (struct g_part_bsd_entry *)baseentry; 155 table = (struct g_part_bsd_table *)basetable; 156 157 entry->part.p_size = gpp->gpp_size; 158 entry->part.p_offset = gpp->gpp_start + table->offset; 159 entry->part.p_fsize = 0; 160 entry->part.p_frag = 0; 161 entry->part.p_cpg = 0; 162 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype)); 163} 164 165static int 166g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp) 167{ 168 struct g_part_bsd_table *table; 169 const u_char *codeptr; 170 size_t hdsz, tlsz; 171 size_t codesz, tlofs; 172 173 hdsz = 512; 174 tlofs = hdsz + 148 + basetable->gpt_entries * 16; 175 tlsz = BBSIZE - tlofs; 176 table = (struct g_part_bsd_table *)basetable; 177 bzero(table->bbarea, hdsz); 178 bzero(table->bbarea + tlofs, tlsz); 179 codeptr = gpp->gpp_codeptr; 180 codesz = MIN(hdsz, gpp->gpp_codesize); 181 if (codesz > 0) 182 bcopy(codeptr, table->bbarea, codesz); 183 codesz = MIN(tlsz, gpp->gpp_codesize - tlofs); 184 if (codesz > 0) 185 bcopy(codeptr + tlofs, table->bbarea + tlofs, codesz); 186 return (0); 187} 188 189static int 190g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp) 191{ 192 struct g_provider *pp; 193 struct g_part_entry *baseentry; 194 struct g_part_bsd_entry *entry; 195 struct g_part_bsd_table *table; 196 u_char *ptr; 197 uint32_t msize, ncyls, secpercyl; 198 199 pp = gpp->gpp_provider; 200 201 if (pp->sectorsize < sizeof(struct disklabel)) 202 return (ENOSPC); 203 if (BBSIZE % pp->sectorsize) 204 return (ENOTBLK); 205 206 msize = MIN(pp->mediasize / pp->sectorsize, 0xffffffff); 207 secpercyl = basetable->gpt_sectors * basetable->gpt_heads; 208 ncyls = msize / secpercyl; 209 210 table = (struct g_part_bsd_table *)basetable; 211 table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO); 212 ptr = table->bbarea + pp->sectorsize; 213 214 le32enc(ptr + 0, DISKMAGIC); /* d_magic */ 215 le32enc(ptr + 40, pp->sectorsize); /* d_secsize */ 216 le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */ 217 le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */ 218 le32enc(ptr + 52, ncyls); /* d_ncylinders */ 219 le32enc(ptr + 56, secpercyl); /* d_secpercyl */ 220 le32enc(ptr + 60, msize); /* d_secperunit */ 221 le16enc(ptr + 72, 3600); /* d_rpm */ 222 le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */ 223 le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */ 224 le32enc(ptr + 140, BBSIZE); /* d_bbsize */ 225 226 basetable->gpt_first = 0; 227 basetable->gpt_last = msize - 1; 228 basetable->gpt_isleaf = 1; 229 230 baseentry = g_part_new_entry(basetable, RAW_PART + 1, 231 basetable->gpt_first, basetable->gpt_last); 232 baseentry->gpe_internal = 1; 233 entry = (struct g_part_bsd_entry *)baseentry; 234 entry->part.p_size = basetable->gpt_last + 1; 235 entry->part.p_offset = table->offset; 236 237 return (0); 238} 239 240static int 241g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 242{ 243 244 /* Wipe the second sector to clear the partitioning. */ 245 basetable->gpt_smhead |= 2; 246 return (0); 247} 248 249static void 250g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 251 struct sbuf *sb, const char *indent) 252{ 253 struct g_part_bsd_entry *entry; 254 255 entry = (struct g_part_bsd_entry *)baseentry; 256 if (indent == NULL) { 257 /* conftxt: libdisk compatibility */ 258 sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype); 259 } else if (entry != NULL) { 260 /* confxml: partition entry information */ 261 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent, 262 entry->part.p_fstype); 263 } else { 264 /* confxml: scheme information */ 265 } 266} 267 268static int 269g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 270{ 271 struct g_part_bsd_entry *entry; 272 273 /* Allow dumping to a swap partition or an unused partition. */ 274 entry = (struct g_part_bsd_entry *)baseentry; 275 return ((entry->part.p_fstype == FS_UNUSED || 276 entry->part.p_fstype == FS_SWAP) ? 1 : 0); 277} 278 279static int 280g_part_bsd_modify(struct g_part_table *basetable, 281 struct g_part_entry *baseentry, struct g_part_parms *gpp) 282{ 283 struct g_part_bsd_entry *entry; 284 285 if (gpp->gpp_parms & G_PART_PARM_LABEL) 286 return (EINVAL); 287 288 entry = (struct g_part_bsd_entry *)baseentry; 289 if (gpp->gpp_parms & G_PART_PARM_TYPE) 290 return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype)); 291 return (0); 292} 293 294static int 295g_part_bsd_resize(struct g_part_table *basetable, 296 struct g_part_entry *baseentry, struct g_part_parms *gpp) 297{ 298 struct g_part_bsd_entry *entry; 299 300 entry = (struct g_part_bsd_entry *)baseentry; 301 baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1; 302 entry->part.p_size = gpp->gpp_size; 303 304 return (0); 305} 306 307static const char * 308g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry, 309 char *buf, size_t bufsz) 310{ 311 312 snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1); 313 return (buf); 314} 315 316static int 317g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp) 318{ 319 struct g_provider *pp; 320 u_char *buf; 321 uint32_t magic1, magic2; 322 int error; 323 324 pp = cp->provider; 325 326 /* Sanity-check the provider. */ 327 if (pp->sectorsize < sizeof(struct disklabel) || 328 pp->mediasize < BBSIZE) 329 return (ENOSPC); 330 if (BBSIZE % pp->sectorsize) 331 return (ENOTBLK); 332 333 /* Check that there's a disklabel. */ 334 buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); 335 if (buf == NULL) 336 return (error); 337 magic1 = le32dec(buf + 0); 338 magic2 = le32dec(buf + 132); 339 g_free(buf); 340 return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC) 341 ? G_PART_PROBE_PRI_HIGH : ENXIO); 342} 343 344static int 345g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp) 346{ 347 struct g_provider *pp; 348 struct g_part_bsd_table *table; 349 struct g_part_entry *baseentry; 350 struct g_part_bsd_entry *entry; 351 struct partition part; 352 u_char *buf, *p; 353 off_t chs, msize; 354 u_int sectors, heads; 355 int error, index; 356 357 pp = cp->provider; 358 table = (struct g_part_bsd_table *)basetable; 359 msize = pp->mediasize / pp->sectorsize; 360 361 table->bbarea = g_read_data(cp, 0, BBSIZE, &error); 362 if (table->bbarea == NULL) 363 return (error); 364 365 buf = table->bbarea + pp->sectorsize; 366 367 if (le32dec(buf + 40) != pp->sectorsize) 368 goto invalid_label; 369 sectors = le32dec(buf + 44); 370 if (sectors < 1 || sectors > 255) 371 goto invalid_label; 372 if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) { 373 g_part_geometry_heads(msize, sectors, &chs, &heads); 374 if (chs != 0) { 375 basetable->gpt_sectors = sectors; 376 basetable->gpt_heads = heads; 377 } 378 } 379 heads = le32dec(buf + 48); 380 if (heads < 1 || heads > 255) 381 goto invalid_label; 382 if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom) 383 basetable->gpt_heads = heads; 384 if (sectors != basetable->gpt_sectors || heads != basetable->gpt_heads) 385 printf("GEOM: %s: geometry does not match label" 386 " (%uh,%us != %uh,%us).\n", pp->name, heads, sectors, 387 basetable->gpt_heads, basetable->gpt_sectors); 388 389 chs = le32dec(buf + 60); 390 if (chs < 1) 391 goto invalid_label; 392 /* Fix-up a sysinstall bug. */ 393 if (chs > msize) { 394 chs = msize; 395 le32enc(buf + 60, msize); 396 } 397 if (chs != msize) 398 printf("GEOM: %s: media size does not match label.\n", 399 pp->name); 400 401 basetable->gpt_first = 0; 402 basetable->gpt_last = msize - 1; 403 basetable->gpt_isleaf = 1; 404 405 basetable->gpt_entries = le16dec(buf + 138); 406 if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent || 407 basetable->gpt_entries > g_part_bsd_scheme.gps_maxent) 408 goto invalid_label; 409 410 table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4); 411 for (index = basetable->gpt_entries - 1; index >= 0; index--) { 412 p = buf + 148 + index * 16; 413 part.p_size = le32dec(p + 0); 414 part.p_offset = le32dec(p + 4); 415 part.p_fsize = le32dec(p + 8); 416 part.p_fstype = p[12]; 417 part.p_frag = p[13]; 418 part.p_cpg = le16dec(p + 14); 419 if (part.p_size == 0) 420 continue; 421 if (part.p_offset < table->offset) 422 continue; 423 baseentry = g_part_new_entry(basetable, index + 1, 424 part.p_offset - table->offset, 425 part.p_offset - table->offset + part.p_size - 1); 426 entry = (struct g_part_bsd_entry *)baseentry; 427 entry->part = part; 428 if (index == RAW_PART) 429 baseentry->gpe_internal = 1; 430 } 431 432 return (0); 433 434 invalid_label: 435 printf("GEOM: %s: invalid disklabel.\n", pp->name); 436 g_free(table->bbarea); 437 return (EINVAL); 438} 439 440static const char * 441g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 442 char *buf, size_t bufsz) 443{ 444 struct g_part_bsd_entry *entry; 445 int type; 446 447 entry = (struct g_part_bsd_entry *)baseentry; 448 type = entry->part.p_fstype; 449 if (type == FS_SWAP) 450 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP)); 451 if (type == FS_BSDFFS) 452 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS)); 453 if (type == FS_VINUM) 454 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM)); 455 if (type == FS_ZFS) 456 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS)); 457 snprintf(buf, bufsz, "!%d", type); 458 return (buf); 459} 460 461static int 462g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp) 463{ 464 struct g_provider *pp; 465 struct g_part_entry *baseentry; 466 struct g_part_bsd_entry *entry; 467 struct g_part_bsd_table *table; 468 uint16_t sum; 469 u_char *label, *p, *pe; 470 int error, index; 471 472 pp = cp->provider; 473 table = (struct g_part_bsd_table *)basetable; 474 baseentry = LIST_FIRST(&basetable->gpt_entry); 475 label = table->bbarea + pp->sectorsize; 476 for (index = 1; index <= basetable->gpt_entries; index++) { 477 p = label + 148 + (index - 1) * 16; 478 entry = (baseentry != NULL && index == baseentry->gpe_index) 479 ? (struct g_part_bsd_entry *)baseentry : NULL; 480 if (entry != NULL && !baseentry->gpe_deleted) { 481 le32enc(p + 0, entry->part.p_size); 482 le32enc(p + 4, entry->part.p_offset); 483 le32enc(p + 8, entry->part.p_fsize); 484 p[12] = entry->part.p_fstype; 485 p[13] = entry->part.p_frag; 486 le16enc(p + 14, entry->part.p_cpg); 487 } else 488 bzero(p, 16); 489 490 if (entry != NULL) 491 baseentry = LIST_NEXT(baseentry, gpe_entry); 492 } 493 494 /* Calculate checksum. */ 495 le16enc(label + 136, 0); 496 pe = label + 148 + basetable->gpt_entries * 16; 497 sum = 0; 498 for (p = label; p < pe; p += 2) 499 sum ^= le16dec(p); 500 le16enc(label + 136, sum); 501 502 error = g_write_data(cp, 0, table->bbarea, BBSIZE); 503 return (error); 504} 505