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