disklabel.c revision 1.3
1/* $NetBSD: disklabel.c,v 1.3 2019/06/20 16:57:25 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#if defined(__sun__) || defined(__sparc__) 79 /* sun labels are always cylinder aligned */ 80 parts->ptn_alignment = track; 81#else 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#endif 91} 92 93static bool 94disklabel_change_geom(struct disk_partitions *arg, int ncyl, int nhead, 95 int nsec) 96{ 97 struct disklabel_disk_partitions *parts = 98 (struct disklabel_disk_partitions*)arg; 99 100 disklabel_init_default_alignment(parts, nhead * nsec); 101 102 parts->l.d_ncylinders = ncyl; 103 parts->l.d_ntracks = nhead; 104 parts->l.d_nsectors = nsec; 105 parts->l.d_secsize = DEV_BSIZE; 106 parts->l.d_secpercyl = nsec * nhead; 107 108 if (ncyl*nhead*nsec <= TINY_DISK_SIZE) 109 set_default_sizemult(1); 110 else 111 set_default_sizemult(MEG/512); 112 return true; 113} 114 115static struct disk_partitions * 116disklabel_parts_new(const char *dev, daddr_t start, daddr_t len, 117 daddr_t total_size, bool is_boot_drive) 118{ 119 struct disklabel_disk_partitions *parts; 120 struct disk_geom geo; 121 122 if (!get_disk_geom(dev, &geo)) 123 return NULL; 124 125 parts = calloc(1, sizeof(*parts)); 126 if (parts == NULL) 127 return NULL; 128 129 if (len > disklabel_parts.size_limit) 130 len = disklabel_parts.size_limit; 131 if (total_size > disklabel_parts.size_limit) 132 total_size = disklabel_parts.size_limit; 133 134 parts->l.d_ncylinders = geo.dg_ncylinders; 135 parts->l.d_ntracks = geo.dg_ntracks; 136 parts->l.d_nsectors = geo.dg_nsectors; 137 parts->l.d_secsize = geo.dg_secsize; 138 parts->l.d_secpercyl = geo.dg_nsectors * geo.dg_ntracks; 139 140 parts->dp.pscheme = &disklabel_parts; 141 parts->dp.disk = dev; 142 parts->dp.disk_start = start; 143 parts->dp.disk_size = parts->dp.free_space = len; 144 disklabel_init_default_alignment(parts, parts->l.d_secpercyl); 145 146 strncpy(parts->l.d_packname, "fictious", sizeof parts->l.d_packname); 147 148#if RAW_PART > 2 149 parts->l.d_partitions[RAW_PART-1].p_fstype = FS_UNUSED; 150 parts->l.d_partitions[RAW_PART-1].p_offset = start; 151 parts->l.d_partitions[RAW_PART-1].p_size = len; 152 parts->dp.num_part++; 153#endif 154 parts->l.d_partitions[RAW_PART].p_fstype = FS_UNUSED; 155 parts->l.d_partitions[RAW_PART].p_offset = 0; 156 parts->l.d_partitions[RAW_PART].p_size = total_size; 157 parts->dp.num_part++; 158 159 parts->l.d_npartitions = RAW_PART+1; 160 161 return &parts->dp; 162} 163 164static struct disk_partitions * 165disklabel_parts_read(const char *disk, daddr_t start, daddr_t len) 166{ 167 int fd; 168 char diskpath[MAXPATHLEN]; 169 uint flags; 170 171 if (run_program(RUN_SILENT | RUN_ERROR_OK, 172 "disklabel -r %s", disk) != 0) 173 return NULL; 174 175 /* read partitions */ 176 177 struct disklabel_disk_partitions *parts = calloc(1, sizeof(*parts)); 178 if (parts == NULL) 179 return NULL; 180 181 fd = opendisk(disk, O_RDONLY, diskpath, sizeof(diskpath), 0); 182 if (fd == -1) { 183 free(parts); 184 return NULL; 185 } 186 187 /* 188 * We should actually try to read the label inside the start/len 189 * boundary, but for simplicity just rely on the kernel and 190 * instead verify a FS_UNUSED partition at RAW_PART-1 (if 191 * RAW_PART > 'c') is within the given limits. 192 */ 193 if (ioctl(fd, DIOCGDINFO, &parts->l) < 0) { 194 free(parts); 195 close(fd); 196 return NULL; 197 } 198#if RAW_PART > 2 199 if (parts->l.d_partitions[RAW_PART-1].p_fstype == FS_UNUSED) { 200 daddr_t dlstart = parts->l.d_partitions[RAW_PART-1].p_offset; 201 daddr_t dlend = start + 202 parts->l.d_partitions[RAW_PART-1].p_size; 203 204 if (dlstart < start && dlend > (start+len)) { 205 assert(false); 206 free(parts); 207 close(fd); 208 return NULL; 209 } 210 } 211#endif 212 213 if (len > disklabel_parts.size_limit) 214 len = disklabel_parts.size_limit; 215 parts->dp.pscheme = &disklabel_parts; 216 parts->dp.disk = disk; 217 parts->dp.disk_start = start; 218 parts->dp.disk_size = parts->dp.free_space = len; 219 disklabel_init_default_alignment(parts, 0); 220 221 for (int part = 0; part < parts->l.d_npartitions; part++) { 222 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED 223 && parts->l.d_partitions[part].p_size == 0) 224 continue; 225 226 parts->dp.num_part++; 227 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED) 228 continue; 229 230 flags = 0; 231 if (parts->l.d_partitions[part].p_fstype == FS_MSDOS) 232 flags = GLM_MAYBE_FAT32; 233 else if (parts->l.d_partitions[part].p_fstype == FS_BSDFFS) 234 flags = GLM_LIKELY_FFS; 235 if (flags != 0) { 236 uint fs_type, fs_sub_type; 237 const char *lm = get_last_mounted(fd, 238 parts->l.d_partitions[part].p_offset, 239 &fs_type, &fs_sub_type, flags); 240 if (lm != NULL && *lm != 0) { 241 strlcpy(parts->last_mounted[part], lm, 242 sizeof(parts->last_mounted[part])); 243 if (parts->l.d_partitions[part].p_fstype == 244 fs_type) 245 parts->fs_sub_type[part] = fs_sub_type; 246 } 247 } 248 249 if (parts->l.d_partitions[part].p_size > parts->dp.free_space) 250 parts->dp.free_space = 0; 251 else 252 parts->dp.free_space -= 253 parts->l.d_partitions[part].p_size; 254 } 255 close(fd); 256 257 return &parts->dp; 258} 259 260static bool 261disklabel_write_to_disk(struct disk_partitions *arg) 262{ 263 struct disklabel_disk_partitions *parts = 264 (struct disklabel_disk_partitions*)arg; 265 FILE *f; 266 char fname[PATH_MAX], packname[sizeof(parts->l.d_packname)+1]; 267 int i, rv = 0; 268 const char *disk = parts->dp.disk, *s; 269 const struct partition *lp; 270 char *d; 271 size_t n; 272 273 sprintf(fname, "/tmp/disklabel.%u", getpid()); 274 f = fopen(fname, "w"); 275 if (f == NULL) 276 return false; 277 278 /* make sure we have a 0 terminated packname */ 279 strlcpy(packname, parts->l.d_packname, sizeof packname); 280 281 /* fill typename with disk name prefix, if not already set */ 282 if (strlen(parts->l.d_typename) == 0) { 283 for (n = 0, d = parts->l.d_typename, s = disk; 284 *s && n < sizeof(parts->l.d_typename); d++, s++, n++) { 285 if (isdigit((unsigned char)*s)) 286 break; 287 *d = *s; 288 } 289 } 290 parts->l.d_typename[sizeof(parts->l.d_typename)-1] = 0; 291 292 /* we need a valid disk type name, so enforce an arbitrary if 293 * above did not yield a usable one */ 294 if (strlen(parts->l.d_typename) == 0) 295 strncpy(parts->l.d_typename, "SCSI", 296 sizeof(parts->l.d_typename)); 297 298 lp = parts->l.d_partitions; 299 fprintf(f, "%s|NetBSD installation generated:\\\n", 300 parts->l.d_typename); 301 fprintf(f, "\t:nc#%d:nt#%d:ns#%d:\\\n", 302 parts->l.d_ncylinders, parts->l.d_ntracks, parts->l.d_nsectors); 303 fprintf(f, "\t:sc#%d:su#%" PRIu32 ":\\\n", 304 parts->l.d_secpercyl, lp[RAW_PART].p_offset+lp[RAW_PART].p_size); 305 fprintf(f, "\t:se#%d:\\\n", parts->l.d_secsize); 306 307 for (i = 0; i < parts->l.d_npartitions; i++) { 308 fprintf(f, "\t:p%c#%" PRIu32 ":o%c#%" PRIu32 309 ":t%c=%s:", 'a'+i, (uint32_t)lp[i].p_size, 310 'a'+i, (uint32_t)lp[i].p_offset, 'a'+i, 311 getfslabelname(lp[i].p_fstype, 0)); 312 if (lp[i].p_fstype == FS_BSDLFS || 313 lp[i].p_fstype == FS_BSDFFS) 314 fprintf (f, "b%c#%" PRIu32 ":f%c#%" PRIu32 315 ":", 'a'+i, 316 (uint32_t)(lp[i].p_fsize * 317 lp[i].p_frag), 318 'a'+i, (uint32_t)lp[i].p_fsize); 319 320 if (i < parts->l.d_npartitions - 1) 321 fprintf(f, "\\\n"); 322 else 323 fprintf(f, "\n"); 324 } 325 326 fclose(f); 327 328 /* 329 * Label a disk using an MD-specific string DISKLABEL_CMD for 330 * to invoke disklabel. 331 * if MD code does not define DISKLABEL_CMD, this is a no-op. 332 * 333 * i386 port uses "/sbin/disklabel -w -r", just like i386 334 * miniroot scripts, though this may leave a bogus incore label. 335 * 336 * Sun ports should use DISKLABEL_CMD "/sbin/disklabel -w" 337 * to get incore to ondisk inode translation for the Sun proms. 338 */ 339#ifdef DISKLABEL_CMD 340 /* disklabel the disk */ 341 rv = run_program(RUN_DISPLAY, "%s -f %s %s %s %s", 342 DISKLABEL_CMD, fname, disk, parts->l.d_typename, packname); 343#endif 344 345 unlink(fname); 346 347 return rv == 0; 348} 349 350static bool 351disklabel_delete_all(struct disk_partitions *arg) 352{ 353 struct disklabel_disk_partitions *parts = 354 (struct disklabel_disk_partitions*)arg; 355 daddr_t total_size = parts->l.d_partitions[RAW_PART].p_size; 356 357 memset(&parts->l, 0, sizeof(parts->l)); 358 parts->dp.num_part = 0; 359 360#if RAW_PART > 2 361 parts->l.d_partitions[RAW_PART-1].p_fstype = FS_UNUSED; 362 parts->l.d_partitions[RAW_PART-1].p_offset = parts->dp.disk_start; 363 parts->l.d_partitions[RAW_PART-1].p_size = parts->dp.disk_size; 364 parts->dp.num_part++; 365#endif 366 parts->l.d_partitions[RAW_PART].p_fstype = FS_UNUSED; 367 parts->l.d_partitions[RAW_PART].p_offset = 0; 368 parts->l.d_partitions[RAW_PART].p_size = total_size; 369 parts->dp.num_part++; 370 371 parts->l.d_npartitions = RAW_PART+1; 372 return true; 373} 374 375static bool 376disklabel_delete(struct disk_partitions *arg, part_id id, 377 const char **err_msg) 378{ 379 struct disklabel_disk_partitions *parts = 380 (struct disklabel_disk_partitions*)arg; 381 part_id ndx; 382 383 ndx = 0; 384 for (int part = 0; part < parts->l.d_npartitions; part++) { 385 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED 386 && parts->l.d_partitions[part].p_size == 0) 387 continue; 388 389 if (ndx == id) { 390 if (part == RAW_PART 391#if RAW_PART > 2 392 || part == RAW_PART-1 393#endif 394 ) { 395 if (err_msg) 396 *err_msg = msg_string( 397 MSG_part_not_deletable); 398 return false; 399 } 400 parts->l.d_partitions[part].p_size = 0; 401 parts->l.d_partitions[part].p_offset = 0; 402 parts->l.d_partitions[part].p_fstype = FS_UNUSED; 403 parts->dp.num_part--; 404 return true; 405 } 406 ndx++; 407 } 408 409 if (err_msg) 410 *err_msg = INTERNAL_ERROR; 411 return false; 412} 413 414static bool 415disklabel_delete_range(struct disk_partitions *arg, daddr_t r_start, 416 daddr_t r_size) 417{ 418 struct disklabel_disk_partitions *parts = 419 (struct disklabel_disk_partitions*)arg; 420 421 for (int part = 0; part < parts->l.d_npartitions; part++) { 422 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED 423 && parts->l.d_partitions[part].p_size == 0) 424 continue; 425 426 if (part == RAW_PART) 427 continue; 428 429 daddr_t start = parts->l.d_partitions[part].p_offset; 430 daddr_t end = start + parts->l.d_partitions[part].p_size; 431 432#if RAW_PART > 2 433 if (part == RAW_PART - 1 && start == r_start && 434 r_start + r_size == end) 435 continue; 436#endif 437 438 if ((start >= r_start && start <= r_start+r_size) || 439 (end >= r_start && end <= r_start+r_size)) { 440 if (parts->dp.num_part > 1) 441 parts->dp.num_part--; 442 parts->dp.free_space += 443 parts->l.d_partitions[part].p_size; 444 parts->l.d_partitions[part].p_fstype = FS_UNUSED; 445 parts->l.d_partitions[part].p_size = 0; 446 } 447 } 448 449 return true; 450} 451 452static void 453dl_init_types(void) 454{ 455 for (size_t i = 0; i < __arraycount(dl_types); i++) { 456 if (fstypenames[i] == NULL) 457 break; 458 dl_types[i].short_desc = 459 dl_types[i].description = getfslabelname(i, 0); 460 enum part_type pt; 461 switch (i) { 462 case FS_UNUSED: pt = PT_undef; break; 463 case FS_BSDFFS: pt = PT_root; break; 464 case FS_SWAP: pt = PT_swap; break; 465 case FS_MSDOS: pt = PT_FAT; break; 466 default: pt = PT_unknown; break; 467 } 468 dl_types[i].generic_ptype = pt; 469 } 470} 471 472static uint8_t 473dl_part_type_from_generic(const struct part_type_desc *gent) 474{ 475 476 if (dl_types[0].description == NULL) 477 dl_init_types(); 478 for (size_t i = 0; i < __arraycount(dl_types); i++) 479 if (gent == &dl_types[i]) 480 return (uint8_t)i; 481 482 for (size_t i = 0; i < dl_custom_ptype_count; i++) 483 if (gent == &dl_custom_ptypes[i].desc) 484 return dl_custom_ptypes[i].type; 485 486 return 0; 487} 488 489static size_t 490disklabel_type_count(void) 491{ 492 return __arraycount(dl_types) + dl_custom_ptype_count; 493} 494 495static const struct part_type_desc * 496disklabel_get_type(size_t ndx) 497{ 498 if (dl_types[0].description == NULL) 499 dl_init_types(); 500 501 if (ndx < __arraycount(dl_types)) 502 return &dl_types[ndx]; 503 504 ndx -= __arraycount(dl_types); 505 if (ndx >= dl_custom_ptype_count) 506 return NULL; 507 508 return &dl_custom_ptypes[ndx].desc; 509} 510 511static const struct part_type_desc * 512disklabel_find_type(uint type, bool create_if_unknown) 513{ 514 if (dl_types[0].description == NULL) 515 dl_init_types(); 516 517 if (type < __arraycount(dl_types)) 518 return &dl_types[type]; 519 520 for (size_t i = 0; i < dl_custom_ptype_count; i++) 521 if (dl_custom_ptypes[i].type == type) 522 return &dl_custom_ptypes[i].desc; 523 524 if (create_if_unknown) { 525 struct dl_custom_ptype *nt; 526 527 nt = realloc(dl_custom_ptypes, dl_custom_ptype_count+1); 528 if (nt == NULL) 529 return NULL; 530 dl_custom_ptypes = nt; 531 nt = dl_custom_ptypes + dl_custom_ptype_count; 532 dl_custom_ptype_count++; 533 memset(nt, 0, sizeof(*nt)); 534 nt->type = type; 535 snprintf(nt->short_desc, sizeof(nt->short_desc), "%u", type); 536 nt->short_desc[sizeof(nt->short_desc)-1] = 0; 537 snprintf(nt->description, sizeof(nt->description), 538 "%s (%u)", msg_string(MSG_custom_type), type); 539 nt->description[sizeof(nt->description)-1] = 0; 540 nt->desc.generic_ptype = PT_unknown; 541 nt->desc.short_desc = nt->short_desc; 542 nt->desc.description = nt->description; 543 return &nt->desc; 544 } 545 546 return NULL; 547} 548 549static const struct part_type_desc * 550disklabel_create_custom_part_type(const char *custom, const char **err_msg) 551{ 552 char *endp; 553 unsigned long fstype; 554 555 fstype = strtoul(custom, &endp, 10); 556 if (*endp != 0) { 557 if (err_msg) 558 *err_msg = msg_string(MSG_dl_type_invalid); 559 return NULL; 560 } 561 562 return disklabel_find_type(fstype, true); 563} 564 565static const struct part_type_desc * 566disklabel_get_fs_part_type(unsigned fstype, unsigned subtype) 567{ 568 return disklabel_find_type(fstype, false); 569} 570 571static const struct part_type_desc * 572disklabel_get_generic_type(enum part_type pt) 573{ 574 size_t nt; 575 576 if (dl_types[0].description == NULL) 577 dl_init_types(); 578 579 switch (pt) { 580 case PT_root: nt = FS_BSDFFS; break; 581 case PT_swap: nt = FS_SWAP; break; 582 case PT_FAT: 583 case PT_EFI_SYSTEM: 584 nt = FS_MSDOS; break; 585 default: nt = FS_UNUSED; break; 586 } 587 588 return disklabel_get_type(nt); 589} 590 591static bool 592disklabel_get_part_info(const struct disk_partitions *arg, part_id id, 593 struct disk_part_info *info) 594{ 595 const struct disklabel_disk_partitions *parts = 596 (const struct disklabel_disk_partitions*)arg; 597 part_id ndx; 598 599 if (dl_types[0].description == NULL) 600 dl_init_types(); 601 602 ndx = 0; 603 for (int part = 0; part < parts->l.d_npartitions; part++) { 604 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED 605 && parts->l.d_partitions[part].p_size == 0) 606 continue; 607 608 if (ndx == id) { 609 memset(info, 0, sizeof(*info)); 610 info->start = parts->l.d_partitions[part].p_offset; 611 info->size = parts->l.d_partitions[part].p_size; 612 info->nat_type = disklabel_find_type( 613 parts->l.d_partitions[part].p_fstype, true); 614 if (parts->last_mounted[part][0] != 0) 615 info->last_mounted = parts->last_mounted[part]; 616 info->fs_type = parts->l.d_partitions[part].p_fstype; 617 info->fs_sub_type = parts->fs_sub_type[part]; 618 if (part == RAW_PART && 619 parts->l.d_partitions[part].p_fstype == FS_UNUSED) 620 info->flags |= 621 PTI_PSCHEME_INTERNAL|PTI_RAW_PART; 622#if RAW_PART > 2 623 if (part == (RAW_PART-1) && 624 parts->l.d_partitions[part].p_fstype == FS_UNUSED) 625 info->flags |= 626 PTI_PSCHEME_INTERNAL|PTI_WHOLE_DISK; 627#endif 628 return true; 629 } 630 631 ndx++; 632 if (ndx > parts->dp.num_part || ndx > id) 633 break; 634 } 635 636 return false; 637} 638 639static bool 640disklabel_set_part_info(struct disk_partitions *arg, part_id id, 641 const struct disk_part_info *info, const char **err_msg) 642{ 643 struct disklabel_disk_partitions *parts = 644 (struct disklabel_disk_partitions*)arg; 645 part_id ndx; 646 647 if (dl_types[0].description == NULL) 648 dl_init_types(); 649 650 ndx = 0; 651 for (int part = 0; part < parts->l.d_npartitions; part++) { 652 if (parts->l.d_partitions[part].p_fstype == FS_UNUSED 653 && parts->l.d_partitions[part].p_size == 0) 654 continue; 655 656 if (ndx == id) { 657 parts->l.d_partitions[part].p_offset = info->start; 658 parts->l.d_partitions[part].p_size = info->size; 659 parts->l.d_partitions[part].p_fstype = 660 dl_part_type_from_generic(info->nat_type); 661 if (info->last_mounted != NULL && 662 info->last_mounted != parts->last_mounted[part]) 663 strlcpy(parts->last_mounted[part], 664 info->last_mounted, 665 sizeof(parts->last_mounted[part])); 666 assert(info->fs_type == 0 || info->fs_type == 667 parts->l.d_partitions[part].p_fstype); 668 if (info->fs_sub_type != 0) 669 parts->fs_sub_type[part] = info->fs_sub_type; 670 return true; 671 } 672 673 ndx++; 674 if (ndx > parts->dp.num_part || ndx > id) 675 break; 676 } 677 678 return false; 679} 680 681static size_t 682disklabel_get_free_spaces_internal(const struct 683 disklabel_disk_partitions *parts, 684 struct disk_part_free_space *result, size_t max_num_result, 685 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore) 686{ 687 size_t cnt = 0, i; 688 daddr_t s, e, from, size, end_of_disk; 689 690 if (start < parts->dp.disk_start) 691 start = parts->dp.disk_start; 692 if (min_space_size < 1) 693 min_space_size = 1; 694 if (align > 1 && (start % align) != 0) 695 start = max(roundup(start, align), align); 696 end_of_disk = parts->dp.disk_start + parts->dp.disk_size; 697 from = start; 698 while (from < end_of_disk && cnt < max_num_result) { 699again: 700 size = parts->dp.disk_start + parts->dp.disk_size - from; 701 start = from; 702 for (i = 0; i < parts->l.d_npartitions; i++) { 703 if (i == RAW_PART) 704 continue; 705 if (parts->l.d_partitions[i].p_fstype == FS_UNUSED) 706 continue; 707 708 s = parts->l.d_partitions[i].p_offset; 709 e = parts->l.d_partitions[i].p_size + s; 710 if (s == ignore) 711 continue; 712 if (e < from) 713 continue; 714 if (s <= from && e > from) { 715 if (e - 1 >= end_of_disk) 716 return cnt; 717 718 from = e + 1; 719 if (align > 1) { 720 from = max(roundup(from, align), align); 721 if (from >= end_of_disk) { 722 size = 0; 723 break; 724 } 725 } 726 goto again; 727 } 728 if (s > from && s - from < size) { 729 size = s - from; 730 } 731 } 732 if (size >= min_space_size) { 733 result->start = start; 734 result->size = size; 735 result++; 736 cnt++; 737 } 738 from += size + 1; 739 if (align > 1) 740 from = max(roundup(from, align), align); 741 } 742 743 return cnt; 744} 745 746static bool 747disklabel_can_add_partition(const struct disk_partitions *arg) 748{ 749 const struct disklabel_disk_partitions *parts = 750 (const struct disklabel_disk_partitions*)arg; 751 struct disk_part_free_space space; 752 int i; 753 754 if (dl_maxpart == 0) 755 dl_maxpart = getmaxpartitions(); 756 if (parts->dp.free_space < parts->ptn_alignment) 757 return false; 758 if (parts->dp.num_part >= dl_maxpart) 759 return false; 760 if (disklabel_get_free_spaces_internal(parts, &space, 1, 761 parts->ptn_alignment, parts->ptn_alignment, 0, -1) < 1) 762 return false; 763 764 for (i = 0; i < parts->l.d_npartitions; i++) { 765 if (i == RAW_PART) 766 continue; 767#if RAW_PART > 2 768 if (i == RAW_PART-1) 769 continue; 770#endif 771 if (parts->l.d_partitions[i].p_fstype == FS_UNUSED) 772 return true; 773 } 774 return false; 775} 776 777static bool 778disklabel_get_disk_pack_name(const struct disk_partitions *arg, 779 char *buf, size_t len) 780{ 781 const struct disklabel_disk_partitions *parts = 782 (const struct disklabel_disk_partitions*)arg; 783 784 strlcpy(buf, parts->l.d_packname, min(len, 785 sizeof(parts->l.d_packname)+1)); 786 return true; 787} 788 789static bool 790disklabel_set_disk_pack_name(struct disk_partitions *arg, const char *pack) 791{ 792 struct disklabel_disk_partitions *parts = 793 (struct disklabel_disk_partitions*)arg; 794 795 strncpy(parts->l.d_packname, pack, sizeof(parts->l.d_packname)); 796 return true; 797} 798 799static bool 800disklabel_get_part_device(const struct disk_partitions *arg, 801 part_id ptn, char *devname, size_t max_devname_len, int *part, 802 enum dev_name_usage which_name, bool with_path) 803{ 804 805 if (part != 0) 806 *part = ptn; 807 808 switch (which_name) { 809 case parent_device_only: 810 strlcpy(devname, arg->disk, max_devname_len); 811 return true; 812 case logical_name: 813 case plain_name: 814 if (with_path) 815 snprintf(devname, max_devname_len, _PATH_DEV "%s%c", 816 arg->disk, (char)ptn + 'a'); 817 else 818 snprintf(devname, max_devname_len, "%s%c", 819 arg->disk, (char)ptn + 'a'); 820 return true; 821 case raw_dev_name: 822 if (with_path) 823 snprintf(devname, max_devname_len, _PATH_DEV "r%s%c", 824 arg->disk, (char)ptn + 'a'); 825 else 826 snprintf(devname, max_devname_len, "r%s%c", 827 arg->disk, (char)ptn + 'a'); 828 return true; 829 } 830 831 return false; 832} 833 834static part_id 835disklabel_add_partition(struct disk_partitions *arg, 836 const struct disk_part_info *info, const char **err_msg) 837{ 838 struct disklabel_disk_partitions *parts = 839 (struct disklabel_disk_partitions*)arg; 840 int i, part = -1; 841 part_id new_id; 842 struct disk_part_free_space space; 843 struct disk_part_info data = *info; 844 845 if (disklabel_get_free_spaces_internal(parts, &space, 1, 1, 1, 846 info->start, -1) < 1) { 847 if (err_msg) 848 *err_msg = msg_string(MSG_No_free_space); 849 return NO_PART; 850 } 851 if (data.size > space.size) 852 data.size = space.size; 853 daddr_t dend = data.start+data.size; 854 if (space.start > data.start) 855 data.start = space.start; 856 if (space.start + space.size < dend) 857 data.size = space.start+space.size-data.start; 858 859 if (dl_maxpart == 0) 860 dl_maxpart = getmaxpartitions(); 861 862 for (new_id = 0, i = 0; i < parts->l.d_npartitions; i++) { 863 if (parts->l.d_partitions[i].p_size > 0) 864 new_id++; 865 if (info->nat_type->generic_ptype != PT_root && 866 info->nat_type->generic_ptype != PT_swap && i < RAW_PART) 867 continue; 868 if (i == 0 && info->nat_type->generic_ptype != PT_root) 869 continue; 870 if (i == 1 && info->nat_type->generic_ptype != PT_swap) 871 continue; 872 if (i == RAW_PART) 873 continue; 874#if RAW_PART > 2 875 if (i == RAW_PART-1) 876 continue; 877#endif 878 if (parts->l.d_partitions[i].p_size > 0) 879 continue; 880 part = i; 881 break; 882 } 883 884 if (part < 0) { 885 if (parts->l.d_npartitions >= dl_maxpart) { 886 if (err_msg) 887 *err_msg = 888 msg_string(MSG_err_too_many_partitions); 889 return NO_PART; 890 } 891 892 part = parts->l.d_npartitions++; 893 } 894 parts->l.d_partitions[part].p_offset = data.start; 895 parts->l.d_partitions[part].p_size = data.size; 896 parts->l.d_partitions[part].p_fstype = 897 dl_part_type_from_generic(info->nat_type); 898 if (info->last_mounted && info->last_mounted[0]) 899 strlcpy(parts->last_mounted[part], info->last_mounted, 900 sizeof(parts->last_mounted[part])); 901 else 902 parts->last_mounted[part][0] = 0; 903 parts->fs_sub_type[part] = info->fs_sub_type; 904 parts->dp.num_part++; 905 if (data.size <= parts->dp.free_space) 906 parts->dp.free_space -= data.size; 907 else 908 parts->dp.free_space = 0; 909 910 return new_id; 911} 912 913static size_t 914disklabel_get_free_spaces(const struct disk_partitions *arg, 915 struct disk_part_free_space *result, size_t max_num_result, 916 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore) 917{ 918 const struct disklabel_disk_partitions *parts = 919 (const struct disklabel_disk_partitions*)arg; 920 921 return disklabel_get_free_spaces_internal(parts, result, 922 max_num_result, min_space_size, align, start, ignore); 923} 924 925static daddr_t 926disklabel_max_free_space_at(const struct disk_partitions *arg, daddr_t start) 927{ 928 const struct disklabel_disk_partitions *parts = 929 (const struct disklabel_disk_partitions*)arg; 930 struct disk_part_free_space space; 931 932 if (disklabel_get_free_spaces_internal(parts, &space, 1, 1, 0, 933 start, start) == 1) 934 return space.size; 935 936 return 0; 937} 938 939static daddr_t 940disklabel_get_alignment(const struct disk_partitions *arg) 941{ 942 const struct disklabel_disk_partitions *parts = 943 (const struct disklabel_disk_partitions*)arg; 944 945 return parts->ptn_alignment; 946} 947 948static void 949disklabel_free(struct disk_partitions *arg) 950{ 951 952 assert(arg != NULL); 953 free(arg); 954} 955 956const struct disk_partitioning_scheme 957disklabel_parts = { 958 .name = MSG_parttype_disklabel, 959 .short_name = MSG_parttype_disklabel_short, 960 .new_type_prompt = MSG_dl_get_custom_fstype, 961 .size_limit = (daddr_t)UINT32_MAX, 962 .write_to_disk = disklabel_write_to_disk, 963 .read_from_disk = disklabel_parts_read, 964 .create_new_for_disk = disklabel_parts_new, 965 .change_disk_geom = disklabel_change_geom, 966 .get_disk_pack_name = disklabel_get_disk_pack_name, 967 .set_disk_pack_name = disklabel_set_disk_pack_name, 968 .delete_all_partitions = disklabel_delete_all, 969 .delete_partitions_in_range = disklabel_delete_range, 970 .delete_partition = disklabel_delete, 971 .get_part_types_count = disklabel_type_count, 972 .get_part_type = disklabel_get_type, 973 .get_generic_part_type = disklabel_get_generic_type, 974 .get_fs_part_type = disklabel_get_fs_part_type, 975 .create_custom_part_type = disklabel_create_custom_part_type, 976 .get_part_alignment = disklabel_get_alignment, 977 .get_part_info = disklabel_get_part_info, 978 .can_add_partition = disklabel_can_add_partition, 979 .set_part_info = disklabel_set_part_info, 980 .add_partition = disklabel_add_partition, 981 .max_free_space_at = disklabel_max_free_space_at, 982 .get_free_spaces = disklabel_get_free_spaces, 983 .get_part_device = disklabel_get_part_device, 984 .free = disklabel_free, 985}; 986