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