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