disklabel.c revision 1.22
1/* $NetBSD: disklabel.c,v 1.22 2019/12/13 21:46:59 martin Exp $ */ 2 3/* 4 * Copyright 2018 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS'' 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 26 * THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 30#include "defs.h" 31#include "md.h" 32#include <assert.h> 33#include <util.h> 34#include <paths.h> 35#include <sys/ioctl.h> 36#include <sys/param.h> 37 38const struct disk_partitioning_scheme disklabel_parts; 39 40/*************** disklabel ******************************************/ 41/* a disklabel based disk_partitions interface */ 42struct disklabel_disk_partitions { 43 struct disk_partitions dp; 44 struct disklabel l; 45 daddr_t ptn_alignment; 46 char last_mounted[MAXPARTITIONS][MOUNTLEN]; 47 uint fs_sub_type[MAXPARTITIONS]; 48}; 49 50/* 51 * Maximum number of disklabel partitions the current kernel supports 52 */ 53size_t dl_maxpart; 54 55/* index into this arrray is the type code */ 56static struct part_type_desc dl_types[__arraycount(fstypenames)-1]; 57 58struct dl_custom_ptype { 59 unsigned int type; 60 char short_desc[6], description[30]; 61 struct part_type_desc desc; 62}; 63struct dl_custom_ptype * dl_custom_ptypes; 64size_t dl_custom_ptype_count; 65 66static uint8_t dl_part_type_from_generic(const struct part_type_desc*); 67 68static void 69disklabel_init_default_alignment(struct disklabel_disk_partitions *parts, 70 uint track) 71{ 72 if (track == 0) 73 track = MEG / 512; 74 75 if (dl_maxpart == 0) 76 dl_maxpart = getmaxpartitions(); 77 78#ifdef MD_DISKLABEL_SET_ALIGN_PRE 79 if (MD_DISKLABEL_SET_ALIGN_PRE(parts->ptn_alignment, track)) 80 return; 81#endif 82 /* Use 1MB alignemnt for large (>128GB) disks */ 83 if (parts->dp.disk_size > HUGE_DISK_SIZE) { 84 parts->ptn_alignment = 2048; 85 } else if (parts->dp.disk_size > TINY_DISK_SIZE) { 86 parts->ptn_alignment = 64; 87 } else { 88 parts->ptn_alignment = 1; 89 } 90#ifdef MD_DISKLABEL_SET_ALIGN_POST 91 MD_DISKLABEL_SET_ALIGN_POST(parts->ptn_alignment, track); 92#endif 93} 94 95static bool 96disklabel_change_geom(struct disk_partitions *arg, int ncyl, int nhead, 97 int nsec) 98{ 99 struct disklabel_disk_partitions *parts = 100 (struct disklabel_disk_partitions*)arg; 101 102 assert(parts->l.d_secsize != 0); 103 assert(parts->l.d_nsectors != 0); 104 assert(parts->l.d_ntracks != 0); 105 assert(parts->l.d_ncylinders != 0); 106 assert(parts->l.d_secpercyl != 0); 107 108 disklabel_init_default_alignment(parts, nhead * nsec); 109 if (ncyl*nhead*nsec <= TINY_DISK_SIZE) 110 set_default_sizemult(1); 111 else 112 set_default_sizemult(MEG/512); 113 114 return true; 115} 116 117static struct disk_partitions * 118disklabel_parts_new(const char *dev, daddr_t start, daddr_t len, 119 daddr_t total_size, bool is_boot_drive) 120{ 121 struct disklabel_disk_partitions *parts; 122 struct disk_geom geo; 123 124 if (!get_disk_geom(dev, &geo)) 125 return NULL; 126 127 parts = calloc(1, sizeof(*parts)); 128 if (parts == NULL) 129 return NULL; 130 131 if (len > disklabel_parts.size_limit) 132 len = disklabel_parts.size_limit; 133 if (total_size > disklabel_parts.size_limit) 134 total_size = disklabel_parts.size_limit; 135 136 parts->l.d_ncylinders = geo.dg_ncylinders; 137 parts->l.d_ntracks = geo.dg_ntracks; 138 parts->l.d_nsectors = geo.dg_nsectors; 139 parts->l.d_secsize = geo.dg_secsize; 140 parts->l.d_secpercyl = geo.dg_nsectors * geo.dg_ntracks; 141 142 parts->dp.pscheme = &disklabel_parts; 143 parts->dp.disk = strdup(dev); 144 parts->dp.disk_start = start; 145 parts->dp.disk_size = parts->dp.free_space = len; 146 disklabel_init_default_alignment(parts, parts->l.d_secpercyl); 147 148 strncpy(parts->l.d_packname, "fictious", sizeof parts->l.d_packname); 149 150#if RAW_PART > 2 151 parts->l.d_partitions[RAW_PART-1].p_fstype = FS_UNUSED; 152 parts->l.d_partitions[RAW_PART-1].p_offset = start; 153 parts->l.d_partitions[RAW_PART-1].p_size = len; 154 parts->dp.num_part++; 155#endif 156 parts->l.d_partitions[RAW_PART].p_fstype = FS_UNUSED; 157 parts->l.d_partitions[RAW_PART].p_offset = 0; 158 parts->l.d_partitions[RAW_PART].p_size = total_size; 159 parts->dp.num_part++; 160 161 parts->l.d_npartitions = RAW_PART+1; 162 163 return &parts->dp; 164} 165 166static struct disk_partitions * 167disklabel_parts_read(const char *disk, daddr_t start, daddr_t len, 168 const struct disk_partitioning_scheme *scheme) 169{ 170 int fd; 171 char diskpath[MAXPATHLEN]; 172 uint flags; 173#ifndef DISKLABEL_NO_ONDISK_VERIFY 174 bool only_dl = only_have_disklabel(); 175 bool have_raw_label = false; 176 177 /* 178 * Verify we really have a disklabel. 179 */ 180 if (run_program(RUN_SILENT | RUN_ERROR_OK, 181 "disklabel -r %s", disk) == 0) 182 have_raw_label = true; 183#endif 184 185 /* read partitions */ 186 187 struct disklabel_disk_partitions *parts = calloc(1, sizeof(*parts)); 188 if (parts == NULL) 189 return NULL; 190 191 fd = opendisk(disk, O_RDONLY, diskpath, sizeof(diskpath), 0); 192 if (fd == -1) { 193 free(parts); 194 return NULL; 195 } 196 197 /* 198 * We should actually try to read the label inside the start/len 199 * boundary, but for simplicity just rely on the kernel and 200 * instead verify a FS_UNUSED partition at RAW_PART-1 (if 201 * RAW_PART > 'c') is within the given limits. 202 */ 203 if (ioctl(fd, DIOCGDINFO, &parts->l) < 0) { 204 free(parts); 205 close(fd); 206 return NULL; 207 } 208#if RAW_PART > 2 209 if (parts->l.d_partitions[RAW_PART-1].p_fstype == FS_UNUSED) { 210 daddr_t dlstart = parts->l.d_partitions[RAW_PART-1].p_offset; 211 daddr_t dlend = start + 212 parts->l.d_partitions[RAW_PART-1].p_size; 213 214 if (dlstart < start && dlend > (start+len)) { 215 assert(false); 216 free(parts); 217 close(fd); 218 return NULL; 219 } 220 } 221#endif 222 223 if (len > disklabel_parts.size_limit) 224 len = disklabel_parts.size_limit; 225 parts->dp.pscheme = scheme; 226 parts->dp.disk = strdup(disk); 227 parts->dp.disk_start = start; 228 parts->dp.disk_size = parts->dp.free_space = len; 229 disklabel_init_default_alignment(parts, 0); 230 231 for (int part = 0; part < parts->l.d_npartitions; part++) { 232 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED 233 && parts->l.d_partitions[part].p_size == 0) 234 continue; 235 236 parts->dp.num_part++; 237 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED) 238 continue; 239 240 flags = 0; 241 if (parts->l.d_partitions[part].p_fstype == FS_MSDOS) 242 flags = GLM_MAYBE_FAT32; 243 else if (parts->l.d_partitions[part].p_fstype == FS_BSDFFS) 244 flags = GLM_LIKELY_FFS; 245 if (flags != 0) { 246 uint fs_type, fs_sub_type; 247 const char *lm = get_last_mounted(fd, 248 parts->l.d_partitions[part].p_offset, 249 &fs_type, &fs_sub_type, flags); 250 if (lm != NULL && *lm != 0) { 251 strlcpy(parts->last_mounted[part], lm, 252 sizeof(parts->last_mounted[part])); 253 if (parts->l.d_partitions[part].p_fstype == 254 fs_type) 255 parts->fs_sub_type[part] = fs_sub_type; 256 canonicalize_last_mounted( 257 parts->last_mounted[part]); 258 } 259 } 260 261 if (parts->l.d_partitions[part].p_size > parts->dp.free_space) 262 parts->dp.free_space = 0; 263 else 264 parts->dp.free_space -= 265 parts->l.d_partitions[part].p_size; 266 } 267 close(fd); 268 269#ifndef DISKLABEL_NO_ONDISK_VERIFY 270 if (!have_raw_label && !only_dl) { 271 bool found_real_part = false; 272 273 /* 274 * Check if kernel translation gave us "something" besides 275 * the raw or the whole-disk partition. 276 * If not: report missing disklabel. 277 */ 278 for (int part = 0; part < parts->l.d_npartitions; part++) { 279 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED) 280 continue; 281 if (part == 0 && 282 parts->l.d_partitions[part].p_offset == 283 parts->l.d_partitions[RAW_PART].p_offset && 284 parts->l.d_partitions[part].p_size == 285 parts->l.d_partitions[RAW_PART].p_size) 286 continue; 287 if (part == RAW_PART) 288 continue; 289 found_real_part = true; 290 break; 291 } 292 if (!found_real_part) { 293 /* no partion there yet */ 294 free(parts); 295 return NULL; 296 } 297 } 298#endif 299 300 return &parts->dp; 301} 302 303/* 304 * Escape a string for usage as a tag name in a capfile(5), 305 * we really know there is enough space in the destination buffer... 306 */ 307static void 308escape_capfile(char *dest, const char *src, size_t len) 309{ 310 while (*src && len > 0) { 311 if (*src == ':') 312 *dest++ = ' '; 313 else 314 *dest++ = *src; 315 src++; 316 len--; 317 } 318 *dest = 0; 319} 320 321static bool 322disklabel_write_to_disk(struct disk_partitions *arg) 323{ 324 struct disklabel_disk_partitions *parts = 325 (struct disklabel_disk_partitions*)arg; 326 FILE *f; 327 char fname[PATH_MAX], packname[sizeof(parts->l.d_packname)+1], 328 disktype[sizeof(parts->l.d_typename)+1]; 329 int i, rv = 0; 330 const char *disk = parts->dp.disk, *s; 331 const struct partition *lp; 332 char *d; 333 size_t n; 334 335 assert(parts->l.d_secsize != 0); 336 assert(parts->l.d_nsectors != 0); 337 assert(parts->l.d_ntracks != 0); 338 assert(parts->l.d_ncylinders != 0); 339 assert(parts->l.d_secpercyl != 0); 340 341 /* make sure we have a 0 terminated packname */ 342 strlcpy(packname, parts->l.d_packname, sizeof packname); 343 if (packname[0] == 0) 344 strcpy(packname, "fictious"); 345 346 /* fill typename with disk name prefix, if not already set */ 347 if (strlen(parts->l.d_typename) == 0) { 348 for (n = 0, d = parts->l.d_typename, s = disk; 349 *s && n < sizeof(parts->l.d_typename); d++, s++, n++) { 350 if (isdigit((unsigned char)*s)) 351 break; 352 *d = *s; 353 } 354 } 355 356 /* we need a valid disk type name, so enforce an arbitrary if 357 * above did not yield a usable one */ 358 if (strlen(parts->l.d_typename) == 0) 359 strncpy(parts->l.d_typename, "SCSI", 360 sizeof(parts->l.d_typename)); 361 escape_capfile(disktype, parts->l.d_typename, 362 sizeof(parts->l.d_typename)); 363 364 sprintf(fname, "/tmp/disklabel.%u", getpid()); 365 f = fopen(fname, "w"); 366 if (f == NULL) 367 return false; 368 369 lp = parts->l.d_partitions; 370 scripting_fprintf(NULL, "cat <<EOF >%s\n", fname); 371 scripting_fprintf(f, "%s|NetBSD installation generated:\\\n", 372 disktype); 373 scripting_fprintf(f, "\t:nc#%d:nt#%d:ns#%d:\\\n", 374 parts->l.d_ncylinders, parts->l.d_ntracks, parts->l.d_nsectors); 375 scripting_fprintf(f, "\t:sc#%d:su#%" PRIu32 ":\\\n", 376 parts->l.d_secpercyl, lp[RAW_PART].p_offset+lp[RAW_PART].p_size); 377 scripting_fprintf(f, "\t:se#%d:\\\n", parts->l.d_secsize); 378 379 for (i = 0; i < parts->l.d_npartitions; i++) { 380 scripting_fprintf(f, "\t:p%c#%" PRIu32 ":o%c#%" PRIu32 381 ":t%c=%s:", 'a'+i, (uint32_t)lp[i].p_size, 382 'a'+i, (uint32_t)lp[i].p_offset, 'a'+i, 383 getfslabelname(lp[i].p_fstype, 0)); 384 if (lp[i].p_fstype == FS_BSDLFS || 385 lp[i].p_fstype == FS_BSDFFS) 386 scripting_fprintf (f, "b%c#%" PRIu32 ":f%c#%" PRIu32 387 ":", 'a'+i, 388 (uint32_t)(lp[i].p_fsize * 389 lp[i].p_frag), 390 'a'+i, (uint32_t)lp[i].p_fsize); 391 392 if (i < parts->l.d_npartitions - 1) 393 scripting_fprintf(f, "\\\n"); 394 else 395 scripting_fprintf(f, "\n"); 396 } 397 scripting_fprintf(NULL, "EOF\n"); 398 399 fclose(f); 400 401 /* 402 * Label a disk using an MD-specific string DISKLABEL_CMD for 403 * to invoke disklabel. 404 * if MD code does not define DISKLABEL_CMD, this is a no-op. 405 * 406 * i386 port uses "/sbin/disklabel -w -r", just like i386 407 * miniroot scripts, though this may leave a bogus incore label. 408 * 409 * Sun ports should use DISKLABEL_CMD "/sbin/disklabel -w" 410 * to get incore to ondisk inode translation for the Sun proms. 411 */ 412#ifdef DISKLABEL_CMD 413 /* disklabel the disk */ 414 rv = run_program(RUN_DISPLAY, "%s -f %s %s '%s' '%s'", 415 DISKLABEL_CMD, fname, disk, disktype, packname); 416#endif 417 418 unlink(fname); 419 420 return rv == 0; 421} 422 423static bool 424disklabel_delete_all(struct disk_partitions *arg) 425{ 426 struct disklabel_disk_partitions *parts = 427 (struct disklabel_disk_partitions*)arg; 428 daddr_t total_size = parts->l.d_partitions[RAW_PART].p_size; 429 430 memset(&parts->l.d_partitions, 0, sizeof(parts->l.d_partitions)); 431 parts->dp.num_part = 0; 432 433#if RAW_PART > 2 434 parts->l.d_partitions[RAW_PART-1].p_fstype = FS_UNUSED; 435 parts->l.d_partitions[RAW_PART-1].p_offset = parts->dp.disk_start; 436 parts->l.d_partitions[RAW_PART-1].p_size = parts->dp.disk_size; 437 parts->dp.num_part++; 438#endif 439 parts->l.d_partitions[RAW_PART].p_fstype = FS_UNUSED; 440 parts->l.d_partitions[RAW_PART].p_offset = 0; 441 parts->l.d_partitions[RAW_PART].p_size = total_size; 442 parts->dp.num_part++; 443 444 parts->l.d_npartitions = RAW_PART+1; 445 return true; 446} 447 448static bool 449disklabel_delete(struct disk_partitions *arg, part_id id, 450 const char **err_msg) 451{ 452 struct disklabel_disk_partitions *parts = 453 (struct disklabel_disk_partitions*)arg; 454 part_id ndx; 455 456 ndx = 0; 457 for (int part = 0; part < parts->l.d_npartitions; part++) { 458 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED 459 && parts->l.d_partitions[part].p_size == 0) 460 continue; 461 462 if (ndx == id) { 463 if (part == RAW_PART 464#if RAW_PART > 2 465 || part == RAW_PART-1 466#endif 467 ) { 468 if (err_msg) 469 *err_msg = msg_string( 470 MSG_part_not_deletable); 471 return false; 472 } 473 parts->l.d_partitions[part].p_size = 0; 474 parts->l.d_partitions[part].p_offset = 0; 475 parts->l.d_partitions[part].p_fstype = FS_UNUSED; 476 parts->dp.num_part--; 477 return true; 478 } 479 ndx++; 480 } 481 482 if (err_msg) 483 *err_msg = INTERNAL_ERROR; 484 return false; 485} 486 487static bool 488disklabel_delete_range(struct disk_partitions *arg, daddr_t r_start, 489 daddr_t r_size) 490{ 491 struct disklabel_disk_partitions *parts = 492 (struct disklabel_disk_partitions*)arg; 493 494 for (int part = 0; part < parts->l.d_npartitions; part++) { 495 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED 496 && parts->l.d_partitions[part].p_size == 0) 497 continue; 498 499 if (part == RAW_PART) 500 continue; 501 502 daddr_t start = parts->l.d_partitions[part].p_offset; 503 daddr_t end = start + parts->l.d_partitions[part].p_size; 504 505#if RAW_PART > 2 506 if (part == RAW_PART - 1 && start == r_start && 507 r_start + r_size == end) 508 continue; 509#endif 510 511 if ((start >= r_start && start <= r_start+r_size) || 512 (end >= r_start && end <= r_start+r_size)) { 513 if (parts->dp.num_part > 1) 514 parts->dp.num_part--; 515 parts->dp.free_space += 516 parts->l.d_partitions[part].p_size; 517 parts->l.d_partitions[part].p_fstype = FS_UNUSED; 518 parts->l.d_partitions[part].p_size = 0; 519 } 520 } 521 522 return true; 523} 524 525static void 526dl_init_types(void) 527{ 528 for (size_t i = 0; i < __arraycount(dl_types); i++) { 529 if (fstypenames[i] == NULL) 530 break; 531 dl_types[i].short_desc = 532 dl_types[i].description = getfslabelname(i, 0); 533 enum part_type pt; 534 switch (i) { 535 case FS_UNUSED: pt = PT_undef; break; 536 case FS_BSDFFS: pt = PT_root; break; 537 case FS_SWAP: pt = PT_swap; break; 538 case FS_MSDOS: pt = PT_FAT; break; 539 default: pt = PT_unknown; break; 540 } 541 dl_types[i].generic_ptype = pt; 542 } 543} 544 545static uint8_t 546dl_part_type_from_generic(const struct part_type_desc *gent) 547{ 548 549 if (dl_types[0].description == NULL) 550 dl_init_types(); 551 for (size_t i = 0; i < __arraycount(dl_types); i++) 552 if (gent == &dl_types[i]) 553 return (uint8_t)i; 554 555 for (size_t i = 0; i < dl_custom_ptype_count; i++) 556 if (gent == &dl_custom_ptypes[i].desc) 557 return dl_custom_ptypes[i].type; 558 559 return 0; 560} 561 562static size_t 563disklabel_type_count(void) 564{ 565 return __arraycount(dl_types) + dl_custom_ptype_count; 566} 567 568static const struct part_type_desc * 569disklabel_get_type(size_t ndx) 570{ 571 if (dl_types[0].description == NULL) 572 dl_init_types(); 573 574 if (ndx < __arraycount(dl_types)) 575 return &dl_types[ndx]; 576 577 ndx -= __arraycount(dl_types); 578 if (ndx >= dl_custom_ptype_count) 579 return NULL; 580 581 return &dl_custom_ptypes[ndx].desc; 582} 583 584static const struct part_type_desc * 585disklabel_find_type(uint type, bool create_if_unknown) 586{ 587 if (dl_types[0].description == NULL) 588 dl_init_types(); 589 590 if (type < __arraycount(dl_types)) 591 return &dl_types[type]; 592 593 for (size_t i = 0; i < dl_custom_ptype_count; i++) 594 if (dl_custom_ptypes[i].type == type) 595 return &dl_custom_ptypes[i].desc; 596 597 if (create_if_unknown) { 598 struct dl_custom_ptype *nt; 599 600 nt = realloc(dl_custom_ptypes, dl_custom_ptype_count+1); 601 if (nt == NULL) 602 return NULL; 603 dl_custom_ptypes = nt; 604 nt = dl_custom_ptypes + dl_custom_ptype_count; 605 dl_custom_ptype_count++; 606 memset(nt, 0, sizeof(*nt)); 607 nt->type = type; 608 snprintf(nt->short_desc, sizeof(nt->short_desc), "%u", type); 609 nt->short_desc[sizeof(nt->short_desc)-1] = 0; 610 snprintf(nt->description, sizeof(nt->description), 611 "%s (%u)", msg_string(MSG_custom_type), type); 612 nt->description[sizeof(nt->description)-1] = 0; 613 nt->desc.generic_ptype = PT_unknown; 614 nt->desc.short_desc = nt->short_desc; 615 nt->desc.description = nt->description; 616 return &nt->desc; 617 } 618 619 return NULL; 620} 621 622static const struct part_type_desc * 623disklabel_create_custom_part_type(const char *custom, const char **err_msg) 624{ 625 char *endp; 626 unsigned long fstype; 627 628 fstype = strtoul(custom, &endp, 10); 629 if (*endp != 0) { 630 if (err_msg) 631 *err_msg = msg_string(MSG_dl_type_invalid); 632 return NULL; 633 } 634 635 return disklabel_find_type(fstype, true); 636} 637 638static const struct part_type_desc * 639disklabel_get_fs_part_type(unsigned fstype, unsigned subtype) 640{ 641 return disklabel_find_type(fstype, false); 642} 643 644static const struct part_type_desc * 645disklabel_create_unknown_part_type(void) 646{ 647 return disklabel_find_type(FS_OTHER, false); 648} 649 650static const struct part_type_desc * 651disklabel_get_generic_type(enum part_type pt) 652{ 653 size_t nt; 654 655 if (dl_types[0].description == NULL) 656 dl_init_types(); 657 658 switch (pt) { 659 case PT_root: nt = FS_BSDFFS; break; 660 case PT_swap: nt = FS_SWAP; break; 661 case PT_FAT: 662 case PT_EFI_SYSTEM: 663 nt = FS_MSDOS; break; 664 default: nt = FS_UNUSED; break; 665 } 666 667 return disklabel_get_type(nt); 668} 669 670static bool 671disklabel_get_part_info(const struct disk_partitions *arg, part_id id, 672 struct disk_part_info *info) 673{ 674 const struct disklabel_disk_partitions *parts = 675 (const struct disklabel_disk_partitions*)arg; 676 part_id ndx; 677 678 if (dl_types[0].description == NULL) 679 dl_init_types(); 680 681 ndx = 0; 682 for (int part = 0; part < parts->l.d_npartitions; part++) { 683 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED 684 && parts->l.d_partitions[part].p_size == 0) 685 continue; 686 687 if (ndx == id) { 688 memset(info, 0, sizeof(*info)); 689 info->start = parts->l.d_partitions[part].p_offset; 690 info->size = parts->l.d_partitions[part].p_size; 691 info->nat_type = disklabel_find_type( 692 parts->l.d_partitions[part].p_fstype, true); 693 if (parts->last_mounted[part][0] != 0) 694 info->last_mounted = parts->last_mounted[part]; 695 info->fs_type = parts->l.d_partitions[part].p_fstype; 696 info->fs_sub_type = parts->fs_sub_type[part]; 697 if (part == RAW_PART && 698 parts->l.d_partitions[part].p_fstype == FS_UNUSED) 699 info->flags |= 700 PTI_PSCHEME_INTERNAL|PTI_RAW_PART; 701#if RAW_PART > 2 702 if (part == (RAW_PART-1) && 703 parts->l.d_partitions[part].p_fstype == FS_UNUSED) 704 info->flags |= 705 PTI_PSCHEME_INTERNAL|PTI_WHOLE_DISK; 706#endif 707 return true; 708 } 709 710 ndx++; 711 if (ndx > parts->dp.num_part || ndx > id) 712 break; 713 } 714 715 return false; 716} 717 718static bool 719disklabel_set_part_info(struct disk_partitions *arg, part_id id, 720 const struct disk_part_info *info, const char **err_msg) 721{ 722 struct disklabel_disk_partitions *parts = 723 (struct disklabel_disk_partitions*)arg; 724 part_id ndx; 725 726 if (dl_types[0].description == NULL) 727 dl_init_types(); 728 729 ndx = 0; 730 for (int part = 0; part < parts->l.d_npartitions; part++) { 731 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED 732 && parts->l.d_partitions[part].p_size == 0) 733 continue; 734 735 if (ndx == id) { 736 parts->l.d_partitions[part].p_offset = info->start; 737 parts->l.d_partitions[part].p_size = info->size; 738 parts->l.d_partitions[part].p_fstype = 739 dl_part_type_from_generic(info->nat_type); 740 if (info->last_mounted != NULL && 741 info->last_mounted != parts->last_mounted[part]) 742 strlcpy(parts->last_mounted[part], 743 info->last_mounted, 744 sizeof(parts->last_mounted[part])); 745 assert(info->fs_type == 0 || info->fs_type == 746 parts->l.d_partitions[part].p_fstype); 747 if (info->fs_sub_type != 0) 748 parts->fs_sub_type[part] = info->fs_sub_type; 749 return true; 750 } 751 752 ndx++; 753 if (ndx > parts->dp.num_part || ndx > id) 754 break; 755 } 756 757 return false; 758} 759 760static size_t 761disklabel_get_free_spaces_internal(const struct 762 disklabel_disk_partitions *parts, 763 struct disk_part_free_space *result, size_t max_num_result, 764 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore) 765{ 766 size_t cnt = 0, i; 767 daddr_t s, e, from, size, end_of_disk; 768 769 if (start < parts->dp.disk_start) 770 start = parts->dp.disk_start; 771 if (min_space_size < 1) 772 min_space_size = 1; 773 if (align > 1 && (start % align) != 0) 774 start = max(roundup(start, align), align); 775 end_of_disk = parts->dp.disk_start + parts->dp.disk_size; 776 from = start; 777 while (from < end_of_disk && cnt < max_num_result) { 778again: 779 size = parts->dp.disk_start + parts->dp.disk_size - from; 780 start = from; 781 for (i = 0; i < parts->l.d_npartitions; i++) { 782 if (i == RAW_PART) 783 continue; 784 if (parts->l.d_partitions[i].p_fstype == FS_UNUSED) 785 continue; 786 if (parts->l.d_partitions[i].p_size == 0) 787 continue; 788 789 s = parts->l.d_partitions[i].p_offset; 790 e = parts->l.d_partitions[i].p_size + s; 791 if (s == ignore) 792 continue; 793 if (e < from) 794 continue; 795 if (s <= from && e > from) { 796 if (e - 1 >= end_of_disk) 797 return cnt; 798 799 from = e + 1; 800 if (align > 1) { 801 from = max(roundup(from, align), align); 802 if (from >= end_of_disk) { 803 size = 0; 804 break; 805 } 806 } 807 goto again; 808 } 809 if (s > from && s - from < size) { 810 size = s - from; 811 } 812 } 813 if (size >= min_space_size) { 814 result->start = start; 815 result->size = size; 816 result++; 817 cnt++; 818 } 819 from += size + 1; 820 if (align > 1) 821 from = max(roundup(from, align), align); 822 } 823 824 return cnt; 825} 826 827static bool 828disklabel_can_add_partition(const struct disk_partitions *arg) 829{ 830 const struct disklabel_disk_partitions *parts = 831 (const struct disklabel_disk_partitions*)arg; 832 struct disk_part_free_space space; 833 int i; 834 835 if (dl_maxpart == 0) 836 dl_maxpart = getmaxpartitions(); 837 if (parts->dp.free_space < parts->ptn_alignment) 838 return false; 839 if (parts->dp.num_part >= dl_maxpart) 840 return false; 841 if (disklabel_get_free_spaces_internal(parts, &space, 1, 842 parts->ptn_alignment, parts->ptn_alignment, 0, -1) < 1) 843 return false; 844 845 for (i = 0; i < parts->l.d_npartitions; i++) { 846 if (i == RAW_PART) 847 continue; 848#if RAW_PART > 2 849 if (i == RAW_PART-1) 850 continue; 851#endif 852 if (parts->l.d_partitions[i].p_fstype == FS_UNUSED) 853 return true; 854 } 855 return false; 856} 857 858static bool 859disklabel_get_disk_pack_name(const struct disk_partitions *arg, 860 char *buf, size_t len) 861{ 862 const struct disklabel_disk_partitions *parts = 863 (const struct disklabel_disk_partitions*)arg; 864 865 strlcpy(buf, parts->l.d_packname, min(len, 866 sizeof(parts->l.d_packname)+1)); 867 return true; 868} 869 870static bool 871disklabel_set_disk_pack_name(struct disk_partitions *arg, const char *pack) 872{ 873 struct disklabel_disk_partitions *parts = 874 (struct disklabel_disk_partitions*)arg; 875 876 strncpy(parts->l.d_packname, pack, sizeof(parts->l.d_packname)); 877 return true; 878} 879 880static bool 881disklabel_get_part_device(const struct disk_partitions *arg, 882 part_id ptn, char *devname, size_t max_devname_len, int *part, 883 enum dev_name_usage which_name, bool with_path) 884{ 885 const struct disklabel_disk_partitions *parts = 886 (const struct disklabel_disk_partitions*)arg; 887 part_id id; 888 int part_index; 889 char pname; 890 891 if (ptn >= parts->l.d_npartitions) 892 return false; 893 894 for (id = part_index = 0; part_index < parts->l.d_npartitions; 895 part_index++) { 896 if (parts->l.d_partitions[part_index].p_fstype == FS_UNUSED && 897 parts->l.d_partitions[part_index].p_size == 0) 898 continue; 899 if (id == ptn) 900 break; 901 id++; 902 if (id > ptn) 903 return false; 904 } 905 906 if (part != 0) 907 *part = part_index; 908 909 pname = 'a'+ part_index; 910 911 switch (which_name) { 912 case parent_device_only: 913 strlcpy(devname, arg->disk, max_devname_len); 914 return true; 915 case logical_name: 916 case plain_name: 917 if (with_path) 918 snprintf(devname, max_devname_len, _PATH_DEV "%s%c", 919 arg->disk, pname); 920 else 921 snprintf(devname, max_devname_len, "%s%c", 922 arg->disk, pname); 923 return true; 924 case raw_dev_name: 925 if (with_path) 926 snprintf(devname, max_devname_len, _PATH_DEV "r%s%c", 927 arg->disk, pname); 928 else 929 snprintf(devname, max_devname_len, "r%s%c", 930 arg->disk, pname); 931 return true; 932 } 933 934 return false; 935} 936 937static part_id 938disklabel_add_partition(struct disk_partitions *arg, 939 const struct disk_part_info *info, const char **err_msg) 940{ 941 struct disklabel_disk_partitions *parts = 942 (struct disklabel_disk_partitions*)arg; 943 int i, part = -1; 944 part_id new_id; 945 struct disk_part_free_space space; 946 struct disk_part_info data = *info; 947 948 if (disklabel_get_free_spaces_internal(parts, &space, 1, 1, 1, 949 info->start, -1) < 1) { 950 if (err_msg) 951 *err_msg = msg_string(MSG_No_free_space); 952 return NO_PART; 953 } 954 if (data.size > space.size) 955 data.size = space.size; 956 daddr_t dend = data.start+data.size; 957 if (space.start > data.start) 958 data.start = space.start; 959 if (space.start + space.size < dend) 960 data.size = space.start+space.size-data.start; 961 962 if (dl_maxpart == 0) 963 dl_maxpart = getmaxpartitions(); 964 965 for (new_id = 0, i = 0; i < parts->l.d_npartitions; i++) { 966 if (parts->l.d_partitions[i].p_size > 0) 967 new_id++; 968 if (info->nat_type->generic_ptype != PT_root && 969 info->nat_type->generic_ptype != PT_swap && i < RAW_PART) 970 continue; 971 if (i == 0 && info->nat_type->generic_ptype != PT_root) 972 continue; 973 if (i == 1 && info->nat_type->generic_ptype != PT_swap) 974 continue; 975 if (i == RAW_PART) 976 continue; 977#if RAW_PART > 2 978 if (i == RAW_PART-1) 979 continue; 980#endif 981 if (parts->l.d_partitions[i].p_size > 0) 982 continue; 983 part = i; 984 break; 985 } 986 987 if (part < 0) { 988 if (parts->l.d_npartitions >= dl_maxpart) { 989 if (err_msg) 990 *err_msg = 991 msg_string(MSG_err_too_many_partitions); 992 return NO_PART; 993 } 994 995 part = parts->l.d_npartitions++; 996 } 997 parts->l.d_partitions[part].p_offset = data.start; 998 parts->l.d_partitions[part].p_size = data.size; 999 parts->l.d_partitions[part].p_fstype = 1000 dl_part_type_from_generic(info->nat_type); 1001 if (info->last_mounted && info->last_mounted[0]) 1002 strlcpy(parts->last_mounted[part], info->last_mounted, 1003 sizeof(parts->last_mounted[part])); 1004 else 1005 parts->last_mounted[part][0] = 0; 1006 parts->fs_sub_type[part] = info->fs_sub_type; 1007 parts->dp.num_part++; 1008 if (data.size <= parts->dp.free_space) 1009 parts->dp.free_space -= data.size; 1010 else 1011 parts->dp.free_space = 0; 1012 1013 return new_id; 1014} 1015 1016static part_id 1017disklabel_add_outer_partition(struct disk_partitions *arg, 1018 const struct disk_part_info *info, const char **err_msg) 1019{ 1020 struct disklabel_disk_partitions *parts = 1021 (struct disklabel_disk_partitions*)arg; 1022 int i, part = -1; 1023 part_id new_id; 1024 1025 if (dl_maxpart == 0) 1026 dl_maxpart = getmaxpartitions(); 1027 1028 for (new_id = 0, i = 0; i < parts->l.d_npartitions; i++) { 1029 if (parts->l.d_partitions[i].p_size > 0) 1030 new_id++; 1031 if (info->nat_type->generic_ptype != PT_root && 1032 info->nat_type->generic_ptype != PT_swap && i < RAW_PART) 1033 continue; 1034 if (i == 0 && info->nat_type->generic_ptype != PT_root) 1035 continue; 1036 if (i == 1 && info->nat_type->generic_ptype != PT_swap) 1037 continue; 1038 if (i == RAW_PART) 1039 continue; 1040#if RAW_PART > 2 1041 if (i == RAW_PART-1) 1042 continue; 1043#endif 1044 if (parts->l.d_partitions[i].p_size > 0) 1045 continue; 1046 part = i; 1047 break; 1048 } 1049 1050 if (part < 0) { 1051 if (parts->l.d_npartitions >= dl_maxpart) { 1052 if (err_msg) 1053 *err_msg = 1054 msg_string(MSG_err_too_many_partitions); 1055 return NO_PART; 1056 } 1057 1058 part = parts->l.d_npartitions++; 1059 } 1060 parts->l.d_partitions[part].p_offset = info->start; 1061 parts->l.d_partitions[part].p_size = info->size; 1062 parts->l.d_partitions[part].p_fstype = 1063 dl_part_type_from_generic(info->nat_type); 1064 if (info->last_mounted && info->last_mounted[0]) 1065 strlcpy(parts->last_mounted[part], info->last_mounted, 1066 sizeof(parts->last_mounted[part])); 1067 else 1068 parts->last_mounted[part][0] = 0; 1069 parts->fs_sub_type[part] = info->fs_sub_type; 1070 parts->dp.num_part++; 1071 1072 return new_id; 1073} 1074 1075static size_t 1076disklabel_get_free_spaces(const struct disk_partitions *arg, 1077 struct disk_part_free_space *result, size_t max_num_result, 1078 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore) 1079{ 1080 const struct disklabel_disk_partitions *parts = 1081 (const struct disklabel_disk_partitions*)arg; 1082 1083 return disklabel_get_free_spaces_internal(parts, result, 1084 max_num_result, min_space_size, align, start, ignore); 1085} 1086 1087static daddr_t 1088disklabel_max_free_space_at(const struct disk_partitions *arg, daddr_t start) 1089{ 1090 const struct disklabel_disk_partitions *parts = 1091 (const struct disklabel_disk_partitions*)arg; 1092 struct disk_part_free_space space; 1093 1094 if (disklabel_get_free_spaces_internal(parts, &space, 1, 1, 0, 1095 start, start) == 1) 1096 return space.size; 1097 1098 return 0; 1099} 1100 1101static daddr_t 1102disklabel_get_alignment(const struct disk_partitions *arg) 1103{ 1104 const struct disklabel_disk_partitions *parts = 1105 (const struct disklabel_disk_partitions*)arg; 1106 1107 return parts->ptn_alignment; 1108} 1109 1110static part_id 1111disklabel_find_by_name(struct disk_partitions *arg, const char *name) 1112{ 1113 const struct disklabel_disk_partitions *parts = 1114 (const struct disklabel_disk_partitions*)arg; 1115 char *sl, part; 1116 ptrdiff_t n; 1117 part_id pno, id, i; 1118 1119 sl = strrchr(name, '/'); 1120 if (sl == NULL) 1121 return NO_PART; 1122 n = sl - name; 1123 if (strncmp(name, parts->l.d_packname, n) != 0) 1124 return NO_PART; 1125 part = name[n+1]; 1126 if (part < 'a') 1127 return NO_PART; 1128 pno = part - 'a'; 1129 if (pno >= parts->l.d_npartitions) 1130 return NO_PART; 1131 if (parts->l.d_partitions[pno].p_fstype == FS_UNUSED) 1132 return NO_PART; 1133 for (id = 0, i = 0; i < pno; i++) 1134 if (parts->l.d_partitions[i].p_fstype != FS_UNUSED || 1135 parts->l.d_partitions[i].p_size != 0) 1136 id++; 1137 return id; 1138} 1139 1140static void 1141disklabel_free(struct disk_partitions *arg) 1142{ 1143 1144 assert(arg != NULL); 1145 free(__UNCONST(arg->disk)); 1146 free(arg); 1147} 1148 1149const struct disk_partitioning_scheme 1150disklabel_parts = { 1151 .name = MSG_parttype_disklabel, 1152 .short_name = MSG_parttype_disklabel_short, 1153 .new_type_prompt = MSG_dl_get_custom_fstype, 1154 .size_limit = (daddr_t)UINT32_MAX, 1155 .write_to_disk = disklabel_write_to_disk, 1156 .read_from_disk = disklabel_parts_read, 1157 .create_new_for_disk = disklabel_parts_new, 1158 .change_disk_geom = disklabel_change_geom, 1159 .find_by_name = disklabel_find_by_name, 1160 .get_disk_pack_name = disklabel_get_disk_pack_name, 1161 .set_disk_pack_name = disklabel_set_disk_pack_name, 1162 .delete_all_partitions = disklabel_delete_all, 1163 .delete_partitions_in_range = disklabel_delete_range, 1164 .delete_partition = disklabel_delete, 1165 .get_part_types_count = disklabel_type_count, 1166 .get_part_type = disklabel_get_type, 1167 .get_generic_part_type = disklabel_get_generic_type, 1168 .get_fs_part_type = disklabel_get_fs_part_type, 1169 .create_custom_part_type = disklabel_create_custom_part_type, 1170 .create_unknown_part_type = disklabel_create_unknown_part_type, 1171 .get_part_alignment = disklabel_get_alignment, 1172 .adapt_foreign_part_info = generic_adapt_foreign_part_info, 1173 .get_part_info = disklabel_get_part_info, 1174 .can_add_partition = disklabel_can_add_partition, 1175 .set_part_info = disklabel_set_part_info, 1176 .add_partition = disklabel_add_partition, 1177 .add_outer_partition = disklabel_add_outer_partition, 1178 .max_free_space_at = disklabel_max_free_space_at, 1179 .get_free_spaces = disklabel_get_free_spaces, 1180 .get_part_device = disklabel_get_part_device, 1181 .free = disklabel_free, 1182}; 1183