xref: /freebsd/sys/geom/part/g_part.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002, 2005-2009 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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bio.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
36 #include <sys/kobj.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.h>
42 #include <sys/sbuf.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
45 #include <sys/uuid.h>
46 #include <geom/geom.h>
47 #include <geom/geom_ctl.h>
48 #include <geom/geom_int.h>
49 #include <geom/part/g_part.h>
50 
51 #include "g_part_if.h"
52 
53 #ifndef _PATH_DEV
54 #define _PATH_DEV "/dev/"
55 #endif
56 
57 static kobj_method_t g_part_null_methods[] = {
58 	{ 0, 0 }
59 };
60 
61 static struct g_part_scheme g_part_null_scheme = {
62 	"(none)",
63 	g_part_null_methods,
64 	sizeof(struct g_part_table),
65 };
66 
67 TAILQ_HEAD(, g_part_scheme) g_part_schemes =
68     TAILQ_HEAD_INITIALIZER(g_part_schemes);
69 
70 struct g_part_alias_list {
71 	const char *lexeme;
72 	enum g_part_alias alias;
73 } g_part_alias_list[G_PART_ALIAS_COUNT] = {
74 	{ "apple-apfs", G_PART_ALIAS_APPLE_APFS },
75 	{ "apple-boot", G_PART_ALIAS_APPLE_BOOT },
76 	{ "apple-core-storage", G_PART_ALIAS_APPLE_CORE_STORAGE },
77 	{ "apple-hfs", G_PART_ALIAS_APPLE_HFS },
78 	{ "apple-label", G_PART_ALIAS_APPLE_LABEL },
79 	{ "apple-raid", G_PART_ALIAS_APPLE_RAID },
80 	{ "apple-raid-offline", G_PART_ALIAS_APPLE_RAID_OFFLINE },
81 	{ "apple-tv-recovery", G_PART_ALIAS_APPLE_TV_RECOVERY },
82 	{ "apple-ufs", G_PART_ALIAS_APPLE_UFS },
83 	{ "bios-boot", G_PART_ALIAS_BIOS_BOOT },
84 	{ "chromeos-firmware", G_PART_ALIAS_CHROMEOS_FIRMWARE },
85 	{ "chromeos-kernel", G_PART_ALIAS_CHROMEOS_KERNEL },
86 	{ "chromeos-reserved", G_PART_ALIAS_CHROMEOS_RESERVED },
87 	{ "chromeos-root", G_PART_ALIAS_CHROMEOS_ROOT },
88 	{ "dragonfly-ccd", G_PART_ALIAS_DFBSD_CCD },
89 	{ "dragonfly-hammer", G_PART_ALIAS_DFBSD_HAMMER },
90 	{ "dragonfly-hammer2", G_PART_ALIAS_DFBSD_HAMMER2 },
91 	{ "dragonfly-label32", G_PART_ALIAS_DFBSD },
92 	{ "dragonfly-label64", G_PART_ALIAS_DFBSD64 },
93 	{ "dragonfly-legacy", G_PART_ALIAS_DFBSD_LEGACY },
94 	{ "dragonfly-swap", G_PART_ALIAS_DFBSD_SWAP },
95 	{ "dragonfly-ufs", G_PART_ALIAS_DFBSD_UFS },
96 	{ "dragonfly-vinum", G_PART_ALIAS_DFBSD_VINUM },
97 	{ "ebr", G_PART_ALIAS_EBR },
98 	{ "efi", G_PART_ALIAS_EFI },
99 	{ "fat16", G_PART_ALIAS_MS_FAT16 },
100 	{ "fat32", G_PART_ALIAS_MS_FAT32 },
101 	{ "freebsd", G_PART_ALIAS_FREEBSD },
102 	{ "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
103 	{ "freebsd-nandfs", G_PART_ALIAS_FREEBSD_NANDFS },
104 	{ "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
105 	{ "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
106 	{ "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
107 	{ "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
108 	{ "linux-data", G_PART_ALIAS_LINUX_DATA },
109 	{ "linux-lvm", G_PART_ALIAS_LINUX_LVM },
110 	{ "linux-raid", G_PART_ALIAS_LINUX_RAID },
111 	{ "linux-swap", G_PART_ALIAS_LINUX_SWAP },
112 	{ "mbr", G_PART_ALIAS_MBR },
113 	{ "ms-basic-data", G_PART_ALIAS_MS_BASIC_DATA },
114 	{ "ms-ldm-data", G_PART_ALIAS_MS_LDM_DATA },
115 	{ "ms-ldm-metadata", G_PART_ALIAS_MS_LDM_METADATA },
116 	{ "ms-recovery", G_PART_ALIAS_MS_RECOVERY },
117 	{ "ms-reserved", G_PART_ALIAS_MS_RESERVED },
118 	{ "ms-spaces", G_PART_ALIAS_MS_SPACES },
119 	{ "netbsd-ccd", G_PART_ALIAS_NETBSD_CCD },
120 	{ "netbsd-cgd", G_PART_ALIAS_NETBSD_CGD },
121 	{ "netbsd-ffs", G_PART_ALIAS_NETBSD_FFS },
122 	{ "netbsd-lfs", G_PART_ALIAS_NETBSD_LFS },
123 	{ "netbsd-raid", G_PART_ALIAS_NETBSD_RAID },
124 	{ "netbsd-swap", G_PART_ALIAS_NETBSD_SWAP },
125 	{ "ntfs", G_PART_ALIAS_MS_NTFS },
126 	{ "openbsd-data", G_PART_ALIAS_OPENBSD_DATA },
127 	{ "prep-boot", G_PART_ALIAS_PREP_BOOT },
128 	{ "vmware-reserved", G_PART_ALIAS_VMRESERVED },
129 	{ "vmware-vmfs", G_PART_ALIAS_VMFS },
130 	{ "vmware-vmkdiag", G_PART_ALIAS_VMKDIAG },
131 	{ "vmware-vsanhdr", G_PART_ALIAS_VMVSANHDR },
132 };
133 
134 SYSCTL_DECL(_kern_geom);
135 SYSCTL_NODE(_kern_geom, OID_AUTO, part, CTLFLAG_RW, 0,
136     "GEOM_PART stuff");
137 static u_int check_integrity = 1;
138 SYSCTL_UINT(_kern_geom_part, OID_AUTO, check_integrity,
139     CTLFLAG_RWTUN, &check_integrity, 1,
140     "Enable integrity checking");
141 static u_int auto_resize = 1;
142 SYSCTL_UINT(_kern_geom_part, OID_AUTO, auto_resize,
143     CTLFLAG_RWTUN, &auto_resize, 1,
144     "Enable auto resize");
145 
146 /*
147  * The GEOM partitioning class.
148  */
149 static g_ctl_req_t g_part_ctlreq;
150 static g_ctl_destroy_geom_t g_part_destroy_geom;
151 static g_fini_t g_part_fini;
152 static g_init_t g_part_init;
153 static g_taste_t g_part_taste;
154 
155 static g_access_t g_part_access;
156 static g_dumpconf_t g_part_dumpconf;
157 static g_orphan_t g_part_orphan;
158 static g_spoiled_t g_part_spoiled;
159 static g_start_t g_part_start;
160 static g_resize_t g_part_resize;
161 static g_ioctl_t g_part_ioctl;
162 
163 static struct g_class g_part_class = {
164 	.name = "PART",
165 	.version = G_VERSION,
166 	/* Class methods. */
167 	.ctlreq = g_part_ctlreq,
168 	.destroy_geom = g_part_destroy_geom,
169 	.fini = g_part_fini,
170 	.init = g_part_init,
171 	.taste = g_part_taste,
172 	/* Geom methods. */
173 	.access = g_part_access,
174 	.dumpconf = g_part_dumpconf,
175 	.orphan = g_part_orphan,
176 	.spoiled = g_part_spoiled,
177 	.start = g_part_start,
178 	.resize = g_part_resize,
179 	.ioctl = g_part_ioctl,
180 };
181 
182 DECLARE_GEOM_CLASS(g_part_class, g_part);
183 MODULE_VERSION(g_part, 0);
184 
185 /*
186  * Support functions.
187  */
188 
189 static void g_part_wither(struct g_geom *, int);
190 
191 const char *
192 g_part_alias_name(enum g_part_alias alias)
193 {
194 	int i;
195 
196 	for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
197 		if (g_part_alias_list[i].alias != alias)
198 			continue;
199 		return (g_part_alias_list[i].lexeme);
200 	}
201 
202 	return (NULL);
203 }
204 
205 void
206 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
207     u_int *bestheads)
208 {
209 	static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
210 	off_t chs, cylinders;
211 	u_int heads;
212 	int idx;
213 
214 	*bestchs = 0;
215 	*bestheads = 0;
216 	for (idx = 0; candidate_heads[idx] != 0; idx++) {
217 		heads = candidate_heads[idx];
218 		cylinders = blocks / heads / sectors;
219 		if (cylinders < heads || cylinders < sectors)
220 			break;
221 		if (cylinders > 1023)
222 			continue;
223 		chs = cylinders * heads * sectors;
224 		if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
225 			*bestchs = chs;
226 			*bestheads = heads;
227 		}
228 	}
229 }
230 
231 static void
232 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
233     off_t blocks)
234 {
235 	static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
236 	off_t chs, bestchs;
237 	u_int heads, sectors;
238 	int idx;
239 
240 	if (g_getattr("GEOM::fwsectors", cp, &sectors) != 0 || sectors == 0 ||
241 	    g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
242 		table->gpt_fixgeom = 0;
243 		table->gpt_heads = 0;
244 		table->gpt_sectors = 0;
245 		bestchs = 0;
246 		for (idx = 0; candidate_sectors[idx] != 0; idx++) {
247 			sectors = candidate_sectors[idx];
248 			g_part_geometry_heads(blocks, sectors, &chs, &heads);
249 			if (chs == 0)
250 				continue;
251 			/*
252 			 * Prefer a geometry with sectors > 1, but only if
253 			 * it doesn't bump down the number of heads to 1.
254 			 */
255 			if (chs > bestchs || (chs == bestchs && heads > 1 &&
256 			    table->gpt_sectors == 1)) {
257 				bestchs = chs;
258 				table->gpt_heads = heads;
259 				table->gpt_sectors = sectors;
260 			}
261 		}
262 		/*
263 		 * If we didn't find a geometry at all, then the disk is
264 		 * too big. This means we can use the maximum number of
265 		 * heads and sectors.
266 		 */
267 		if (bestchs == 0) {
268 			table->gpt_heads = 255;
269 			table->gpt_sectors = 63;
270 		}
271 	} else {
272 		table->gpt_fixgeom = 1;
273 		table->gpt_heads = heads;
274 		table->gpt_sectors = sectors;
275 	}
276 }
277 
278 static void
279 g_part_get_physpath_done(struct bio *bp)
280 {
281 	struct g_geom *gp;
282 	struct g_part_entry *entry;
283 	struct g_part_table *table;
284 	struct g_provider *pp;
285 	struct bio *pbp;
286 
287 	pbp = bp->bio_parent;
288 	pp = pbp->bio_to;
289 	gp = pp->geom;
290 	table = gp->softc;
291 	entry = pp->private;
292 
293 	if (bp->bio_error == 0) {
294 		char *end;
295 		size_t len, remainder;
296 		len = strlcat(bp->bio_data, "/", bp->bio_length);
297 		if (len < bp->bio_length) {
298 			end = bp->bio_data + len;
299 			remainder = bp->bio_length - len;
300 			G_PART_NAME(table, entry, end, remainder);
301 		}
302 	}
303 	g_std_done(bp);
304 }
305 
306 
307 #define	DPRINTF(...)	if (bootverbose) {	\
308 	printf("GEOM_PART: " __VA_ARGS__);	\
309 }
310 
311 static int
312 g_part_check_integrity(struct g_part_table *table, struct g_consumer *cp)
313 {
314 	struct g_part_entry *e1, *e2;
315 	struct g_provider *pp;
316 	off_t offset;
317 	int failed;
318 
319 	failed = 0;
320 	pp = cp->provider;
321 	if (table->gpt_last < table->gpt_first) {
322 		DPRINTF("last LBA is below first LBA: %jd < %jd\n",
323 		    (intmax_t)table->gpt_last, (intmax_t)table->gpt_first);
324 		failed++;
325 	}
326 	if (table->gpt_last > pp->mediasize / pp->sectorsize - 1) {
327 		DPRINTF("last LBA extends beyond mediasize: "
328 		    "%jd > %jd\n", (intmax_t)table->gpt_last,
329 		    (intmax_t)pp->mediasize / pp->sectorsize - 1);
330 		failed++;
331 	}
332 	LIST_FOREACH(e1, &table->gpt_entry, gpe_entry) {
333 		if (e1->gpe_deleted || e1->gpe_internal)
334 			continue;
335 		if (e1->gpe_start < table->gpt_first) {
336 			DPRINTF("partition %d has start offset below first "
337 			    "LBA: %jd < %jd\n", e1->gpe_index,
338 			    (intmax_t)e1->gpe_start,
339 			    (intmax_t)table->gpt_first);
340 			failed++;
341 		}
342 		if (e1->gpe_start > table->gpt_last) {
343 			DPRINTF("partition %d has start offset beyond last "
344 			    "LBA: %jd > %jd\n", e1->gpe_index,
345 			    (intmax_t)e1->gpe_start,
346 			    (intmax_t)table->gpt_last);
347 			failed++;
348 		}
349 		if (e1->gpe_end < e1->gpe_start) {
350 			DPRINTF("partition %d has end offset below start "
351 			    "offset: %jd < %jd\n", e1->gpe_index,
352 			    (intmax_t)e1->gpe_end,
353 			    (intmax_t)e1->gpe_start);
354 			failed++;
355 		}
356 		if (e1->gpe_end > table->gpt_last) {
357 			DPRINTF("partition %d has end offset beyond last "
358 			    "LBA: %jd > %jd\n", e1->gpe_index,
359 			    (intmax_t)e1->gpe_end,
360 			    (intmax_t)table->gpt_last);
361 			failed++;
362 		}
363 		if (pp->stripesize > 0) {
364 			offset = e1->gpe_start * pp->sectorsize;
365 			if (e1->gpe_offset > offset)
366 				offset = e1->gpe_offset;
367 			if ((offset + pp->stripeoffset) % pp->stripesize) {
368 				DPRINTF("partition %d on (%s, %s) is not "
369 				    "aligned on %u bytes\n", e1->gpe_index,
370 				    pp->name, table->gpt_scheme->name,
371 				    pp->stripesize);
372 				/* Don't treat this as a critical failure */
373 			}
374 		}
375 		e2 = e1;
376 		while ((e2 = LIST_NEXT(e2, gpe_entry)) != NULL) {
377 			if (e2->gpe_deleted || e2->gpe_internal)
378 				continue;
379 			if (e1->gpe_start >= e2->gpe_start &&
380 			    e1->gpe_start <= e2->gpe_end) {
381 				DPRINTF("partition %d has start offset inside "
382 				    "partition %d: start[%d] %jd >= start[%d] "
383 				    "%jd <= end[%d] %jd\n",
384 				    e1->gpe_index, e2->gpe_index,
385 				    e2->gpe_index, (intmax_t)e2->gpe_start,
386 				    e1->gpe_index, (intmax_t)e1->gpe_start,
387 				    e2->gpe_index, (intmax_t)e2->gpe_end);
388 				failed++;
389 			}
390 			if (e1->gpe_end >= e2->gpe_start &&
391 			    e1->gpe_end <= e2->gpe_end) {
392 				DPRINTF("partition %d has end offset inside "
393 				    "partition %d: start[%d] %jd >= end[%d] "
394 				    "%jd <= end[%d] %jd\n",
395 				    e1->gpe_index, e2->gpe_index,
396 				    e2->gpe_index, (intmax_t)e2->gpe_start,
397 				    e1->gpe_index, (intmax_t)e1->gpe_end,
398 				    e2->gpe_index, (intmax_t)e2->gpe_end);
399 				failed++;
400 			}
401 			if (e1->gpe_start < e2->gpe_start &&
402 			    e1->gpe_end > e2->gpe_end) {
403 				DPRINTF("partition %d contains partition %d: "
404 				    "start[%d] %jd > start[%d] %jd, end[%d] "
405 				    "%jd < end[%d] %jd\n",
406 				    e1->gpe_index, e2->gpe_index,
407 				    e1->gpe_index, (intmax_t)e1->gpe_start,
408 				    e2->gpe_index, (intmax_t)e2->gpe_start,
409 				    e2->gpe_index, (intmax_t)e2->gpe_end,
410 				    e1->gpe_index, (intmax_t)e1->gpe_end);
411 				failed++;
412 			}
413 		}
414 	}
415 	if (failed != 0) {
416 		printf("GEOM_PART: integrity check failed (%s, %s)\n",
417 		    pp->name, table->gpt_scheme->name);
418 		if (check_integrity != 0)
419 			return (EINVAL);
420 		table->gpt_corrupt = 1;
421 	}
422 	return (0);
423 }
424 #undef	DPRINTF
425 
426 struct g_part_entry *
427 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
428     quad_t end)
429 {
430 	struct g_part_entry *entry, *last;
431 
432 	last = NULL;
433 	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
434 		if (entry->gpe_index == index)
435 			break;
436 		if (entry->gpe_index > index) {
437 			entry = NULL;
438 			break;
439 		}
440 		last = entry;
441 	}
442 	if (entry == NULL) {
443 		entry = g_malloc(table->gpt_scheme->gps_entrysz,
444 		    M_WAITOK | M_ZERO);
445 		entry->gpe_index = index;
446 		if (last == NULL)
447 			LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
448 		else
449 			LIST_INSERT_AFTER(last, entry, gpe_entry);
450 	} else
451 		entry->gpe_offset = 0;
452 	entry->gpe_start = start;
453 	entry->gpe_end = end;
454 	return (entry);
455 }
456 
457 static void
458 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
459     struct g_part_entry *entry)
460 {
461 	struct g_consumer *cp;
462 	struct g_provider *pp;
463 	struct sbuf *sb;
464 	struct g_geom_alias *gap;
465 	off_t offset;
466 
467 	cp = LIST_FIRST(&gp->consumer);
468 	pp = cp->provider;
469 
470 	offset = entry->gpe_start * pp->sectorsize;
471 	if (entry->gpe_offset < offset)
472 		entry->gpe_offset = offset;
473 
474 	if (entry->gpe_pp == NULL) {
475 		/*
476 		 * Add aliases to the geom before we create the provider so that
477 		 * geom_dev can taste it with all the aliases in place so all
478 		 * the aliased dev_t instances get created for each partition
479 		 * (eg foo5p7 gets created for bar5p7 when foo is an alias of bar).
480 		 */
481 		LIST_FOREACH(gap, &table->gpt_gp->aliases, ga_next) {
482 			sb = sbuf_new_auto();
483 			G_PART_FULLNAME(table, entry, sb, gap->ga_alias);
484 			sbuf_finish(sb);
485 			g_geom_add_alias(gp, sbuf_data(sb));
486 			sbuf_delete(sb);
487 		}
488 		sb = sbuf_new_auto();
489 		G_PART_FULLNAME(table, entry, sb, gp->name);
490 		sbuf_finish(sb);
491 		entry->gpe_pp = g_new_providerf(gp, "%s", sbuf_data(sb));
492 		sbuf_delete(sb);
493 		entry->gpe_pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
494 		entry->gpe_pp->private = entry;		/* Close the circle. */
495 	}
496 	entry->gpe_pp->index = entry->gpe_index - 1;	/* index is 1-based. */
497 	entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
498 	    pp->sectorsize;
499 	entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
500 	entry->gpe_pp->sectorsize = pp->sectorsize;
501 	entry->gpe_pp->stripesize = pp->stripesize;
502 	entry->gpe_pp->stripeoffset = pp->stripeoffset + entry->gpe_offset;
503 	if (pp->stripesize > 0)
504 		entry->gpe_pp->stripeoffset %= pp->stripesize;
505 	entry->gpe_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
506 	g_error_provider(entry->gpe_pp, 0);
507 }
508 
509 static struct g_geom*
510 g_part_find_geom(const char *name)
511 {
512 	struct g_geom *gp;
513 	LIST_FOREACH(gp, &g_part_class.geom, geom) {
514 		if ((gp->flags & G_GEOM_WITHER) == 0 &&
515 		    strcmp(name, gp->name) == 0)
516 			break;
517 	}
518 	return (gp);
519 }
520 
521 static int
522 g_part_parm_geom(struct gctl_req *req, const char *name, struct g_geom **v)
523 {
524 	struct g_geom *gp;
525 	const char *gname;
526 
527 	gname = gctl_get_asciiparam(req, name);
528 	if (gname == NULL)
529 		return (ENOATTR);
530 	if (strncmp(gname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
531 		gname += sizeof(_PATH_DEV) - 1;
532 	gp = g_part_find_geom(gname);
533 	if (gp == NULL) {
534 		gctl_error(req, "%d %s '%s'", EINVAL, name, gname);
535 		return (EINVAL);
536 	}
537 	*v = gp;
538 	return (0);
539 }
540 
541 static int
542 g_part_parm_provider(struct gctl_req *req, const char *name,
543     struct g_provider **v)
544 {
545 	struct g_provider *pp;
546 	const char *pname;
547 
548 	pname = gctl_get_asciiparam(req, name);
549 	if (pname == NULL)
550 		return (ENOATTR);
551 	if (strncmp(pname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
552 		pname += sizeof(_PATH_DEV) - 1;
553 	pp = g_provider_by_name(pname);
554 	if (pp == NULL) {
555 		gctl_error(req, "%d %s '%s'", EINVAL, name, pname);
556 		return (EINVAL);
557 	}
558 	*v = pp;
559 	return (0);
560 }
561 
562 static int
563 g_part_parm_quad(struct gctl_req *req, const char *name, quad_t *v)
564 {
565 	const char *p;
566 	char *x;
567 	quad_t q;
568 
569 	p = gctl_get_asciiparam(req, name);
570 	if (p == NULL)
571 		return (ENOATTR);
572 	q = strtoq(p, &x, 0);
573 	if (*x != '\0' || q < 0) {
574 		gctl_error(req, "%d %s '%s'", EINVAL, name, p);
575 		return (EINVAL);
576 	}
577 	*v = q;
578 	return (0);
579 }
580 
581 static int
582 g_part_parm_scheme(struct gctl_req *req, const char *name,
583     struct g_part_scheme **v)
584 {
585 	struct g_part_scheme *s;
586 	const char *p;
587 
588 	p = gctl_get_asciiparam(req, name);
589 	if (p == NULL)
590 		return (ENOATTR);
591 	TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
592 		if (s == &g_part_null_scheme)
593 			continue;
594 		if (!strcasecmp(s->name, p))
595 			break;
596 	}
597 	if (s == NULL) {
598 		gctl_error(req, "%d %s '%s'", EINVAL, name, p);
599 		return (EINVAL);
600 	}
601 	*v = s;
602 	return (0);
603 }
604 
605 static int
606 g_part_parm_str(struct gctl_req *req, const char *name, const char **v)
607 {
608 	const char *p;
609 
610 	p = gctl_get_asciiparam(req, name);
611 	if (p == NULL)
612 		return (ENOATTR);
613 	/* An empty label is always valid. */
614 	if (strcmp(name, "label") != 0 && p[0] == '\0') {
615 		gctl_error(req, "%d %s '%s'", EINVAL, name, p);
616 		return (EINVAL);
617 	}
618 	*v = p;
619 	return (0);
620 }
621 
622 static int
623 g_part_parm_intmax(struct gctl_req *req, const char *name, u_int *v)
624 {
625 	const intmax_t *p;
626 	int size;
627 
628 	p = gctl_get_param(req, name, &size);
629 	if (p == NULL)
630 		return (ENOATTR);
631 	if (size != sizeof(*p) || *p < 0 || *p > INT_MAX) {
632 		gctl_error(req, "%d %s '%jd'", EINVAL, name, *p);
633 		return (EINVAL);
634 	}
635 	*v = (u_int)*p;
636 	return (0);
637 }
638 
639 static int
640 g_part_parm_uint32(struct gctl_req *req, const char *name, u_int *v)
641 {
642 	const uint32_t *p;
643 	int size;
644 
645 	p = gctl_get_param(req, name, &size);
646 	if (p == NULL)
647 		return (ENOATTR);
648 	if (size != sizeof(*p) || *p > INT_MAX) {
649 		gctl_error(req, "%d %s '%u'", EINVAL, name, (unsigned int)*p);
650 		return (EINVAL);
651 	}
652 	*v = (u_int)*p;
653 	return (0);
654 }
655 
656 static int
657 g_part_parm_bootcode(struct gctl_req *req, const char *name, const void **v,
658     unsigned int *s)
659 {
660 	const void *p;
661 	int size;
662 
663 	p = gctl_get_param(req, name, &size);
664 	if (p == NULL)
665 		return (ENOATTR);
666 	*v = p;
667 	*s = size;
668 	return (0);
669 }
670 
671 static int
672 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
673 {
674 	struct g_part_scheme *iter, *scheme;
675 	struct g_part_table *table;
676 	int pri, probe;
677 
678 	table = gp->softc;
679 	scheme = (table != NULL) ? table->gpt_scheme : NULL;
680 	pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
681 	if (pri == 0)
682 		goto done;
683 	if (pri > 0) {	/* error */
684 		scheme = NULL;
685 		pri = INT_MIN;
686 	}
687 
688 	TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
689 		if (iter == &g_part_null_scheme)
690 			continue;
691 		table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
692 		    M_WAITOK);
693 		table->gpt_gp = gp;
694 		table->gpt_scheme = iter;
695 		table->gpt_depth = depth;
696 		probe = G_PART_PROBE(table, cp);
697 		if (probe <= 0 && probe > pri) {
698 			pri = probe;
699 			scheme = iter;
700 			if (gp->softc != NULL)
701 				kobj_delete((kobj_t)gp->softc, M_GEOM);
702 			gp->softc = table;
703 			if (pri == 0)
704 				goto done;
705 		} else
706 			kobj_delete((kobj_t)table, M_GEOM);
707 	}
708 
709 done:
710 	return ((scheme == NULL) ? ENXIO : 0);
711 }
712 
713 /*
714  * Control request functions.
715  */
716 
717 static int
718 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
719 {
720 	struct g_geom *gp;
721 	struct g_provider *pp;
722 	struct g_part_entry *delent, *last, *entry;
723 	struct g_part_table *table;
724 	struct sbuf *sb;
725 	quad_t end;
726 	unsigned int index;
727 	int error;
728 
729 	gp = gpp->gpp_geom;
730 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
731 	g_topology_assert();
732 
733 	pp = LIST_FIRST(&gp->consumer)->provider;
734 	table = gp->softc;
735 	end = gpp->gpp_start + gpp->gpp_size - 1;
736 
737 	if (gpp->gpp_start < table->gpt_first ||
738 	    gpp->gpp_start > table->gpt_last) {
739 		gctl_error(req, "%d start '%jd'", EINVAL,
740 		    (intmax_t)gpp->gpp_start);
741 		return (EINVAL);
742 	}
743 	if (end < gpp->gpp_start || end > table->gpt_last) {
744 		gctl_error(req, "%d size '%jd'", EINVAL,
745 		    (intmax_t)gpp->gpp_size);
746 		return (EINVAL);
747 	}
748 	if (gpp->gpp_index > table->gpt_entries) {
749 		gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
750 		return (EINVAL);
751 	}
752 
753 	delent = last = NULL;
754 	index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
755 	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
756 		if (entry->gpe_deleted) {
757 			if (entry->gpe_index == index)
758 				delent = entry;
759 			continue;
760 		}
761 		if (entry->gpe_index == index)
762 			index = entry->gpe_index + 1;
763 		if (entry->gpe_index < index)
764 			last = entry;
765 		if (entry->gpe_internal)
766 			continue;
767 		if (gpp->gpp_start >= entry->gpe_start &&
768 		    gpp->gpp_start <= entry->gpe_end) {
769 			gctl_error(req, "%d start '%jd'", ENOSPC,
770 			    (intmax_t)gpp->gpp_start);
771 			return (ENOSPC);
772 		}
773 		if (end >= entry->gpe_start && end <= entry->gpe_end) {
774 			gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
775 			return (ENOSPC);
776 		}
777 		if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
778 			gctl_error(req, "%d size '%jd'", ENOSPC,
779 			    (intmax_t)gpp->gpp_size);
780 			return (ENOSPC);
781 		}
782 	}
783 	if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
784 		gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
785 		return (EEXIST);
786 	}
787 	if (index > table->gpt_entries) {
788 		gctl_error(req, "%d index '%d'", ENOSPC, index);
789 		return (ENOSPC);
790 	}
791 
792 	entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
793 	    M_WAITOK | M_ZERO) : delent;
794 	entry->gpe_index = index;
795 	entry->gpe_start = gpp->gpp_start;
796 	entry->gpe_end = end;
797 	error = G_PART_ADD(table, entry, gpp);
798 	if (error) {
799 		gctl_error(req, "%d", error);
800 		if (delent == NULL)
801 			g_free(entry);
802 		return (error);
803 	}
804 	if (delent == NULL) {
805 		if (last == NULL)
806 			LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
807 		else
808 			LIST_INSERT_AFTER(last, entry, gpe_entry);
809 		entry->gpe_created = 1;
810 	} else {
811 		entry->gpe_deleted = 0;
812 		entry->gpe_modified = 1;
813 	}
814 	g_part_new_provider(gp, table, entry);
815 
816 	/* Provide feedback if so requested. */
817 	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
818 		sb = sbuf_new_auto();
819 		G_PART_FULLNAME(table, entry, sb, gp->name);
820 		if (pp->stripesize > 0 && entry->gpe_pp->stripeoffset != 0)
821 			sbuf_printf(sb, " added, but partition is not "
822 			    "aligned on %u bytes\n", pp->stripesize);
823 		else
824 			sbuf_cat(sb, " added\n");
825 		sbuf_finish(sb);
826 		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
827 		sbuf_delete(sb);
828 	}
829 	return (0);
830 }
831 
832 static int
833 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
834 {
835 	struct g_geom *gp;
836 	struct g_part_table *table;
837 	struct sbuf *sb;
838 	int error, sz;
839 
840 	gp = gpp->gpp_geom;
841 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
842 	g_topology_assert();
843 
844 	table = gp->softc;
845 	sz = table->gpt_scheme->gps_bootcodesz;
846 	if (sz == 0) {
847 		error = ENODEV;
848 		goto fail;
849 	}
850 	if (gpp->gpp_codesize > sz) {
851 		error = EFBIG;
852 		goto fail;
853 	}
854 
855 	error = G_PART_BOOTCODE(table, gpp);
856 	if (error)
857 		goto fail;
858 
859 	/* Provide feedback if so requested. */
860 	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
861 		sb = sbuf_new_auto();
862 		sbuf_printf(sb, "bootcode written to %s\n", gp->name);
863 		sbuf_finish(sb);
864 		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
865 		sbuf_delete(sb);
866 	}
867 	return (0);
868 
869  fail:
870 	gctl_error(req, "%d", error);
871 	return (error);
872 }
873 
874 static int
875 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
876 {
877 	struct g_consumer *cp;
878 	struct g_geom *gp;
879 	struct g_provider *pp;
880 	struct g_part_entry *entry, *tmp;
881 	struct g_part_table *table;
882 	char *buf;
883 	int error, i;
884 
885 	gp = gpp->gpp_geom;
886 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
887 	g_topology_assert();
888 
889 	table = gp->softc;
890 	if (!table->gpt_opened) {
891 		gctl_error(req, "%d", EPERM);
892 		return (EPERM);
893 	}
894 
895 	g_topology_unlock();
896 
897 	cp = LIST_FIRST(&gp->consumer);
898 	if ((table->gpt_smhead | table->gpt_smtail) != 0) {
899 		pp = cp->provider;
900 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
901 		while (table->gpt_smhead != 0) {
902 			i = ffs(table->gpt_smhead) - 1;
903 			error = g_write_data(cp, i * pp->sectorsize, buf,
904 			    pp->sectorsize);
905 			if (error) {
906 				g_free(buf);
907 				goto fail;
908 			}
909 			table->gpt_smhead &= ~(1 << i);
910 		}
911 		while (table->gpt_smtail != 0) {
912 			i = ffs(table->gpt_smtail) - 1;
913 			error = g_write_data(cp, pp->mediasize - (i + 1) *
914 			    pp->sectorsize, buf, pp->sectorsize);
915 			if (error) {
916 				g_free(buf);
917 				goto fail;
918 			}
919 			table->gpt_smtail &= ~(1 << i);
920 		}
921 		g_free(buf);
922 	}
923 
924 	if (table->gpt_scheme == &g_part_null_scheme) {
925 		g_topology_lock();
926 		g_access(cp, -1, -1, -1);
927 		g_part_wither(gp, ENXIO);
928 		return (0);
929 	}
930 
931 	error = G_PART_WRITE(table, cp);
932 	if (error)
933 		goto fail;
934 
935 	LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
936 		if (!entry->gpe_deleted) {
937 			/* Notify consumers that provider might be changed. */
938 			if (entry->gpe_modified && (
939 			    entry->gpe_pp->acw + entry->gpe_pp->ace +
940 			    entry->gpe_pp->acr) == 0)
941 				g_media_changed(entry->gpe_pp, M_NOWAIT);
942 			entry->gpe_created = 0;
943 			entry->gpe_modified = 0;
944 			continue;
945 		}
946 		LIST_REMOVE(entry, gpe_entry);
947 		g_free(entry);
948 	}
949 	table->gpt_created = 0;
950 	table->gpt_opened = 0;
951 
952 	g_topology_lock();
953 	g_access(cp, -1, -1, -1);
954 	return (0);
955 
956 fail:
957 	g_topology_lock();
958 	gctl_error(req, "%d", error);
959 	return (error);
960 }
961 
962 static int
963 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
964 {
965 	struct g_consumer *cp;
966 	struct g_geom *gp;
967 	struct g_provider *pp;
968 	struct g_part_scheme *scheme;
969 	struct g_part_table *null, *table;
970 	struct sbuf *sb;
971 	int attr, error;
972 
973 	pp = gpp->gpp_provider;
974 	scheme = gpp->gpp_scheme;
975 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
976 	g_topology_assert();
977 
978 	/* Check that there isn't already a g_part geom on the provider. */
979 	gp = g_part_find_geom(pp->name);
980 	if (gp != NULL) {
981 		null = gp->softc;
982 		if (null->gpt_scheme != &g_part_null_scheme) {
983 			gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
984 			return (EEXIST);
985 		}
986 	} else
987 		null = NULL;
988 
989 	if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
990 	    (gpp->gpp_entries < scheme->gps_minent ||
991 	     gpp->gpp_entries > scheme->gps_maxent)) {
992 		gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
993 		return (EINVAL);
994 	}
995 
996 	if (null == NULL)
997 		gp = g_new_geomf(&g_part_class, "%s", pp->name);
998 	gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
999 	    M_WAITOK);
1000 	table = gp->softc;
1001 	table->gpt_gp = gp;
1002 	table->gpt_scheme = gpp->gpp_scheme;
1003 	table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
1004 	    gpp->gpp_entries : scheme->gps_minent;
1005 	LIST_INIT(&table->gpt_entry);
1006 	if (null == NULL) {
1007 		cp = g_new_consumer(gp);
1008 		cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1009 		error = g_attach(cp, pp);
1010 		if (error == 0)
1011 			error = g_access(cp, 1, 1, 1);
1012 		if (error != 0) {
1013 			g_part_wither(gp, error);
1014 			gctl_error(req, "%d geom '%s'", error, pp->name);
1015 			return (error);
1016 		}
1017 		table->gpt_opened = 1;
1018 	} else {
1019 		cp = LIST_FIRST(&gp->consumer);
1020 		table->gpt_opened = null->gpt_opened;
1021 		table->gpt_smhead = null->gpt_smhead;
1022 		table->gpt_smtail = null->gpt_smtail;
1023 	}
1024 
1025 	g_topology_unlock();
1026 
1027 	/* Make sure the provider has media. */
1028 	if (pp->mediasize == 0 || pp->sectorsize == 0) {
1029 		error = ENODEV;
1030 		goto fail;
1031 	}
1032 
1033 	/* Make sure we can nest and if so, determine our depth. */
1034 	error = g_getattr("PART::isleaf", cp, &attr);
1035 	if (!error && attr) {
1036 		error = ENODEV;
1037 		goto fail;
1038 	}
1039 	error = g_getattr("PART::depth", cp, &attr);
1040 	table->gpt_depth = (!error) ? attr + 1 : 0;
1041 
1042 	/*
1043 	 * Synthesize a disk geometry. Some partitioning schemes
1044 	 * depend on it and since some file systems need it even
1045 	 * when the partitition scheme doesn't, we do it here in
1046 	 * scheme-independent code.
1047 	 */
1048 	g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1049 
1050 	error = G_PART_CREATE(table, gpp);
1051 	if (error)
1052 		goto fail;
1053 
1054 	g_topology_lock();
1055 
1056 	table->gpt_created = 1;
1057 	if (null != NULL)
1058 		kobj_delete((kobj_t)null, M_GEOM);
1059 
1060 	/*
1061 	 * Support automatic commit by filling in the gpp_geom
1062 	 * parameter.
1063 	 */
1064 	gpp->gpp_parms |= G_PART_PARM_GEOM;
1065 	gpp->gpp_geom = gp;
1066 
1067 	/* Provide feedback if so requested. */
1068 	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1069 		sb = sbuf_new_auto();
1070 		sbuf_printf(sb, "%s created\n", gp->name);
1071 		sbuf_finish(sb);
1072 		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1073 		sbuf_delete(sb);
1074 	}
1075 	return (0);
1076 
1077 fail:
1078 	g_topology_lock();
1079 	if (null == NULL) {
1080 		g_access(cp, -1, -1, -1);
1081 		g_part_wither(gp, error);
1082 	} else {
1083 		kobj_delete((kobj_t)gp->softc, M_GEOM);
1084 		gp->softc = null;
1085 	}
1086 	gctl_error(req, "%d provider", error);
1087 	return (error);
1088 }
1089 
1090 static int
1091 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
1092 {
1093 	struct g_geom *gp;
1094 	struct g_provider *pp;
1095 	struct g_part_entry *entry;
1096 	struct g_part_table *table;
1097 	struct sbuf *sb;
1098 
1099 	gp = gpp->gpp_geom;
1100 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1101 	g_topology_assert();
1102 
1103 	table = gp->softc;
1104 
1105 	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1106 		if (entry->gpe_deleted || entry->gpe_internal)
1107 			continue;
1108 		if (entry->gpe_index == gpp->gpp_index)
1109 			break;
1110 	}
1111 	if (entry == NULL) {
1112 		gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1113 		return (ENOENT);
1114 	}
1115 
1116 	pp = entry->gpe_pp;
1117 	if (pp != NULL) {
1118 		if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
1119 			gctl_error(req, "%d", EBUSY);
1120 			return (EBUSY);
1121 		}
1122 
1123 		pp->private = NULL;
1124 		entry->gpe_pp = NULL;
1125 	}
1126 
1127 	if (pp != NULL)
1128 		g_wither_provider(pp, ENXIO);
1129 
1130 	/* Provide feedback if so requested. */
1131 	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1132 		sb = sbuf_new_auto();
1133 		G_PART_FULLNAME(table, entry, sb, gp->name);
1134 		sbuf_cat(sb, " deleted\n");
1135 		sbuf_finish(sb);
1136 		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1137 		sbuf_delete(sb);
1138 	}
1139 
1140 	if (entry->gpe_created) {
1141 		LIST_REMOVE(entry, gpe_entry);
1142 		g_free(entry);
1143 	} else {
1144 		entry->gpe_modified = 0;
1145 		entry->gpe_deleted = 1;
1146 	}
1147 	return (0);
1148 }
1149 
1150 static int
1151 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
1152 {
1153 	struct g_consumer *cp;
1154 	struct g_geom *gp;
1155 	struct g_provider *pp;
1156 	struct g_part_entry *entry, *tmp;
1157 	struct g_part_table *null, *table;
1158 	struct sbuf *sb;
1159 	int error;
1160 
1161 	gp = gpp->gpp_geom;
1162 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1163 	g_topology_assert();
1164 
1165 	table = gp->softc;
1166 	/* Check for busy providers. */
1167 	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1168 		if (entry->gpe_deleted || entry->gpe_internal)
1169 			continue;
1170 		if (gpp->gpp_force) {
1171 			pp = entry->gpe_pp;
1172 			if (pp == NULL)
1173 				continue;
1174 			if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1175 				continue;
1176 		}
1177 		gctl_error(req, "%d", EBUSY);
1178 		return (EBUSY);
1179 	}
1180 
1181 	if (gpp->gpp_force) {
1182 		/* Destroy all providers. */
1183 		LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1184 			pp = entry->gpe_pp;
1185 			if (pp != NULL) {
1186 				pp->private = NULL;
1187 				g_wither_provider(pp, ENXIO);
1188 			}
1189 			LIST_REMOVE(entry, gpe_entry);
1190 			g_free(entry);
1191 		}
1192 	}
1193 
1194 	error = G_PART_DESTROY(table, gpp);
1195 	if (error) {
1196 		gctl_error(req, "%d", error);
1197 		return (error);
1198 	}
1199 
1200 	gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
1201 	    M_WAITOK);
1202 	null = gp->softc;
1203 	null->gpt_gp = gp;
1204 	null->gpt_scheme = &g_part_null_scheme;
1205 	LIST_INIT(&null->gpt_entry);
1206 
1207 	cp = LIST_FIRST(&gp->consumer);
1208 	pp = cp->provider;
1209 	null->gpt_last = pp->mediasize / pp->sectorsize - 1;
1210 
1211 	null->gpt_depth = table->gpt_depth;
1212 	null->gpt_opened = table->gpt_opened;
1213 	null->gpt_smhead = table->gpt_smhead;
1214 	null->gpt_smtail = table->gpt_smtail;
1215 
1216 	while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1217 		LIST_REMOVE(entry, gpe_entry);
1218 		g_free(entry);
1219 	}
1220 	kobj_delete((kobj_t)table, M_GEOM);
1221 
1222 	/* Provide feedback if so requested. */
1223 	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1224 		sb = sbuf_new_auto();
1225 		sbuf_printf(sb, "%s destroyed\n", gp->name);
1226 		sbuf_finish(sb);
1227 		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1228 		sbuf_delete(sb);
1229 	}
1230 	return (0);
1231 }
1232 
1233 static int
1234 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
1235 {
1236 	struct g_geom *gp;
1237 	struct g_part_entry *entry;
1238 	struct g_part_table *table;
1239 	struct sbuf *sb;
1240 	int error;
1241 
1242 	gp = gpp->gpp_geom;
1243 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1244 	g_topology_assert();
1245 
1246 	table = gp->softc;
1247 
1248 	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1249 		if (entry->gpe_deleted || entry->gpe_internal)
1250 			continue;
1251 		if (entry->gpe_index == gpp->gpp_index)
1252 			break;
1253 	}
1254 	if (entry == NULL) {
1255 		gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1256 		return (ENOENT);
1257 	}
1258 
1259 	error = G_PART_MODIFY(table, entry, gpp);
1260 	if (error) {
1261 		gctl_error(req, "%d", error);
1262 		return (error);
1263 	}
1264 
1265 	if (!entry->gpe_created)
1266 		entry->gpe_modified = 1;
1267 
1268 	/* Provide feedback if so requested. */
1269 	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1270 		sb = sbuf_new_auto();
1271 		G_PART_FULLNAME(table, entry, sb, gp->name);
1272 		sbuf_cat(sb, " modified\n");
1273 		sbuf_finish(sb);
1274 		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1275 		sbuf_delete(sb);
1276 	}
1277 	return (0);
1278 }
1279 
1280 static int
1281 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
1282 {
1283 	gctl_error(req, "%d verb 'move'", ENOSYS);
1284 	return (ENOSYS);
1285 }
1286 
1287 static int
1288 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
1289 {
1290 	struct g_part_table *table;
1291 	struct g_geom *gp;
1292 	struct sbuf *sb;
1293 	int error, recovered;
1294 
1295 	gp = gpp->gpp_geom;
1296 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1297 	g_topology_assert();
1298 	table = gp->softc;
1299 	error = recovered = 0;
1300 
1301 	if (table->gpt_corrupt) {
1302 		error = G_PART_RECOVER(table);
1303 		if (error == 0)
1304 			error = g_part_check_integrity(table,
1305 			    LIST_FIRST(&gp->consumer));
1306 		if (error) {
1307 			gctl_error(req, "%d recovering '%s' failed",
1308 			    error, gp->name);
1309 			return (error);
1310 		}
1311 		recovered = 1;
1312 	}
1313 	/* Provide feedback if so requested. */
1314 	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1315 		sb = sbuf_new_auto();
1316 		if (recovered)
1317 			sbuf_printf(sb, "%s recovered\n", gp->name);
1318 		else
1319 			sbuf_printf(sb, "%s recovering is not needed\n",
1320 			    gp->name);
1321 		sbuf_finish(sb);
1322 		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1323 		sbuf_delete(sb);
1324 	}
1325 	return (0);
1326 }
1327 
1328 static int
1329 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
1330 {
1331 	struct g_geom *gp;
1332 	struct g_provider *pp;
1333 	struct g_part_entry *pe, *entry;
1334 	struct g_part_table *table;
1335 	struct sbuf *sb;
1336 	quad_t end;
1337 	int error;
1338 	off_t mediasize;
1339 
1340 	gp = gpp->gpp_geom;
1341 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1342 	g_topology_assert();
1343 	table = gp->softc;
1344 
1345 	/* check gpp_index */
1346 	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1347 		if (entry->gpe_deleted || entry->gpe_internal)
1348 			continue;
1349 		if (entry->gpe_index == gpp->gpp_index)
1350 			break;
1351 	}
1352 	if (entry == NULL) {
1353 		gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1354 		return (ENOENT);
1355 	}
1356 
1357 	/* check gpp_size */
1358 	end = entry->gpe_start + gpp->gpp_size - 1;
1359 	if (gpp->gpp_size < 1 || end > table->gpt_last) {
1360 		gctl_error(req, "%d size '%jd'", EINVAL,
1361 		    (intmax_t)gpp->gpp_size);
1362 		return (EINVAL);
1363 	}
1364 
1365 	LIST_FOREACH(pe, &table->gpt_entry, gpe_entry) {
1366 		if (pe->gpe_deleted || pe->gpe_internal || pe == entry)
1367 			continue;
1368 		if (end >= pe->gpe_start && end <= pe->gpe_end) {
1369 			gctl_error(req, "%d end '%jd'", ENOSPC,
1370 			    (intmax_t)end);
1371 			return (ENOSPC);
1372 		}
1373 		if (entry->gpe_start < pe->gpe_start && end > pe->gpe_end) {
1374 			gctl_error(req, "%d size '%jd'", ENOSPC,
1375 			    (intmax_t)gpp->gpp_size);
1376 			return (ENOSPC);
1377 		}
1378 	}
1379 
1380 	pp = entry->gpe_pp;
1381 	if ((g_debugflags & 16) == 0 &&
1382 	    (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)) {
1383 		if (entry->gpe_end - entry->gpe_start + 1 > gpp->gpp_size) {
1384 			/* Deny shrinking of an opened partition. */
1385 			gctl_error(req, "%d", EBUSY);
1386 			return (EBUSY);
1387 		}
1388 	}
1389 
1390 	error = G_PART_RESIZE(table, entry, gpp);
1391 	if (error) {
1392 		gctl_error(req, "%d%s", error, error != EBUSY ? "":
1393 		    " resizing will lead to unexpected shrinking"
1394 		    " due to alignment");
1395 		return (error);
1396 	}
1397 
1398 	if (!entry->gpe_created)
1399 		entry->gpe_modified = 1;
1400 
1401 	/* update mediasize of changed provider */
1402 	mediasize = (entry->gpe_end - entry->gpe_start + 1) *
1403 		pp->sectorsize;
1404 	g_resize_provider(pp, mediasize);
1405 
1406 	/* Provide feedback if so requested. */
1407 	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1408 		sb = sbuf_new_auto();
1409 		G_PART_FULLNAME(table, entry, sb, gp->name);
1410 		sbuf_cat(sb, " resized\n");
1411 		sbuf_finish(sb);
1412 		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1413 		sbuf_delete(sb);
1414 	}
1415 	return (0);
1416 }
1417 
1418 static int
1419 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
1420     unsigned int set)
1421 {
1422 	struct g_geom *gp;
1423 	struct g_part_entry *entry;
1424 	struct g_part_table *table;
1425 	struct sbuf *sb;
1426 	int error;
1427 
1428 	gp = gpp->gpp_geom;
1429 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1430 	g_topology_assert();
1431 
1432 	table = gp->softc;
1433 
1434 	if (gpp->gpp_parms & G_PART_PARM_INDEX) {
1435 		LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1436 			if (entry->gpe_deleted || entry->gpe_internal)
1437 				continue;
1438 			if (entry->gpe_index == gpp->gpp_index)
1439 				break;
1440 		}
1441 		if (entry == NULL) {
1442 			gctl_error(req, "%d index '%d'", ENOENT,
1443 			    gpp->gpp_index);
1444 			return (ENOENT);
1445 		}
1446 	} else
1447 		entry = NULL;
1448 
1449 	error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
1450 	if (error) {
1451 		gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
1452 		return (error);
1453 	}
1454 
1455 	/* Provide feedback if so requested. */
1456 	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1457 		sb = sbuf_new_auto();
1458 		sbuf_printf(sb, "%s %sset on ", gpp->gpp_attrib,
1459 		    (set) ? "" : "un");
1460 		if (entry)
1461 			G_PART_FULLNAME(table, entry, sb, gp->name);
1462 		else
1463 			sbuf_cat(sb, gp->name);
1464 		sbuf_cat(sb, "\n");
1465 		sbuf_finish(sb);
1466 		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1467 		sbuf_delete(sb);
1468 	}
1469 	return (0);
1470 }
1471 
1472 static int
1473 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1474 {
1475 	struct g_consumer *cp;
1476 	struct g_provider *pp;
1477 	struct g_geom *gp;
1478 	struct g_part_entry *entry, *tmp;
1479 	struct g_part_table *table;
1480 	int error, reprobe;
1481 
1482 	gp = gpp->gpp_geom;
1483 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1484 	g_topology_assert();
1485 
1486 	table = gp->softc;
1487 	if (!table->gpt_opened) {
1488 		gctl_error(req, "%d", EPERM);
1489 		return (EPERM);
1490 	}
1491 
1492 	cp = LIST_FIRST(&gp->consumer);
1493 	LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1494 		entry->gpe_modified = 0;
1495 		if (entry->gpe_created) {
1496 			pp = entry->gpe_pp;
1497 			if (pp != NULL) {
1498 				pp->private = NULL;
1499 				entry->gpe_pp = NULL;
1500 				g_wither_provider(pp, ENXIO);
1501 			}
1502 			entry->gpe_deleted = 1;
1503 		}
1504 		if (entry->gpe_deleted) {
1505 			LIST_REMOVE(entry, gpe_entry);
1506 			g_free(entry);
1507 		}
1508 	}
1509 
1510 	g_topology_unlock();
1511 
1512 	reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1513 	    table->gpt_created) ? 1 : 0;
1514 
1515 	if (reprobe) {
1516 		LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1517 			if (entry->gpe_internal)
1518 				continue;
1519 			error = EBUSY;
1520 			goto fail;
1521 		}
1522 		while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1523 			LIST_REMOVE(entry, gpe_entry);
1524 			g_free(entry);
1525 		}
1526 		error = g_part_probe(gp, cp, table->gpt_depth);
1527 		if (error) {
1528 			g_topology_lock();
1529 			g_access(cp, -1, -1, -1);
1530 			g_part_wither(gp, error);
1531 			return (0);
1532 		}
1533 		table = gp->softc;
1534 
1535 		/*
1536 		 * Synthesize a disk geometry. Some partitioning schemes
1537 		 * depend on it and since some file systems need it even
1538 		 * when the partitition scheme doesn't, we do it here in
1539 		 * scheme-independent code.
1540 		 */
1541 		pp = cp->provider;
1542 		g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1543 	}
1544 
1545 	error = G_PART_READ(table, cp);
1546 	if (error)
1547 		goto fail;
1548 	error = g_part_check_integrity(table, cp);
1549 	if (error)
1550 		goto fail;
1551 
1552 	g_topology_lock();
1553 	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1554 		if (!entry->gpe_internal)
1555 			g_part_new_provider(gp, table, entry);
1556 	}
1557 
1558 	table->gpt_opened = 0;
1559 	g_access(cp, -1, -1, -1);
1560 	return (0);
1561 
1562 fail:
1563 	g_topology_lock();
1564 	gctl_error(req, "%d", error);
1565 	return (error);
1566 }
1567 
1568 static void
1569 g_part_wither(struct g_geom *gp, int error)
1570 {
1571 	struct g_part_entry *entry;
1572 	struct g_part_table *table;
1573 	struct g_provider *pp;
1574 
1575 	table = gp->softc;
1576 	if (table != NULL) {
1577 		gp->softc = NULL;
1578 		while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1579 			LIST_REMOVE(entry, gpe_entry);
1580 			pp = entry->gpe_pp;
1581 			entry->gpe_pp = NULL;
1582 			if (pp != NULL) {
1583 				pp->private = NULL;
1584 				g_wither_provider(pp, error);
1585 			}
1586 			g_free(entry);
1587 		}
1588 		G_PART_DESTROY(table, NULL);
1589 		kobj_delete((kobj_t)table, M_GEOM);
1590 	}
1591 	g_wither_geom(gp, error);
1592 }
1593 
1594 /*
1595  * Class methods.
1596  */
1597 
1598 static void
1599 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1600 {
1601 	struct g_part_parms gpp;
1602 	struct g_part_table *table;
1603 	struct gctl_req_arg *ap;
1604 	enum g_part_ctl ctlreq;
1605 	unsigned int i, mparms, oparms, parm;
1606 	int auto_commit, close_on_error;
1607 	int error, modifies;
1608 
1609 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1610 	g_topology_assert();
1611 
1612 	ctlreq = G_PART_CTL_NONE;
1613 	modifies = 1;
1614 	mparms = 0;
1615 	oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1616 	switch (*verb) {
1617 	case 'a':
1618 		if (!strcmp(verb, "add")) {
1619 			ctlreq = G_PART_CTL_ADD;
1620 			mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1621 			    G_PART_PARM_START | G_PART_PARM_TYPE;
1622 			oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1623 		}
1624 		break;
1625 	case 'b':
1626 		if (!strcmp(verb, "bootcode")) {
1627 			ctlreq = G_PART_CTL_BOOTCODE;
1628 			mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1629 		}
1630 		break;
1631 	case 'c':
1632 		if (!strcmp(verb, "commit")) {
1633 			ctlreq = G_PART_CTL_COMMIT;
1634 			mparms |= G_PART_PARM_GEOM;
1635 			modifies = 0;
1636 		} else if (!strcmp(verb, "create")) {
1637 			ctlreq = G_PART_CTL_CREATE;
1638 			mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1639 			oparms |= G_PART_PARM_ENTRIES;
1640 		}
1641 		break;
1642 	case 'd':
1643 		if (!strcmp(verb, "delete")) {
1644 			ctlreq = G_PART_CTL_DELETE;
1645 			mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1646 		} else if (!strcmp(verb, "destroy")) {
1647 			ctlreq = G_PART_CTL_DESTROY;
1648 			mparms |= G_PART_PARM_GEOM;
1649 			oparms |= G_PART_PARM_FORCE;
1650 		}
1651 		break;
1652 	case 'm':
1653 		if (!strcmp(verb, "modify")) {
1654 			ctlreq = G_PART_CTL_MODIFY;
1655 			mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1656 			oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1657 		} else if (!strcmp(verb, "move")) {
1658 			ctlreq = G_PART_CTL_MOVE;
1659 			mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1660 		}
1661 		break;
1662 	case 'r':
1663 		if (!strcmp(verb, "recover")) {
1664 			ctlreq = G_PART_CTL_RECOVER;
1665 			mparms |= G_PART_PARM_GEOM;
1666 		} else if (!strcmp(verb, "resize")) {
1667 			ctlreq = G_PART_CTL_RESIZE;
1668 			mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX |
1669 			    G_PART_PARM_SIZE;
1670 		}
1671 		break;
1672 	case 's':
1673 		if (!strcmp(verb, "set")) {
1674 			ctlreq = G_PART_CTL_SET;
1675 			mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1676 			oparms |= G_PART_PARM_INDEX;
1677 		}
1678 		break;
1679 	case 'u':
1680 		if (!strcmp(verb, "undo")) {
1681 			ctlreq = G_PART_CTL_UNDO;
1682 			mparms |= G_PART_PARM_GEOM;
1683 			modifies = 0;
1684 		} else if (!strcmp(verb, "unset")) {
1685 			ctlreq = G_PART_CTL_UNSET;
1686 			mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1687 			oparms |= G_PART_PARM_INDEX;
1688 		}
1689 		break;
1690 	}
1691 	if (ctlreq == G_PART_CTL_NONE) {
1692 		gctl_error(req, "%d verb '%s'", EINVAL, verb);
1693 		return;
1694 	}
1695 
1696 	bzero(&gpp, sizeof(gpp));
1697 	for (i = 0; i < req->narg; i++) {
1698 		ap = &req->arg[i];
1699 		parm = 0;
1700 		switch (ap->name[0]) {
1701 		case 'a':
1702 			if (!strcmp(ap->name, "arg0")) {
1703 				parm = mparms &
1704 				    (G_PART_PARM_GEOM | G_PART_PARM_PROVIDER);
1705 			}
1706 			if (!strcmp(ap->name, "attrib"))
1707 				parm = G_PART_PARM_ATTRIB;
1708 			break;
1709 		case 'b':
1710 			if (!strcmp(ap->name, "bootcode"))
1711 				parm = G_PART_PARM_BOOTCODE;
1712 			break;
1713 		case 'c':
1714 			if (!strcmp(ap->name, "class"))
1715 				continue;
1716 			break;
1717 		case 'e':
1718 			if (!strcmp(ap->name, "entries"))
1719 				parm = G_PART_PARM_ENTRIES;
1720 			break;
1721 		case 'f':
1722 			if (!strcmp(ap->name, "flags"))
1723 				parm = G_PART_PARM_FLAGS;
1724 			else if (!strcmp(ap->name, "force"))
1725 				parm = G_PART_PARM_FORCE;
1726 			break;
1727 		case 'i':
1728 			if (!strcmp(ap->name, "index"))
1729 				parm = G_PART_PARM_INDEX;
1730 			break;
1731 		case 'l':
1732 			if (!strcmp(ap->name, "label"))
1733 				parm = G_PART_PARM_LABEL;
1734 			break;
1735 		case 'o':
1736 			if (!strcmp(ap->name, "output"))
1737 				parm = G_PART_PARM_OUTPUT;
1738 			break;
1739 		case 's':
1740 			if (!strcmp(ap->name, "scheme"))
1741 				parm = G_PART_PARM_SCHEME;
1742 			else if (!strcmp(ap->name, "size"))
1743 				parm = G_PART_PARM_SIZE;
1744 			else if (!strcmp(ap->name, "start"))
1745 				parm = G_PART_PARM_START;
1746 			break;
1747 		case 't':
1748 			if (!strcmp(ap->name, "type"))
1749 				parm = G_PART_PARM_TYPE;
1750 			break;
1751 		case 'v':
1752 			if (!strcmp(ap->name, "verb"))
1753 				continue;
1754 			else if (!strcmp(ap->name, "version"))
1755 				parm = G_PART_PARM_VERSION;
1756 			break;
1757 		}
1758 		if ((parm & (mparms | oparms)) == 0) {
1759 			gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1760 			return;
1761 		}
1762 		switch (parm) {
1763 		case G_PART_PARM_ATTRIB:
1764 			error = g_part_parm_str(req, ap->name,
1765 			    &gpp.gpp_attrib);
1766 			break;
1767 		case G_PART_PARM_BOOTCODE:
1768 			error = g_part_parm_bootcode(req, ap->name,
1769 			    &gpp.gpp_codeptr, &gpp.gpp_codesize);
1770 			break;
1771 		case G_PART_PARM_ENTRIES:
1772 			error = g_part_parm_intmax(req, ap->name,
1773 			    &gpp.gpp_entries);
1774 			break;
1775 		case G_PART_PARM_FLAGS:
1776 			error = g_part_parm_str(req, ap->name, &gpp.gpp_flags);
1777 			break;
1778 		case G_PART_PARM_FORCE:
1779 			error = g_part_parm_uint32(req, ap->name,
1780 			    &gpp.gpp_force);
1781 			break;
1782 		case G_PART_PARM_GEOM:
1783 			error = g_part_parm_geom(req, ap->name, &gpp.gpp_geom);
1784 			break;
1785 		case G_PART_PARM_INDEX:
1786 			error = g_part_parm_intmax(req, ap->name,
1787 			    &gpp.gpp_index);
1788 			break;
1789 		case G_PART_PARM_LABEL:
1790 			error = g_part_parm_str(req, ap->name, &gpp.gpp_label);
1791 			break;
1792 		case G_PART_PARM_OUTPUT:
1793 			error = 0;	/* Write-only parameter */
1794 			break;
1795 		case G_PART_PARM_PROVIDER:
1796 			error = g_part_parm_provider(req, ap->name,
1797 			    &gpp.gpp_provider);
1798 			break;
1799 		case G_PART_PARM_SCHEME:
1800 			error = g_part_parm_scheme(req, ap->name,
1801 			    &gpp.gpp_scheme);
1802 			break;
1803 		case G_PART_PARM_SIZE:
1804 			error = g_part_parm_quad(req, ap->name, &gpp.gpp_size);
1805 			break;
1806 		case G_PART_PARM_START:
1807 			error = g_part_parm_quad(req, ap->name,
1808 			    &gpp.gpp_start);
1809 			break;
1810 		case G_PART_PARM_TYPE:
1811 			error = g_part_parm_str(req, ap->name, &gpp.gpp_type);
1812 			break;
1813 		case G_PART_PARM_VERSION:
1814 			error = g_part_parm_uint32(req, ap->name,
1815 			    &gpp.gpp_version);
1816 			break;
1817 		default:
1818 			error = EDOOFUS;
1819 			gctl_error(req, "%d %s", error, ap->name);
1820 			break;
1821 		}
1822 		if (error != 0) {
1823 			if (error == ENOATTR) {
1824 				gctl_error(req, "%d param '%s'", error,
1825 				    ap->name);
1826 			}
1827 			return;
1828 		}
1829 		gpp.gpp_parms |= parm;
1830 	}
1831 	if ((gpp.gpp_parms & mparms) != mparms) {
1832 		parm = mparms - (gpp.gpp_parms & mparms);
1833 		gctl_error(req, "%d param '%x'", ENOATTR, parm);
1834 		return;
1835 	}
1836 
1837 	/* Obtain permissions if possible/necessary. */
1838 	close_on_error = 0;
1839 	table = NULL;
1840 	if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1841 		table = gpp.gpp_geom->softc;
1842 		if (table != NULL && table->gpt_corrupt &&
1843 		    ctlreq != G_PART_CTL_DESTROY &&
1844 		    ctlreq != G_PART_CTL_RECOVER) {
1845 			gctl_error(req, "%d table '%s' is corrupt",
1846 			    EPERM, gpp.gpp_geom->name);
1847 			return;
1848 		}
1849 		if (table != NULL && !table->gpt_opened) {
1850 			error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1851 			    1, 1, 1);
1852 			if (error) {
1853 				gctl_error(req, "%d geom '%s'", error,
1854 				    gpp.gpp_geom->name);
1855 				return;
1856 			}
1857 			table->gpt_opened = 1;
1858 			close_on_error = 1;
1859 		}
1860 	}
1861 
1862 	/* Allow the scheme to check or modify the parameters. */
1863 	if (table != NULL) {
1864 		error = G_PART_PRECHECK(table, ctlreq, &gpp);
1865 		if (error) {
1866 			gctl_error(req, "%d pre-check failed", error);
1867 			goto out;
1868 		}
1869 	} else
1870 		error = EDOOFUS;	/* Prevent bogus uninit. warning. */
1871 
1872 	switch (ctlreq) {
1873 	case G_PART_CTL_NONE:
1874 		panic("%s", __func__);
1875 	case G_PART_CTL_ADD:
1876 		error = g_part_ctl_add(req, &gpp);
1877 		break;
1878 	case G_PART_CTL_BOOTCODE:
1879 		error = g_part_ctl_bootcode(req, &gpp);
1880 		break;
1881 	case G_PART_CTL_COMMIT:
1882 		error = g_part_ctl_commit(req, &gpp);
1883 		break;
1884 	case G_PART_CTL_CREATE:
1885 		error = g_part_ctl_create(req, &gpp);
1886 		break;
1887 	case G_PART_CTL_DELETE:
1888 		error = g_part_ctl_delete(req, &gpp);
1889 		break;
1890 	case G_PART_CTL_DESTROY:
1891 		error = g_part_ctl_destroy(req, &gpp);
1892 		break;
1893 	case G_PART_CTL_MODIFY:
1894 		error = g_part_ctl_modify(req, &gpp);
1895 		break;
1896 	case G_PART_CTL_MOVE:
1897 		error = g_part_ctl_move(req, &gpp);
1898 		break;
1899 	case G_PART_CTL_RECOVER:
1900 		error = g_part_ctl_recover(req, &gpp);
1901 		break;
1902 	case G_PART_CTL_RESIZE:
1903 		error = g_part_ctl_resize(req, &gpp);
1904 		break;
1905 	case G_PART_CTL_SET:
1906 		error = g_part_ctl_setunset(req, &gpp, 1);
1907 		break;
1908 	case G_PART_CTL_UNDO:
1909 		error = g_part_ctl_undo(req, &gpp);
1910 		break;
1911 	case G_PART_CTL_UNSET:
1912 		error = g_part_ctl_setunset(req, &gpp, 0);
1913 		break;
1914 	}
1915 
1916 	/* Implement automatic commit. */
1917 	if (!error) {
1918 		auto_commit = (modifies &&
1919 		    (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1920 		    strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1921 		if (auto_commit) {
1922 			KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, ("%s",
1923 			    __func__));
1924 			error = g_part_ctl_commit(req, &gpp);
1925 		}
1926 	}
1927 
1928  out:
1929 	if (error && close_on_error) {
1930 		g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1931 		table->gpt_opened = 0;
1932 	}
1933 }
1934 
1935 static int
1936 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1937     struct g_geom *gp)
1938 {
1939 
1940 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1941 	g_topology_assert();
1942 
1943 	g_part_wither(gp, EINVAL);
1944 	return (0);
1945 }
1946 
1947 static struct g_geom *
1948 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1949 {
1950 	struct g_consumer *cp;
1951 	struct g_geom *gp;
1952 	struct g_part_entry *entry;
1953 	struct g_part_table *table;
1954 	struct root_hold_token *rht;
1955 	struct g_geom_alias *gap;
1956 	int attr, depth;
1957 	int error;
1958 
1959 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1960 	g_topology_assert();
1961 
1962 	/* Skip providers that are already open for writing. */
1963 	if (pp->acw > 0)
1964 		return (NULL);
1965 
1966 	/*
1967 	 * Create a GEOM with consumer and hook it up to the provider.
1968 	 * With that we become part of the topology. Obtain read access
1969 	 * to the provider.
1970 	 */
1971 	gp = g_new_geomf(mp, "%s", pp->name);
1972 	LIST_FOREACH(gap, &pp->geom->aliases, ga_next)
1973 		g_geom_add_alias(gp, gap->ga_alias);
1974 	cp = g_new_consumer(gp);
1975 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1976 	error = g_attach(cp, pp);
1977 	if (error == 0)
1978 		error = g_access(cp, 1, 0, 0);
1979 	if (error != 0) {
1980 		if (cp->provider)
1981 			g_detach(cp);
1982 		g_destroy_consumer(cp);
1983 		g_destroy_geom(gp);
1984 		return (NULL);
1985 	}
1986 
1987 	rht = root_mount_hold(mp->name);
1988 	g_topology_unlock();
1989 
1990 	/*
1991 	 * Short-circuit the whole probing galore when there's no
1992 	 * media present.
1993 	 */
1994 	if (pp->mediasize == 0 || pp->sectorsize == 0) {
1995 		error = ENODEV;
1996 		goto fail;
1997 	}
1998 
1999 	/* Make sure we can nest and if so, determine our depth. */
2000 	error = g_getattr("PART::isleaf", cp, &attr);
2001 	if (!error && attr) {
2002 		error = ENODEV;
2003 		goto fail;
2004 	}
2005 	error = g_getattr("PART::depth", cp, &attr);
2006 	depth = (!error) ? attr + 1 : 0;
2007 
2008 	error = g_part_probe(gp, cp, depth);
2009 	if (error)
2010 		goto fail;
2011 
2012 	table = gp->softc;
2013 
2014 	/*
2015 	 * Synthesize a disk geometry. Some partitioning schemes
2016 	 * depend on it and since some file systems need it even
2017 	 * when the partitition scheme doesn't, we do it here in
2018 	 * scheme-independent code.
2019 	 */
2020 	g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
2021 
2022 	error = G_PART_READ(table, cp);
2023 	if (error)
2024 		goto fail;
2025 	error = g_part_check_integrity(table, cp);
2026 	if (error)
2027 		goto fail;
2028 
2029 	g_topology_lock();
2030 	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
2031 		if (!entry->gpe_internal)
2032 			g_part_new_provider(gp, table, entry);
2033 	}
2034 
2035 	root_mount_rel(rht);
2036 	g_access(cp, -1, 0, 0);
2037 	return (gp);
2038 
2039  fail:
2040 	g_topology_lock();
2041 	root_mount_rel(rht);
2042 	g_access(cp, -1, 0, 0);
2043 	g_detach(cp);
2044 	g_destroy_consumer(cp);
2045 	g_destroy_geom(gp);
2046 	return (NULL);
2047 }
2048 
2049 /*
2050  * Geom methods.
2051  */
2052 
2053 static int
2054 g_part_access(struct g_provider *pp, int dr, int dw, int de)
2055 {
2056 	struct g_consumer *cp;
2057 
2058 	G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
2059 	    dw, de));
2060 
2061 	cp = LIST_FIRST(&pp->geom->consumer);
2062 
2063 	/* We always gain write-exclusive access. */
2064 	return (g_access(cp, dr, dw, dw + de));
2065 }
2066 
2067 static void
2068 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2069     struct g_consumer *cp, struct g_provider *pp)
2070 {
2071 	char buf[64];
2072 	struct g_part_entry *entry;
2073 	struct g_part_table *table;
2074 
2075 	KASSERT(sb != NULL && gp != NULL, ("%s", __func__));
2076 	table = gp->softc;
2077 
2078 	if (indent == NULL) {
2079 		KASSERT(cp == NULL && pp != NULL, ("%s", __func__));
2080 		entry = pp->private;
2081 		if (entry == NULL)
2082 			return;
2083 		sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
2084 		    (uintmax_t)entry->gpe_offset,
2085 		    G_PART_TYPE(table, entry, buf, sizeof(buf)));
2086 		/*
2087 		 * libdisk compatibility quirk - the scheme dumps the
2088 		 * slicer name and partition type in a way that is
2089 		 * compatible with libdisk. When libdisk is not used
2090 		 * anymore, this should go away.
2091 		 */
2092 		G_PART_DUMPCONF(table, entry, sb, indent);
2093 	} else if (cp != NULL) {	/* Consumer configuration. */
2094 		KASSERT(pp == NULL, ("%s", __func__));
2095 		/* none */
2096 	} else if (pp != NULL) {	/* Provider configuration. */
2097 		entry = pp->private;
2098 		if (entry == NULL)
2099 			return;
2100 		sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
2101 		    (uintmax_t)entry->gpe_start);
2102 		sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
2103 		    (uintmax_t)entry->gpe_end);
2104 		sbuf_printf(sb, "%s<index>%u</index>\n", indent,
2105 		    entry->gpe_index);
2106 		sbuf_printf(sb, "%s<type>%s</type>\n", indent,
2107 		    G_PART_TYPE(table, entry, buf, sizeof(buf)));
2108 		sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
2109 		    (uintmax_t)entry->gpe_offset);
2110 		sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
2111 		    (uintmax_t)pp->mediasize);
2112 		G_PART_DUMPCONF(table, entry, sb, indent);
2113 	} else {			/* Geom configuration. */
2114 		sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
2115 		    table->gpt_scheme->name);
2116 		sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
2117 		    table->gpt_entries);
2118 		sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
2119 		    (uintmax_t)table->gpt_first);
2120 		sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
2121 		    (uintmax_t)table->gpt_last);
2122 		sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
2123 		    table->gpt_sectors);
2124 		sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
2125 		    table->gpt_heads);
2126 		sbuf_printf(sb, "%s<state>%s</state>\n", indent,
2127 		    table->gpt_corrupt ? "CORRUPT": "OK");
2128 		sbuf_printf(sb, "%s<modified>%s</modified>\n", indent,
2129 		    table->gpt_opened ? "true": "false");
2130 		G_PART_DUMPCONF(table, NULL, sb, indent);
2131 	}
2132 }
2133 
2134 /*-
2135  * This start routine is only called for non-trivial requests, all the
2136  * trivial ones are handled autonomously by the slice code.
2137  * For requests we handle here, we must call the g_io_deliver() on the
2138  * bio, and return non-zero to indicate to the slice code that we did so.
2139  * This code executes in the "DOWN" I/O path, this means:
2140  *    * No sleeping.
2141  *    * Don't grab the topology lock.
2142  *    * Don't call biowait, g_getattr(), g_setattr() or g_read_data()
2143  */
2144 static int
2145 g_part_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
2146 {
2147 	struct g_part_table *table;
2148 
2149 	table = pp->geom->softc;
2150 	return G_PART_IOCTL(table, pp, cmd, data, fflag, td);
2151 }
2152 
2153 static void
2154 g_part_resize(struct g_consumer *cp)
2155 {
2156 	struct g_part_table *table;
2157 
2158 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2159 	g_topology_assert();
2160 
2161 	if (auto_resize == 0)
2162 		return;
2163 
2164 	table = cp->geom->softc;
2165 	if (table->gpt_opened == 0) {
2166 		if (g_access(cp, 1, 1, 1) != 0)
2167 			return;
2168 		table->gpt_opened = 1;
2169 	}
2170 	if (G_PART_RESIZE(table, NULL, NULL) == 0)
2171 		printf("GEOM_PART: %s was automatically resized.\n"
2172 		    "  Use `gpart commit %s` to save changes or "
2173 		    "`gpart undo %s` to revert them.\n", cp->geom->name,
2174 		    cp->geom->name, cp->geom->name);
2175 	if (g_part_check_integrity(table, cp) != 0) {
2176 		g_access(cp, -1, -1, -1);
2177 		table->gpt_opened = 0;
2178 		g_part_wither(table->gpt_gp, ENXIO);
2179 	}
2180 }
2181 
2182 static void
2183 g_part_orphan(struct g_consumer *cp)
2184 {
2185 	struct g_provider *pp;
2186 	struct g_part_table *table;
2187 
2188 	pp = cp->provider;
2189 	KASSERT(pp != NULL, ("%s", __func__));
2190 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
2191 	g_topology_assert();
2192 
2193 	KASSERT(pp->error != 0, ("%s", __func__));
2194 	table = cp->geom->softc;
2195 	if (table != NULL && table->gpt_opened)
2196 		g_access(cp, -1, -1, -1);
2197 	g_part_wither(cp->geom, pp->error);
2198 }
2199 
2200 static void
2201 g_part_spoiled(struct g_consumer *cp)
2202 {
2203 
2204 	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2205 	g_topology_assert();
2206 
2207 	cp->flags |= G_CF_ORPHAN;
2208 	g_part_wither(cp->geom, ENXIO);
2209 }
2210 
2211 static void
2212 g_part_start(struct bio *bp)
2213 {
2214 	struct bio *bp2;
2215 	struct g_consumer *cp;
2216 	struct g_geom *gp;
2217 	struct g_part_entry *entry;
2218 	struct g_part_table *table;
2219 	struct g_kerneldump *gkd;
2220 	struct g_provider *pp;
2221 	void (*done_func)(struct bio *) = g_std_done;
2222 	char buf[64];
2223 
2224 	biotrack(bp, __func__);
2225 
2226 	pp = bp->bio_to;
2227 	gp = pp->geom;
2228 	table = gp->softc;
2229 	cp = LIST_FIRST(&gp->consumer);
2230 
2231 	G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
2232 	    pp->name));
2233 
2234 	entry = pp->private;
2235 	if (entry == NULL) {
2236 		g_io_deliver(bp, ENXIO);
2237 		return;
2238 	}
2239 
2240 	switch(bp->bio_cmd) {
2241 	case BIO_DELETE:
2242 	case BIO_READ:
2243 	case BIO_WRITE:
2244 		if (bp->bio_offset >= pp->mediasize) {
2245 			g_io_deliver(bp, EIO);
2246 			return;
2247 		}
2248 		bp2 = g_clone_bio(bp);
2249 		if (bp2 == NULL) {
2250 			g_io_deliver(bp, ENOMEM);
2251 			return;
2252 		}
2253 		if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
2254 			bp2->bio_length = pp->mediasize - bp2->bio_offset;
2255 		bp2->bio_done = g_std_done;
2256 		bp2->bio_offset += entry->gpe_offset;
2257 		g_io_request(bp2, cp);
2258 		return;
2259 	case BIO_FLUSH:
2260 		break;
2261 	case BIO_GETATTR:
2262 		if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
2263 			return;
2264 		if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
2265 			return;
2266 		if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
2267 			return;
2268 		if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
2269 			return;
2270 		if (g_handleattr_str(bp, "PART::scheme",
2271 		    table->gpt_scheme->name))
2272 			return;
2273 		if (g_handleattr_str(bp, "PART::type",
2274 		    G_PART_TYPE(table, entry, buf, sizeof(buf))))
2275 			return;
2276 		if (!strcmp("GEOM::physpath", bp->bio_attribute)) {
2277 			done_func = g_part_get_physpath_done;
2278 			break;
2279 		}
2280 		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
2281 			/*
2282 			 * Check that the partition is suitable for kernel
2283 			 * dumps. Typically only swap partitions should be
2284 			 * used. If the request comes from the nested scheme
2285 			 * we allow dumping there as well.
2286 			 */
2287 			if ((bp->bio_from == NULL ||
2288 			    bp->bio_from->geom->class != &g_part_class) &&
2289 			    G_PART_DUMPTO(table, entry) == 0) {
2290 				g_io_deliver(bp, ENODEV);
2291 				printf("GEOM_PART: Partition '%s' not suitable"
2292 				    " for kernel dumps (wrong type?)\n",
2293 				    pp->name);
2294 				return;
2295 			}
2296 			gkd = (struct g_kerneldump *)bp->bio_data;
2297 			if (gkd->offset >= pp->mediasize) {
2298 				g_io_deliver(bp, EIO);
2299 				return;
2300 			}
2301 			if (gkd->offset + gkd->length > pp->mediasize)
2302 				gkd->length = pp->mediasize - gkd->offset;
2303 			gkd->offset += entry->gpe_offset;
2304 		}
2305 		break;
2306 	default:
2307 		g_io_deliver(bp, EOPNOTSUPP);
2308 		return;
2309 	}
2310 
2311 	bp2 = g_clone_bio(bp);
2312 	if (bp2 == NULL) {
2313 		g_io_deliver(bp, ENOMEM);
2314 		return;
2315 	}
2316 	bp2->bio_done = done_func;
2317 	g_io_request(bp2, cp);
2318 }
2319 
2320 static void
2321 g_part_init(struct g_class *mp)
2322 {
2323 
2324 	TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
2325 }
2326 
2327 static void
2328 g_part_fini(struct g_class *mp)
2329 {
2330 
2331 	TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
2332 }
2333 
2334 static void
2335 g_part_unload_event(void *arg, int flag)
2336 {
2337 	struct g_consumer *cp;
2338 	struct g_geom *gp;
2339 	struct g_provider *pp;
2340 	struct g_part_scheme *scheme;
2341 	struct g_part_table *table;
2342 	uintptr_t *xchg;
2343 	int acc, error;
2344 
2345 	if (flag == EV_CANCEL)
2346 		return;
2347 
2348 	xchg = arg;
2349 	error = 0;
2350 	scheme = (void *)(*xchg);
2351 
2352 	g_topology_assert();
2353 
2354 	LIST_FOREACH(gp, &g_part_class.geom, geom) {
2355 		table = gp->softc;
2356 		if (table->gpt_scheme != scheme)
2357 			continue;
2358 
2359 		acc = 0;
2360 		LIST_FOREACH(pp, &gp->provider, provider)
2361 			acc += pp->acr + pp->acw + pp->ace;
2362 		LIST_FOREACH(cp, &gp->consumer, consumer)
2363 			acc += cp->acr + cp->acw + cp->ace;
2364 
2365 		if (!acc)
2366 			g_part_wither(gp, ENOSYS);
2367 		else
2368 			error = EBUSY;
2369 	}
2370 
2371 	if (!error)
2372 		TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
2373 
2374 	*xchg = error;
2375 }
2376 
2377 int
2378 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
2379 {
2380 	struct g_part_scheme *iter;
2381 	uintptr_t arg;
2382 	int error;
2383 
2384 	error = 0;
2385 	switch (type) {
2386 	case MOD_LOAD:
2387 		TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
2388 			if (scheme == iter) {
2389 				printf("GEOM_PART: scheme %s is already "
2390 				    "registered!\n", scheme->name);
2391 				break;
2392 			}
2393 		}
2394 		if (iter == NULL) {
2395 			TAILQ_INSERT_TAIL(&g_part_schemes, scheme,
2396 			    scheme_list);
2397 			g_retaste(&g_part_class);
2398 		}
2399 		break;
2400 	case MOD_UNLOAD:
2401 		arg = (uintptr_t)scheme;
2402 		error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,
2403 		    NULL);
2404 		if (error == 0)
2405 			error = arg;
2406 		break;
2407 	default:
2408 		error = EOPNOTSUPP;
2409 		break;
2410 	}
2411 
2412 	return (error);
2413 }
2414