xref: /freebsd/lib/geom/part/geom_part.c (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007, 2008 Marcel Moolenaar
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 THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * 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 THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/stat.h>
33 #include <sys/vtoc.h>
34 
35 #include <assert.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <libgeom.h>
41 #include <libutil.h>
42 #include <paths.h>
43 #include <signal.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <limits.h>
48 #include <inttypes.h>
49 #include <string.h>
50 #include <strings.h>
51 #include <unistd.h>
52 
53 #include "core/geom.h"
54 #include "misc/subr.h"
55 
56 #ifdef STATIC_GEOM_CLASSES
57 #define	PUBSYM(x)	gpart_##x
58 #else
59 #define	PUBSYM(x)	x
60 #endif
61 
62 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
63 uint32_t PUBSYM(version) = 0;
64 
65 static char sstart[32];
66 static char ssize[32];
67 volatile sig_atomic_t undo_restore;
68 
69 #define	GPART_AUTOFILL	"*"
70 #define	GPART_FLAGS	"C"
71 
72 #define	GPART_PARAM_BOOTCODE	"bootcode"
73 #define	GPART_PARAM_INDEX	"index"
74 #define	GPART_PARAM_PARTCODE	"partcode"
75 #define	GPART_PARAM_SKIP_DSN	"skip_dsn"
76 
77 static struct gclass *find_class(struct gmesh *, const char *);
78 static struct ggeom * find_geom(struct gclass *, const char *);
79 static int geom_is_withered(struct ggeom *);
80 static const char *find_geomcfg(struct ggeom *, const char *);
81 static const char *find_provcfg(struct gprovider *, const char *);
82 static struct gprovider *find_provider(struct ggeom *, off_t);
83 static const char *fmtsize(int64_t);
84 static int gpart_autofill(struct gctl_req *);
85 static int gpart_autofill_resize(struct gctl_req *);
86 static void gpart_bootcode(struct gctl_req *, unsigned int);
87 static void *gpart_bootfile_read(const char *, ssize_t *);
88 static _Noreturn void gpart_issue(struct gctl_req *, unsigned int);
89 static void gpart_show(struct gctl_req *, unsigned int);
90 static void gpart_show_geom(struct ggeom *, const char *, int);
91 static int gpart_show_hasopt(struct gctl_req *, const char *, const char *);
92 static void gpart_write_partcode(struct ggeom *, int, void *, ssize_t);
93 static void gpart_write_partcode_vtoc8(struct ggeom *, int, void *);
94 static void gpart_print_error(const char *);
95 static void gpart_backup(struct gctl_req *, unsigned int);
96 static void gpart_restore(struct gctl_req *, unsigned int);
97 
98 struct g_command PUBSYM(class_commands)[] = {
99 	{ "add", 0, gpart_issue, {
100 		{ 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
101 		{ 'b', "start", GPART_AUTOFILL, G_TYPE_STRING },
102 		{ 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
103 		{ 't', "type", NULL, G_TYPE_STRING },
104 		{ 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
105 		{ 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
106 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
107 		G_OPT_SENTINEL },
108 	    "-t type [-a alignment] [-b start] [-s size] [-i index] "
109 		"[-l label] [-f flags] geom"
110 	},
111 	{ "backup", 0, gpart_backup, G_NULL_OPTS,
112 	    "geom"
113 	},
114 	{ "bootcode", 0, gpart_bootcode, {
115 		{ 'b', GPART_PARAM_BOOTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
116 		{ 'p', GPART_PARAM_PARTCODE, G_VAL_OPTIONAL, G_TYPE_STRING },
117 		{ 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
118 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
119 		{ 'N', GPART_PARAM_SKIP_DSN, NULL, G_TYPE_BOOL },
120 		G_OPT_SENTINEL },
121 	    "[-N] [-b bootcode] [-p partcode -i index] [-f flags] geom"
122 	},
123 	{ "commit", 0, gpart_issue, G_NULL_OPTS,
124 	    "geom"
125 	},
126 	{ "create", 0, gpart_issue, {
127 		{ 's', "scheme", NULL, G_TYPE_STRING },
128 		{ 'n', "entries", G_VAL_OPTIONAL, G_TYPE_NUMBER },
129 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
130 		G_OPT_SENTINEL },
131 	    "-s scheme [-n entries] [-f flags] provider"
132 	},
133 	{ "delete", 0, gpart_issue, {
134 		{ 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
135 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
136 		G_OPT_SENTINEL },
137 	    "-i index [-f flags] geom"
138 	},
139 	{ "destroy", 0, gpart_issue, {
140 		{ 'F', "force", NULL, G_TYPE_BOOL },
141 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
142 		G_OPT_SENTINEL },
143 	    "[-F] [-f flags] geom"
144 	},
145 	{ "modify", 0, gpart_issue, {
146 		{ 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
147 		{ 'l', "label", G_VAL_OPTIONAL, G_TYPE_STRING },
148 		{ 't', "type", G_VAL_OPTIONAL, G_TYPE_STRING },
149 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
150 		G_OPT_SENTINEL },
151 	    "-i index [-l label] [-t type] [-f flags] geom"
152 	},
153 	{ "set", 0, gpart_issue, {
154 		{ 'a', "attrib", NULL, G_TYPE_STRING },
155 		{ 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
156 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
157 		G_OPT_SENTINEL },
158 	    "-a attrib [-i index] [-f flags] geom"
159 	},
160 	{ "show", 0, gpart_show, {
161 		{ 'l', "show_label", NULL, G_TYPE_BOOL },
162 		{ 'r', "show_rawtype", NULL, G_TYPE_BOOL },
163 		{ 'p', "show_providers", NULL, G_TYPE_BOOL },
164 		G_OPT_SENTINEL },
165 	    "[-l | -r] [-p] [geom ...]"
166 	},
167 	{ "undo", 0, gpart_issue, G_NULL_OPTS,
168 	    "geom"
169 	},
170 	{ "unset", 0, gpart_issue, {
171 		{ 'a', "attrib", NULL, G_TYPE_STRING },
172 		{ 'i', GPART_PARAM_INDEX, G_VAL_OPTIONAL, G_TYPE_NUMBER },
173 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
174 		G_OPT_SENTINEL },
175 	    "-a attrib [-i index] [-f flags] geom"
176 	},
177 	{ "resize", 0, gpart_issue, {
178 		{ 'a', "alignment", GPART_AUTOFILL, G_TYPE_STRING },
179 		{ 's', "size", GPART_AUTOFILL, G_TYPE_STRING },
180 		{ 'i', GPART_PARAM_INDEX, NULL, G_TYPE_NUMBER },
181 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
182 		G_OPT_SENTINEL },
183 	    "-i index [-a alignment] [-s size] [-f flags] geom"
184 	},
185 	{ "restore", 0, gpart_restore, {
186 		{ 'F', "force", NULL, G_TYPE_BOOL },
187 		{ 'l', "restore_labels", NULL, G_TYPE_BOOL },
188 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
189 		G_OPT_SENTINEL },
190 	    "[-lF] [-f flags] provider [...]"
191 	},
192 	{ "recover", 0, gpart_issue, {
193 		{ 'f', "flags", GPART_FLAGS, G_TYPE_STRING },
194 		G_OPT_SENTINEL },
195 	    "[-f flags] geom"
196 	},
197 	G_CMD_SENTINEL
198 };
199 
200 static struct gclass *
201 find_class(struct gmesh *mesh, const char *name)
202 {
203 	struct gclass *classp;
204 
205 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
206 		if (strcmp(classp->lg_name, name) == 0)
207 			return (classp);
208 	}
209 	return (NULL);
210 }
211 
212 static struct ggeom *
213 find_geom(struct gclass *classp, const char *name)
214 {
215 	struct ggeom *gp, *wgp;
216 
217 	if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
218 		name += sizeof(_PATH_DEV) - 1;
219 	wgp = NULL;
220 	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
221 		if (strcmp(gp->lg_name, name) != 0)
222 			continue;
223 		if (!geom_is_withered(gp))
224 			return (gp);
225 		else
226 			wgp = gp;
227 	}
228 	return (wgp);
229 }
230 
231 static int
232 geom_is_withered(struct ggeom *gp)
233 {
234 	struct gconfig *gc;
235 
236 	LIST_FOREACH(gc, &gp->lg_config, lg_config) {
237 		if (!strcmp(gc->lg_name, "wither"))
238 			return (1);
239 	}
240 	return (0);
241 }
242 
243 static const char *
244 find_geomcfg(struct ggeom *gp, const char *cfg)
245 {
246 	struct gconfig *gc;
247 
248 	LIST_FOREACH(gc, &gp->lg_config, lg_config) {
249 		if (!strcmp(gc->lg_name, cfg))
250 			return (gc->lg_val);
251 	}
252 	return (NULL);
253 }
254 
255 static const char *
256 find_provcfg(struct gprovider *pp, const char *cfg)
257 {
258 	struct gconfig *gc;
259 
260 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
261 		if (!strcmp(gc->lg_name, cfg))
262 			return (gc->lg_val);
263 	}
264 	return (NULL);
265 }
266 
267 static struct gprovider *
268 find_provider(struct ggeom *gp, off_t minsector)
269 {
270 	struct gprovider *pp, *bestpp;
271 	const char *s;
272 	off_t sector, bestsector;
273 
274 	bestpp = NULL;
275 	bestsector = 0;
276 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
277 		s = find_provcfg(pp, "start");
278 		sector = (off_t)strtoimax(s, NULL, 0);
279 		if (sector < minsector)
280 			continue;
281 		if (bestpp != NULL && sector >= bestsector)
282 			continue;
283 
284 		bestpp = pp;
285 		bestsector = sector;
286 	}
287 	return (bestpp);
288 }
289 
290 static const char *
291 fmtsize(int64_t rawsz)
292 {
293 	static char buf[5];
294 
295 	humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
296 	    HN_B | HN_NOSPACE | HN_DECIMAL);
297 	return (buf);
298 }
299 
300 static const char *
301 fmtattrib(struct gprovider *pp)
302 {
303 	static char buf[128];
304 	struct gconfig *gc;
305 	u_int idx;
306 
307 	buf[0] = '\0';
308 	idx = 0;
309 	LIST_FOREACH(gc, &pp->lg_config, lg_config) {
310 		if (strcmp(gc->lg_name, "attrib") != 0)
311 			continue;
312 		idx += snprintf(buf + idx, sizeof(buf) - idx, "%s%s",
313 		    (idx == 0) ? " [" : ",", gc->lg_val);
314 	}
315 	if (idx > 0)
316 		snprintf(buf + idx, sizeof(buf) - idx, "] ");
317 	return (buf);
318 }
319 
320 #define	ALIGNDOWN(d, a)	((d) - (d) % (a))
321 #define	ALIGNUP(d, a)	((d) % (a) ? (d) - (d) % (a) + (a): (d))
322 
323 static int
324 gpart_autofill_resize(struct gctl_req *req)
325 {
326 	struct gmesh mesh;
327 	struct gclass *cp;
328 	struct ggeom *gp;
329 	struct gprovider *pp;
330 	off_t last, size, start, new_size;
331 	off_t lba, new_lba, alignment, offset;
332 	const char *s;
333 	int error, idx, has_alignment;
334 
335 	idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
336 	if (idx < 1)
337 		errx(EXIT_FAILURE, "invalid partition index");
338 
339 	error = geom_gettree(&mesh);
340 	if (error)
341 		return (error);
342 	s = gctl_get_ascii(req, "class");
343 	if (s == NULL)
344 		abort();
345 	cp = find_class(&mesh, s);
346 	if (cp == NULL)
347 		errx(EXIT_FAILURE, "Class %s not found.", s);
348 	s = gctl_get_ascii(req, "arg0");
349 	if (s == NULL)
350 		abort();
351 	gp = find_geom(cp, s);
352 	if (gp == NULL)
353 		errx(EXIT_FAILURE, "No such geom: %s.", s);
354 	pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
355 	if (pp == NULL)
356 		errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
357 
358 	s = gctl_get_ascii(req, "alignment");
359 	has_alignment = (*s == '*') ? 0 : 1;
360 	alignment = 1;
361 	if (has_alignment) {
362 		error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
363 		if (error)
364 			errc(EXIT_FAILURE, error, "Invalid alignment param");
365 		if (alignment == 0)
366 			errx(EXIT_FAILURE, "Invalid alignment param");
367 	} else {
368 		lba = pp->lg_stripesize / pp->lg_sectorsize;
369 		if (lba > 0)
370 			alignment = lba;
371 	}
372 	error = gctl_delete_param(req, "alignment");
373 	if (error)
374 		errc(EXIT_FAILURE, error, "internal error");
375 
376 	s = gctl_get_ascii(req, "size");
377 	if (*s == '*')
378 		new_size = 0;
379 	else {
380 		error = g_parse_lba(s, pp->lg_sectorsize, &new_size);
381 		if (error)
382 			errc(EXIT_FAILURE, error, "Invalid size param");
383 		/* no autofill necessary. */
384 		if (has_alignment == 0)
385 			goto done;
386 	}
387 
388 	offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
389 	s = find_geomcfg(gp, "last");
390 	if (s == NULL)
391 		errx(EXIT_FAILURE, "Final block not found for geom %s",
392 		    gp->lg_name);
393 	last = (off_t)strtoimax(s, NULL, 0);
394 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
395 		s = find_provcfg(pp, "index");
396 		if (s == NULL)
397 			continue;
398 		if (atoi(s) == idx)
399 			break;
400 	}
401 	if (pp == NULL)
402 		errx(EXIT_FAILURE, "invalid partition index");
403 
404 	s = find_provcfg(pp, "start");
405 	start = (off_t)strtoimax(s, NULL, 0);
406 	s = find_provcfg(pp, "end");
407 	lba = (off_t)strtoimax(s, NULL, 0);
408 	size = lba - start + 1;
409 
410 	pp = find_provider(gp, lba + 1);
411 	if (new_size > 0 && (new_size <= size || pp == NULL)) {
412 		/* The start offset may be not aligned, so we align the end
413 		 * offset and then calculate the size.
414 		 */
415 		new_size = ALIGNDOWN(start + offset + new_size,
416 		    alignment) - start - offset;
417 		goto done;
418 	}
419 	if (pp == NULL) {
420 		new_size = ALIGNDOWN(last + offset + 1, alignment) -
421 		    start - offset;
422 		if (new_size < size)
423 			return (ENOSPC);
424 	} else {
425 		s = find_provcfg(pp, "start");
426 		new_lba = (off_t)strtoimax(s, NULL, 0);
427 		/*
428 		 * Is there any free space between current and
429 		 * next providers?
430 		 */
431 		new_lba = ALIGNDOWN(new_lba + offset, alignment) - offset;
432 		if (new_lba > lba)
433 			new_size = new_lba - start;
434 		else {
435 			geom_deletetree(&mesh);
436 			return (ENOSPC);
437 		}
438 	}
439 done:
440 	snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)new_size);
441 	gctl_change_param(req, "size", -1, ssize);
442 	geom_deletetree(&mesh);
443 	return (0);
444 }
445 
446 static int
447 gpart_autofill(struct gctl_req *req)
448 {
449 	struct gmesh mesh;
450 	struct gclass *cp;
451 	struct ggeom *gp;
452 	struct gprovider *pp;
453 	off_t first, last, a_first;
454 	off_t size, start, a_lba;
455 	off_t lba, len, alignment, offset;
456 	uintmax_t grade;
457 	const char *s;
458 	int error, has_size, has_start, has_alignment;
459 
460 	s = gctl_get_ascii(req, "verb");
461 	if (strcmp(s, "resize") == 0)
462 		return gpart_autofill_resize(req);
463 	if (strcmp(s, "add") != 0)
464 		return (0);
465 
466 	error = geom_gettree(&mesh);
467 	if (error)
468 		return (error);
469 	s = gctl_get_ascii(req, "class");
470 	if (s == NULL)
471 		abort();
472 	cp = find_class(&mesh, s);
473 	if (cp == NULL)
474 		errx(EXIT_FAILURE, "Class %s not found.", s);
475 	s = gctl_get_ascii(req, "arg0");
476 	if (s == NULL)
477 		abort();
478 	gp = find_geom(cp, s);
479 	if (gp == NULL) {
480 		if (g_device_path(s) == NULL) {
481 			errx(EXIT_FAILURE, "No such geom %s.", s);
482 		} else {
483 			/*
484 			 * We don't free memory allocated by g_device_path() as
485 			 * we are about to exit.
486 			 */
487 			errx(EXIT_FAILURE,
488 			    "No partitioning scheme found on geom %s. Create one first using 'gpart create'.",
489 			    s);
490 		}
491 	}
492 	pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
493 	if (pp == NULL)
494 		errx(EXIT_FAILURE, "Provider for geom %s not found.", s);
495 
496 	s = gctl_get_ascii(req, "alignment");
497 	has_alignment = (*s == '*') ? 0 : 1;
498 	alignment = 1;
499 	if (has_alignment) {
500 		error = g_parse_lba(s, pp->lg_sectorsize, &alignment);
501 		if (error)
502 			errc(EXIT_FAILURE, error, "Invalid alignment param");
503 		if (alignment == 0)
504 			errx(EXIT_FAILURE, "Invalid alignment param");
505 	}
506 	error = gctl_delete_param(req, "alignment");
507 	if (error)
508 		errc(EXIT_FAILURE, error, "internal error");
509 
510 	s = gctl_get_ascii(req, "size");
511 	has_size = (*s == '*') ? 0 : 1;
512 	size = 0;
513 	if (has_size) {
514 		error = g_parse_lba(s, pp->lg_sectorsize, &size);
515 		if (error)
516 			errc(EXIT_FAILURE, error, "Invalid size param");
517 	}
518 
519 	s = gctl_get_ascii(req, "start");
520 	has_start = (*s == '*') ? 0 : 1;
521 	start = 0ULL;
522 	if (has_start) {
523 		error = g_parse_lba(s, pp->lg_sectorsize, &start);
524 		if (error)
525 			errc(EXIT_FAILURE, error, "Invalid start param");
526 	}
527 
528 	/* No autofill necessary. */
529 	if (has_size && has_start && !has_alignment)
530 		goto done;
531 
532 	len = pp->lg_stripesize / pp->lg_sectorsize;
533 	if (len > 0 && !has_alignment)
534 		alignment = len;
535 
536 	/* Adjust parameters to stripeoffset */
537 	offset = (pp->lg_stripeoffset / pp->lg_sectorsize) % alignment;
538 	start = ALIGNUP(start + offset, alignment);
539 	if (size > alignment)
540 		size = ALIGNDOWN(size, alignment);
541 
542 	s = find_geomcfg(gp, "first");
543 	if (s == NULL)
544 		errx(EXIT_FAILURE, "Starting block not found for geom %s",
545 		    gp->lg_name);
546 	first = (off_t)strtoimax(s, NULL, 0);
547 	s = find_geomcfg(gp, "last");
548 	if (s == NULL)
549 		errx(EXIT_FAILURE, "Final block not found for geom %s",
550 		    gp->lg_name);
551 	last = (off_t)strtoimax(s, NULL, 0);
552 	grade = ~0ULL;
553 	a_first = ALIGNUP(first + offset, alignment);
554 	last = ALIGNDOWN(last + offset + 1, alignment) - 1;
555 	if (a_first < start)
556 		a_first = start;
557 	while ((pp = find_provider(gp, first)) != NULL) {
558 		s = find_provcfg(pp, "start");
559 		lba = (off_t)strtoimax(s, NULL, 0);
560 		a_lba = ALIGNDOWN(lba + offset, alignment);
561 		if (first < a_lba && a_first < a_lba) {
562 			/* Free space [first, lba> */
563 			len = a_lba - a_first;
564 			if (has_size) {
565 				if (len >= size &&
566 				    (uintmax_t)(len - size) < grade) {
567 					start = a_first;
568 					grade = len - size;
569 				}
570 			} else if (has_start) {
571 				if (start >= a_first && start < a_lba) {
572 					size = a_lba - start;
573 					grade = start - a_first;
574 				}
575 			} else {
576 				if (grade == ~0ULL || len > size) {
577 					start = a_first;
578 					size = len;
579 					grade = 0;
580 				}
581 			}
582 		}
583 
584 		s = find_provcfg(pp, "end");
585 		first = (off_t)strtoimax(s, NULL, 0) + 1;
586 		if (first + offset > a_first)
587 			a_first = ALIGNUP(first + offset, alignment);
588 	}
589 	if (a_first <= last) {
590 		/* Free space [first-last] */
591 		len = ALIGNDOWN(last - a_first + 1, alignment);
592 		if (has_size) {
593 			if (len >= size &&
594 			    (uintmax_t)(len - size) < grade) {
595 				start = a_first;
596 				grade = len - size;
597 			}
598 		} else if (has_start) {
599 			if (start >= a_first && start <= last) {
600 				size = ALIGNDOWN(last - start + 1, alignment);
601 				grade = start - a_first;
602 			}
603 		} else {
604 			if (grade == ~0ULL || len > size) {
605 				start = a_first;
606 				size = len;
607 				grade = 0;
608 			}
609 		}
610 	}
611 	if (grade == ~0ULL) {
612 		geom_deletetree(&mesh);
613 		return (ENOSPC);
614 	}
615 	start -= offset;	/* Return back to real offset */
616 done:
617 	snprintf(ssize, sizeof(ssize), "%jd", (intmax_t)size);
618 	gctl_change_param(req, "size", -1, ssize);
619 	snprintf(sstart, sizeof(sstart), "%jd", (intmax_t)start);
620 	gctl_change_param(req, "start", -1, sstart);
621 	geom_deletetree(&mesh);
622 	return (0);
623 }
624 
625 static void
626 gpart_show_geom(struct ggeom *gp, const char *element, int show_providers)
627 {
628 	struct gprovider *pp;
629 	const char *s, *scheme;
630 	off_t first, last, sector, end;
631 	off_t length, secsz;
632 	int idx, wblocks, wname, wmax;
633 
634 	if (geom_is_withered(gp))
635 		return;
636 	scheme = find_geomcfg(gp, "scheme");
637 	if (scheme == NULL)
638 		errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
639 	s = find_geomcfg(gp, "first");
640 	if (s == NULL)
641 		errx(EXIT_FAILURE, "Starting block not found for geom %s",
642 		    gp->lg_name);
643 	first = (off_t)strtoimax(s, NULL, 0);
644 	s = find_geomcfg(gp, "last");
645 	if (s == NULL)
646 		errx(EXIT_FAILURE, "Final block not found for geom %s",
647 		    gp->lg_name);
648 	last = (off_t)strtoimax(s, NULL, 0);
649 	wblocks = strlen(s);
650 	s = find_geomcfg(gp, "state");
651 	if (s == NULL)
652 		errx(EXIT_FAILURE, "State not found for geom %s", gp->lg_name);
653 	if (s != NULL && *s != 'C')
654 		s = NULL;
655 	wmax = strlen(gp->lg_name);
656 	if (show_providers) {
657 		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
658 			wname = strlen(pp->lg_name);
659 			if (wname > wmax)
660 				wmax = wname;
661 		}
662 	}
663 	wname = wmax;
664 	pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
665 	secsz = pp->lg_sectorsize;
666 	printf("=>%*jd  %*jd  %*s  %s  (%s)%s\n",
667 	    wblocks, (intmax_t)first, wblocks, (intmax_t)(last - first + 1),
668 	    wname, gp->lg_name,
669 	    scheme, fmtsize(pp->lg_mediasize),
670 	    s ? " [CORRUPT]": "");
671 
672 	while ((pp = find_provider(gp, first)) != NULL) {
673 		s = find_provcfg(pp, "start");
674 		sector = (off_t)strtoimax(s, NULL, 0);
675 
676 		s = find_provcfg(pp, "end");
677 		end = (off_t)strtoimax(s, NULL, 0);
678 		length = end - sector + 1;
679 
680 		s = find_provcfg(pp, "index");
681 		idx = atoi(s);
682 		if (first < sector) {
683 			printf("  %*jd  %*jd  %*s  - free -  (%s)\n",
684 			    wblocks, (intmax_t)first, wblocks,
685 			    (intmax_t)(sector - first), wname, "",
686 			    fmtsize((sector - first) * secsz));
687 		}
688 		if (show_providers) {
689 			printf("  %*jd  %*jd  %*s  %s %s (%s)\n",
690 			    wblocks, (intmax_t)sector, wblocks,
691 			    (intmax_t)length, wname, pp->lg_name,
692 			    find_provcfg(pp, element), fmtattrib(pp),
693 			    fmtsize(pp->lg_mediasize));
694 		} else
695 			printf("  %*jd  %*jd  %*d  %s %s (%s)\n",
696 			    wblocks, (intmax_t)sector, wblocks,
697 			    (intmax_t)length, wname, idx,
698 			    find_provcfg(pp, element), fmtattrib(pp),
699 			    fmtsize(pp->lg_mediasize));
700 		first = end + 1;
701 	}
702 	if (first <= last) {
703 		length = last - first + 1;
704 		printf("  %*jd  %*jd  %*s  - free -  (%s)\n",
705 		    wblocks, (intmax_t)first, wblocks, (intmax_t)length,
706 		    wname, "",
707 		    fmtsize(length * secsz));
708 	}
709 	printf("\n");
710 }
711 
712 static int
713 gpart_show_hasopt(struct gctl_req *req, const char *opt, const char *elt)
714 {
715 
716 	if (!gctl_get_int(req, "%s", opt))
717 		return (0);
718 
719 	if (elt != NULL)
720 		errx(EXIT_FAILURE, "-l and -r are mutually exclusive");
721 
722 	return (1);
723 }
724 
725 static void
726 gpart_show(struct gctl_req *req, unsigned int fl __unused)
727 {
728 	struct gmesh mesh;
729 	struct gclass *classp;
730 	struct ggeom *gp;
731 	const char *element, *name;
732 	int error, i, nargs, show_providers;
733 
734 	element = NULL;
735 	if (gpart_show_hasopt(req, "show_label", element))
736 		element = "label";
737 	if (gpart_show_hasopt(req, "show_rawtype", element))
738 		element = "rawtype";
739 	if (element == NULL)
740 		element = "type";
741 
742 	name = gctl_get_ascii(req, "class");
743 	if (name == NULL)
744 		abort();
745 	error = geom_gettree(&mesh);
746 	if (error != 0)
747 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
748 	classp = find_class(&mesh, name);
749 	if (classp == NULL) {
750 		geom_deletetree(&mesh);
751 		errx(EXIT_FAILURE, "Class %s not found.", name);
752 	}
753 	show_providers = gctl_get_int(req, "show_providers");
754 	nargs = gctl_get_int(req, "nargs");
755 	if (nargs > 0) {
756 		for (i = 0; i < nargs; i++) {
757 			name = gctl_get_ascii(req, "arg%d", i);
758 			gp = find_geom(classp, name);
759 			if (gp != NULL)
760 				gpart_show_geom(gp, element, show_providers);
761 			else
762 				errx(EXIT_FAILURE, "No such geom: %s.", name);
763 		}
764 	} else {
765 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
766 			gpart_show_geom(gp, element, show_providers);
767 		}
768 	}
769 	geom_deletetree(&mesh);
770 }
771 
772 static void
773 gpart_backup(struct gctl_req *req, unsigned int fl __unused)
774 {
775 	struct gmesh mesh;
776 	struct gclass *classp;
777 	struct gprovider *pp;
778 	struct ggeom *gp;
779 	const char *s, *scheme;
780 	off_t sector, end;
781 	off_t length;
782 	int error, i, windex, wblocks, wtype;
783 
784 	if (gctl_get_int(req, "nargs") != 1)
785 		errx(EXIT_FAILURE, "Invalid number of arguments.");
786 	error = geom_gettree(&mesh);
787 	if (error != 0)
788 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
789 	s = gctl_get_ascii(req, "class");
790 	if (s == NULL)
791 		abort();
792 	classp = find_class(&mesh, s);
793 	if (classp == NULL) {
794 		geom_deletetree(&mesh);
795 		errx(EXIT_FAILURE, "Class %s not found.", s);
796 	}
797 	s = gctl_get_ascii(req, "arg0");
798 	if (s == NULL)
799 		abort();
800 	gp = find_geom(classp, s);
801 	if (gp == NULL)
802 		errx(EXIT_FAILURE, "No such geom: %s.", s);
803 	scheme = find_geomcfg(gp, "scheme");
804 	if (scheme == NULL)
805 		abort();
806 	pp = LIST_FIRST(&gp->lg_consumer)->lg_provider;
807 	s = find_geomcfg(gp, "last");
808 	if (s == NULL)
809 		abort();
810 	wblocks = strlen(s);
811 	wtype = 0;
812 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
813 		s = find_provcfg(pp, "type");
814 		i = strlen(s);
815 		if (i > wtype)
816 			wtype = i;
817 	}
818 	s = find_geomcfg(gp, "entries");
819 	if (s == NULL)
820 		abort();
821 	windex = strlen(s);
822 	printf("%s %s\n", scheme, s);
823 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
824 		s = find_provcfg(pp, "start");
825 		sector = (off_t)strtoimax(s, NULL, 0);
826 
827 		s = find_provcfg(pp, "end");
828 		end = (off_t)strtoimax(s, NULL, 0);
829 		length = end - sector + 1;
830 
831 		s = find_provcfg(pp, "label");
832 		printf("%-*s %*s %*jd %*jd %s %s\n",
833 		    windex, find_provcfg(pp, "index"),
834 		    wtype, find_provcfg(pp, "type"),
835 		    wblocks, (intmax_t)sector,
836 		    wblocks, (intmax_t)length,
837 		    (s != NULL) ? s: "", fmtattrib(pp));
838 	}
839 	geom_deletetree(&mesh);
840 }
841 
842 static int
843 skip_line(const char *p)
844 {
845 
846 	while (*p != '\0') {
847 		if (*p == '#')
848 			return (1);
849 		if (isspace(*p) == 0)
850 			return (0);
851 		p++;
852 	}
853 	return (1);
854 }
855 
856 static void
857 gpart_sighndl(int sig __unused)
858 {
859 	undo_restore = 1;
860 }
861 
862 static void
863 gpart_restore(struct gctl_req *req, unsigned int fl __unused)
864 {
865 	struct gmesh mesh;
866 	struct gclass *classp;
867 	struct gctl_req *r;
868 	struct ggeom *gp;
869 	struct sigaction si_sa;
870 	const char *s, *flags, *errstr, *label;
871 	char **ap, *argv[6], line[BUFSIZ], *pline;
872 	int error, forced, i, l, nargs, created, rl;
873 	intmax_t n;
874 
875 	nargs = gctl_get_int(req, "nargs");
876 	if (nargs < 1)
877 		errx(EXIT_FAILURE, "Invalid number of arguments.");
878 
879 	forced = gctl_get_int(req, "force");
880 	flags = gctl_get_ascii(req, "flags");
881 	rl = gctl_get_int(req, "restore_labels");
882 	s = gctl_get_ascii(req, "class");
883 	if (s == NULL)
884 		abort();
885 	error = geom_gettree(&mesh);
886 	if (error != 0)
887 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
888 	classp = find_class(&mesh, s);
889 	if (classp == NULL) {
890 		geom_deletetree(&mesh);
891 		errx(EXIT_FAILURE, "Class %s not found.", s);
892 	}
893 
894 	sigemptyset(&si_sa.sa_mask);
895 	si_sa.sa_flags = 0;
896 	si_sa.sa_handler = gpart_sighndl;
897 	if (sigaction(SIGINT, &si_sa, 0) == -1)
898 		err(EXIT_FAILURE, "sigaction SIGINT");
899 
900 	if (forced) {
901 		/* destroy existent partition table before restore */
902 		for (i = 0; i < nargs; i++) {
903 			s = gctl_get_ascii(req, "arg%d", i);
904 			gp = find_geom(classp, s);
905 			if (gp != NULL) {
906 				r = gctl_get_handle();
907 				gctl_ro_param(r, "class", -1,
908 				    classp->lg_name);
909 				gctl_ro_param(r, "verb", -1, "destroy");
910 				gctl_ro_param(r, "flags", -1, "restore");
911 				gctl_ro_param(r, "force", sizeof(forced),
912 				    &forced);
913 				gctl_ro_param(r, "arg0", -1, s);
914 				errstr = gctl_issue(r);
915 				if (errstr != NULL && errstr[0] != '\0') {
916 					gpart_print_error(errstr);
917 					gctl_free(r);
918 					goto backout;
919 				}
920 				gctl_free(r);
921 			}
922 		}
923 	}
924 	created = 0;
925 	while (undo_restore == 0 &&
926 	    fgets(line, sizeof(line) - 1, stdin) != NULL) {
927 		/* Format of backup entries:
928 		 * <scheme name> <number of entries>
929 		 * <index> <type> <start> <size> [label] ['['attrib[,attrib]']']
930 		 */
931 		pline = (char *)line;
932 		pline[strlen(line) - 1] = 0;
933 		if (skip_line(pline))
934 			continue;
935 		for (ap = argv;
936 		    (*ap = strsep(&pline, " \t")) != NULL;)
937 			if (**ap != '\0' && ++ap >= &argv[6])
938 				break;
939 		l = ap - &argv[0];
940 		label = pline = NULL;
941 		if (l == 1 || l == 2) { /* create table */
942 			if (created)
943 				errx(EXIT_FAILURE, "Incorrect backup format.");
944 			if (l == 2)
945 				n = strtoimax(argv[1], NULL, 0);
946 			for (i = 0; i < nargs; i++) {
947 				s = gctl_get_ascii(req, "arg%d", i);
948 				r = gctl_get_handle();
949 				gctl_ro_param(r, "class", -1,
950 				    classp->lg_name);
951 				gctl_ro_param(r, "verb", -1, "create");
952 				gctl_ro_param(r, "scheme", -1, argv[0]);
953 				if (l == 2)
954 					gctl_ro_param(r, "entries",
955 					    sizeof(n), &n);
956 				gctl_ro_param(r, "flags", -1, "restore");
957 				gctl_ro_param(r, "arg0", -1, s);
958 				errstr = gctl_issue(r);
959 				if (errstr != NULL && errstr[0] != '\0') {
960 					gpart_print_error(errstr);
961 					gctl_free(r);
962 					goto backout;
963 				}
964 				gctl_free(r);
965 			}
966 			created = 1;
967 			continue;
968 		} else if (l < 4 || created == 0)
969 			errx(EXIT_FAILURE, "Incorrect backup format.");
970 		else if (l == 5) {
971 			if (strchr(argv[4], '[') == NULL)
972 				label = argv[4];
973 			else
974 				pline = argv[4];
975 		} else if (l == 6) {
976 			label = argv[4];
977 			pline = argv[5];
978 		}
979 		/* Add partitions to each table */
980 		for (i = 0; i < nargs; i++) {
981 			s = gctl_get_ascii(req, "arg%d", i);
982 			r = gctl_get_handle();
983 			n = strtoimax(argv[0], NULL, 0);
984 			gctl_ro_param(r, "class", -1, classp->lg_name);
985 			gctl_ro_param(r, "verb", -1, "add");
986 			gctl_ro_param(r, "flags", -1, "restore");
987 			gctl_ro_param(r, GPART_PARAM_INDEX, sizeof(n), &n);
988 			gctl_ro_param(r, "type", -1, argv[1]);
989 			gctl_ro_param(r, "start", -1, argv[2]);
990 			gctl_ro_param(r, "size", -1, argv[3]);
991 			if (rl != 0 && label != NULL)
992 				gctl_ro_param(r, "label", -1, argv[4]);
993 			gctl_ro_param(r, "alignment", -1, GPART_AUTOFILL);
994 			gctl_ro_param(r, "arg0", -1, s);
995 			error = gpart_autofill(r);
996 			if (error != 0)
997 				errc(EXIT_FAILURE, error, "autofill");
998 			errstr = gctl_issue(r);
999 			if (errstr != NULL && errstr[0] != '\0') {
1000 				gpart_print_error(errstr);
1001 				gctl_free(r);
1002 				goto backout;
1003 			}
1004 			gctl_free(r);
1005 		}
1006 		if (pline == NULL || *pline != '[')
1007 			continue;
1008 		/* set attributes */
1009 		pline++;
1010 		for (ap = argv;
1011 		    (*ap = strsep(&pline, ",]")) != NULL;)
1012 			if (**ap != '\0' && ++ap >= &argv[6])
1013 				break;
1014 		for (i = 0; i < nargs; i++) {
1015 			l = ap - &argv[0];
1016 			s = gctl_get_ascii(req, "arg%d", i);
1017 			while (l > 0) {
1018 				r = gctl_get_handle();
1019 				gctl_ro_param(r, "class", -1, classp->lg_name);
1020 				gctl_ro_param(r, "verb", -1, "set");
1021 				gctl_ro_param(r, "flags", -1, "restore");
1022 				gctl_ro_param(r, GPART_PARAM_INDEX,
1023 				    sizeof(n), &n);
1024 				gctl_ro_param(r, "attrib", -1, argv[--l]);
1025 				gctl_ro_param(r, "arg0", -1, s);
1026 				errstr = gctl_issue(r);
1027 				if (errstr != NULL && errstr[0] != '\0') {
1028 					gpart_print_error(errstr);
1029 					gctl_free(r);
1030 					goto backout;
1031 				}
1032 				gctl_free(r);
1033 			}
1034 		}
1035 	}
1036 	if (undo_restore)
1037 		goto backout;
1038 	/* commit changes if needed */
1039 	if (strchr(flags, 'C') != NULL) {
1040 		for (i = 0; i < nargs; i++) {
1041 			s = gctl_get_ascii(req, "arg%d", i);
1042 			r = gctl_get_handle();
1043 			gctl_ro_param(r, "class", -1, classp->lg_name);
1044 			gctl_ro_param(r, "verb", -1, "commit");
1045 			gctl_ro_param(r, "arg0", -1, s);
1046 			errstr = gctl_issue(r);
1047 			if (errstr != NULL && errstr[0] != '\0') {
1048 				gpart_print_error(errstr);
1049 				gctl_free(r);
1050 				goto backout;
1051 			}
1052 			gctl_free(r);
1053 		}
1054 	}
1055 	gctl_free(req);
1056 	geom_deletetree(&mesh);
1057 	exit(EXIT_SUCCESS);
1058 
1059 backout:
1060 	for (i = 0; i < nargs; i++) {
1061 		s = gctl_get_ascii(req, "arg%d", i);
1062 		r = gctl_get_handle();
1063 		gctl_ro_param(r, "class", -1, classp->lg_name);
1064 		gctl_ro_param(r, "verb", -1, "undo");
1065 		gctl_ro_param(r, "arg0", -1, s);
1066 		gctl_issue(r);
1067 		gctl_free(r);
1068 	}
1069 	gctl_free(req);
1070 	geom_deletetree(&mesh);
1071 	exit(EXIT_FAILURE);
1072 }
1073 
1074 static void *
1075 gpart_bootfile_read(const char *bootfile, ssize_t *size)
1076 {
1077 	struct stat sb;
1078 	void *code;
1079 	int fd;
1080 
1081 	if (stat(bootfile, &sb) == -1)
1082 		err(EXIT_FAILURE, "%s", bootfile);
1083 	if (!S_ISREG(sb.st_mode))
1084 		errx(EXIT_FAILURE, "%s: not a regular file", bootfile);
1085 	if (sb.st_size == 0)
1086 		errx(EXIT_FAILURE, "%s: empty file", bootfile);
1087 	if (*size > 0 && sb.st_size > *size)
1088 		errx(EXIT_FAILURE, "%s: file too big (%zu limit)", bootfile,
1089 		    *size);
1090 
1091 	*size = sb.st_size;
1092 
1093 	fd = open(bootfile, O_RDONLY);
1094 	if (fd == -1)
1095 		err(EXIT_FAILURE, "%s", bootfile);
1096 	code = malloc(*size);
1097 	if (code == NULL)
1098 		err(EXIT_FAILURE, NULL);
1099 	if (read(fd, code, *size) != *size)
1100 		err(EXIT_FAILURE, "%s", bootfile);
1101 	close(fd);
1102 
1103 	return (code);
1104 }
1105 
1106 static void
1107 gpart_write_partcode(struct ggeom *gp, int idx, void *code, ssize_t size)
1108 {
1109 	char dsf[128];
1110 	struct gprovider *pp;
1111 	const char *s;
1112 	char *buf;
1113 	off_t bsize;
1114 	int fd;
1115 
1116 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1117 		s = find_provcfg(pp, "index");
1118 		if (s == NULL)
1119 			continue;
1120 		if (atoi(s) == idx)
1121 			break;
1122 	}
1123 
1124 	if (pp != NULL) {
1125 		snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1126 		if (pp->lg_mediasize < size)
1127 			errx(EXIT_FAILURE, "%s: not enough space", dsf);
1128 		fd = open(dsf, O_WRONLY);
1129 		if (fd == -1)
1130 			err(EXIT_FAILURE, "%s", dsf);
1131 		/*
1132 		 * When writing to a disk device, the write must be
1133 		 * sector aligned and not write to any partial sectors,
1134 		 * so round up the buffer size to the next sector and zero it.
1135 		 */
1136 		bsize = (size + pp->lg_sectorsize - 1) /
1137 		    pp->lg_sectorsize * pp->lg_sectorsize;
1138 		buf = calloc(1, bsize);
1139 		if (buf == NULL)
1140 			err(EXIT_FAILURE, "%s", dsf);
1141 		bcopy(code, buf, size);
1142 		if (write(fd, buf, bsize) != bsize)
1143 			err(EXIT_FAILURE, "%s", dsf);
1144 		free(buf);
1145 		close(fd);
1146 		printf("partcode written to %s\n", pp->lg_name);
1147 	} else
1148 		errx(EXIT_FAILURE, "invalid partition index");
1149 }
1150 
1151 static void
1152 gpart_write_partcode_vtoc8(struct ggeom *gp, int idx, void *code)
1153 {
1154 	char dsf[128];
1155 	struct gprovider *pp;
1156 	const char *s;
1157 	int installed, fd;
1158 
1159 	installed = 0;
1160 	LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1161 		s = find_provcfg(pp, "index");
1162 		if (s == NULL)
1163 			continue;
1164 		if (idx != 0 && atoi(s) != idx)
1165 			continue;
1166 		snprintf(dsf, sizeof(dsf), "/dev/%s", pp->lg_name);
1167 		if (pp->lg_sectorsize != sizeof(struct vtoc8))
1168 			errx(EXIT_FAILURE, "%s: unexpected sector "
1169 			    "size (%d)\n", dsf, pp->lg_sectorsize);
1170 		if (pp->lg_mediasize < VTOC_BOOTSIZE)
1171 			continue;
1172 		fd = open(dsf, O_WRONLY);
1173 		if (fd == -1)
1174 			err(EXIT_FAILURE, "%s", dsf);
1175 		/*
1176 		 * We ignore the first VTOC_BOOTSIZE bytes of boot code in
1177 		 * order to avoid overwriting the label.
1178 		 */
1179 		if (lseek(fd, sizeof(struct vtoc8), SEEK_SET) !=
1180 		    sizeof(struct vtoc8))
1181 			err(EXIT_FAILURE, "%s", dsf);
1182 		if (write(fd, (caddr_t)code + sizeof(struct vtoc8),
1183 		    VTOC_BOOTSIZE - sizeof(struct vtoc8)) != VTOC_BOOTSIZE -
1184 		    sizeof(struct vtoc8))
1185 			err(EXIT_FAILURE, "%s", dsf);
1186 		installed++;
1187 		close(fd);
1188 		if (idx != 0 && atoi(s) == idx)
1189 			break;
1190 	}
1191 	if (installed == 0)
1192 		errx(EXIT_FAILURE, "%s: no partitions", gp->lg_name);
1193 	else
1194 		printf("partcode written to %s\n",
1195 		    idx != 0 ? pp->lg_name: gp->lg_name);
1196 }
1197 
1198 static void
1199 gpart_bootcode(struct gctl_req *req, unsigned int fl)
1200 {
1201 	struct gmesh mesh;
1202 	struct gclass *classp;
1203 	struct ggeom *gp;
1204 	const char *s;
1205 	void *bootcode, *partcode;
1206 	size_t bootsize, partsize;
1207 	int error, idx, vtoc8;
1208 
1209 	if (gctl_has_param(req, GPART_PARAM_BOOTCODE)) {
1210 		s = gctl_get_ascii(req, GPART_PARAM_BOOTCODE);
1211 		bootsize = 800 * 1024;		/* Arbitrary limit. */
1212 		bootcode = gpart_bootfile_read(s, &bootsize);
1213 		error = gctl_change_param(req, GPART_PARAM_BOOTCODE, bootsize,
1214 		    bootcode);
1215 		if (error)
1216 			errc(EXIT_FAILURE, error, "internal error");
1217 	} else
1218 		bootcode = NULL;
1219 
1220 	s = gctl_get_ascii(req, "class");
1221 	if (s == NULL)
1222 		abort();
1223 	error = geom_gettree(&mesh);
1224 	if (error != 0)
1225 		errc(EXIT_FAILURE, error, "Cannot get GEOM tree");
1226 	classp = find_class(&mesh, s);
1227 	if (classp == NULL) {
1228 		geom_deletetree(&mesh);
1229 		errx(EXIT_FAILURE, "Class %s not found.", s);
1230 	}
1231 	if (gctl_get_int(req, "nargs") != 1)
1232 		errx(EXIT_FAILURE, "Invalid number of arguments.");
1233 	s = gctl_get_ascii(req, "arg0");
1234 	if (s == NULL)
1235 		abort();
1236 	gp = find_geom(classp, s);
1237 	if (gp == NULL)
1238 		errx(EXIT_FAILURE, "No such geom: %s.", s);
1239 	s = find_geomcfg(gp, "scheme");
1240 	if (s == NULL)
1241 		errx(EXIT_FAILURE, "Scheme not found for geom %s", gp->lg_name);
1242 	if (strcmp(s, "VTOC8") == 0)
1243 		vtoc8 = 1;
1244 	else
1245 		vtoc8 = 0;
1246 
1247 	if (gctl_has_param(req, GPART_PARAM_PARTCODE)) {
1248 		s = gctl_get_ascii(req, GPART_PARAM_PARTCODE);
1249 		if (vtoc8 != 0)
1250 			partsize = VTOC_BOOTSIZE;
1251 		else
1252 			partsize = 1024 * 1024;		/* Arbitrary limit. */
1253 		partcode = gpart_bootfile_read(s, &partsize);
1254 		error = gctl_delete_param(req, GPART_PARAM_PARTCODE);
1255 		if (error)
1256 			errc(EXIT_FAILURE, error, "internal error");
1257 	} else
1258 		partcode = NULL;
1259 
1260 	if (gctl_has_param(req, GPART_PARAM_INDEX)) {
1261 		if (partcode == NULL)
1262 			errx(EXIT_FAILURE, "-i is only valid with -p");
1263 		idx = (int)gctl_get_intmax(req, GPART_PARAM_INDEX);
1264 		if (idx < 1)
1265 			errx(EXIT_FAILURE, "invalid partition index");
1266 		error = gctl_delete_param(req, GPART_PARAM_INDEX);
1267 		if (error)
1268 			errc(EXIT_FAILURE, error, "internal error");
1269 	} else
1270 		idx = 0;
1271 
1272 	if (partcode != NULL) {
1273 		if (vtoc8 == 0) {
1274 			if (idx == 0)
1275 				errx(EXIT_FAILURE, "missing -i option");
1276 			gpart_write_partcode(gp, idx, partcode, partsize);
1277 		} else {
1278 			if (partsize != VTOC_BOOTSIZE)
1279 				errx(EXIT_FAILURE, "invalid bootcode");
1280 			gpart_write_partcode_vtoc8(gp, idx, partcode);
1281 		}
1282 	} else
1283 		if (bootcode == NULL)
1284 			errx(EXIT_FAILURE, "no -b nor -p");
1285 
1286 	if (bootcode != NULL)
1287 		gpart_issue(req, fl);
1288 
1289 	geom_deletetree(&mesh);
1290 	free(partcode);
1291 }
1292 
1293 static void
1294 gpart_print_error(const char *errstr)
1295 {
1296 	char *errmsg;
1297 	int error;
1298 
1299 	error = strtol(errstr, &errmsg, 0);
1300 	if (errmsg != errstr) {
1301 		while (errmsg[0] == ' ')
1302 			errmsg++;
1303 		if (errmsg[0] != '\0')
1304 			warnc(error, "%s", errmsg);
1305 		else
1306 			warnc(error, NULL);
1307 	} else
1308 		warnx("%s", errmsg);
1309 }
1310 
1311 static _Noreturn void
1312 gpart_issue(struct gctl_req *req, unsigned int fl __unused)
1313 {
1314 	char buf[4096];
1315 	const char *errstr;
1316 	int error, status;
1317 
1318 	if (gctl_get_int(req, "nargs") != 1)
1319 		errx(EXIT_FAILURE, "Invalid number of arguments.");
1320 	(void)gctl_delete_param(req, "nargs");
1321 
1322 	/* autofill parameters (if applicable). */
1323 	error = gpart_autofill(req);
1324 	if (error) {
1325 		warnc(error, "autofill");
1326 		status = EXIT_FAILURE;
1327 		goto done;
1328 	}
1329 
1330 	bzero(buf, sizeof(buf));
1331 	gctl_rw_param(req, "output", sizeof(buf), buf);
1332 	errstr = gctl_issue(req);
1333 	if (errstr == NULL || errstr[0] == '\0') {
1334 		if (buf[0] != '\0')
1335 			printf("%s", buf);
1336 		status = EXIT_SUCCESS;
1337 		goto done;
1338 	}
1339 
1340 	gpart_print_error(errstr);
1341 	status = EXIT_FAILURE;
1342 
1343  done:
1344 	gctl_free(req);
1345 	exit(status);
1346 }
1347