Home | History | Annotate | Line # | Download | only in sysinst
label.c revision 1.10.2.9
      1 /*	$NetBSD: label.c,v 1.10.2.9 2022/12/31 05:03:14 snj Exp $	*/
      2 
      3 /*
      4  * Copyright 1997 Jonathan Stone
      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  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed for the NetBSD Project by
     18  *      Jonathan Stone.
     19  * 4. The name of Jonathan Stone may not be used to endorse
     20  *    or promote products derived from this software without specific prior
     21  *    written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
     24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     33  * THE POSSIBILITY OF SUCH DAMAGE.
     34  *
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #if defined(LIBC_SCCS) && !defined(lint)
     39 __RCSID("$NetBSD: label.c,v 1.10.2.9 2022/12/31 05:03:14 snj Exp $");
     40 #endif
     41 
     42 #include <sys/types.h>
     43 #include <stddef.h>
     44 #include <assert.h>
     45 #include <errno.h>
     46 #include <stdio.h>
     47 #include <fcntl.h>
     48 #include <util.h>
     49 #include <unistd.h>
     50 #include <sys/dkio.h>
     51 #include <sys/param.h>
     52 #include <sys/bootblock.h>
     53 #include <sys/bitops.h>
     54 #include <ufs/ffs/fs.h>
     55 
     56 #include "defs.h"
     57 #include "msg_defs.h"
     58 #include "menu_defs.h"
     59 
     60 /*
     61  * local prototypes
     62  */
     63 static bool boringpart(const struct disk_part_info *info);
     64 static bool checklabel(struct disk_partitions*, char *, char *);
     65 static void show_partition_adder(menudesc *, struct partition_usage_set*);
     66 
     67 /*
     68  * Return 1 if a partition should be ignored when checking
     69  * for overlapping partitions.
     70  */
     71 static bool
     72 boringpart(const struct disk_part_info *info)
     73 {
     74 
     75 	if (info->size == 0)
     76 		return true;
     77 	if (info->flags &
     78 	     (PTI_PSCHEME_INTERNAL|PTI_WHOLE_DISK|PTI_SEC_CONTAINER|
     79 	     PTI_RAW_PART))
     80 		return true;
     81 
     82 	return false;
     83 }
     84 
     85 /*
     86  * We have some partitions in our "wanted" list that we may not edit,
     87  * like the RAW_PART in disklabel, some that just represent external
     88  * mount entries for the final fstab or similar.
     89  * We have previously sorted pset->parts and pset->infos to be in sync,
     90  * but the former "array" may be shorter.
     91  * Here are a few quick predicates to check for them.
     92  */
     93 static bool
     94 real_partition(const struct partition_usage_set *pset, int index)
     95 {
     96 	if (index < 0 || (size_t)index >= pset->num)
     97 		return false;
     98 
     99 	return pset->infos[index].cur_part_id != NO_PART;
    100 }
    101 
    102 /*
    103  * Check partitioning for overlapping partitions.
    104  * Returns 0 if no overlapping partition found, nonzero otherwise.
    105  * Sets reference arguments ovly1 and ovly2 to the indices of
    106  * overlapping partitions if any are found.
    107  */
    108 static bool
    109 checklabel(struct disk_partitions *parts,
    110     char *ovl1, char *ovl2)
    111 {
    112 	part_id i, j;
    113 	struct disk_part_info info;
    114 	daddr_t istart, iend, jstart, jend;
    115 	unsigned int fs_type, fs_sub_type;
    116 
    117 	for (i = 0; i < parts->num_part - 1; i ++ ) {
    118 		if (!parts->pscheme->get_part_info(parts, i, &info))
    119 			continue;
    120 
    121 		/* skip unused or reserved partitions */
    122 		if (boringpart(&info))
    123 			continue;
    124 
    125 		/*
    126 		 * check succeeding partitions for overlap.
    127 		 * O(n^2), but n is small.
    128 		 */
    129 		istart = info.start;
    130 		iend = istart + info.size;
    131 		fs_type = info.fs_type;
    132 		fs_sub_type = info.fs_sub_type;
    133 
    134 		for (j = i+1; j < parts->num_part; j++) {
    135 
    136 			if (!parts->pscheme->get_part_info(parts, j, &info))
    137 				continue;
    138 
    139 			/* skip unused or reserved partitions */
    140 			if (boringpart(&info))
    141 				continue;
    142 
    143 			jstart = info.start;
    144 			jend = jstart + info.size;
    145 
    146 			/* overlap? */
    147 			if ((istart <= jstart && jstart < iend) ||
    148 			    (jstart <= istart && istart < jend)) {
    149 				snprintf(ovl1, MENUSTRSIZE,
    150 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
    151 				    istart / sizemult, iend / sizemult,
    152 				    multname,
    153 				    getfslabelname(fs_type, fs_sub_type));
    154 				snprintf(ovl2, MENUSTRSIZE,
    155 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
    156 				    jstart / sizemult, jend / sizemult,
    157 				    multname,
    158 				    getfslabelname(info.fs_type,
    159 				        info.fs_sub_type));
    160 				return false;
    161 			}
    162 		}
    163 	}
    164 
    165 	return true;
    166 }
    167 
    168 int
    169 checkoverlap(struct disk_partitions *parts)
    170 {
    171 	char desc1[MENUSTRSIZE], desc2[MENUSTRSIZE];
    172 	if (!checklabel(parts, desc1, desc2)) {
    173 		msg_display_subst(MSG_partitions_overlap, 2, desc1, desc2);
    174 		return 1;
    175 	}
    176 	return 0;
    177 }
    178 
    179 /*
    180  * return (see post_edit_verify):
    181  *  0 -> abort
    182  *  1 -> re-edit
    183  *  2 -> continue installation
    184  */
    185 static int
    186 verify_parts(struct partition_usage_set *pset, bool install)
    187 {
    188 	struct part_usage_info *wanted;
    189 	struct disk_partitions *parts;
    190 	size_t i, num_root;
    191 	daddr_t first_bsdstart, inst_start;
    192 	int rv;
    193 
    194 	first_bsdstart = inst_start = -1;
    195 	num_root = 0;
    196 	parts = pset->parts;
    197 	for (i = 0; i < pset->num; i++) {
    198 		wanted = &pset->infos[i];
    199 
    200 		if (wanted->flags & PUIFLG_JUST_MOUNTPOINT)
    201 			continue;
    202 		if (wanted->cur_part_id == NO_PART)
    203 			continue;
    204 		if (!(wanted->instflags & PUIINST_MOUNT))
    205 			continue;
    206 		if (strcmp(wanted->mount, "/") != 0)
    207 			continue;
    208 		num_root++;
    209 
    210 		if (first_bsdstart <= 0) {
    211 			first_bsdstart = wanted->cur_start;
    212 		}
    213 		if (inst_start < 0 &&
    214 		    (wanted->cur_flags & PTI_INSTALL_TARGET)) {
    215 			inst_start = wanted->cur_start;
    216 		}
    217 	}
    218 
    219 	if ((num_root == 0 && install) ||
    220 	    (num_root > 1 && inst_start < 0)) {
    221 		if (num_root == 0 && install)
    222 			msg_display_subst(MSG_must_be_one_root, 2,
    223 			    msg_string(parts->pscheme->name),
    224 			    msg_string(parts->pscheme->short_name));
    225 		else
    226 			msg_display_subst(MSG_multbsdpart, 2,
    227 			    msg_string(parts->pscheme->name),
    228 			    msg_string(parts->pscheme->short_name));
    229 		rv = ask_reedit(parts);
    230 		if (rv != 2)
    231 			return rv;
    232 	}
    233 
    234 	/* Check for overlaps */
    235 	if (checkoverlap(parts) != 0) {
    236 		rv = ask_reedit(parts);
    237 		if (rv != 2)
    238 			return rv;
    239 	}
    240 
    241 	/*
    242 	 * post_edit_verify returns:
    243 	 *  0 -> abort
    244 	 *  1 -> re-edit
    245 	 *  2 -> continue installation
    246 	 */
    247 	if (parts->pscheme->post_edit_verify)
    248 		return parts->pscheme->post_edit_verify(parts, false);
    249 
    250 	return 2;
    251 }
    252 
    253 static int
    254 edit_fs_start(menudesc *m, void *arg)
    255 {
    256 	struct single_part_fs_edit *edit = arg;
    257 	daddr_t start, end;
    258 
    259 	start = getpartoff(edit->pset->parts, edit->info.start);
    260 	if (edit->info.size != 0) {
    261 		if (start < (edit->info.start+edit->info.size)) {
    262 			/* Try to keep end in the same place */
    263 			end = edit->info.start + edit->info.size;
    264 			if (end < start)
    265 				edit->info.size = edit->pset->parts->pscheme->
    266 				    max_free_space_at(edit->pset->parts,
    267 				    edit->info.start);
    268 			else
    269 				edit->info.size = end - start;
    270 		} else {
    271 			edit->info.size = 0;
    272 		}
    273 	}
    274 	edit->info.start = start;
    275 	return 0;
    276 }
    277 
    278 static int
    279 edit_fs_size(menudesc *m, void *arg)
    280 {
    281 	struct single_part_fs_edit *edit = arg;
    282 	struct disk_part_info pinfo;
    283 	daddr_t size;
    284 
    285 	/* get original partition data, in case start moved already */
    286 	if (!edit->pset->parts->pscheme->get_part_info(edit->pset->parts,
    287 	    edit->id, &pinfo))
    288 		pinfo = edit->info;
    289 	/* ask for new size with old start and current values */
    290 	size = getpartsize(edit->pset->parts, pinfo.start,
    291 	    edit->info.start, edit->info.size);
    292 	if (size < 0)
    293 		return 0;
    294 	if (size > edit->pset->parts->disk_size)
    295 		size = edit->pset->parts->disk_size - edit->info.start;
    296 	edit->info.size = size;
    297 	return 0;
    298 }
    299 
    300 static int
    301 set_ffs_opt_pow2(menudesc *m, void *arg)
    302 {
    303 	struct single_part_fs_edit *edit = arg;
    304 	size_t val = 1 << (edit->offset+m->cursel);
    305 
    306 	if (edit->mode == 1) {
    307 		edit->info.fs_opt1 = val;
    308 		edit->wanted->fs_opt1 = val;
    309 	} else if (edit->mode == 2) {
    310 		edit->info.fs_opt2 = val;
    311 		edit->wanted->fs_opt2 = val;
    312 	}
    313 	return 0;
    314 }
    315 
    316 static int
    317 edit_fs_ffs_opt(menudesc *m, void *arg, msg head,
    318     size_t min_val, size_t max_val)
    319 {
    320 	struct single_part_fs_edit *edit = arg;
    321 	menu_ent opts[min(MAXPHYS/4096, 8)];
    322 	char names[min(MAXPHYS/4096, 8)][20];
    323 	size_t i, val;
    324 	int menu;
    325 
    326 	edit->offset = ilog2(min_val);
    327 	memset(opts, 0, sizeof opts);
    328 	for (i = 0, val = min_val; val <= max_val; i++, val <<= 1) {
    329 		snprintf(names[i], sizeof names[i], "%zu", val);
    330 		opts[i].opt_name = names[i];
    331 		opts[i].opt_action = set_ffs_opt_pow2;
    332 		opts[i].opt_flags = OPT_EXIT;
    333 	}
    334 	menu = new_menu(head, opts, i, 40, 6, 0, 0, MC_NOEXITOPT,
    335 	    NULL, NULL, NULL, NULL, NULL);
    336 	if (menu < 0)
    337 		return 1;
    338 	process_menu(menu, arg);
    339 	free_menu(menu);
    340 	return 0;
    341 }
    342 
    343 static int
    344 edit_fs_ffs_block(menudesc *m, void *arg)
    345 {
    346 	struct single_part_fs_edit *edit = arg;
    347 
    348 	edit->mode = 1;		/* edit fs_opt1 */
    349 	return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_block_size,
    350 	    4096, MAXPHYS);
    351 }
    352 
    353 static int
    354 edit_fs_ffs_frag(menudesc *m, void *arg)
    355 {
    356 	struct single_part_fs_edit *edit = arg;
    357 	size_t bsize, sec_size;
    358 
    359 	edit->mode = 2;		/* edit fs_opt2 */
    360 	bsize = edit->info.fs_opt1;
    361 	if (bsize == 0) {
    362 		sec_size = edit->wanted->parts->bytes_per_sector;
    363 		if (edit->wanted->size >= (daddr_t)(128L*(GIG/sec_size)))
    364 			bsize = 32*1024;
    365 		else if (edit->wanted->size >= (daddr_t)(1000L*(MEG/sec_size)))
    366 			bsize = 16*1024;
    367 		else if (edit->wanted->size >= (daddr_t)(20L*(MEG/sec_size)))
    368 			bsize = 8*1024;
    369 		else
    370 			bsize = 4+1024;
    371 	}
    372 	return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_fragment_size,
    373 		bsize / 8, bsize);
    374 }
    375 
    376 static int
    377 edit_fs_ffs_avg_size(menudesc *m, void *arg)
    378 {
    379 	struct single_part_fs_edit *edit = arg;
    380 	char answer[12];
    381 
    382 	snprintf(answer, sizeof answer, "%u", edit->info.fs_opt3);
    383 	msg_prompt_win(MSG_ptn_isize_prompt, -1, 18, 0, 0,
    384 		answer, answer, sizeof answer);
    385 	edit->info.fs_opt3 = atol(answer);
    386 	edit->wanted->fs_opt3 = edit->info.fs_opt3;
    387 
    388 	return 0;
    389 }
    390 
    391 static int
    392 edit_fs_preserve(menudesc *m, void *arg)
    393 {
    394 	struct single_part_fs_edit *edit = arg;
    395 
    396 	edit->wanted->instflags ^= PUIINST_NEWFS;
    397 	return 0;
    398 }
    399 
    400 static int
    401 edit_install(menudesc *m, void *arg)
    402 {
    403 	struct single_part_fs_edit *edit = arg;
    404 
    405 	edit->info.flags ^= PTI_INSTALL_TARGET;
    406 	return 0;
    407 }
    408 
    409 static int
    410 edit_fs_mount(menudesc *m, void *arg)
    411 {
    412 	struct single_part_fs_edit *edit = arg;
    413 
    414 	edit->wanted->instflags ^= PUIINST_MOUNT;
    415 	return 0;
    416 }
    417 
    418 static int
    419 edit_fs_mountpt(menudesc *m, void *arg)
    420 {
    421 	struct single_part_fs_edit *edit = arg;
    422 	char *p, *first, *last, buf[MOUNTLEN];
    423 
    424 	strlcpy(buf, edit->wanted->mount, sizeof buf);
    425 	msg_prompt_win(MSG_mountpoint, -1, 18, 0, 0,
    426 		buf, buf, MOUNTLEN);
    427 
    428 	/*
    429 	 * Trim all leading and trailing whitespace
    430 	 */
    431 	for (first = NULL, last = NULL, p = buf; *p; p++) {
    432 		if (isspace((unsigned char)*p))
    433 			continue;
    434 		if (first == NULL)
    435 			first = p;
    436 		last = p;
    437 	}
    438 	if (last != NULL)
    439 		last[1] = 0;
    440 
    441 	if (first == NULL || *first == 0 || strcmp(first, "none") == 0) {
    442 		edit->wanted->mount[0] = 0;
    443 		edit->wanted->instflags &= ~PUIINST_MOUNT;
    444 		return 0;
    445 	}
    446 
    447 	if (*first != '/') {
    448 		edit->wanted->mount[0] = '/';
    449 		strlcpy(&edit->wanted->mount[1], first,
    450 		    sizeof(edit->wanted->mount)-1);
    451 	} else {
    452 		strlcpy(edit->wanted->mount, first, sizeof edit->wanted->mount);
    453 	}
    454 
    455 	return 0;
    456 }
    457 
    458 static int
    459 edit_restore(menudesc *m, void *arg)
    460 {
    461 	struct single_part_fs_edit *edit = arg;
    462 
    463 	edit->info = edit->old_info;
    464 	*edit->wanted = edit->old_usage;
    465 	return 0;
    466 }
    467 
    468 static int
    469 edit_cancel(menudesc *m, void *arg)
    470 {
    471 	struct single_part_fs_edit *edit = arg;
    472 
    473 	edit->rv = -1;
    474 	return 1;
    475 }
    476 
    477 static int
    478 edit_delete_ptn(menudesc *m, void *arg)
    479 {
    480 	struct single_part_fs_edit *edit = arg;
    481 
    482 	edit->rv = -2;
    483 	return 1;
    484 }
    485 
    486 /*
    487  * We have added/removed partitions, all cur_part_id values are
    488  * out of sync. Re-fetch and reorder partitions accordingly.
    489  */
    490 static void
    491 renumber_partitions(struct partition_usage_set *pset)
    492 {
    493 	struct part_usage_info *ninfos;
    494 	struct disk_part_info info;
    495 	size_t i;
    496 	part_id pno;
    497 
    498 	ninfos = calloc(pset->parts->num_part, sizeof(*ninfos));
    499 	if (ninfos == NULL) {
    500 		err_msg_win(err_outofmem);
    501 		return;
    502 	}
    503 
    504 	for (pno = 0; pno < pset->parts->num_part; pno++) {
    505 		if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
    506 		    &info))
    507 			continue;
    508 		for (i = 0; i < pset->num; i++) {
    509 			if (pset->infos[i].cur_start != info.start)
    510 				continue;
    511 			if (pset->infos[i].cur_flags != info.flags)
    512 				continue;
    513 			if ((info.fs_type != FS_UNUSED &&
    514 			    info.fs_type == pset->infos[i].fs_type) ||
    515 			    (pset->infos[i].type ==
    516 			    info.nat_type->generic_ptype)) {
    517 				memcpy(&ninfos[pno], &pset->infos[i],
    518 				    sizeof(ninfos[pno]));
    519 				ninfos[pno].cur_part_id = pno;
    520 				break;
    521 			}
    522 		}
    523 	}
    524 
    525 	free(pset->infos);
    526 	pset->infos = ninfos;
    527 	pset->num = pset->parts->num_part;
    528 }
    529 
    530 /*
    531  * Most often used file system types, we offer them in a first level menu.
    532  */
    533 static const uint edit_fs_common_types[] =
    534     { FS_BSDFFS, FS_SWAP, FS_MSDOS, FS_BSDLFS, FS_EX2FS };
    535 
    536 /*
    537  * Functions for uncommon file system types - we offer the full list,
    538  * but put FFSv2 and FFSv1 at the front.
    539  */
    540 static void
    541 init_fs_type_ext(menudesc *menu, void *arg)
    542 {
    543 	struct single_part_fs_edit *edit = arg;
    544 	uint t = edit->info.fs_type;
    545 	size_t i, ndx, max = menu->numopts;
    546 
    547 	if (t == FS_BSDFFS) {
    548 		if (edit->info.fs_sub_type == 2)
    549 			menu->cursel = 0;
    550 		else
    551 			menu->cursel = 1;
    552 		return;
    553 	} else if (t == FS_EX2FS && edit->info.fs_sub_type == 1) {
    554 		menu->cursel = FSMAXTYPES;
    555 		return;
    556 	}
    557 	/* skip the two FFS entries, and do not add FFS later again */
    558 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
    559 		if (i == FS_UNUSED)
    560 			continue;
    561 		if (i == FS_BSDFFS)
    562 			continue;
    563 		if (fstypenames[i] == NULL)
    564 			continue;
    565 
    566 		if (i == t) {
    567 			menu->cursel = ndx;
    568 			break;
    569 		}
    570 		ndx++;
    571 	}
    572 }
    573 
    574 static int
    575 set_fstype_ext(menudesc *menu, void *arg)
    576 {
    577 	struct single_part_fs_edit *edit = arg;
    578 	size_t i, ndx, max = menu->numopts;
    579 	enum part_type pt;
    580 
    581 	if (menu->cursel == 0 || menu->cursel == 1) {
    582 		edit->info.fs_type = FS_BSDFFS;
    583 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
    584 		goto found_type;
    585 	} else if (menu->cursel == FSMAXTYPES) {
    586 		edit->info.fs_type = FS_EX2FS;
    587 		edit->info.fs_sub_type = 1;
    588 		goto found_type;
    589 	}
    590 
    591 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
    592 		if (i == FS_UNUSED)
    593 			continue;
    594 		if (i == FS_BSDFFS)
    595 			continue;
    596 		if (fstypenames[i] == NULL)
    597 			continue;
    598 
    599 		if (ndx == (size_t)menu->cursel) {
    600 			edit->info.fs_type = i;
    601 			edit->info.fs_sub_type = 0;
    602 			goto found_type;
    603 		}
    604 		ndx++;
    605 	}
    606 	return 1;
    607 
    608 found_type:
    609 	pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
    610 	edit->info.nat_type = edit->pset->parts->pscheme->
    611 	    get_fs_part_type(pt, edit->info.fs_type, edit->info.fs_sub_type);
    612 	if (edit->info.nat_type == NULL)
    613 		edit->info.nat_type = edit->pset->parts->pscheme->
    614 		    get_generic_part_type(PT_root);
    615 	edit->wanted->type = edit->info.nat_type->generic_ptype;
    616 	edit->wanted->fs_type = edit->info.fs_type;
    617 	edit->wanted->fs_version = edit->info.fs_sub_type;
    618 	return 1;
    619 }
    620 
    621 /*
    622  * Offer a menu with "exotic" file system types, start with FFSv2 and FFSv1,
    623  * skip later FFS entry in the generic list.
    624  */
    625 static int
    626 edit_fs_type_ext(menudesc *menu, void *arg)
    627 {
    628 	menu_ent *opts;
    629 	int m;
    630 	size_t i, ndx, cnt;
    631 
    632 	cnt = __arraycount(fstypenames);
    633 	opts = calloc(cnt, sizeof(*opts));
    634 	if (opts == NULL)
    635 		return 1;
    636 
    637 	ndx = 0;
    638 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2);
    639 	opts[ndx].opt_action = set_fstype_ext;
    640 	ndx++;
    641 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffs);
    642 	opts[ndx].opt_action = set_fstype_ext;
    643 	ndx++;
    644 	for (i = 0; i < FSMAXTYPES && ndx < cnt; i++) {
    645 		if (i == FS_UNUSED)
    646 			continue;
    647 		if (i == FS_BSDFFS)
    648 			continue;
    649 		if (fstypenames[i] == NULL)
    650 			continue;
    651 		opts[ndx].opt_name = fstypenames[i];
    652 		opts[ndx].opt_action = set_fstype_ext;
    653 		ndx++;
    654 	}
    655 	opts[ndx].opt_name = msg_string(MSG_fs_type_ext2old);
    656 	opts[ndx].opt_action = set_fstype_ext;
    657 	ndx++;
    658 	assert(ndx == cnt);
    659 	m = new_menu(MSG_Select_the_type, opts, ndx,
    660 		30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
    661 		init_fs_type_ext, NULL, NULL, NULL, MSG_unchanged);
    662 
    663 	if (m < 0)
    664 		return 1;
    665 	process_menu(m, arg);
    666 	free_menu(m);
    667 	free(opts);
    668 
    669 	return 1;
    670 }
    671 
    672 static void
    673 init_fs_type(menudesc *menu, void *arg)
    674 {
    675 	struct single_part_fs_edit *edit = arg;
    676 	size_t i;
    677 
    678 	/* init menu->cursel from fs type in arg */
    679 	if (edit->info.fs_type == FS_BSDFFS) {
    680 		if (edit->info.fs_sub_type == 2)
    681 			menu->cursel = 0;
    682 		else
    683 			menu->cursel = 1;
    684 	}
    685 	for (i = 1; i < __arraycount(edit_fs_common_types); i++) {
    686 		if (edit->info.fs_type == edit_fs_common_types[i]) {
    687 			menu->cursel = i+1;
    688 			break;
    689 		}
    690 	}
    691 }
    692 
    693 static int
    694 set_fstype(menudesc *menu, void *arg)
    695 {
    696 	struct single_part_fs_edit *edit = arg;
    697 	enum part_type pt;
    698 	int ndx;
    699 
    700 	pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
    701 	if (menu->cursel < 2) {
    702 		edit->info.fs_type = FS_BSDFFS;
    703 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
    704 		edit->info.nat_type = edit->pset->parts->pscheme->
    705 		    get_fs_part_type(pt, FS_BSDFFS, 2);
    706 		if (edit->info.nat_type == NULL)
    707 			edit->info.nat_type = edit->pset->parts->
    708 			    pscheme->get_generic_part_type(PT_root);
    709 		edit->wanted->type = edit->info.nat_type->generic_ptype;
    710 		edit->wanted->fs_type = edit->info.fs_type;
    711 		edit->wanted->fs_version = edit->info.fs_sub_type;
    712 		return 1;
    713 	}
    714 	ndx = menu->cursel-1;
    715 
    716 	if (ndx < 0 ||
    717 	    (size_t)ndx >= __arraycount(edit_fs_common_types))
    718 		return 1;
    719 
    720 	edit->info.fs_type = edit_fs_common_types[ndx];
    721 	edit->info.fs_sub_type = 0;
    722 	edit->info.nat_type = edit->pset->parts->pscheme->
    723 	    get_fs_part_type(pt, edit->info.fs_type, 0);
    724 	if (edit->info.nat_type == NULL)
    725 		edit->info.nat_type = edit->pset->parts->
    726 		    pscheme->get_generic_part_type(PT_root);
    727 	edit->wanted->type = edit->info.nat_type->generic_ptype;
    728 	edit->wanted->fs_type = edit->info.fs_type;
    729 	edit->wanted->fs_version = edit->info.fs_sub_type;
    730 	return 1;
    731 }
    732 
    733 /*
    734  * Offer a menu selecting the common file system types
    735  */
    736 static int
    737 edit_fs_type(menudesc *menu, void *arg)
    738 {
    739 	struct single_part_fs_edit *edit = arg;
    740 	menu_ent *opts;
    741 	int m, cnt;
    742 	size_t i;
    743 
    744 	/*
    745 	 * Shortcut to full menu if we have an exotic value
    746 	 */
    747 	if (edit->info.fs_type == FS_EX2FS && edit->info.fs_sub_type == 1) {
    748 		edit_fs_type_ext(menu, arg);
    749 		return 0;
    750 	}
    751 	for (i = 0; i < __arraycount(edit_fs_common_types); i++)
    752 		if (edit->info.fs_type == edit_fs_common_types[i])
    753 			break;
    754 	if (i >= __arraycount(edit_fs_common_types)) {
    755 		edit_fs_type_ext(menu, arg);
    756 		return 0;
    757 	}
    758 
    759 	/*
    760 	 * Starting with a common type, show short menu first
    761 	 */
    762 	cnt = __arraycount(edit_fs_common_types) + 2;
    763 	opts = calloc(cnt, sizeof(*opts));
    764 	if (opts == NULL)
    765 		return 0;
    766 
    767 	/* special case entry 0: two FFS entries */
    768 	for (i = 0; i < __arraycount(edit_fs_common_types); i++) {
    769 		opts[i+1].opt_name = getfslabelname(edit_fs_common_types[i], 0);
    770 		opts[i+1].opt_action = set_fstype;
    771 	}
    772 	/* duplicate FFS (at offset 1) into first entry */
    773 	opts[0] = opts[1];
    774 	opts[0].opt_name = msg_string(MSG_fs_type_ffsv2);
    775 	opts[1].opt_name = msg_string(MSG_fs_type_ffs);
    776 	/* add secondary sub-menu */
    777 	assert(i+1 < (size_t)cnt);
    778 	opts[i+1].opt_name = msg_string(MSG_other_fs_type);
    779 	opts[i+1].opt_action = edit_fs_type_ext;
    780 
    781 	m = new_menu(MSG_Select_the_type, opts, cnt,
    782 		30, 6, 0, 0, MC_SUBMENU | MC_SCROLL,
    783 		init_fs_type, NULL, NULL, NULL, MSG_unchanged);
    784 
    785 	if (m < 0)
    786 		return 0;
    787 	process_menu(m, arg);
    788 	free_menu(m);
    789 	free(opts);
    790 
    791 	return 0;
    792 }
    793 
    794 
    795 static void update_edit_ptn_menu(menudesc *m, void *arg);
    796 static void draw_edit_ptn_line(menudesc *m, int opt, void *arg);
    797 static int edit_ptn_custom_type(menudesc *m, void *arg);
    798 
    799 static void
    800 remember_deleted(struct partition_usage_set *pset,
    801     struct disk_partitions *parts)
    802 {
    803 	size_t i, num;
    804 	struct disk_partitions **tab;
    805 
    806 	/* do we have parts on record already? */
    807 	for (i = 0; i < pset->num_write_back; i++)
    808 		if (pset->write_back[i] == parts)
    809 			return;
    810 	/*
    811 	 * Need to record this partition table for write back
    812 	 */
    813 	num = pset->num_write_back + 1;
    814 	tab = realloc(pset->write_back, num*sizeof(*pset->write_back));
    815 	if (!tab)
    816 		return;
    817 	tab[pset->num_write_back] = parts;
    818 	pset->write_back = tab;
    819 	pset->num_write_back = num;
    820 }
    821 
    822 int
    823 edit_ptn(menudesc *menu, void *arg)
    824 {
    825 	struct partition_usage_set *pset = arg;
    826 	struct single_part_fs_edit edit;
    827 	int fspart_menu, num_opts;
    828 	const char *err;
    829 	menu_ent *mopts, *popt;
    830 	bool is_new_part, with_inst_opt = pset->parts->parent == NULL;
    831 
    832 	static const menu_ent edit_ptn_fields_head[] = {
    833 		{ .opt_action=edit_fs_type },
    834 		{ .opt_action=edit_fs_start },
    835 		{ .opt_action=edit_fs_size },
    836 		{ .opt_flags=OPT_IGNORE },
    837 	};
    838 
    839 	static const menu_ent edit_ptn_fields_head_add[] = {
    840 		{ .opt_action=edit_install },
    841 	};
    842 
    843 	static const menu_ent edit_ptn_fields_head2[] = {
    844 		{ .opt_action=edit_fs_preserve },
    845 		{ .opt_action=edit_fs_mount },
    846 		{ .opt_menu=MENU_mountoptions, .opt_flags=OPT_SUB },
    847 		{ .opt_action=edit_fs_mountpt },
    848 	};
    849 
    850 	static const menu_ent edit_ptn_fields_ffs[] = {
    851 		{ .opt_action=edit_fs_ffs_avg_size },
    852 		{ .opt_action=edit_fs_ffs_block },
    853 		{ .opt_action=edit_fs_ffs_frag },
    854 	};
    855 
    856 	static const menu_ent edit_ptn_fields_tail[] = {
    857 		{ .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
    858 		  .opt_flags=OPT_SUB },
    859 		{ .opt_name=MSG_restore,
    860 		  .opt_action=edit_restore},
    861 		{ .opt_name=MSG_Delete_partition,
    862 		  .opt_action=edit_delete_ptn},
    863 		{ .opt_name=MSG_cancel,
    864 		  .opt_action=edit_cancel},
    865 	};
    866 
    867 	memset(&edit, 0, sizeof edit);
    868 	edit.pset = pset;
    869 	edit.index = menu->cursel;
    870 	edit.wanted = &pset->infos[edit.index];
    871 
    872 	if (menu->cursel < 0 || (size_t)menu->cursel > pset->parts->num_part)
    873 		return 0;
    874 	is_new_part = (size_t)menu->cursel == pset->parts->num_part;
    875 
    876 	num_opts = __arraycount(edit_ptn_fields_head) +
    877 	    __arraycount(edit_ptn_fields_head2) +
    878 	    __arraycount(edit_ptn_fields_tail);
    879 	if (edit.wanted->fs_type == FS_BSDFFS ||
    880 	    edit.wanted->fs_type == FS_BSDLFS)
    881 		num_opts += __arraycount(edit_ptn_fields_ffs);
    882 	if (with_inst_opt)
    883 		num_opts += __arraycount(edit_ptn_fields_head_add);
    884 	if (is_new_part)
    885 		num_opts--;
    886 	else
    887 		num_opts += pset->parts->pscheme->custom_attribute_count;
    888 
    889 	mopts = calloc(num_opts, sizeof(*mopts));
    890 	if (mopts == NULL) {
    891 		err_msg_win(err_outofmem);
    892 		return 0;
    893 	}
    894 	memcpy(mopts, edit_ptn_fields_head, sizeof(edit_ptn_fields_head));
    895 	popt = mopts + __arraycount(edit_ptn_fields_head);
    896 	if (with_inst_opt) {
    897 		memcpy(popt, edit_ptn_fields_head_add,
    898 		    sizeof(edit_ptn_fields_head_add));
    899 		popt +=  __arraycount(edit_ptn_fields_head_add);
    900 	}
    901 	memcpy(popt, edit_ptn_fields_head2, sizeof(edit_ptn_fields_head2));
    902 	popt +=  __arraycount(edit_ptn_fields_head2);
    903 	if (edit.wanted->fs_type == FS_BSDFFS ||
    904 	    edit.wanted->fs_type == FS_BSDLFS) {
    905 		memcpy(popt, edit_ptn_fields_ffs, sizeof(edit_ptn_fields_ffs));
    906 		popt +=  __arraycount(edit_ptn_fields_ffs);
    907 	}
    908 	edit.first_custom_attr = popt - mopts;
    909 	if (!is_new_part) {
    910 		for (size_t i = 0;
    911 		    i < pset->parts->pscheme->custom_attribute_count;
    912 		    i++, popt++) {
    913 			popt->opt_action = edit_ptn_custom_type;
    914 		}
    915 	}
    916 	memcpy(popt, edit_ptn_fields_tail, sizeof(edit_ptn_fields_tail));
    917 	popt += __arraycount(edit_ptn_fields_tail) - 1;
    918 	if (is_new_part)
    919 		memcpy(popt-1, popt, sizeof(*popt));
    920 
    921 	if (is_new_part) {
    922 		struct disk_part_free_space space;
    923 		daddr_t align = pset->parts->pscheme->get_part_alignment(
    924 		    pset->parts);
    925 
    926 		edit.id = NO_PART;
    927 		if (pset->parts->pscheme->get_free_spaces(pset->parts,
    928 		    &space, 1, align, align, -1, -1) == 1) {
    929 			edit.info.start = space.start;
    930 			edit.info.size = space.size;
    931 			edit.info.fs_type = FS_BSDFFS;
    932 			edit.info.fs_sub_type = 2;
    933 			edit.info.nat_type = pset->parts->pscheme->
    934 			    get_fs_part_type(PT_root, edit.info.fs_type,
    935 			    edit.info.fs_sub_type);
    936 			edit.wanted->instflags = PUIINST_NEWFS;
    937 		}
    938 	} else {
    939 		edit.id = pset->infos[edit.index].cur_part_id;
    940 		if (!pset->parts->pscheme->get_part_info(pset->parts, edit.id,
    941 		    &edit.info)) {
    942 			free(mopts);
    943 			return 0;
    944 		}
    945 	}
    946 
    947 	edit.old_usage = *edit.wanted;
    948 	edit.old_info = edit.info;
    949 
    950 	fspart_menu = new_menu(MSG_edfspart, mopts, num_opts,
    951 		15, 2, 0, 55, MC_NOCLEAR | MC_SCROLL,
    952 		update_edit_ptn_menu, draw_edit_ptn_line, NULL,
    953 		NULL, MSG_OK);
    954 
    955 	process_menu(fspart_menu, &edit);
    956 	free(mopts);
    957 	free_menu(fspart_menu);
    958 
    959 	if (edit.rv == 0) {	/* OK, set new data */
    960 		edit.info.last_mounted = edit.wanted->mount;
    961 		if (is_new_part) {
    962 			edit.wanted->parts = pset->parts;
    963 			edit.wanted->cur_part_id = pset->parts->pscheme->
    964 			    add_partition(pset->parts, &edit.info, &err);
    965 			if (edit.wanted->cur_part_id == NO_PART)
    966 				err_msg_win(err);
    967 			else {
    968 				pset->parts->pscheme->get_part_info(
    969 				    pset->parts, edit.wanted->cur_part_id,
    970 				    &edit.info);
    971 				edit.wanted->cur_start = edit.info.start;
    972 				edit.wanted->size = edit.info.size;
    973 				edit.wanted->type =
    974 				    edit.info.nat_type->generic_ptype;
    975 				edit.wanted->fs_type = edit.info.fs_type;
    976 				edit.wanted->fs_version = edit.info.fs_sub_type;
    977 				/* things have changed, re-sort */
    978 				renumber_partitions(pset);
    979 			}
    980 		} else {
    981 			if (!pset->parts->pscheme->set_part_info(pset->parts,
    982 			    edit.id, &edit.info, &err))
    983 				err_msg_win(err);
    984 			else
    985 				pset->cur_free_space += edit.old_info.size -
    986 				    edit.info.size;
    987 		}
    988 
    989 		/*
    990 		 * if size has changed, we may need to add or remove
    991 		 * the option to add partitions
    992 		 */
    993 		show_partition_adder(menu, pset);
    994 	} else if (edit.rv == -1) {	/* cancel edit */
    995 		if (is_new_part) {
    996 			memmove(pset->infos+edit.index,
    997 			    pset->infos+edit.index+1,
    998 			    sizeof(*pset->infos)*(pset->num-edit.index));
    999 			memmove(menu->opts+edit.index,
   1000 			    menu->opts+edit.index+1,
   1001 			    sizeof(*menu->opts)*(menu->numopts-edit.index));
   1002 			menu->numopts--;
   1003 			menu->cursel = 0;
   1004 			pset->num--;
   1005 			return -1;
   1006 		}
   1007 		pset->infos[edit.index] = edit.old_usage;
   1008 	} else if (!is_new_part && edit.rv == -2) {	/* delete partition */
   1009 		if (!pset->parts->pscheme->delete_partition(pset->parts,
   1010 		    edit.id, &err)) {
   1011 			err_msg_win(err);
   1012 			return 0;
   1013 		}
   1014 		remember_deleted(pset,
   1015 		    pset->infos[edit.index].parts);
   1016 		pset->cur_free_space += edit.info.size;
   1017 		memmove(pset->infos+edit.index,
   1018 		    pset->infos+edit.index+1,
   1019 		    sizeof(*pset->infos)*(pset->num-edit.index));
   1020 		memmove(menu->opts+edit.index,
   1021 		    menu->opts+edit.index+1,
   1022 		    sizeof(*menu->opts)*(menu->numopts-edit.index));
   1023 		menu->numopts--;
   1024 		menu->cursel = 0;
   1025 		if (pset->parts->num_part == 0)
   1026 			menu->cursel = 1;	/* skip sentinel line */
   1027 
   1028 		/* things have changed, re-sort */
   1029 		pset->num--;
   1030 		renumber_partitions(pset);
   1031 
   1032 		/* we can likely add new partitions now */
   1033 		show_partition_adder(menu, pset);
   1034 
   1035 		return -1;
   1036 	}
   1037 
   1038 	return 0;
   1039 }
   1040 
   1041 static void
   1042 update_edit_ptn_menu(menudesc *m, void *arg)
   1043 {
   1044 	struct single_part_fs_edit *edit = arg;
   1045 	int i;
   1046 	uint t = edit->info.fs_type;
   1047 	size_t attr_no;
   1048 
   1049 	/* Determine which of the properties can be changed */
   1050 	for (i = 0; i < m->numopts; i++) {
   1051 		if (m->opts[i].opt_action == NULL &&
   1052 		    m->opts[i].opt_menu != MENU_mountoptions)
   1053 			continue;
   1054 
   1055 		/* Default to disabled... */
   1056 		m->opts[i].opt_flags |= OPT_IGNORE;
   1057 		if ((t == FS_UNUSED || t == FS_SWAP) &&
   1058 		    (m->opts[i].opt_action == edit_fs_preserve ||
   1059 		     m->opts[i].opt_action == edit_fs_mount ||
   1060 		     m->opts[i].opt_action == edit_fs_mountpt ||
   1061 		     m->opts[i].opt_menu == MENU_mountoptions))
   1062 			continue;
   1063 		if (m->opts[i].opt_action == edit_install &&
   1064 		    edit->info.nat_type &&
   1065 		    edit->info.nat_type->generic_ptype != PT_root)
   1066 			/* can only install onto PT_root partitions */
   1067 			continue;
   1068 		if (m->opts[i].opt_action == edit_fs_preserve &&
   1069 		    t != FS_BSDFFS && t != FS_BSDLFS && t != FS_APPLEUFS &&
   1070 		    t != FS_MSDOS && t != FS_EX2FS) {
   1071 			/* Can not newfs this filesystem */
   1072 			edit->wanted->instflags &= ~PUIINST_NEWFS;
   1073 			continue;
   1074 		}
   1075 		if (m->opts[i].opt_action == edit_ptn_custom_type) {
   1076 			attr_no = (size_t)i - edit->first_custom_attr;
   1077 			if (!edit->pset->parts->pscheme->
   1078 			    custom_attribute_writable(
   1079 			    edit->pset->parts, edit->id, attr_no))
   1080 				continue;
   1081 		}
   1082 		/*
   1083 		 * Do not allow editing of size/start/type when partition
   1084 		 * is defined in some outer partition table already
   1085 		 */
   1086 		if ((edit->pset->infos[edit->index].flags & PUIFLG_IS_OUTER)
   1087 		    && (m->opts[i].opt_action == edit_fs_type
   1088 			|| m->opts[i].opt_action == edit_fs_start
   1089 			|| m->opts[i].opt_action == edit_fs_size))
   1090 				continue;
   1091 		/* Ok: we want this one */
   1092 		m->opts[i].opt_flags &= ~OPT_IGNORE;
   1093 	}
   1094 
   1095 	/* Avoid starting at a (now) disabled menu item */
   1096 	while (m->cursel >= 0 && m->cursel < m->numopts
   1097 	    && (m->opts[m->cursel].opt_flags & OPT_IGNORE))
   1098 		m->cursel++;
   1099 }
   1100 
   1101 static void
   1102 draw_edit_ptn_line(menudesc *m, int opt, void *arg)
   1103 {
   1104 	struct single_part_fs_edit *edit = arg;
   1105 	static int col_width;
   1106 	static const char *ptn_type, *ptn_start, *ptn_size, *ptn_end,
   1107 	     *ptn_newfs, *ptn_mount, *ptn_mount_options, *ptn_mountpt,
   1108 	     *ptn_install, *ptn_bsize, *ptn_fsize, *ptn_isize;
   1109 	const char *c;
   1110 	char val[MENUSTRSIZE];
   1111 	const char *attrname;
   1112 	size_t attr_no;
   1113 
   1114 	if (col_width == 0) {
   1115 		int l;
   1116 
   1117 #define	LOAD(STR)	STR = msg_string(MSG_##STR); l = strlen(STR); \
   1118 			if (l > col_width) col_width = l
   1119 
   1120 		LOAD(ptn_type);
   1121 		LOAD(ptn_start);
   1122 		LOAD(ptn_size);
   1123 		LOAD(ptn_end);
   1124 		LOAD(ptn_install);
   1125 		LOAD(ptn_newfs);
   1126 		LOAD(ptn_mount);
   1127 		LOAD(ptn_mount_options);
   1128 		LOAD(ptn_mountpt);
   1129 		LOAD(ptn_bsize);
   1130 		LOAD(ptn_fsize);
   1131 		LOAD(ptn_isize);
   1132 #undef LOAD
   1133 
   1134 		for (size_t i = 0;
   1135 		    i < edit->pset->parts->pscheme->custom_attribute_count;
   1136 		    i++) {
   1137 			attrname = msg_string(
   1138 			    edit->pset->parts->pscheme->custom_attributes[i]
   1139 			    .label);
   1140 			l = strlen(attrname);
   1141 			if (l > col_width) col_width = l;
   1142 		}
   1143 
   1144 		col_width += 3;
   1145 	}
   1146 
   1147 	if (m->opts[opt].opt_flags & OPT_IGNORE
   1148 	    && (opt != 3 || edit->info.fs_type == FS_UNUSED)
   1149 	    && m->opts[opt].opt_action != edit_ptn_custom_type
   1150 	    && m->opts[opt].opt_action != edit_fs_type
   1151 	    && m->opts[opt].opt_action != edit_fs_start
   1152 	    && m->opts[opt].opt_action != edit_fs_size) {
   1153 		wprintw(m->mw, "%*s -", col_width, "");
   1154 		return;
   1155 	}
   1156 
   1157 	if (opt < 4) {
   1158 		switch (opt) {
   1159 		case 0:
   1160 			if (edit->info.fs_type == FS_BSDFFS)
   1161 				if (edit->info.fs_sub_type == 2)
   1162 					c = msg_string(MSG_fs_type_ffsv2);
   1163 				else
   1164 					c = msg_string(MSG_fs_type_ffs);
   1165 			else
   1166 				c = getfslabelname(edit->info.fs_type,
   1167 				    edit->info.fs_sub_type);
   1168 			wprintw(m->mw, "%*s : %s", col_width, ptn_type, c);
   1169 			return;
   1170 		case 1:
   1171 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1172 			    ptn_start, edit->info.start / sizemult, multname);
   1173 			return;
   1174 		case 2:
   1175 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1176 			    ptn_size, edit->info.size / sizemult, multname);
   1177 			return;
   1178 		case 3:
   1179 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1180 			    ptn_end, (edit->info.start + edit->info.size)
   1181 			    / sizemult, multname);
   1182 			return;
   1183 		 }
   1184 	}
   1185 	if (m->opts[opt].opt_action == edit_install) {
   1186 		wprintw(m->mw, "%*s : %s", col_width, ptn_install,
   1187 			msg_string((edit->info.flags & PTI_INSTALL_TARGET)
   1188 			    ? MSG_Yes : MSG_No));
   1189 		return;
   1190 	}
   1191 	if (m->opts[opt].opt_action == edit_fs_preserve) {
   1192 		wprintw(m->mw, "%*s : %s", col_width, ptn_newfs,
   1193 			msg_string(edit->wanted->instflags & PUIINST_NEWFS
   1194 			    ? MSG_Yes : MSG_No));
   1195 		return;
   1196 	}
   1197 	if (m->opts[opt].opt_action == edit_fs_mount) {
   1198 		wprintw(m->mw, "%*s : %s", col_width, ptn_mount,
   1199 			msg_string(edit->wanted->instflags & PUIINST_MOUNT
   1200 			    ? MSG_Yes : MSG_No));
   1201 		return;
   1202 	}
   1203 	if (m->opts[opt].opt_action == edit_fs_ffs_block) {
   1204 		wprintw(m->mw, "%*s : %u", col_width, ptn_bsize,
   1205 			edit->wanted->fs_opt1);
   1206 		return;
   1207 	}
   1208 	if (m->opts[opt].opt_action == edit_fs_ffs_frag) {
   1209 		wprintw(m->mw, "%*s : %u", col_width, ptn_fsize,
   1210 			edit->wanted->fs_opt2);
   1211 		return;
   1212 	}
   1213 	if (m->opts[opt].opt_action == edit_fs_ffs_avg_size) {
   1214 		if (edit->wanted->fs_opt3 == 0)
   1215 			wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
   1216 				msg_string(MSG_ptn_isize_dflt));
   1217 		else {
   1218         	        char buf[24], *line;
   1219 			const char *t = buf;
   1220 
   1221 			snprintf(buf, sizeof buf, "%u", edit->wanted->fs_opt3);
   1222 			line = str_arg_subst(msg_string(MSG_ptn_isize_bytes),
   1223 			    1, &t);
   1224 			wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
   1225 				line);
   1226 			free(line);
   1227 		}
   1228 		return;
   1229 	}
   1230 	if (m->opts[opt].opt_menu == MENU_mountoptions) {
   1231 		wprintw(m->mw, "%*s : ", col_width, ptn_mount_options);
   1232 		if (edit->wanted->mountflags & PUIMNT_ASYNC)
   1233 			wprintw(m->mw, "async ");
   1234 		if (edit->wanted->mountflags & PUIMNT_NOATIME)
   1235 			wprintw(m->mw, "noatime ");
   1236 		if (edit->wanted->mountflags & PUIMNT_NODEV)
   1237 			wprintw(m->mw, "nodev ");
   1238 		if (edit->wanted->mountflags & PUIMNT_NODEVMTIME)
   1239 			wprintw(m->mw, "nodevmtime ");
   1240 		if (edit->wanted->mountflags & PUIMNT_NOEXEC)
   1241 			wprintw(m->mw, "noexec ");
   1242 		if (edit->wanted->mountflags & PUIMNT_NOSUID)
   1243 			wprintw(m->mw, "nosuid ");
   1244 		if (edit->wanted->mountflags & PUIMNT_LOG)
   1245 			wprintw(m->mw, "log ");
   1246 		if (edit->wanted->mountflags & PUIMNT_NOAUTO)
   1247 			wprintw(m->mw, "noauto  ");
   1248 		return;
   1249 	}
   1250 	if (m->opts[opt].opt_action == edit_fs_mountpt) {
   1251 		wprintw(m->mw, "%*s : %s", col_width, ptn_mountpt,
   1252 		    edit->wanted->mount);
   1253 		return;
   1254 	}
   1255 
   1256 	attr_no = opt - edit->first_custom_attr;
   1257 	edit->pset->parts->pscheme->format_custom_attribute(
   1258 	    edit->pset->parts, edit->id, attr_no, &edit->info,
   1259 	    val, sizeof val);
   1260 	attrname = msg_string(edit->pset->parts->pscheme->
   1261 	    custom_attributes[attr_no].label);
   1262 	wprintw(m->mw, "%*s : %s", col_width, attrname, val);
   1263 }
   1264 
   1265 static int
   1266 edit_ptn_custom_type(menudesc *m, void *arg)
   1267 {
   1268 	struct single_part_fs_edit *edit = arg;
   1269 	size_t attr_no = m->cursel - edit->first_custom_attr;
   1270 	char line[STRSIZE];
   1271 
   1272 	switch (edit->pset->parts->pscheme->custom_attributes[attr_no].type) {
   1273 	case pet_bool:
   1274 		edit->pset->parts->pscheme->custom_attribute_toggle(
   1275 		    edit->pset->parts, edit->id, attr_no);
   1276 		break;
   1277 	case pet_cardinal:
   1278 	case pet_str:
   1279 		edit->pset->parts->pscheme->format_custom_attribute(
   1280 		    edit->pset->parts, edit->id, attr_no, &edit->info,
   1281 		    line, sizeof(line));
   1282 		msg_prompt_win(
   1283 		    edit->pset->parts->pscheme->custom_attributes[attr_no].
   1284 		    label, -1, 18, 0, 0, line, line, sizeof(line));
   1285 		edit->pset->parts->pscheme->custom_attribute_set_str(
   1286 		    edit->pset->parts, edit->id, attr_no, line);
   1287 		break;
   1288 	}
   1289 
   1290 	return 0;
   1291 }
   1292 
   1293 
   1294 /*
   1295  * Some column width depend on translation, we will set these in
   1296  * fmt_fspart_header and later use it when formatting single entries
   1297  * in fmt_fspart_row.
   1298  * The table consist of 3 "size like" columns, all fixed width, then
   1299  * ptnheaders_fstype, part_header_col_flag, and finally the mount point
   1300  * (which is variable width).
   1301  */
   1302 static int fstype_width, flags_width;
   1303 static char fspart_separator[MENUSTRSIZE];
   1304 static char fspart_title[2*MENUSTRSIZE];
   1305 
   1306 /*
   1307  * Format the header of the main partition editor menu.
   1308  */
   1309 static void
   1310 fmt_fspart_header(menudesc *menu, void *arg)
   1311 {
   1312 	struct partition_usage_set *pset = arg;
   1313 	char total[6], free_space[6], scol[13], ecol[13], szcol[13],
   1314 	    sepline[MENUSTRSIZE], *p, desc[MENUSTRSIZE];
   1315 	const char *fstype, *flags;
   1316 	int i;
   1317 	size_t ptn;
   1318 	bool with_clone, with_inst_flag = pset->parts->parent == NULL;
   1319 
   1320 	with_clone = false;
   1321 	for (ptn = 0; ptn < pset->num && !with_clone; ptn++)
   1322 		if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
   1323 			with_clone = true;
   1324 	humanize_number(total, sizeof total,
   1325 	    pset->parts->disk_size * pset->parts->bytes_per_sector,
   1326 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1327 	humanize_number(free_space, sizeof free_space,
   1328 	    pset->cur_free_space * pset->parts->bytes_per_sector,
   1329 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1330 
   1331 	if (with_clone)
   1332 		strlcpy(desc, msg_string(MSG_clone_flag_desc), sizeof desc);
   1333 	else
   1334 		desc[0] = 0;
   1335 	if (pset->parts->pscheme->part_flag_desc)
   1336 		strlcat(desc, msg_string(pset->parts->pscheme->part_flag_desc),
   1337 		sizeof desc);
   1338 
   1339 	msg_display_subst(MSG_fspart, 7, pset->parts->disk,
   1340 	    msg_string(pset->parts->pscheme->name),
   1341 	    msg_string(pset->parts->pscheme->short_name),
   1342 	    with_inst_flag ? msg_string(MSG_ptn_instflag_desc) : "",
   1343 	    desc, total, free_space);
   1344 
   1345 	snprintf(scol, sizeof scol, "%s (%s)",
   1346 	    msg_string(MSG_ptnheaders_start), multname);
   1347 	snprintf(ecol, sizeof ecol, "%s (%s)",
   1348 	    msg_string(MSG_ptnheaders_end), multname);
   1349 	snprintf(szcol, sizeof szcol, "%s (%s)",
   1350 	    msg_string(MSG_ptnheaders_size), multname);
   1351 
   1352 	fstype = msg_string(MSG_ptnheaders_fstype);
   1353 	flags = msg_string(MSG_part_header_col_flag);
   1354 	fstype_width = max(strlen(fstype), 8);
   1355 	flags_width = strlen(flags);
   1356 	for (i = 0, p = sepline; i < fstype_width; i++)
   1357 		*p++ = '-';
   1358 	for (i = 0, *p++ = ' '; i < flags_width; i++)
   1359 		*p++ = '-';
   1360 	*p = 0;
   1361 
   1362 	snprintf(fspart_separator, sizeof(fspart_separator),
   1363 	    "------------ ------------ ------------ %s ----------------",
   1364 	    sepline);
   1365 
   1366 	snprintf(fspart_title, sizeof(fspart_title),
   1367 	    "   %12.12s %12.12s %12.12s %*s %*s %s\n"
   1368 	    "   %s", scol, ecol, szcol, fstype_width, fstype,
   1369 	    flags_width, flags, msg_string(MSG_ptnheaders_filesystem),
   1370 	    fspart_separator);
   1371 
   1372 	msg_table_add("\n\n");
   1373 }
   1374 
   1375 /*
   1376  * Format one partition entry in the main partition editor.
   1377  */
   1378 static void
   1379 fmt_fspart_row(menudesc *m, int ptn, void *arg)
   1380 {
   1381 	struct partition_usage_set *pset = arg;
   1382 	struct disk_part_info info;
   1383 	daddr_t poffset, psize, pend;
   1384 	const char *desc;
   1385 	static const char *Yes;
   1386 	char flag_str[MENUSTRSIZE], *fp;
   1387 	unsigned inst_flags;
   1388 #ifndef NO_CLONES
   1389 	size_t clone_cnt;
   1390 #endif
   1391 	bool with_inst_flag = pset->parts->parent == NULL;
   1392 
   1393 	if (Yes == NULL)
   1394 		Yes = msg_string(MSG_Yes);
   1395 
   1396 #ifndef NO_CLONES
   1397 	if ((pset->infos[ptn].flags & PUIFLG_CLONE_PARTS) &&
   1398 	   pset->infos[ptn].cur_part_id == NO_PART) {
   1399 		psize = pset->infos[ptn].size / sizemult;
   1400 		if (pset->infos[ptn].clone_ndx <
   1401 		    pset->infos[ptn].clone_src->num_sel)
   1402 			clone_cnt = 1;
   1403 		else
   1404 			clone_cnt = pset->infos[ptn].clone_src->num_sel;
   1405 		if (pset->infos[ptn].cur_part_id == NO_PART)
   1406 			wprintw(m->mw, "                          %12" PRIu64
   1407 			    " [%zu %s]", psize, clone_cnt,
   1408 			    msg_string(MSG_clone_target_disp));
   1409 		else {
   1410 			poffset = pset->infos[ptn].cur_start / sizemult;
   1411 			pend = (pset->infos[ptn].cur_start +
   1412 			    pset->infos[ptn].size) / sizemult - 1;
   1413 			wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1414 			    " [%zu %s]",
   1415 			    poffset, pend, psize, clone_cnt,
   1416 			    msg_string(MSG_clone_target_disp));
   1417 		}
   1418 		if (m->title == fspart_title)
   1419 			m->opts[ptn].opt_flags |= OPT_IGNORE;
   1420 		else
   1421 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
   1422 		return;
   1423 	}
   1424 #endif
   1425 
   1426 	if (!real_partition(pset, ptn))
   1427 		return;
   1428 
   1429 	if (!pset->parts->pscheme->get_part_info(pset->parts,
   1430 	    pset->infos[ptn].cur_part_id, &info))
   1431 		return;
   1432 
   1433 	/*
   1434 	 * We use this function in multiple menus, but only want it
   1435 	 * to play with enable/disable in a single one:
   1436 	 */
   1437 	if (m->title == fspart_title) {
   1438 		/*
   1439 		 * Enable / disable this line if it is something
   1440 		 * like RAW_PART
   1441 		 */
   1442 		if ((info.flags &
   1443 		    (PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
   1444 		    || (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS))
   1445 			m->opts[ptn].opt_flags |= OPT_IGNORE;
   1446 		else
   1447 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
   1448 	}
   1449 
   1450 	poffset = info.start / sizemult;
   1451 	psize = info.size / sizemult;
   1452 	if (psize == 0)
   1453 		pend = 0;
   1454 	else
   1455 		pend = (info.start + info.size) / sizemult - 1;
   1456 
   1457 	if (info.flags & PTI_WHOLE_DISK)
   1458 		desc = msg_string(MSG_NetBSD_partition_cant_change);
   1459 	else if (info.flags & PTI_RAW_PART)
   1460 		desc = msg_string(MSG_Whole_disk_cant_change);
   1461 	else if (info.flags & PTI_BOOT)
   1462 		desc = msg_string(MSG_Boot_partition_cant_change);
   1463 	else
   1464 		desc = getfslabelname(info.fs_type, info.fs_sub_type);
   1465 
   1466 	fp = flag_str;
   1467 	inst_flags = pset->infos[ptn].instflags;
   1468 	if (with_inst_flag && (info.flags & PTI_INSTALL_TARGET) &&
   1469 	    info.nat_type->generic_ptype == PT_root) {
   1470 		static char inst_flag;
   1471 
   1472 		if (inst_flag == 0)
   1473 			inst_flag = msg_string(MSG_install_flag)[0];
   1474 		*fp++ = inst_flag;
   1475 	}
   1476 	if (inst_flags & PUIINST_NEWFS)
   1477 		*fp++ = msg_string(MSG_newfs_flag)[0];
   1478 	if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
   1479 		*fp++ = msg_string(MSG_clone_flag)[0];
   1480 	*fp = 0;
   1481 	if (pset->parts->pscheme->get_part_attr_str != NULL)
   1482 		pset->parts->pscheme->get_part_attr_str(pset->parts,
   1483 		    pset->infos[ptn].cur_part_id, fp, sizeof(flag_str)-1);
   1484 
   1485 	/* if the fstype description does not fit, check if we can overrun */
   1486 	if (strlen(desc) > (size_t)fstype_width &&
   1487 	     flag_str[0] == 0 && (info.last_mounted == NULL ||
   1488 	     info.last_mounted[0] == 0))
   1489 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1490 		    " %s",
   1491 		    poffset, pend, psize, desc);
   1492 	else
   1493 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1494 		    " %*.*s %*s %s",
   1495 		    poffset, pend, psize, fstype_width, fstype_width, desc,
   1496 		    -flags_width, flag_str,
   1497 		    (inst_flags & PUIINST_MOUNT) && info.last_mounted &&
   1498 		     info.last_mounted[0] ? info.last_mounted : "");
   1499 }
   1500 
   1501 #ifndef NO_CLONES
   1502 static int
   1503 part_ext_clone(menudesc *m, void *arg)
   1504 {
   1505 	struct selected_partitions selected, *clone_src;
   1506 	struct clone_target_menu_data data;
   1507 	struct partition_usage_set *pset = arg;
   1508 	struct part_usage_info *p;
   1509 	struct disk_part_info sinfo, cinfo;
   1510 	struct disk_partitions *csrc;
   1511 	struct disk_part_free_space space;
   1512 	menu_ent *men;
   1513 	daddr_t clone_size, free_size, offset, align;
   1514 	int num_men, i;
   1515 	size_t s, clone_cnt;
   1516 	part_id cid;
   1517 	struct clone_data {
   1518 		struct disk_part_info info;
   1519 		part_id new_id;
   1520 		size_t ndx;
   1521 	};
   1522 	struct clone_data *clones = NULL;
   1523 
   1524 	if (!select_partitions(&selected, pm->parts))
   1525 		return 0;
   1526 
   1527 	clone_size = selected_parts_size(&selected);
   1528 	num_men = pset->num+1;
   1529 	men = calloc(num_men, sizeof *men);
   1530 	if (men == NULL)
   1531 		return 0;
   1532 	for (i = 0; i < num_men; i++) {
   1533 		men[i].opt_action = clone_target_select;
   1534 		if (i == 0)
   1535 			free_size = pset->infos[i].cur_start;
   1536 		else if (i > 0 && (size_t)i < pset->num)
   1537 			free_size = pset->infos[i].cur_start -
   1538 			    pset->infos[i-1].cur_start - pset->infos[i-1].size;
   1539 		else
   1540 			free_size = pset->parts->free_space;
   1541 		if (free_size < clone_size)
   1542 			men[i].opt_flags = OPT_IGNORE;
   1543 	}
   1544 	men[num_men-1].opt_name = MSG_clone_target_end;
   1545 
   1546 	memset(&data, 0, sizeof data);
   1547 	data.usage = *pset;
   1548 	data.res = -1;
   1549 
   1550 	data.usage.menu = new_menu(MSG_clone_target_hdr,
   1551 	    men, num_men, 3, 2, 0, 65, MC_SCROLL,
   1552 	    NULL, fmt_fspart_row, NULL, NULL, MSG_cancel);
   1553 	process_menu(data.usage.menu, &data);
   1554 	free_menu(data.usage.menu);
   1555 	free(men);
   1556 
   1557 	if (data.res < 0)
   1558 		goto err;
   1559 
   1560 	/* create temporary infos for all clones that work out */
   1561 	clone_cnt = 0;
   1562 	clones = calloc(selected.num_sel, sizeof(*clones));
   1563 	if (clones == NULL)
   1564 		goto err;
   1565 
   1566 	clone_src = malloc(sizeof(selected));
   1567 	if (clone_src == NULL)
   1568 		goto err;
   1569 	*clone_src = selected;
   1570 
   1571 	/* find selected offset from data.res and insert clones there */
   1572 	align = pset->parts->pscheme->get_part_alignment(pset->parts);
   1573 	offset = -1;
   1574 	if (data.res > 0)
   1575 		offset = pset->infos[data.res-1].cur_start
   1576 		    + pset->infos[data.res-1].size;
   1577 	else
   1578 		offset = 0;
   1579 	for (s = 0; s < selected.num_sel; s++) {
   1580 		csrc = selected.selection[s].parts;
   1581 		cid = selected.selection[s].id;
   1582 		csrc->pscheme->get_part_info(csrc, cid, &sinfo);
   1583 		if (!pset->parts->pscheme->adapt_foreign_part_info(
   1584 		    pset->parts, &cinfo, csrc->pscheme, &sinfo))
   1585 			continue;
   1586 		size_t cnt = pset->parts->pscheme->get_free_spaces(
   1587 		    pset->parts, &space, 1, cinfo.size-align, align,
   1588 		    offset, -1);
   1589 		if (cnt == 0)
   1590 			continue;
   1591 		cinfo.start = space.start;
   1592 		cid = pset->parts->pscheme->add_partition(
   1593 		    pset->parts, &cinfo, NULL);
   1594 		if (cid == NO_PART)
   1595 			continue;
   1596 		pset->parts->pscheme->get_part_info(pset->parts, cid, &cinfo);
   1597 		clones[clone_cnt].info = cinfo;
   1598 		clones[clone_cnt].new_id = cid;
   1599 		clones[clone_cnt].ndx = s;
   1600 		clone_cnt++;
   1601 		offset = roundup(cinfo.start+cinfo.size, align);
   1602 	}
   1603 
   1604 	/* insert new clone records at offset data.res */
   1605 	men = realloc(m->opts, (m->numopts+clone_cnt)*sizeof(*m->opts));
   1606 	if (men == NULL)
   1607 		goto err;
   1608 	pset->menu_opts = men;
   1609 	m->opts = men;
   1610 	m->numopts += clone_cnt;
   1611 
   1612 	p = realloc(pset->infos, (pset->num+clone_cnt)*sizeof(*pset->infos));
   1613 	if (p == NULL)
   1614 		goto err;
   1615 	pset->infos = p;
   1616 
   1617 	men += data.res;
   1618 	p += data.res;
   1619 	memmove(men+clone_cnt, men,
   1620 	    sizeof(*men)*(m->numopts-data.res-clone_cnt));
   1621 	if (pset->num > (size_t)data.res)
   1622 		memmove(p+clone_cnt, p, sizeof(*p)*(pset->num-data.res));
   1623 	memset(men, 0, sizeof(*men)*clone_cnt);
   1624 	memset(p, 0, sizeof(*p)*clone_cnt);
   1625 	for (s = 0; s < clone_cnt; s++) {
   1626 		p[s].cur_part_id = clones[s].new_id;
   1627 		p[s].cur_start = clones[s].info.start;
   1628 		p[s].size = clones[s].info.size;
   1629 		p[s].cur_flags = clones[s].info.flags;
   1630 		p[s].flags = PUIFLG_CLONE_PARTS;
   1631 		p[s].parts = pset->parts;
   1632 		p[s].clone_src = clone_src;
   1633 		p[s].clone_ndx = s;
   1634 	}
   1635 	free(clones);
   1636 	m->cursel = ((size_t)data.res >= pset->num) ? 0 : data.res+clone_cnt;
   1637 	pset->num += clone_cnt;
   1638 	m->h = 0;
   1639 	resize_menu_height(m);
   1640 
   1641 	return -1;
   1642 
   1643 err:
   1644 	free(clones);
   1645 	free_selected_partitions(&selected);
   1646 	return 0;
   1647 }
   1648 #endif
   1649 
   1650 static int
   1651 edit_fspart_pack(menudesc *m, void *arg)
   1652 {
   1653 	struct partition_usage_set *pset = arg;
   1654 	char buf[STRSIZE];
   1655 
   1656 	if (!pset->parts->pscheme->get_disk_pack_name(pset->parts,
   1657 	    buf, sizeof buf))
   1658 		return 0;
   1659 
   1660 	msg_prompt_win(MSG_edit_disk_pack_hdr,
   1661 	    -1, 18, 0, -1, buf, buf, sizeof(buf));
   1662 
   1663 	pset->parts->pscheme->set_disk_pack_name(pset->parts, buf);
   1664 	return 0;
   1665 }
   1666 
   1667 static int
   1668 edit_fspart_add(menudesc *m, void *arg)
   1669 {
   1670 	struct partition_usage_set *pset = arg;
   1671 	struct part_usage_info *ninfo;
   1672 	menu_ent *nmenopts;
   1673 	size_t cnt, off;
   1674 
   1675 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
   1676 	if (ninfo == NULL)
   1677 		return 0;
   1678 	pset->infos = ninfo;
   1679 	off = pset->parts->num_part;
   1680 	cnt = pset->num-pset->parts->num_part;
   1681 	if (cnt > 0)
   1682 		memmove(pset->infos+off+1,pset->infos+off,
   1683 		    cnt*sizeof(*pset->infos));
   1684 	memset(pset->infos+off, 0, sizeof(*pset->infos));
   1685 
   1686 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
   1687 	if (nmenopts == NULL)
   1688 		return 0;
   1689 	memmove(nmenopts+off+1, nmenopts+off,
   1690 	    (m->numopts-off)*sizeof(*nmenopts));
   1691 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
   1692 	nmenopts[off].opt_action = edit_ptn;
   1693 	pset->menu_opts = m->opts = nmenopts;
   1694 	m->numopts++;
   1695 	m->cursel = off;
   1696 	pset->num++;
   1697 
   1698 	/* now update edit menu to fit */
   1699 	m->h = 0;
   1700 	resize_menu_height(m);
   1701 
   1702 	/* and directly invoke the partition editor for the new part */
   1703 	edit_ptn(m, arg);
   1704 
   1705 	show_partition_adder(m, pset);
   1706 
   1707 	return -1;
   1708 }
   1709 
   1710 static void
   1711 add_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1712 {
   1713 	struct part_usage_info *ninfo;
   1714 	menu_ent *nmenopts;
   1715 	size_t off;
   1716 
   1717 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
   1718 	if (ninfo == NULL)
   1719 		return;
   1720 	pset->infos = ninfo;
   1721 	off = pset->parts->num_part+1;
   1722 
   1723 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
   1724 	if (nmenopts == NULL)
   1725 		return;
   1726 	memmove(nmenopts+off+1, nmenopts+off,
   1727 	    (m->numopts-off)*sizeof(*nmenopts));
   1728 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
   1729 
   1730 	nmenopts[off].opt_name = MSG_addpart;
   1731 	nmenopts[off].opt_flags = OPT_SUB;
   1732 	nmenopts[off].opt_action = edit_fspart_add;
   1733 
   1734 	m->opts = nmenopts;
   1735 	m->numopts++;
   1736 }
   1737 
   1738 static void
   1739 remove_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1740 {
   1741 	size_t off;
   1742 
   1743 	off = pset->parts->num_part+1;
   1744 	memmove(m->opts+off, m->opts+off+1,
   1745 	    (m->numopts-off-1)*sizeof(*m->opts));
   1746 	m->numopts--;
   1747 }
   1748 
   1749 /*
   1750  * Called whenever the "add a partition" option may need to be removed
   1751  * or added
   1752  */
   1753 static void
   1754 show_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1755 {
   1756 	if (m->opts == NULL)
   1757 		return;
   1758 
   1759 	bool can_add_partition = pset->parts->pscheme->can_add_partition(
   1760 	    pset->parts);
   1761 	bool part_adder_present =
   1762 	    (m->opts[pset->parts->num_part].opt_flags & OPT_IGNORE) &&
   1763 	    (m->opts[pset->parts->num_part+1].opt_action == edit_fspart_add);
   1764 
   1765 	if (can_add_partition == part_adder_present)
   1766 		return;
   1767 
   1768 	if (can_add_partition)
   1769 		add_partition_adder(m, pset);
   1770 	else
   1771 		remove_partition_adder(m, pset);
   1772 
   1773 	/* now update edit menu to fit */
   1774 	m->h = 0;
   1775 	resize_menu_height(m);
   1776 }
   1777 
   1778 static int
   1779 edit_fspart_abort(menudesc *m, void *arg)
   1780 {
   1781 	struct partition_usage_set *pset = arg;
   1782 
   1783 	pset->ok = false;
   1784 	return 1;
   1785 }
   1786 
   1787 /*
   1788  * Check a disklabel.
   1789  * If there are overlapping active partitions,
   1790  * Ask the user if they want to edit the partition or give up.
   1791  */
   1792 int
   1793 edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset,
   1794     bool install)
   1795 {
   1796 	menu_ent *op;
   1797 	size_t cnt, i;
   1798 	bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
   1799 	bool may_edit_pack =
   1800 	    pset->parts->pscheme->get_disk_pack_name != NULL &&
   1801 	    pset->parts->pscheme->set_disk_pack_name != NULL;
   1802 
   1803 #ifdef NO_CLONES
   1804 #define	C_M_ITEMS	0
   1805 #else
   1806 #define	C_M_ITEMS	1
   1807 #endif
   1808 	pset->menu_opts = calloc(pset->parts->num_part
   1809 	     +3+C_M_ITEMS+may_add+may_edit_pack,
   1810 	     sizeof *pset->menu_opts);
   1811 	if (pset->menu_opts == NULL)
   1812 		return 0;
   1813 
   1814 	op = pset->menu_opts;
   1815 	for (i = 0; i < pset->parts->num_part; i++) {
   1816 		op->opt_action = edit_ptn;
   1817 		op++;
   1818 	}
   1819 	/* separator line between partitions and actions */
   1820 	op->opt_name = fspart_separator;
   1821 	op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
   1822 	op++;
   1823 
   1824 	/* followed by new partition adder */
   1825 	if (may_add) {
   1826 		op->opt_name = MSG_addpart;
   1827 		op->opt_flags = OPT_SUB;
   1828 		op->opt_action = edit_fspart_add;
   1829 		op++;
   1830 	}
   1831 
   1832 	/* and unit changer */
   1833 	op->opt_name = MSG_askunits;
   1834 	op->opt_menu = MENU_sizechoice;
   1835 	op->opt_flags = OPT_SUB;
   1836 	op->opt_action = NULL;
   1837 	op++;
   1838 
   1839 	if (may_edit_pack) {
   1840 		op->opt_name = MSG_editpack;
   1841 		op->opt_flags = OPT_SUB;
   1842 		op->opt_action = edit_fspart_pack;
   1843 		op++;
   1844 	}
   1845 
   1846 #ifndef NO_CLONES
   1847 	/* add a clone-from-elsewhere option */
   1848 	op->opt_name = MSG_clone_from_elsewhere;
   1849 	op->opt_action = part_ext_clone;
   1850 	op++;
   1851 #endif
   1852 
   1853 	/* and abort option */
   1854 	op->opt_name = MSG_cancel;
   1855 	op->opt_flags = OPT_EXIT;
   1856 	op->opt_action = edit_fspart_abort;
   1857 	op++;
   1858 	cnt = op - pset->menu_opts;
   1859 	assert(cnt == pset->parts->num_part+3+C_M_ITEMS+may_add+may_edit_pack);
   1860 
   1861 	pset->menu = new_menu(fspart_title, pset->menu_opts, cnt,
   1862 			0, -1, 0, 74,
   1863 			MC_ALWAYS_SCROLL|MC_NOBOX|MC_DFLTEXIT|
   1864 			MC_NOCLEAR|MC_CONTINUOUS,
   1865 			fmt_fspart_header, fmt_fspart_row, NULL, NULL,
   1866 			MSG_partition_sizes_ok);
   1867 
   1868 	if (pset->menu < 0) {
   1869 		free(pset->menu_opts);
   1870 		pset->menu_opts = NULL;
   1871 		return 0;
   1872 	}
   1873 
   1874 	p->current_cylsize = p->dlcylsize;
   1875 
   1876 	for (;;) {
   1877 		/* first give the user the option to edit the label... */
   1878 		pset->ok = true;
   1879 		process_menu(pset->menu, pset);
   1880 		if (!pset->ok) {
   1881 			i = 0;
   1882 			break;
   1883 		}
   1884 
   1885 		/* User thinks the label is OK. */
   1886 		i = verify_parts(pset, install);
   1887 		if (i == 1)
   1888 			continue;
   1889 		break;
   1890 	}
   1891 	free(pset->menu_opts);
   1892 	pset->menu_opts = NULL;
   1893 	free_menu(pset->menu);
   1894 	pset->menu = -1;
   1895 
   1896 	return i != 0;
   1897 }
   1898 
   1899 /*
   1900  * strip trailing / to avoid confusion in path comparisions later
   1901  */
   1902 void
   1903 canonicalize_last_mounted(char *path)
   1904 {
   1905 	char *p;
   1906 
   1907 	if (path == NULL)
   1908 		return;
   1909 
   1910 	if (strcmp(path, "/") == 0)
   1911 		return;	/* in this case a "trailing" slash is allowed */
   1912 
   1913 	for (;;) {
   1914 		p = strrchr(path, '/');
   1915 		if (p == NULL)
   1916 			return;
   1917 		if (p[1] != 0)
   1918 			return;
   1919 		p[0] = 0;
   1920 	}
   1921 }
   1922 
   1923 /*
   1924  * Try to get 'last mounted on' (or equiv) from fs superblock.
   1925  */
   1926 const char *
   1927 get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
   1928     uint flags)
   1929 {
   1930 	static char sblk[SBLOCKSIZE];		/* is this enough? */
   1931 	struct fs *SB = (struct fs *)sblk;
   1932 	static const off_t sblocks[] = SBLOCKSEARCH;
   1933 	const off_t *sbp;
   1934 	const char *mnt = NULL;
   1935 	int len;
   1936 
   1937 	if (fd == -1)
   1938 		return "";
   1939 
   1940 	if (fs_type)
   1941 		*fs_type = 0;
   1942 	if (fs_sub_type)
   1943 		*fs_sub_type = 0;
   1944 
   1945 	/* Check UFS1/2 (and hence LFS) superblock */
   1946 	for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
   1947 		if (pread(fd, sblk, sizeof sblk,
   1948 		    (off_t)partstart * (off_t)512 + *sbp) != sizeof sblk)
   1949 			continue;
   1950 
   1951 		/*
   1952 		 * If start of partition and allowed by flags check
   1953 		 * for other fs types
   1954 		 */
   1955 		if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
   1956 		    sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
   1957 			/* Probably a FAT filesystem, report volume name */
   1958 			size_t i;
   1959 			for (i = 0x51; i >= 0x47; i--) {
   1960 				if (sblk[i] != ' ')
   1961 					break;
   1962 				sblk[i] = 0;
   1963 			}
   1964 			sblk[0x52] = 0;
   1965 			if (fs_type)
   1966 				*fs_type = FS_MSDOS;
   1967 			if (fs_sub_type)
   1968 				*fs_sub_type = sblk[0x53];
   1969 			return sblk + 0x47;
   1970 		} else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
   1971 		    memcmp(sblk+3, "NTFS    ", 8) == 0) {
   1972 			if (fs_type)
   1973 				*fs_type = FS_NTFS;
   1974 			if (fs_sub_type)
   1975 				*fs_sub_type = MBR_PTYPE_NTFS;
   1976 			/* XXX dig for volume name attribute ? */
   1977 			return "";
   1978 		}
   1979 
   1980 		if (!(flags & GLM_LIKELY_FFS))
   1981 			continue;
   1982 
   1983 		/* Maybe we should validate the checksum??? */
   1984 		switch (SB->fs_magic) {
   1985 		case FS_UFS1_MAGIC:
   1986 		case FS_UFS1_MAGIC_SWAPPED:
   1987 			if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
   1988 				if (*sbp == SBLOCK_UFS1)
   1989 					mnt = (const char *)SB->fs_fsmnt;
   1990 			} else {
   1991 				/* Check we have the main superblock */
   1992 				if (SB->fs_sblockloc == *sbp)
   1993 					mnt = (const char *)SB->fs_fsmnt;
   1994 			}
   1995 			if (fs_type)
   1996 				*fs_type = FS_BSDFFS;
   1997 			if (fs_sub_type)
   1998 				*fs_sub_type = 1;
   1999 			continue;
   2000 		case FS_UFS2_MAGIC:
   2001 		case FS_UFS2_MAGIC_SWAPPED:
   2002 			/* Check we have the main superblock */
   2003 			if (SB->fs_sblockloc == *sbp) {
   2004 				mnt = (const char *)SB->fs_fsmnt;
   2005 				if (fs_type)
   2006 					*fs_type = FS_BSDFFS;
   2007 				if (fs_sub_type)
   2008 					*fs_sub_type = 2;
   2009 			}
   2010 			continue;
   2011 		}
   2012 	}
   2013 
   2014 	if (mnt == NULL)
   2015 		return "";
   2016 
   2017 	/* If sysinst mounted this last then strip prefix */
   2018 	len = strlen(targetroot_mnt);
   2019 	if (memcmp(mnt, targetroot_mnt, len) == 0) {
   2020 		if (mnt[len] == 0)
   2021 			return "/";
   2022 		if (mnt[len] == '/')
   2023 			return mnt + len;
   2024 	}
   2025 	return mnt;
   2026 	#undef SB
   2027 }
   2028 
   2029 /* Ask for a partition offset, check bounds and do the needed roundups */
   2030 daddr_t
   2031 getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
   2032 {
   2033 	char defstart[24], isize[24], maxpart, minspace, maxspace,
   2034 	    *prompt, *label_msg, valid_parts[4], valid_spaces[4],
   2035 	    space_prompt[1024], *head, *hint_part, *hint_space, *tail;
   2036 	size_t num_freespace, spaces, ndx;
   2037 	struct disk_part_free_space *freespace;
   2038 	daddr_t i, localsizemult, ptn_alignment, min, max;
   2039 	part_id partn;
   2040 	struct disk_part_info info;
   2041 	const char *errmsg = NULL;
   2042 
   2043 	min = parts->disk_start;
   2044 	max = min + parts->disk_size;
   2045 
   2046 	/* upper bound on the number of free spaces, plus some slope */
   2047 	num_freespace = parts->num_part * 2 + 5;
   2048 	freespace = calloc(num_freespace, sizeof(*freespace));
   2049 	if (freespace == NULL)
   2050 		return -1;
   2051 
   2052 	ptn_alignment = parts->pscheme->get_part_alignment(parts);
   2053 	spaces = parts->pscheme->get_free_spaces(parts, freespace,
   2054 	    num_freespace, max(sizemult, ptn_alignment), ptn_alignment, -1,
   2055 	    defpartstart);
   2056 
   2057 	maxpart = 'a' + parts->num_part -1;
   2058 	if (parts->num_part > 1) {
   2059 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpart);
   2060 	} else if (parts->num_part == 1) {
   2061 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpart);
   2062 	} else {
   2063 		strcpy(valid_parts, "---");
   2064 	}
   2065 	if (spaces > 1) {
   2066 		minspace = maxpart + 1;
   2067 		maxspace = minspace + spaces -1;
   2068 		snprintf(valid_spaces, sizeof valid_spaces, "%c-%c", minspace,
   2069 		    maxspace);
   2070 	} else if (spaces == 1) {
   2071 		maxspace = minspace = maxpart + 1;
   2072 		snprintf(valid_spaces, sizeof valid_spaces, "  %c", minspace);
   2073 	} else {
   2074 		minspace = 0;
   2075 		maxspace = 0;
   2076 		strcpy(valid_spaces, "---");
   2077 	}
   2078 
   2079 	/* Add description of start/size to user prompt */
   2080 	const char *mstr = msg_string(MSG_free_space_line);
   2081         space_prompt[0] = 0;
   2082         for (ndx = 0; ndx < spaces; ndx++) {
   2083                 char str_start[40], str_end[40], str_size[40], str_tag[4];
   2084 
   2085 		sprintf(str_tag, "%c: ", minspace+(int)ndx);
   2086                 sprintf(str_start, "%" PRIu64, freespace[ndx].start / sizemult);
   2087                 sprintf(str_end, "%" PRIu64,
   2088                     (freespace[ndx].start + freespace[ndx].size) / sizemult);
   2089                 sprintf(str_size, "%" PRIu64, freespace[ndx].size / sizemult);
   2090                 const char *args[4] = { str_start, str_end, str_size,
   2091                     multname };
   2092                 char *line = str_arg_subst(mstr, 4, args);
   2093 		strlcat(space_prompt, str_tag, sizeof(space_prompt));
   2094                 size_t len = strlcat(space_prompt, line, sizeof(space_prompt));
   2095                 free (line);
   2096                 if (len >= sizeof space_prompt)
   2097                         break;
   2098         }
   2099 
   2100 	const char *args[] = { valid_parts, valid_spaces, multname };
   2101 	hint_part = NULL;
   2102 	hint_space = NULL;
   2103 	head = str_arg_subst(msg_string(MSG_label_offset_head),
   2104 	    __arraycount(args), args);
   2105 	if (parts->num_part)
   2106 		hint_part = str_arg_subst(msg_string(
   2107 		    MSG_label_offset_part_hint), __arraycount(args), args);
   2108 	if (spaces)
   2109 		hint_space = str_arg_subst(msg_string(
   2110 		    MSG_label_offset_space_hint), __arraycount(args), args);
   2111 	tail = str_arg_subst(msg_string(MSG_label_offset_tail),
   2112 	    __arraycount(args), args);
   2113 
   2114 	if (hint_part && hint_space)
   2115 		asprintf(&label_msg, "%s\n%s\n%s\n\n%s\n%s",
   2116 		    head, hint_part, hint_space, space_prompt, tail);
   2117 	else if (hint_part)
   2118 		asprintf(&label_msg, "%s\n%s\n\n%s",
   2119 		    head, hint_part, tail);
   2120 	else if (hint_space)
   2121 		asprintf(&label_msg, "%s\n%s\n\n%s\n%s",
   2122 		    head, hint_space, space_prompt, tail);
   2123 	else
   2124 		asprintf(&label_msg, "%s\n\n%s",
   2125 		    head, tail);
   2126 	free(head); free(hint_part); free(hint_space); free(tail);
   2127 
   2128 	localsizemult = sizemult;
   2129 	errmsg = NULL;
   2130 	for (;;) {
   2131 		snprintf(defstart, sizeof defstart, "%" PRIu64,
   2132 		    defpartstart/sizemult);
   2133 		if (errmsg != NULL && errmsg[0] != 0)
   2134 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
   2135 		else
   2136 			prompt = label_msg;
   2137 		msg_prompt_win(prompt, -1, 13, 70, -1,
   2138 		    (defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
   2139 		if (label_msg != prompt)
   2140 			free(prompt);
   2141 		if (strcmp(defstart, isize) == 0) {
   2142 			/* Don't do rounding if default accepted */
   2143 			i = defpartstart;
   2144 			break;
   2145 		}
   2146 		if (isize[1] == '\0' && isize[0] >= 'a' &&
   2147 		    isize[0] <= maxpart) {
   2148 			partn = isize[0] - 'a';
   2149 			if (parts->pscheme->get_part_info(parts, partn,
   2150 			    &info)) {
   2151 				i = info.start + info.size;
   2152 				localsizemult = 1;
   2153 			} else {
   2154 				errmsg = msg_string(MSG_invalid_sector_number);
   2155 				continue;
   2156 			}
   2157 		} else if (isize[1] == '\0' && isize[0] >= minspace &&
   2158 		    isize[0] <= maxspace) {
   2159 			ndx = isize[0] - minspace;
   2160 			i = freespace[ndx].start;
   2161 			localsizemult = 1;
   2162 		} else if (atoi(isize) == -1) {
   2163 			i = min;
   2164 			localsizemult = 1;
   2165 		} else {
   2166 			i = parse_disk_pos(isize, &localsizemult,
   2167 			    parts->bytes_per_sector,
   2168 			    parts->pscheme->get_cylinder_size(parts), NULL);
   2169 			if (i < 0) {
   2170 				errmsg = msg_string(MSG_invalid_sector_number);
   2171 				continue;
   2172 			}
   2173 		}
   2174 		/* round to cylinder size if localsizemult != 1 */
   2175 		int cylsize = parts->pscheme->get_cylinder_size(parts);
   2176 		i = NUMSEC(i, localsizemult, cylsize);
   2177 		/* Adjust to start of slice if needed */
   2178 		if ((i < min && (min - i) < localsizemult) ||
   2179 		    (i > min && (i - min) < localsizemult)) {
   2180 			i = min;
   2181 		}
   2182 		if (max == 0 || i <= max)
   2183 			break;
   2184 		errmsg = msg_string(MSG_startoutsidedisk);
   2185 	}
   2186 	free(label_msg);
   2187 	free(freespace);
   2188 
   2189 	return i;
   2190 }
   2191 
   2192 
   2193 /* Ask for a partition size, check bounds and do the needed roundups */
   2194 daddr_t
   2195 getpartsize(struct disk_partitions *parts, daddr_t orig_start,
   2196     daddr_t partstart, daddr_t dflt)
   2197 {
   2198 	char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
   2199 	    *label_msg, *prompt, *head, *hint, *tail;
   2200 	const char *errmsg = NULL;
   2201 	daddr_t i, partend, diskend, localsizemult, max, max_r, dflt_r;
   2202 	struct disk_part_info info;
   2203 	part_id partn;
   2204 
   2205 	diskend = parts->disk_start + parts->disk_size;
   2206 	max = parts->pscheme->max_free_space_at(parts, orig_start);
   2207 	max += orig_start - partstart;
   2208 	if (sizemult == 1)
   2209 		max--;	/* with hugher scale proper rounding later will be ok */
   2210 
   2211 	/* We need to keep both the unrounded and rounded (_r) max and dflt */
   2212 	dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
   2213 	if (max == dflt)
   2214 		max_r = dflt_r;
   2215 	else
   2216 		max_r = max / sizemult;
   2217 	/* the partition may have been moved and now not fit any longer */
   2218 	if (dflt > max)
   2219 		dflt = max;
   2220 	if (dflt_r > max_r)
   2221 		dflt_r = max_r;
   2222 
   2223 	snprintf(max_size, sizeof max_size, "%" PRIu64, max_r);
   2224 
   2225 	maxpartc = 'a' + parts->num_part -1;
   2226 	if (parts->num_part > 1) {
   2227 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpartc);
   2228 	} else if (parts->num_part == 1) {
   2229 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpartc);
   2230 	} else {
   2231 		strcpy(valid_parts, "---");
   2232 	}
   2233 
   2234 	const char *args[] = { valid_parts, max_size, multname };
   2235 	hint = NULL;
   2236 	head = str_arg_subst(msg_string(MSG_label_size_head),
   2237 	    __arraycount(args), args);
   2238 	if (parts->num_part)
   2239 		hint  = str_arg_subst(msg_string(MSG_label_size_part_hint),
   2240 		    __arraycount(args), args);
   2241 	tail = str_arg_subst(msg_string(MSG_label_size_tail),
   2242 	    __arraycount(args), args);
   2243 
   2244 	if (hint != NULL)
   2245 		asprintf(&label_msg, "%s\n%s\n\n%s", head, hint, tail);
   2246 	else
   2247 		asprintf(&label_msg, "%s\n\n%s", head, tail);
   2248 	free(head); free(hint); free(tail);
   2249 
   2250 	localsizemult = sizemult;
   2251 	i = -1;
   2252 	for (;;) {
   2253 		snprintf(dsize, sizeof dsize, "%" PRIu64, dflt_r);
   2254 
   2255 		if (errmsg != NULL && errmsg[0] != 0)
   2256 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
   2257 		else
   2258 			prompt = label_msg;
   2259 		msg_prompt_win(prompt, -1, 12, 70, -1,
   2260 		    (dflt != 0) ? dsize : 0, isize, sizeof isize);
   2261 		if (prompt != label_msg)
   2262 			free(prompt);
   2263 
   2264 		if (strcmp(isize, dsize) == 0) {
   2265 			free(label_msg);
   2266 			return dflt;
   2267 		}
   2268 		if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
   2269 		    isize[0] <= maxpartc) {
   2270 			partn = isize[0] - 'a';
   2271 			if (parts->pscheme->get_part_info(parts, partn,
   2272 			    &info)) {
   2273 				i = info.start - partstart -1;
   2274 				localsizemult = 1;
   2275 				max_r = max;
   2276 			}
   2277 		} else if (atoi(isize) == -1) {
   2278 			i = max;
   2279 			localsizemult = 1;
   2280 			max_r = max;
   2281 		} else {
   2282 			i = parse_disk_pos(isize, &localsizemult,
   2283 			    parts->bytes_per_sector,
   2284 			    parts->pscheme->get_cylinder_size(parts), NULL);
   2285 			if (localsizemult != sizemult)
   2286 				max_r = max;
   2287 		}
   2288 		if (i < 0) {
   2289 			errmsg = msg_string(MSG_Invalid_numeric);
   2290 			continue;
   2291 		} else if (i > max_r) {
   2292 			errmsg = msg_string(MSG_Too_large);
   2293 			continue;
   2294 		}
   2295 		/*
   2296 		 * partend is aligned to a cylinder if localsizemult
   2297 		 * is not 1 sector
   2298 		 */
   2299 		int cylsize = parts->pscheme->get_cylinder_size(parts);
   2300 		partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
   2301 		    localsizemult, cylsize);
   2302 		/* Align to end-of-disk or end-of-slice if close enough */
   2303 		if (partend > (diskend - sizemult)
   2304 		    && partend < (diskend + sizemult))
   2305 			partend = diskend;
   2306 		if (partend > (partstart + max - sizemult)
   2307 		    && partend < (partstart + max + sizemult))
   2308 			partend = partstart + max;
   2309 		/* sanity checks */
   2310 		if (partend > diskend) {
   2311 			partend = diskend;
   2312 			errmsg = msg_string(MSG_endoutsidedisk);
   2313 			continue;
   2314 		}
   2315 		free(label_msg);
   2316 		if (partend < partstart)
   2317 			return 0;
   2318 		return (partend - partstart);
   2319 	}
   2320 	/* NOTREACHED */
   2321 }
   2322 
   2323 /*
   2324  * convert a string to a number of sectors, with a possible unit
   2325  * 150M = 150 Megabytes
   2326  * 2000c = 2000 cylinders
   2327  * 150256s = 150256 sectors
   2328  * Without units, use the default (sizemult).
   2329  * returns the raw input value, and the unit used. Caller needs to multiply!
   2330  * On invalid inputs, returns -1.
   2331  */
   2332 daddr_t
   2333 parse_disk_pos(
   2334 	const char *str,
   2335 	daddr_t *localsizemult,
   2336 	daddr_t bps,
   2337 	daddr_t cyl_size,
   2338 	bool *extend_this)
   2339 {
   2340 	daddr_t val;
   2341 	char *cp;
   2342 	bool mult_found;
   2343 
   2344 	if (str[0] == '\0') {
   2345 		return -1;
   2346 	}
   2347 	val = strtoull(str, &cp, 10);
   2348 	mult_found = false;
   2349 	if (extend_this)
   2350 		*extend_this = false;
   2351 	while (*cp != 0) {
   2352 		if (*cp == 'G' || *cp == 'g') {
   2353 			if (mult_found)
   2354 				return -1;
   2355 			*localsizemult = GIG / bps;
   2356 			goto next;
   2357 		}
   2358 		if (*cp == 'M' || *cp == 'm') {
   2359 			if (mult_found)
   2360 				return -1;
   2361 			*localsizemult = MEG / bps;
   2362 			goto next;
   2363 		}
   2364 		if (*cp == 'c' || *cp == 'C') {
   2365 			if (mult_found)
   2366 				return -1;
   2367 			*localsizemult = cyl_size;
   2368 			goto next;
   2369 		}
   2370 		if (*cp == 's' || *cp == 'S') {
   2371 			if (mult_found)
   2372 				return -1;
   2373 			*localsizemult = 1;
   2374 			goto next;
   2375 		}
   2376 		if (*cp == '+' && extend_this) {
   2377 			*extend_this = true;
   2378 			cp++;
   2379 			break;
   2380 		}
   2381 
   2382 		/* not a known unit */
   2383 		return -1;
   2384 
   2385 next:
   2386 		mult_found = true;
   2387 		cp++;
   2388 		continue;
   2389 	}
   2390 	if (*cp != 0)
   2391 		return -1;
   2392 
   2393 	return val;
   2394 }
   2395