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