From 32a71e42b5b15681007be8f91ac790b320b74d43 Mon Sep 17 00:00:00 2001 From: Toomas Soome Date: Mon, 18 Nov 2019 12:22:56 +0200 Subject: [PATCH] 11993 format: reserved partition size calculation should use sector size Reviewed by: Robert Mustacchi Approved by: Dan McDonald --- usr/src/cmd/format/auto_sense.c | 24 ++++---- usr/src/cmd/format/io.c | 6 +- usr/src/cmd/format/label.c | 79 +++++++++++---------------- usr/src/cmd/format/menu_command.c | 6 +- usr/src/cmd/format/menu_partition.c | 11 ++-- usr/src/cmd/format/modify_partition.c | 74 ++++++++++++------------- 6 files changed, 97 insertions(+), 103 deletions(-) diff --git a/usr/src/cmd/format/auto_sense.c b/usr/src/cmd/format/auto_sense.c index 6d51a72414..eefb43809e 100644 --- a/usr/src/cmd/format/auto_sense.c +++ b/usr/src/cmd/format/auto_sense.c @@ -226,7 +226,7 @@ static char *strcopy( int n); static int adjust_disk_geometry(diskaddr_t capacity, uint_t *cyl, uint_t *nsect, uint_t *nhead); -static void compute_chs_values(diskaddr_t total_capacity, +static void compute_chs_values(diskaddr_t total_capacity, diskaddr_t usable_capacity, uint_t *pcylp, uint_t *nheadp, uint_t *nsectp); #if defined(_SUNOS_VTOC_8) @@ -254,6 +254,7 @@ auto_efi_sense(int fd, struct efi_info *label) struct ctlr_info *ctlr; struct dk_cinfo dkinfo; struct partition_info *part; + uint64_t reserved; if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) { if (option_msg && diag_msg) { @@ -286,6 +287,7 @@ auto_efi_sense(int fd, struct efi_info *label) } label->e_parts = vtoc; + reserved = efi_reserved_sectors(vtoc); /* * Create a whole hog EFI partition table: @@ -295,7 +297,7 @@ auto_efi_sense(int fd, struct efi_info *label) vtoc->efi_parts[0].p_tag = V_USR; vtoc->efi_parts[0].p_start = vtoc->efi_first_u_lba; vtoc->efi_parts[0].p_size = vtoc->efi_last_u_lba - vtoc->efi_first_u_lba - - EFI_MIN_RESV_SIZE + 1; + - reserved + 1; /* * S1-S6 are unassigned slices. @@ -311,8 +313,8 @@ auto_efi_sense(int fd, struct efi_info *label) */ vtoc->efi_parts[vtoc->efi_nparts - 1].p_tag = V_RESERVED; vtoc->efi_parts[vtoc->efi_nparts - 1].p_start = - vtoc->efi_last_u_lba - EFI_MIN_RESV_SIZE + 1; - vtoc->efi_parts[vtoc->efi_nparts - 1].p_size = EFI_MIN_RESV_SIZE; + vtoc->efi_last_u_lba - reserved + 1; + vtoc->efi_parts[vtoc->efi_nparts - 1].p_size = reserved; /* * Now stick all of it into the disk_type struct @@ -462,7 +464,7 @@ auto_label_init(struct dk_label *label) efi_gpt_t *databack = NULL; struct dk_geom disk_geom; struct dk_minfo disk_info; - efi_gpt_t *backsigp; + efi_gpt_t *backsigp; int fd = cur_file; int rval = -1; int efisize = EFI_LABEL_SIZE * 2; @@ -2190,7 +2192,7 @@ square_box( } if (((*dim1) > lim1) || ((*dim2) > lim2) || ((*dim3) > lim3)) { - double d[4]; + double d[4]; /* * Second: @@ -2269,15 +2271,15 @@ compute_chs_values(diskaddr_t total_capacity, diskaddr_t usable_capacity, * The following table (in order) illustrates some end result * calculations: * - * Maximum number of blocks nhead nsect + * Maximum number of blocks nhead nsect * * 2097152 (1GB) 64 32 * 16777216 (8GB) 128 32 - * 1052819775 (502.02GB) 255 63 + * 1052819775 (502.02GB) 255 63 * 2105639550 (0.98TB) 255 126 - * 3158459325 (1.47TB) 255 189 - * 4211279100 (1.96TB) 255 252 - * 5264098875 (2.45TB) 255 315 + * 3158459325 (1.47TB) 255 189 + * 4211279100 (1.96TB) 255 252 + * 5264098875 (2.45TB) 255 315 * ... */ diff --git a/usr/src/cmd/format/io.c b/usr/src/cmd/format/io.c index 7fd8baf6bc..d0953a59d6 100644 --- a/usr/src/cmd/format/io.c +++ b/usr/src/cmd/format/io.c @@ -38,6 +38,7 @@ #include #include #include +#include #include "startup.h" #include "misc.h" @@ -1556,7 +1557,10 @@ or g(gigabytes)\n"); * either blocks/cyls/megabytes - a convenient fiction. */ if (strcmp(cleantoken, WILD_STRING) == 0) { - return (bounds->upper - EFI_MIN_RESV_SIZE - + uint64_t reserved; + + reserved = efi_reserved_sectors(cur_parts->etoc); + return (bounds->upper - reserved - efi_deflt->start_sector); } diff --git a/usr/src/cmd/format/label.c b/usr/src/cmd/format/label.c index f4fde20d68..ac0bbd066a 100644 --- a/usr/src/cmd/format/label.c +++ b/usr/src/cmd/format/label.c @@ -55,40 +55,22 @@ #define WD_NODE 7 #endif -#ifdef __STDC__ -/* - * Prototypes for ANSI C compilers - */ static int do_geometry_sanity_check(void); -static int vtoc_to_label(struct dk_label *label, struct extvtoc *vtoc, - struct dk_geom *geom, struct dk_cinfo *cinfo); +static int vtoc_to_label(struct dk_label *, struct extvtoc *, + struct dk_geom *, struct dk_cinfo *); extern int read_extvtoc(int, struct extvtoc *); extern int write_extvtoc(int, struct extvtoc *); static int vtoc64_to_label(struct efi_info *, struct dk_gpt *); -#else /* __STDC__ */ - -/* - * Prototypes for non-ANSI C compilers - */ -static int do_geometry_sanity_check(); -static int vtoc_to_label(); -extern int read_extvtoc(); -extern int write_extvtoc(); -static int vtoc64_to_label(); - -#endif /* __STDC__ */ - #ifdef DEBUG -static void dump_label(struct dk_label *label); +static void dump_label(struct dk_label *); #endif /* * This routine checks the given label to see if it is valid. */ int -checklabel(label) - register struct dk_label *label; +checklabel(struct dk_label *label) { /* @@ -109,12 +91,10 @@ checklabel(label) * the mode it is called in. */ int -checksum(label, mode) - struct dk_label *label; - int mode; +checksum(struct dk_label *label, int mode) { - register short *sp, sum = 0; - register short count = (sizeof (struct dk_label)) / (sizeof (short)); + short *sp, sum = 0; + short count = (sizeof (struct dk_label)) / (sizeof (short)); /* * If we are generating a checksum, don't include the checksum @@ -152,10 +132,9 @@ checksum(label, mode) * and truncate it there. */ int -trim_id(id) - char *id; +trim_id(char *id) { - register char *c; + char *c; /* * Start at the end of the string. When we match the word ' cyl', @@ -167,7 +146,8 @@ trim_id(id) * Remove any white space. */ for (; (((*(c - 1) == ' ') || (*(c - 1) == '\t')) && - (c >= id)); c--); + (c >= id)); c--) + ; break; } } @@ -222,13 +202,15 @@ int SMI_vtoc_to_EFI(int fd, struct dk_gpt **new_vtoc) { int i; - struct dk_gpt *efi; + struct dk_gpt *efi; + uint64_t reserved; if (efi_alloc_and_init(fd, EFI_NUMPAR, new_vtoc) != 0) { err_print("SMI vtoc to EFI failed\n"); return (-1); } efi = *new_vtoc; + reserved = efi_reserved_sectors(efi); /* * create a clear EFI partition table: @@ -239,7 +221,7 @@ SMI_vtoc_to_EFI(int fd, struct dk_gpt **new_vtoc) efi->efi_parts[0].p_tag = V_USR; efi->efi_parts[0].p_start = efi->efi_first_u_lba; efi->efi_parts[0].p_size = efi->efi_last_u_lba - efi->efi_first_u_lba - - EFI_MIN_RESV_SIZE + 1; + - reserved + 1; /* * s1-s6 are unassigned slices @@ -255,8 +237,8 @@ SMI_vtoc_to_EFI(int fd, struct dk_gpt **new_vtoc) */ efi->efi_parts[efi->efi_nparts - 1].p_tag = V_RESERVED; efi->efi_parts[efi->efi_nparts - 1].p_start = - efi->efi_last_u_lba - EFI_MIN_RESV_SIZE + 1; - efi->efi_parts[efi->efi_nparts - 1].p_size = EFI_MIN_RESV_SIZE; + efi->efi_last_u_lba - reserved + 1; + efi->efi_parts[efi->efi_nparts - 1].p_size = reserved; return (0); } @@ -1001,14 +983,16 @@ is_efi_type(int fd) void err_check(struct dk_gpt *vtoc) { - int resv_part = -1; - int i, j; - diskaddr_t istart, jstart, isize, jsize, endsect; - int overlap = 0; + int resv_part = -1; + int i, j; + diskaddr_t istart, jstart, isize, jsize, endsect; + int overlap = 0; + uint_t reserved; /* * make sure no partitions overlap */ + reserved = efi_reserved_sectors(vtoc); for (i = 0; i < vtoc->efi_nparts; i++) { /* It can't be unassigned and have an actual size */ if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) && @@ -1026,10 +1010,10 @@ err_check(struct dk_gpt *vtoc) "found duplicate reserved partition at %d\n", i); } resv_part = i; - if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE) + if (vtoc->efi_parts[i].p_size != reserved) (void) fprintf(stderr, -"Warning: reserved partition size must be %d sectors\n", - EFI_MIN_RESV_SIZE); +"Warning: reserved partition size must be %u sectors\n", + reserved); } if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) || (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) { @@ -1088,8 +1072,7 @@ err_check(struct dk_gpt *vtoc) #ifdef DEBUG static void -dump_label(label) - struct dk_label *label; +dump_label(struct dk_label *label) { int i; @@ -1141,8 +1124,8 @@ dump_label(label) #if defined(_SUNOS_VTOC_8) fmt_print("%c: cyl=%d, blocks=%d", i+'a', - label->dkl_map[i].dkl_cylno, - label->dkl_map[i].dkl_nblk); + label->dkl_map[i].dkl_cylno, + label->dkl_map[i].dkl_nblk); #elif defined(_SUNOS_VTOC_16) fmt_print("%c: start=%u, blocks=%u", i+'a', @@ -1153,8 +1136,8 @@ dump_label(label) #endif /* defined(_SUNOS_VTOC_8) */ fmt_print(", tag=%d, flag=%d", - label->dkl_vtoc.v_part[i].p_tag, - label->dkl_vtoc.v_part[i].p_flag); + label->dkl_vtoc.v_part[i].p_tag, + label->dkl_vtoc.v_part[i].p_flag); fmt_print("\n"); } diff --git a/usr/src/cmd/format/menu_command.c b/usr/src/cmd/format/menu_command.c index 031c94e70d..18836ecfc2 100644 --- a/usr/src/cmd/format/menu_command.c +++ b/usr/src/cmd/format/menu_command.c @@ -575,7 +575,9 @@ c_type() new_partitiontable(tptr, oldtype); } else if ((index == other_choice) && (cur_label == L_TYPE_EFI)) { uint64_t start_lba = cur_parts->etoc->efi_first_u_lba; + uint64_t reserved; + reserved = efi_reserved_sectors(cur_parts->etoc); maxLBA = get_mlba(); cur_parts->etoc->efi_last_lba = maxLBA; cur_parts->etoc->efi_last_u_lba = maxLBA - start_lba; @@ -585,8 +587,8 @@ c_type() cur_parts->etoc->efi_parts[i].p_tag = V_UNASSIGNED; } cur_parts->etoc->efi_parts[8].p_start = - maxLBA - start_lba - (1024 * 16); - cur_parts->etoc->efi_parts[8].p_size = (1024 * 16); + maxLBA - start_lba - reserved; + cur_parts->etoc->efi_parts[8].p_size = reserved; cur_parts->etoc->efi_parts[8].p_tag = V_RESERVED; if (write_label()) { err_print("Write label failed\n"); diff --git a/usr/src/cmd/format/menu_partition.c b/usr/src/cmd/format/menu_partition.c index b228821beb..444d4f585d 100644 --- a/usr/src/cmd/format/menu_partition.c +++ b/usr/src/cmd/format/menu_partition.c @@ -380,7 +380,7 @@ p_name() * for all the partitions in the current partition map. */ int -p_print() +p_print(void) { /* * check if there exists a partition table for the disk. @@ -411,13 +411,16 @@ p_print() fmt_print("Total disk cylinders available: %d + %d " "(reserved cylinders)\n\n", ncyl, acyl); } else if (cur_label == L_TYPE_EFI) { + unsigned reserved; + + reserved = efi_reserved_sectors(cur_parts->etoc); fmt_print("Current partition table (%s):\n", cur_parts->pinfo_name != NULL ? cur_parts->pinfo_name : "unnamed"); - fmt_print("Total disk sectors available: %llu + %d " + fmt_print("Total disk sectors available: %llu + %u " "(reserved sectors)\n\n", - cur_parts->etoc->efi_last_u_lba - EFI_MIN_RESV_SIZE - - cur_parts->etoc->efi_first_u_lba + 1, EFI_MIN_RESV_SIZE); + cur_parts->etoc->efi_last_u_lba - reserved - + cur_parts->etoc->efi_first_u_lba + 1, reserved); } diff --git a/usr/src/cmd/format/modify_partition.c b/usr/src/cmd/format/modify_partition.c index 23ea44a971..d01463e757 100644 --- a/usr/src/cmd/format/modify_partition.c +++ b/usr/src/cmd/format/modify_partition.c @@ -560,53 +560,53 @@ get_user_map_efi(map, float_part) char tmpstr[80]; uint64_t i64; uint64_t start_lba = map->efi_first_u_lba; + uint64_t reserved; + reserved = efi_reserved_sectors(map); for (i = 0; i < map->efi_nparts - 1; i++) { if (i == float_part) continue; - else { - ioparam.io_bounds.lower = start_lba; - ioparam.io_bounds.upper = map->efi_last_u_lba; - efi_deflt.start_sector = ioparam.io_bounds.lower; - efi_deflt.end_sector = map->efi_parts[i].p_size; - (void) sprintf(tmpstr, - "Enter size of partition %d ", i); - i64 = input(FIO_EFI, tmpstr, ':', - &ioparam, (int *)&efi_deflt, DATA_INPUT); - if (i64 == 0) { - map->efi_parts[i].p_tag = V_UNASSIGNED; - } else if ((i64 != 0) && (map->efi_parts[i].p_tag == - V_UNASSIGNED)) { - map->efi_parts[i].p_tag = V_USR; - } - if (i64 == 0) { - map->efi_parts[i].p_start = 0; - } else { - map->efi_parts[i].p_start = start_lba; - } - map->efi_parts[i].p_size = i64; - start_lba += i64; + + ioparam.io_bounds.lower = start_lba; + ioparam.io_bounds.upper = map->efi_last_u_lba; + efi_deflt.start_sector = ioparam.io_bounds.lower; + efi_deflt.end_sector = map->efi_parts[i].p_size; + (void) sprintf(tmpstr, "Enter size of partition %d ", i); + i64 = input(FIO_EFI, tmpstr, ':', + &ioparam, (int *)&efi_deflt, DATA_INPUT); + if (i64 == 0) { + map->efi_parts[i].p_tag = V_UNASSIGNED; + } else if ((i64 != 0) && (map->efi_parts[i].p_tag == + V_UNASSIGNED)) { + map->efi_parts[i].p_tag = V_USR; } - } - map->efi_parts[float_part].p_start = start_lba; - map->efi_parts[float_part].p_size = map->efi_last_u_lba - - start_lba - (1024 * 16); - map->efi_parts[float_part].p_tag = V_USR; - if (map->efi_parts[float_part].p_size == UINT_MAX64) { - map->efi_parts[float_part].p_size = 0; - map->efi_parts[float_part].p_start = 0; - map->efi_parts[float_part].p_tag = V_UNASSIGNED; - fmt_print("Warning: No space left for HOG\n"); + if (i64 == 0) { + map->efi_parts[i].p_start = 0; + } else { + map->efi_parts[i].p_start = start_lba; } + map->efi_parts[i].p_size = i64; + start_lba += i64; + } + map->efi_parts[float_part].p_start = start_lba; + map->efi_parts[float_part].p_size = map->efi_last_u_lba - + start_lba - reserved; + map->efi_parts[float_part].p_tag = V_USR; + if (map->efi_parts[float_part].p_size == UINT_MAX64) { + map->efi_parts[float_part].p_size = 0; + map->efi_parts[float_part].p_start = 0; + map->efi_parts[float_part].p_tag = V_UNASSIGNED; + fmt_print("Warning: No space left for HOG\n"); + } - for (i = 0; i < map->efi_nparts; i++) { - if (map->efi_parts[i].p_tag == V_RESERVED) { + for (i = 0; i < map->efi_nparts; i++) { + if (map->efi_parts[i].p_tag == V_RESERVED) { map->efi_parts[i].p_start = map->efi_last_u_lba - - (1024 * 16); - map->efi_parts[i].p_size = (1024 * 16); + reserved; + map->efi_parts[i].p_size = reserved; break; - } } + } } -- 2.45.2