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