g_part.c revision 197449
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: head/sys/geom/part/g_part.c 197449 2009-09-24 06:00:49Z marcel $");
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/systm.h>
43#include <sys/uuid.h>
44#include <geom/geom.h>
45#include <geom/geom_ctl.h>
46#include <geom/geom_int.h>
47#include <geom/part/g_part.h>
48
49#include "g_part_if.h"
50
51#ifndef _PATH_DEV
52#define _PATH_DEV "/dev/"
53#endif
54
55static kobj_method_t g_part_null_methods[] = {
56	{ 0, 0 }
57};
58
59static struct g_part_scheme g_part_null_scheme = {
60	"(none)",
61	g_part_null_methods,
62	sizeof(struct g_part_table),
63};
64
65TAILQ_HEAD(, g_part_scheme) g_part_schemes =
66    TAILQ_HEAD_INITIALIZER(g_part_schemes);
67
68struct g_part_alias_list {
69	const char *lexeme;
70	enum g_part_alias alias;
71} g_part_alias_list[G_PART_ALIAS_COUNT] = {
72	{ "apple-hfs", G_PART_ALIAS_APPLE_HFS },
73	{ "efi", G_PART_ALIAS_EFI },
74	{ "freebsd", G_PART_ALIAS_FREEBSD },
75	{ "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
76	{ "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
77	{ "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
78	{ "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
79	{ "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
80	{ "mbr", G_PART_ALIAS_MBR }
81};
82
83/*
84 * The GEOM partitioning class.
85 */
86static g_ctl_req_t g_part_ctlreq;
87static g_ctl_destroy_geom_t g_part_destroy_geom;
88static g_fini_t g_part_fini;
89static g_init_t g_part_init;
90static g_taste_t g_part_taste;
91
92static g_access_t g_part_access;
93static g_dumpconf_t g_part_dumpconf;
94static g_orphan_t g_part_orphan;
95static g_spoiled_t g_part_spoiled;
96static g_start_t g_part_start;
97
98static struct g_class g_part_class = {
99	.name = "PART",
100	.version = G_VERSION,
101	/* Class methods. */
102	.ctlreq = g_part_ctlreq,
103	.destroy_geom = g_part_destroy_geom,
104	.fini = g_part_fini,
105	.init = g_part_init,
106	.taste = g_part_taste,
107	/* Geom methods. */
108	.access = g_part_access,
109	.dumpconf = g_part_dumpconf,
110	.orphan = g_part_orphan,
111	.spoiled = g_part_spoiled,
112	.start = g_part_start,
113};
114
115DECLARE_GEOM_CLASS(g_part_class, g_part);
116
117/*
118 * Support functions.
119 */
120
121static void g_part_wither(struct g_geom *, int);
122
123const char *
124g_part_alias_name(enum g_part_alias alias)
125{
126	int i;
127
128	for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
129		if (g_part_alias_list[i].alias != alias)
130			continue;
131		return (g_part_alias_list[i].lexeme);
132	}
133
134	return (NULL);
135}
136
137void
138g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
139    u_int *bestheads)
140{
141	static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
142	off_t chs, cylinders;
143	u_int heads;
144	int idx;
145
146	*bestchs = 0;
147	*bestheads = 0;
148	for (idx = 0; candidate_heads[idx] != 0; idx++) {
149		heads = candidate_heads[idx];
150		cylinders = blocks / heads / sectors;
151		if (cylinders < heads || cylinders < sectors)
152			break;
153		if (cylinders > 1023)
154			continue;
155		chs = cylinders * heads * sectors;
156		if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
157			*bestchs = chs;
158			*bestheads = heads;
159		}
160	}
161}
162
163static void
164g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
165    off_t blocks)
166{
167	static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
168	off_t chs, bestchs;
169	u_int heads, sectors;
170	int idx;
171
172	if (g_getattr("GEOM::fwsectors", cp, &sectors) != 0 || sectors == 0 ||
173	    g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
174		table->gpt_fixgeom = 0;
175		table->gpt_heads = 0;
176		table->gpt_sectors = 0;
177		bestchs = 0;
178		for (idx = 0; candidate_sectors[idx] != 0; idx++) {
179			sectors = candidate_sectors[idx];
180			g_part_geometry_heads(blocks, sectors, &chs, &heads);
181			if (chs == 0)
182				continue;
183			/*
184			 * Prefer a geometry with sectors > 1, but only if
185			 * it doesn't bump down the numbver of heads to 1.
186			 */
187			if (chs > bestchs || (chs == bestchs && heads > 1 &&
188			    table->gpt_sectors == 1)) {
189				bestchs = chs;
190				table->gpt_heads = heads;
191				table->gpt_sectors = sectors;
192			}
193		}
194		/*
195		 * If we didn't find a geometry at all, then the disk is
196		 * too big. This means we can use the maximum number of
197		 * heads and sectors.
198		 */
199		if (bestchs == 0) {
200			table->gpt_heads = 255;
201			table->gpt_sectors = 63;
202		}
203	} else {
204		table->gpt_fixgeom = 1;
205		table->gpt_heads = heads;
206		table->gpt_sectors = sectors;
207	}
208}
209
210struct g_part_entry *
211g_part_new_entry(struct g_part_table *table, int index, quad_t start,
212    quad_t end)
213{
214	struct g_part_entry *entry, *last;
215
216	last = NULL;
217	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
218		if (entry->gpe_index == index)
219			break;
220		if (entry->gpe_index > index) {
221			entry = NULL;
222			break;
223		}
224		last = entry;
225	}
226	if (entry == NULL) {
227		entry = g_malloc(table->gpt_scheme->gps_entrysz,
228		    M_WAITOK | M_ZERO);
229		entry->gpe_index = index;
230		if (last == NULL)
231			LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
232		else
233			LIST_INSERT_AFTER(last, entry, gpe_entry);
234	} else
235		entry->gpe_offset = 0;
236	entry->gpe_start = start;
237	entry->gpe_end = end;
238	return (entry);
239}
240
241static void
242g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
243    struct g_part_entry *entry)
244{
245	struct g_consumer *cp;
246	struct g_provider *pp;
247	struct sbuf *sb;
248	off_t offset;
249
250	cp = LIST_FIRST(&gp->consumer);
251	pp = cp->provider;
252
253	offset = entry->gpe_start * pp->sectorsize;
254	if (entry->gpe_offset < offset)
255		entry->gpe_offset = offset;
256
257	if (entry->gpe_pp == NULL) {
258		sb = sbuf_new_auto();
259		G_PART_FULLNAME(table, entry, sb, gp->name);
260		sbuf_finish(sb);
261		entry->gpe_pp = g_new_providerf(gp, "%s", sbuf_data(sb));
262		sbuf_delete(sb);
263		entry->gpe_pp->private = entry;		/* Close the circle. */
264	}
265	entry->gpe_pp->index = entry->gpe_index - 1;	/* index is 1-based. */
266	entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
267	    pp->sectorsize;
268	entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
269	entry->gpe_pp->sectorsize = pp->sectorsize;
270	entry->gpe_pp->flags = pp->flags & G_PF_CANDELETE;
271	if (pp->stripesize > 0) {
272		entry->gpe_pp->stripesize = pp->stripesize;
273		entry->gpe_pp->stripeoffset = (pp->stripeoffset +
274		    entry->gpe_offset) % pp->stripesize;
275	}
276	g_error_provider(entry->gpe_pp, 0);
277}
278
279static int
280g_part_parm_geom(const char *rawname, struct g_geom **v)
281{
282	struct g_geom *gp;
283	const char *pname;
284
285	if (strncmp(rawname, _PATH_DEV, strlen(_PATH_DEV)) == 0)
286		pname = rawname + strlen(_PATH_DEV);
287	else
288		pname = rawname;
289	LIST_FOREACH(gp, &g_part_class.geom, geom) {
290		if (!strcmp(pname, gp->name))
291			break;
292	}
293	if (gp == NULL)
294		return (EINVAL);
295	*v = gp;
296	return (0);
297}
298
299static int
300g_part_parm_provider(const char *pname, struct g_provider **v)
301{
302	struct g_provider *pp;
303
304	if (strncmp(pname, _PATH_DEV, strlen(_PATH_DEV)) == 0)
305		pp = g_provider_by_name(pname + strlen(_PATH_DEV));
306	else
307		pp = g_provider_by_name(pname);
308	if (pp == NULL)
309		return (EINVAL);
310	*v = pp;
311	return (0);
312}
313
314static int
315g_part_parm_quad(const char *p, quad_t *v)
316{
317	char *x;
318	quad_t q;
319
320	q = strtoq(p, &x, 0);
321	if (*x != '\0' || q < 0)
322		return (EINVAL);
323	*v = q;
324	return (0);
325}
326
327static int
328g_part_parm_scheme(const char *p, struct g_part_scheme **v)
329{
330	struct g_part_scheme *s;
331
332	TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
333		if (s == &g_part_null_scheme)
334			continue;
335		if (!strcasecmp(s->name, p))
336			break;
337	}
338	if (s == NULL)
339		return (EINVAL);
340	*v = s;
341	return (0);
342}
343
344static int
345g_part_parm_str(const char *p, const char **v)
346{
347
348	if (p[0] == '\0')
349		return (EINVAL);
350	*v = p;
351	return (0);
352}
353
354static int
355g_part_parm_uint(const char *p, u_int *v)
356{
357	char *x;
358	long l;
359
360	l = strtol(p, &x, 0);
361	if (*x != '\0' || l < 0 || l > INT_MAX)
362		return (EINVAL);
363	*v = (unsigned int)l;
364	return (0);
365}
366
367static int
368g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
369{
370	struct g_part_scheme *iter, *scheme;
371	struct g_part_table *table;
372	int pri, probe;
373
374	table = gp->softc;
375	scheme = (table != NULL) ? table->gpt_scheme : NULL;
376	pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
377	if (pri == 0)
378		goto done;
379	if (pri > 0) {	/* error */
380		scheme = NULL;
381		pri = INT_MIN;
382	}
383
384	TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
385		if (iter == &g_part_null_scheme)
386			continue;
387		table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
388		    M_WAITOK);
389		table->gpt_gp = gp;
390		table->gpt_scheme = iter;
391		table->gpt_depth = depth;
392		probe = G_PART_PROBE(table, cp);
393		if (probe <= 0 && probe > pri) {
394			pri = probe;
395			scheme = iter;
396			if (gp->softc != NULL)
397				kobj_delete((kobj_t)gp->softc, M_GEOM);
398			gp->softc = table;
399			if (pri == 0)
400				goto done;
401		} else
402			kobj_delete((kobj_t)table, M_GEOM);
403	}
404
405done:
406	return ((scheme == NULL) ? ENXIO : 0);
407}
408
409/*
410 * Control request functions.
411 */
412
413static int
414g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
415{
416	struct g_geom *gp;
417	struct g_provider *pp;
418	struct g_part_entry *delent, *last, *entry;
419	struct g_part_table *table;
420	struct sbuf *sb;
421	quad_t end;
422	unsigned int index;
423	int error;
424
425	gp = gpp->gpp_geom;
426	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
427	g_topology_assert();
428
429	pp = LIST_FIRST(&gp->consumer)->provider;
430	table = gp->softc;
431	end = gpp->gpp_start + gpp->gpp_size - 1;
432
433	if (gpp->gpp_start < table->gpt_first ||
434	    gpp->gpp_start > table->gpt_last) {
435		gctl_error(req, "%d start '%jd'", EINVAL,
436		    (intmax_t)gpp->gpp_start);
437		return (EINVAL);
438	}
439	if (end < gpp->gpp_start || end > table->gpt_last) {
440		gctl_error(req, "%d size '%jd'", EINVAL,
441		    (intmax_t)gpp->gpp_size);
442		return (EINVAL);
443	}
444	if (gpp->gpp_index > table->gpt_entries) {
445		gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
446		return (EINVAL);
447	}
448
449	delent = last = NULL;
450	index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
451	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
452		if (entry->gpe_deleted) {
453			if (entry->gpe_index == index)
454				delent = entry;
455			continue;
456		}
457		if (entry->gpe_index == index)
458			index = entry->gpe_index + 1;
459		if (entry->gpe_index < index)
460			last = entry;
461		if (entry->gpe_internal)
462			continue;
463		if (gpp->gpp_start >= entry->gpe_start &&
464		    gpp->gpp_start <= entry->gpe_end) {
465			gctl_error(req, "%d start '%jd'", ENOSPC,
466			    (intmax_t)gpp->gpp_start);
467			return (ENOSPC);
468		}
469		if (end >= entry->gpe_start && end <= entry->gpe_end) {
470			gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
471			return (ENOSPC);
472		}
473		if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
474			gctl_error(req, "%d size '%jd'", ENOSPC,
475			    (intmax_t)gpp->gpp_size);
476			return (ENOSPC);
477		}
478	}
479	if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
480		gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
481		return (EEXIST);
482	}
483	if (index > table->gpt_entries) {
484		gctl_error(req, "%d index '%d'", ENOSPC, index);
485		return (ENOSPC);
486	}
487
488	entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
489	    M_WAITOK | M_ZERO) : delent;
490	entry->gpe_index = index;
491	entry->gpe_start = gpp->gpp_start;
492	entry->gpe_end = end;
493	error = G_PART_ADD(table, entry, gpp);
494	if (error) {
495		gctl_error(req, "%d", error);
496		if (delent == NULL)
497			g_free(entry);
498		return (error);
499	}
500	if (delent == NULL) {
501		if (last == NULL)
502			LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
503		else
504			LIST_INSERT_AFTER(last, entry, gpe_entry);
505		entry->gpe_created = 1;
506	} else {
507		entry->gpe_deleted = 0;
508		entry->gpe_modified = 1;
509	}
510	g_part_new_provider(gp, table, entry);
511
512	/* Provide feedback if so requested. */
513	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
514		sb = sbuf_new_auto();
515		G_PART_FULLNAME(table, entry, sb, gp->name);
516		sbuf_cat(sb, " added\n");
517		sbuf_finish(sb);
518		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
519		sbuf_delete(sb);
520	}
521	return (0);
522}
523
524static int
525g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
526{
527	struct g_geom *gp;
528	struct g_part_table *table;
529	struct sbuf *sb;
530	int error, sz;
531
532	gp = gpp->gpp_geom;
533	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
534	g_topology_assert();
535
536	table = gp->softc;
537	sz = table->gpt_scheme->gps_bootcodesz;
538	if (sz == 0) {
539		error = ENODEV;
540		goto fail;
541	}
542	if (gpp->gpp_codesize > sz) {
543		error = EFBIG;
544		goto fail;
545	}
546
547	error = G_PART_BOOTCODE(table, gpp);
548	if (error)
549		goto fail;
550
551	/* Provide feedback if so requested. */
552	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
553		sb = sbuf_new_auto();
554		sbuf_printf(sb, "%s has bootcode\n", gp->name);
555		sbuf_finish(sb);
556		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
557		sbuf_delete(sb);
558	}
559	return (0);
560
561 fail:
562	gctl_error(req, "%d", error);
563	return (error);
564}
565
566static int
567g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
568{
569	struct g_consumer *cp;
570	struct g_geom *gp;
571	struct g_provider *pp;
572	struct g_part_entry *entry, *tmp;
573	struct g_part_table *table;
574	char *buf;
575	int error, i;
576
577	gp = gpp->gpp_geom;
578	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
579	g_topology_assert();
580
581	table = gp->softc;
582	if (!table->gpt_opened) {
583		gctl_error(req, "%d", EPERM);
584		return (EPERM);
585	}
586
587	g_topology_unlock();
588
589	cp = LIST_FIRST(&gp->consumer);
590	if ((table->gpt_smhead | table->gpt_smtail) != 0) {
591		pp = cp->provider;
592		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
593		while (table->gpt_smhead != 0) {
594			i = ffs(table->gpt_smhead) - 1;
595			error = g_write_data(cp, i * pp->sectorsize, buf,
596			    pp->sectorsize);
597			if (error) {
598				g_free(buf);
599				goto fail;
600			}
601			table->gpt_smhead &= ~(1 << i);
602		}
603		while (table->gpt_smtail != 0) {
604			i = ffs(table->gpt_smtail) - 1;
605			error = g_write_data(cp, pp->mediasize - (i + 1) *
606			    pp->sectorsize, buf, pp->sectorsize);
607			if (error) {
608				g_free(buf);
609				goto fail;
610			}
611			table->gpt_smtail &= ~(1 << i);
612		}
613		g_free(buf);
614	}
615
616	if (table->gpt_scheme == &g_part_null_scheme) {
617		g_topology_lock();
618		g_access(cp, -1, -1, -1);
619		g_part_wither(gp, ENXIO);
620		return (0);
621	}
622
623	error = G_PART_WRITE(table, cp);
624	if (error)
625		goto fail;
626
627	LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
628		if (!entry->gpe_deleted) {
629			entry->gpe_created = 0;
630			entry->gpe_modified = 0;
631			continue;
632		}
633		LIST_REMOVE(entry, gpe_entry);
634		g_free(entry);
635	}
636	table->gpt_created = 0;
637	table->gpt_opened = 0;
638
639	g_topology_lock();
640	g_access(cp, -1, -1, -1);
641	return (0);
642
643fail:
644	g_topology_lock();
645	gctl_error(req, "%d", error);
646	return (error);
647}
648
649static int
650g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
651{
652	struct g_consumer *cp;
653	struct g_geom *gp;
654	struct g_provider *pp;
655	struct g_part_scheme *scheme;
656	struct g_part_table *null, *table;
657	struct sbuf *sb;
658	int attr, error;
659
660	pp = gpp->gpp_provider;
661	scheme = gpp->gpp_scheme;
662	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
663	g_topology_assert();
664
665	/* Check that there isn't already a g_part geom on the provider. */
666	error = g_part_parm_geom(pp->name, &gp);
667	if (!error) {
668		null = gp->softc;
669		if (null->gpt_scheme != &g_part_null_scheme) {
670			gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
671			return (EEXIST);
672		}
673	} else
674		null = NULL;
675
676	if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
677	    (gpp->gpp_entries < scheme->gps_minent ||
678	     gpp->gpp_entries > scheme->gps_maxent)) {
679		gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
680		return (EINVAL);
681	}
682
683	if (null == NULL)
684		gp = g_new_geomf(&g_part_class, "%s", pp->name);
685	gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
686	    M_WAITOK);
687	table = gp->softc;
688	table->gpt_gp = gp;
689	table->gpt_scheme = gpp->gpp_scheme;
690	table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
691	    gpp->gpp_entries : scheme->gps_minent;
692	LIST_INIT(&table->gpt_entry);
693	if (null == NULL) {
694		cp = g_new_consumer(gp);
695		error = g_attach(cp, pp);
696		if (error == 0)
697			error = g_access(cp, 1, 1, 1);
698		if (error != 0) {
699			g_part_wither(gp, error);
700			gctl_error(req, "%d geom '%s'", error, pp->name);
701			return (error);
702		}
703		table->gpt_opened = 1;
704	} else {
705		cp = LIST_FIRST(&gp->consumer);
706		table->gpt_opened = null->gpt_opened;
707		table->gpt_smhead = null->gpt_smhead;
708		table->gpt_smtail = null->gpt_smtail;
709	}
710
711	g_topology_unlock();
712
713	/* Make sure the provider has media. */
714	if (pp->mediasize == 0 || pp->sectorsize == 0) {
715		error = ENODEV;
716		goto fail;
717	}
718
719	/* Make sure we can nest and if so, determine our depth. */
720	error = g_getattr("PART::isleaf", cp, &attr);
721	if (!error && attr) {
722		error = ENODEV;
723		goto fail;
724	}
725	error = g_getattr("PART::depth", cp, &attr);
726	table->gpt_depth = (!error) ? attr + 1 : 0;
727
728	/*
729	 * Synthesize a disk geometry. Some partitioning schemes
730	 * depend on it and since some file systems need it even
731	 * when the partitition scheme doesn't, we do it here in
732	 * scheme-independent code.
733	 */
734	g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
735
736	error = G_PART_CREATE(table, gpp);
737	if (error)
738		goto fail;
739
740	g_topology_lock();
741
742	table->gpt_created = 1;
743	if (null != NULL)
744		kobj_delete((kobj_t)null, M_GEOM);
745
746	/*
747	 * Support automatic commit by filling in the gpp_geom
748	 * parameter.
749	 */
750	gpp->gpp_parms |= G_PART_PARM_GEOM;
751	gpp->gpp_geom = gp;
752
753	/* Provide feedback if so requested. */
754	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
755		sb = sbuf_new_auto();
756		sbuf_printf(sb, "%s created\n", gp->name);
757		sbuf_finish(sb);
758		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
759		sbuf_delete(sb);
760	}
761	return (0);
762
763fail:
764	g_topology_lock();
765	if (null == NULL) {
766		g_access(cp, -1, -1, -1);
767		g_part_wither(gp, error);
768	} else {
769		kobj_delete((kobj_t)gp->softc, M_GEOM);
770		gp->softc = null;
771	}
772	gctl_error(req, "%d provider", error);
773	return (error);
774}
775
776static int
777g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
778{
779	struct g_geom *gp;
780	struct g_provider *pp;
781	struct g_part_entry *entry;
782	struct g_part_table *table;
783	struct sbuf *sb;
784
785	gp = gpp->gpp_geom;
786	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
787	g_topology_assert();
788
789	table = gp->softc;
790
791	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
792		if (entry->gpe_deleted || entry->gpe_internal)
793			continue;
794		if (entry->gpe_index == gpp->gpp_index)
795			break;
796	}
797	if (entry == NULL) {
798		gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
799		return (ENOENT);
800	}
801
802	pp = entry->gpe_pp;
803	if (pp != NULL) {
804		if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
805			gctl_error(req, "%d", EBUSY);
806			return (EBUSY);
807		}
808
809		pp->private = NULL;
810		entry->gpe_pp = NULL;
811	}
812
813	if (entry->gpe_created) {
814		LIST_REMOVE(entry, gpe_entry);
815		g_free(entry);
816	} else {
817		entry->gpe_modified = 0;
818		entry->gpe_deleted = 1;
819	}
820
821	if (pp != NULL)
822		g_wither_provider(pp, ENXIO);
823
824	/* Provide feedback if so requested. */
825	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
826		sb = sbuf_new_auto();
827		G_PART_FULLNAME(table, entry, sb, gp->name);
828		sbuf_cat(sb, " deleted\n");
829		sbuf_finish(sb);
830		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
831		sbuf_delete(sb);
832	}
833	return (0);
834}
835
836static int
837g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
838{
839	struct g_geom *gp;
840	struct g_part_entry *entry;
841	struct g_part_table *null, *table;
842	struct sbuf *sb;
843	int error;
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	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
851		if (entry->gpe_deleted || entry->gpe_internal)
852			continue;
853		gctl_error(req, "%d", EBUSY);
854		return (EBUSY);
855	}
856
857	error = G_PART_DESTROY(table, gpp);
858	if (error) {
859		gctl_error(req, "%d", error);
860		return (error);
861	}
862
863	gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
864	    M_WAITOK);
865	null = gp->softc;
866	null->gpt_gp = gp;
867	null->gpt_scheme = &g_part_null_scheme;
868	LIST_INIT(&null->gpt_entry);
869	null->gpt_depth = table->gpt_depth;
870	null->gpt_opened = table->gpt_opened;
871	null->gpt_smhead = table->gpt_smhead;
872	null->gpt_smtail = table->gpt_smtail;
873
874	while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
875		LIST_REMOVE(entry, gpe_entry);
876		g_free(entry);
877	}
878	kobj_delete((kobj_t)table, M_GEOM);
879
880	/* Provide feedback if so requested. */
881	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
882		sb = sbuf_new_auto();
883		sbuf_printf(sb, "%s destroyed\n", gp->name);
884		sbuf_finish(sb);
885		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
886		sbuf_delete(sb);
887	}
888	return (0);
889}
890
891static int
892g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
893{
894	struct g_geom *gp;
895	struct g_part_entry *entry;
896	struct g_part_table *table;
897	struct sbuf *sb;
898	int error;
899
900	gp = gpp->gpp_geom;
901	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
902	g_topology_assert();
903
904	table = gp->softc;
905
906	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
907		if (entry->gpe_deleted || entry->gpe_internal)
908			continue;
909		if (entry->gpe_index == gpp->gpp_index)
910			break;
911	}
912	if (entry == NULL) {
913		gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
914		return (ENOENT);
915	}
916
917	error = G_PART_MODIFY(table, entry, gpp);
918	if (error) {
919		gctl_error(req, "%d", error);
920		return (error);
921	}
922
923	if (!entry->gpe_created)
924		entry->gpe_modified = 1;
925
926	/* Provide feedback if so requested. */
927	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
928		sb = sbuf_new_auto();
929		G_PART_FULLNAME(table, entry, sb, gp->name);
930		sbuf_cat(sb, " modified\n");
931		sbuf_finish(sb);
932		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
933		sbuf_delete(sb);
934	}
935	return (0);
936}
937
938static int
939g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
940{
941	gctl_error(req, "%d verb 'move'", ENOSYS);
942	return (ENOSYS);
943}
944
945static int
946g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
947{
948	gctl_error(req, "%d verb 'recover'", ENOSYS);
949	return (ENOSYS);
950}
951
952static int
953g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
954{
955	gctl_error(req, "%d verb 'resize'", ENOSYS);
956	return (ENOSYS);
957}
958
959static int
960g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
961    unsigned int set)
962{
963	struct g_geom *gp;
964	struct g_part_entry *entry;
965	struct g_part_table *table;
966	struct sbuf *sb;
967	int error;
968
969	gp = gpp->gpp_geom;
970	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
971	g_topology_assert();
972
973	table = gp->softc;
974
975	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
976		if (entry->gpe_deleted || entry->gpe_internal)
977			continue;
978		if (entry->gpe_index == gpp->gpp_index)
979			break;
980	}
981	if (entry == NULL) {
982		gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
983		return (ENOENT);
984	}
985
986	error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
987	if (error) {
988		gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
989		return (error);
990	}
991
992	/* Provide feedback if so requested. */
993	if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
994		sb = sbuf_new_auto();
995		G_PART_FULLNAME(table, entry, sb, gp->name);
996		sbuf_printf(sb, " has %s %sset\n", gpp->gpp_attrib,
997		    (set) ? "" : "un");
998		sbuf_finish(sb);
999		gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1000		sbuf_delete(sb);
1001	}
1002	return (0);
1003}
1004
1005static int
1006g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1007{
1008	struct g_consumer *cp;
1009	struct g_provider *pp;
1010	struct g_geom *gp;
1011	struct g_part_entry *entry, *tmp;
1012	struct g_part_table *table;
1013	int error, reprobe;
1014
1015	gp = gpp->gpp_geom;
1016	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1017	g_topology_assert();
1018
1019	table = gp->softc;
1020	if (!table->gpt_opened) {
1021		gctl_error(req, "%d", EPERM);
1022		return (EPERM);
1023	}
1024
1025	cp = LIST_FIRST(&gp->consumer);
1026	LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1027		entry->gpe_modified = 0;
1028		if (entry->gpe_created) {
1029			pp = entry->gpe_pp;
1030			if (pp != NULL) {
1031				pp->private = NULL;
1032				entry->gpe_pp = NULL;
1033				g_wither_provider(pp, ENXIO);
1034			}
1035			entry->gpe_deleted = 1;
1036		}
1037		if (entry->gpe_deleted) {
1038			LIST_REMOVE(entry, gpe_entry);
1039			g_free(entry);
1040		}
1041	}
1042
1043	g_topology_unlock();
1044
1045	reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1046	    table->gpt_created) ? 1 : 0;
1047
1048	if (reprobe) {
1049		if (!LIST_EMPTY(&table->gpt_entry)) {
1050			error = EBUSY;
1051			goto fail;
1052		}
1053		error = g_part_probe(gp, cp, table->gpt_depth);
1054		if (error) {
1055			g_topology_lock();
1056			g_access(cp, -1, -1, -1);
1057			g_part_wither(gp, error);
1058			return (0);
1059		}
1060		table = gp->softc;
1061	}
1062
1063	error = G_PART_READ(table, cp);
1064	if (error)
1065		goto fail;
1066
1067	g_topology_lock();
1068
1069	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1070		if (!entry->gpe_internal)
1071			g_part_new_provider(gp, table, entry);
1072	}
1073
1074	table->gpt_opened = 0;
1075	g_access(cp, -1, -1, -1);
1076	return (0);
1077
1078fail:
1079	g_topology_lock();
1080	gctl_error(req, "%d", error);
1081	return (error);
1082}
1083
1084static void
1085g_part_wither(struct g_geom *gp, int error)
1086{
1087	struct g_part_entry *entry;
1088	struct g_part_table *table;
1089
1090	table = gp->softc;
1091	if (table != NULL) {
1092		while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1093			LIST_REMOVE(entry, gpe_entry);
1094			g_free(entry);
1095		}
1096		if (gp->softc != NULL) {
1097			kobj_delete((kobj_t)gp->softc, M_GEOM);
1098			gp->softc = NULL;
1099		}
1100	}
1101	g_wither_geom(gp, error);
1102}
1103
1104/*
1105 * Class methods.
1106 */
1107
1108static void
1109g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1110{
1111	struct g_part_parms gpp;
1112	struct g_part_table *table;
1113	struct gctl_req_arg *ap;
1114	const char *p;
1115	enum g_part_ctl ctlreq;
1116	unsigned int i, mparms, oparms, parm;
1117	int auto_commit, close_on_error;
1118	int error, len, modifies;
1119
1120	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1121	g_topology_assert();
1122
1123	ctlreq = G_PART_CTL_NONE;
1124	modifies = 1;
1125	mparms = 0;
1126	oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1127	switch (*verb) {
1128	case 'a':
1129		if (!strcmp(verb, "add")) {
1130			ctlreq = G_PART_CTL_ADD;
1131			mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1132			    G_PART_PARM_START | G_PART_PARM_TYPE;
1133			oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1134		}
1135		break;
1136	case 'b':
1137		if (!strcmp(verb, "bootcode")) {
1138			ctlreq = G_PART_CTL_BOOTCODE;
1139			mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1140		}
1141		break;
1142	case 'c':
1143		if (!strcmp(verb, "commit")) {
1144			ctlreq = G_PART_CTL_COMMIT;
1145			mparms |= G_PART_PARM_GEOM;
1146			modifies = 0;
1147		} else if (!strcmp(verb, "create")) {
1148			ctlreq = G_PART_CTL_CREATE;
1149			mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1150			oparms |= G_PART_PARM_ENTRIES;
1151		}
1152		break;
1153	case 'd':
1154		if (!strcmp(verb, "delete")) {
1155			ctlreq = G_PART_CTL_DELETE;
1156			mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1157		} else if (!strcmp(verb, "destroy")) {
1158			ctlreq = G_PART_CTL_DESTROY;
1159			mparms |= G_PART_PARM_GEOM;
1160		}
1161		break;
1162	case 'm':
1163		if (!strcmp(verb, "modify")) {
1164			ctlreq = G_PART_CTL_MODIFY;
1165			mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1166			oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1167		} else if (!strcmp(verb, "move")) {
1168			ctlreq = G_PART_CTL_MOVE;
1169			mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1170		}
1171		break;
1172	case 'r':
1173		if (!strcmp(verb, "recover")) {
1174			ctlreq = G_PART_CTL_RECOVER;
1175			mparms |= G_PART_PARM_GEOM;
1176		} else if (!strcmp(verb, "resize")) {
1177			ctlreq = G_PART_CTL_RESIZE;
1178			mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1179		}
1180		break;
1181	case 's':
1182		if (!strcmp(verb, "set")) {
1183			ctlreq = G_PART_CTL_SET;
1184			mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1185			    G_PART_PARM_INDEX;
1186		}
1187		break;
1188	case 'u':
1189		if (!strcmp(verb, "undo")) {
1190			ctlreq = G_PART_CTL_UNDO;
1191			mparms |= G_PART_PARM_GEOM;
1192			modifies = 0;
1193		} else if (!strcmp(verb, "unset")) {
1194			ctlreq = G_PART_CTL_UNSET;
1195			mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1196			    G_PART_PARM_INDEX;
1197		}
1198		break;
1199	}
1200	if (ctlreq == G_PART_CTL_NONE) {
1201		gctl_error(req, "%d verb '%s'", EINVAL, verb);
1202		return;
1203	}
1204
1205	bzero(&gpp, sizeof(gpp));
1206	for (i = 0; i < req->narg; i++) {
1207		ap = &req->arg[i];
1208		parm = 0;
1209		switch (ap->name[0]) {
1210		case 'a':
1211			if (!strcmp(ap->name, "attrib"))
1212				parm = G_PART_PARM_ATTRIB;
1213			break;
1214		case 'b':
1215			if (!strcmp(ap->name, "bootcode"))
1216				parm = G_PART_PARM_BOOTCODE;
1217			break;
1218		case 'c':
1219			if (!strcmp(ap->name, "class"))
1220				continue;
1221			break;
1222		case 'e':
1223			if (!strcmp(ap->name, "entries"))
1224				parm = G_PART_PARM_ENTRIES;
1225			break;
1226		case 'f':
1227			if (!strcmp(ap->name, "flags"))
1228				parm = G_PART_PARM_FLAGS;
1229			break;
1230		case 'g':
1231			if (!strcmp(ap->name, "geom"))
1232				parm = G_PART_PARM_GEOM;
1233			break;
1234		case 'i':
1235			if (!strcmp(ap->name, "index"))
1236				parm = G_PART_PARM_INDEX;
1237			break;
1238		case 'l':
1239			if (!strcmp(ap->name, "label"))
1240				parm = G_PART_PARM_LABEL;
1241			break;
1242		case 'o':
1243			if (!strcmp(ap->name, "output"))
1244				parm = G_PART_PARM_OUTPUT;
1245			break;
1246		case 'p':
1247			if (!strcmp(ap->name, "provider"))
1248				parm = G_PART_PARM_PROVIDER;
1249			break;
1250		case 's':
1251			if (!strcmp(ap->name, "scheme"))
1252				parm = G_PART_PARM_SCHEME;
1253			else if (!strcmp(ap->name, "size"))
1254				parm = G_PART_PARM_SIZE;
1255			else if (!strcmp(ap->name, "start"))
1256				parm = G_PART_PARM_START;
1257			break;
1258		case 't':
1259			if (!strcmp(ap->name, "type"))
1260				parm = G_PART_PARM_TYPE;
1261			break;
1262		case 'v':
1263			if (!strcmp(ap->name, "verb"))
1264				continue;
1265			else if (!strcmp(ap->name, "version"))
1266				parm = G_PART_PARM_VERSION;
1267			break;
1268		}
1269		if ((parm & (mparms | oparms)) == 0) {
1270			gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1271			return;
1272		}
1273		if (parm == G_PART_PARM_BOOTCODE)
1274			p = gctl_get_param(req, ap->name, &len);
1275		else
1276			p = gctl_get_asciiparam(req, ap->name);
1277		if (p == NULL) {
1278			gctl_error(req, "%d param '%s'", ENOATTR, ap->name);
1279			return;
1280		}
1281		switch (parm) {
1282		case G_PART_PARM_ATTRIB:
1283			error = g_part_parm_str(p, &gpp.gpp_attrib);
1284			break;
1285		case G_PART_PARM_BOOTCODE:
1286			gpp.gpp_codeptr = p;
1287			gpp.gpp_codesize = len;
1288			error = 0;
1289			break;
1290		case G_PART_PARM_ENTRIES:
1291			error = g_part_parm_uint(p, &gpp.gpp_entries);
1292			break;
1293		case G_PART_PARM_FLAGS:
1294			if (p[0] == '\0')
1295				continue;
1296			error = g_part_parm_str(p, &gpp.gpp_flags);
1297			break;
1298		case G_PART_PARM_GEOM:
1299			error = g_part_parm_geom(p, &gpp.gpp_geom);
1300			break;
1301		case G_PART_PARM_INDEX:
1302			error = g_part_parm_uint(p, &gpp.gpp_index);
1303			break;
1304		case G_PART_PARM_LABEL:
1305			/* An empty label is always valid. */
1306			gpp.gpp_label = p;
1307			error = 0;
1308			break;
1309		case G_PART_PARM_OUTPUT:
1310			error = 0;	/* Write-only parameter */
1311			break;
1312		case G_PART_PARM_PROVIDER:
1313			error = g_part_parm_provider(p, &gpp.gpp_provider);
1314			break;
1315		case G_PART_PARM_SCHEME:
1316			error = g_part_parm_scheme(p, &gpp.gpp_scheme);
1317			break;
1318		case G_PART_PARM_SIZE:
1319			error = g_part_parm_quad(p, &gpp.gpp_size);
1320			break;
1321		case G_PART_PARM_START:
1322			error = g_part_parm_quad(p, &gpp.gpp_start);
1323			break;
1324		case G_PART_PARM_TYPE:
1325			error = g_part_parm_str(p, &gpp.gpp_type);
1326			break;
1327		case G_PART_PARM_VERSION:
1328			error = g_part_parm_uint(p, &gpp.gpp_version);
1329			break;
1330		default:
1331			error = EDOOFUS;
1332			break;
1333		}
1334		if (error) {
1335			gctl_error(req, "%d %s '%s'", error, ap->name, p);
1336			return;
1337		}
1338		gpp.gpp_parms |= parm;
1339	}
1340	if ((gpp.gpp_parms & mparms) != mparms) {
1341		parm = mparms - (gpp.gpp_parms & mparms);
1342		gctl_error(req, "%d param '%x'", ENOATTR, parm);
1343		return;
1344	}
1345
1346	/* Obtain permissions if possible/necessary. */
1347	close_on_error = 0;
1348	table = NULL;
1349	if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1350		table = gpp.gpp_geom->softc;
1351		if (table != NULL && !table->gpt_opened) {
1352			error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1353			    1, 1, 1);
1354			if (error) {
1355				gctl_error(req, "%d geom '%s'", error,
1356				    gpp.gpp_geom->name);
1357				return;
1358			}
1359			table->gpt_opened = 1;
1360			close_on_error = 1;
1361		}
1362	}
1363
1364	/* Allow the scheme to check or modify the parameters. */
1365	if (table != NULL) {
1366		error = G_PART_PRECHECK(table, ctlreq, &gpp);
1367		if (error) {
1368			gctl_error(req, "%d pre-check failed", error);
1369			goto out;
1370		}
1371	} else
1372		error = EDOOFUS;	/* Prevent bogus uninit. warning. */
1373
1374	switch (ctlreq) {
1375	case G_PART_CTL_NONE:
1376		panic("%s", __func__);
1377	case G_PART_CTL_ADD:
1378		error = g_part_ctl_add(req, &gpp);
1379		break;
1380	case G_PART_CTL_BOOTCODE:
1381		error = g_part_ctl_bootcode(req, &gpp);
1382		break;
1383	case G_PART_CTL_COMMIT:
1384		error = g_part_ctl_commit(req, &gpp);
1385		break;
1386	case G_PART_CTL_CREATE:
1387		error = g_part_ctl_create(req, &gpp);
1388		break;
1389	case G_PART_CTL_DELETE:
1390		error = g_part_ctl_delete(req, &gpp);
1391		break;
1392	case G_PART_CTL_DESTROY:
1393		error = g_part_ctl_destroy(req, &gpp);
1394		break;
1395	case G_PART_CTL_MODIFY:
1396		error = g_part_ctl_modify(req, &gpp);
1397		break;
1398	case G_PART_CTL_MOVE:
1399		error = g_part_ctl_move(req, &gpp);
1400		break;
1401	case G_PART_CTL_RECOVER:
1402		error = g_part_ctl_recover(req, &gpp);
1403		break;
1404	case G_PART_CTL_RESIZE:
1405		error = g_part_ctl_resize(req, &gpp);
1406		break;
1407	case G_PART_CTL_SET:
1408		error = g_part_ctl_setunset(req, &gpp, 1);
1409		break;
1410	case G_PART_CTL_UNDO:
1411		error = g_part_ctl_undo(req, &gpp);
1412		break;
1413	case G_PART_CTL_UNSET:
1414		error = g_part_ctl_setunset(req, &gpp, 0);
1415		break;
1416	}
1417
1418	/* Implement automatic commit. */
1419	if (!error) {
1420		auto_commit = (modifies &&
1421		    (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1422		    strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1423		if (auto_commit) {
1424			KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, (__func__));
1425			error = g_part_ctl_commit(req, &gpp);
1426		}
1427	}
1428
1429 out:
1430	if (error && close_on_error) {
1431		g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1432		table->gpt_opened = 0;
1433	}
1434}
1435
1436static int
1437g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1438    struct g_geom *gp)
1439{
1440
1441	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1442	g_topology_assert();
1443
1444	g_part_wither(gp, EINVAL);
1445	return (0);
1446}
1447
1448static struct g_geom *
1449g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1450{
1451	struct g_consumer *cp;
1452	struct g_geom *gp;
1453	struct g_part_entry *entry;
1454	struct g_part_table *table;
1455	struct root_hold_token *rht;
1456	int attr, depth;
1457	int error;
1458
1459	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1460	g_topology_assert();
1461
1462	/*
1463	 * Create a GEOM with consumer and hook it up to the provider.
1464	 * With that we become part of the topology. Optain read access
1465	 * to the provider.
1466	 */
1467	gp = g_new_geomf(mp, "%s", pp->name);
1468	cp = g_new_consumer(gp);
1469	error = g_attach(cp, pp);
1470	if (error == 0)
1471		error = g_access(cp, 1, 0, 0);
1472	if (error != 0) {
1473		g_part_wither(gp, error);
1474		return (NULL);
1475	}
1476
1477	rht = root_mount_hold(mp->name);
1478	g_topology_unlock();
1479
1480	/*
1481	 * Short-circuit the whole probing galore when there's no
1482	 * media present.
1483	 */
1484	if (pp->mediasize == 0 || pp->sectorsize == 0) {
1485		error = ENODEV;
1486		goto fail;
1487	}
1488
1489	/* Make sure we can nest and if so, determine our depth. */
1490	error = g_getattr("PART::isleaf", cp, &attr);
1491	if (!error && attr) {
1492		error = ENODEV;
1493		goto fail;
1494	}
1495	error = g_getattr("PART::depth", cp, &attr);
1496	depth = (!error) ? attr + 1 : 0;
1497
1498	error = g_part_probe(gp, cp, depth);
1499	if (error)
1500		goto fail;
1501
1502	table = gp->softc;
1503
1504	/*
1505	 * Synthesize a disk geometry. Some partitioning schemes
1506	 * depend on it and since some file systems need it even
1507	 * when the partitition scheme doesn't, we do it here in
1508	 * scheme-independent code.
1509	 */
1510	g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1511
1512	error = G_PART_READ(table, cp);
1513	if (error)
1514		goto fail;
1515
1516	g_topology_lock();
1517	LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1518		if (!entry->gpe_internal)
1519			g_part_new_provider(gp, table, entry);
1520	}
1521
1522	root_mount_rel(rht);
1523	g_access(cp, -1, 0, 0);
1524	return (gp);
1525
1526 fail:
1527	g_topology_lock();
1528	root_mount_rel(rht);
1529	g_access(cp, -1, 0, 0);
1530	g_part_wither(gp, error);
1531	return (NULL);
1532}
1533
1534/*
1535 * Geom methods.
1536 */
1537
1538static int
1539g_part_access(struct g_provider *pp, int dr, int dw, int de)
1540{
1541	struct g_consumer *cp;
1542
1543	G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1544	    dw, de));
1545
1546	cp = LIST_FIRST(&pp->geom->consumer);
1547
1548	/* We always gain write-exclusive access. */
1549	return (g_access(cp, dr, dw, dw + de));
1550}
1551
1552static void
1553g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1554    struct g_consumer *cp, struct g_provider *pp)
1555{
1556	char buf[64];
1557	struct g_part_entry *entry;
1558	struct g_part_table *table;
1559
1560	KASSERT(sb != NULL && gp != NULL, (__func__));
1561	table = gp->softc;
1562
1563	if (indent == NULL) {
1564		KASSERT(cp == NULL && pp != NULL, (__func__));
1565		entry = pp->private;
1566		if (entry == NULL)
1567			return;
1568		sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
1569		    (uintmax_t)entry->gpe_offset,
1570		    G_PART_TYPE(table, entry, buf, sizeof(buf)));
1571		/*
1572		 * libdisk compatibility quirk - the scheme dumps the
1573		 * slicer name and partition type in a way that is
1574		 * compatible with libdisk. When libdisk is not used
1575		 * anymore, this should go away.
1576		 */
1577		G_PART_DUMPCONF(table, entry, sb, indent);
1578	} else if (cp != NULL) {	/* Consumer configuration. */
1579		KASSERT(pp == NULL, (__func__));
1580		/* none */
1581	} else if (pp != NULL) {	/* Provider configuration. */
1582		entry = pp->private;
1583		if (entry == NULL)
1584			return;
1585		sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
1586		    (uintmax_t)entry->gpe_start);
1587		sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
1588		    (uintmax_t)entry->gpe_end);
1589		sbuf_printf(sb, "%s<index>%u</index>\n", indent,
1590		    entry->gpe_index);
1591		sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1592		    G_PART_TYPE(table, entry, buf, sizeof(buf)));
1593		sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
1594		    (uintmax_t)entry->gpe_offset);
1595		sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
1596		    (uintmax_t)pp->mediasize);
1597		G_PART_DUMPCONF(table, entry, sb, indent);
1598	} else {			/* Geom configuration. */
1599		sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
1600		    table->gpt_scheme->name);
1601		sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
1602		    table->gpt_entries);
1603		sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
1604		    (uintmax_t)table->gpt_first);
1605		sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
1606		    (uintmax_t)table->gpt_last);
1607		sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
1608		    table->gpt_sectors);
1609		sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
1610		    table->gpt_heads);
1611		G_PART_DUMPCONF(table, NULL, sb, indent);
1612	}
1613}
1614
1615static void
1616g_part_orphan(struct g_consumer *cp)
1617{
1618	struct g_provider *pp;
1619
1620	pp = cp->provider;
1621	KASSERT(pp != NULL, (__func__));
1622	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
1623	g_topology_assert();
1624
1625	KASSERT(pp->error != 0, (__func__));
1626	g_part_wither(cp->geom, pp->error);
1627}
1628
1629static void
1630g_part_spoiled(struct g_consumer *cp)
1631{
1632
1633	G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
1634	g_topology_assert();
1635
1636	g_part_wither(cp->geom, ENXIO);
1637}
1638
1639static void
1640g_part_start(struct bio *bp)
1641{
1642	struct bio *bp2;
1643	struct g_consumer *cp;
1644	struct g_geom *gp;
1645	struct g_part_entry *entry;
1646	struct g_part_table *table;
1647	struct g_kerneldump *gkd;
1648	struct g_provider *pp;
1649
1650	pp = bp->bio_to;
1651	gp = pp->geom;
1652	table = gp->softc;
1653	cp = LIST_FIRST(&gp->consumer);
1654
1655	G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
1656	    pp->name));
1657
1658	entry = pp->private;
1659	if (entry == NULL) {
1660		g_io_deliver(bp, ENXIO);
1661		return;
1662	}
1663
1664	switch(bp->bio_cmd) {
1665	case BIO_DELETE:
1666	case BIO_READ:
1667	case BIO_WRITE:
1668		if (bp->bio_offset >= pp->mediasize) {
1669			g_io_deliver(bp, EIO);
1670			return;
1671		}
1672		bp2 = g_clone_bio(bp);
1673		if (bp2 == NULL) {
1674			g_io_deliver(bp, ENOMEM);
1675			return;
1676		}
1677		if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
1678			bp2->bio_length = pp->mediasize - bp2->bio_offset;
1679		bp2->bio_done = g_std_done;
1680		bp2->bio_offset += entry->gpe_offset;
1681		g_io_request(bp2, cp);
1682		return;
1683	case BIO_FLUSH:
1684		break;
1685	case BIO_GETATTR:
1686		if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
1687			return;
1688		if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
1689			return;
1690		if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
1691			return;
1692		if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
1693			return;
1694		if (g_handleattr_str(bp, "PART::scheme",
1695		    table->gpt_scheme->name))
1696			return;
1697		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
1698			/*
1699			 * Check that the partition is suitable for kernel
1700			 * dumps. Typically only swap partitions should be
1701			 * used.
1702			 */
1703			if (!G_PART_DUMPTO(table, entry)) {
1704				g_io_deliver(bp, ENODEV);
1705				printf("GEOM_PART: Partition '%s' not suitable"
1706				    " for kernel dumps (wrong type?)\n",
1707				    pp->name);
1708				return;
1709			}
1710			gkd = (struct g_kerneldump *)bp->bio_data;
1711			if (gkd->offset >= pp->mediasize) {
1712				g_io_deliver(bp, EIO);
1713				return;
1714			}
1715			if (gkd->offset + gkd->length > pp->mediasize)
1716				gkd->length = pp->mediasize - gkd->offset;
1717			gkd->offset += entry->gpe_offset;
1718		}
1719		break;
1720	default:
1721		g_io_deliver(bp, EOPNOTSUPP);
1722		return;
1723	}
1724
1725	bp2 = g_clone_bio(bp);
1726	if (bp2 == NULL) {
1727		g_io_deliver(bp, ENOMEM);
1728		return;
1729	}
1730	bp2->bio_done = g_std_done;
1731	g_io_request(bp2, cp);
1732}
1733
1734static void
1735g_part_init(struct g_class *mp)
1736{
1737
1738	TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
1739}
1740
1741static void
1742g_part_fini(struct g_class *mp)
1743{
1744
1745	TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
1746}
1747
1748static void
1749g_part_unload_event(void *arg, int flag)
1750{
1751	struct g_consumer *cp;
1752	struct g_geom *gp;
1753	struct g_provider *pp;
1754	struct g_part_scheme *scheme;
1755	struct g_part_table *table;
1756	uintptr_t *xchg;
1757	int acc, error;
1758
1759	if (flag == EV_CANCEL)
1760		return;
1761
1762	xchg = arg;
1763	error = 0;
1764	scheme = (void *)(*xchg);
1765
1766	g_topology_assert();
1767
1768	LIST_FOREACH(gp, &g_part_class.geom, geom) {
1769		table = gp->softc;
1770		if (table->gpt_scheme != scheme)
1771			continue;
1772
1773		acc = 0;
1774		LIST_FOREACH(pp, &gp->provider, provider)
1775			acc += pp->acr + pp->acw + pp->ace;
1776		LIST_FOREACH(cp, &gp->consumer, consumer)
1777			acc += cp->acr + cp->acw + cp->ace;
1778
1779		if (!acc)
1780			g_part_wither(gp, ENOSYS);
1781		else
1782			error = EBUSY;
1783	}
1784
1785	if (!error)
1786		TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1787
1788	*xchg = error;
1789}
1790
1791int
1792g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
1793{
1794	uintptr_t arg;
1795	int error;
1796
1797	switch (type) {
1798	case MOD_LOAD:
1799		TAILQ_INSERT_TAIL(&g_part_schemes, scheme, scheme_list);
1800
1801		error = g_retaste(&g_part_class);
1802		if (error)
1803			TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1804		break;
1805	case MOD_UNLOAD:
1806		arg = (uintptr_t)scheme;
1807		error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,
1808		    NULL);
1809		if (!error)
1810			error = (arg == (uintptr_t)scheme) ? EDOOFUS : arg;
1811		break;
1812	default:
1813		error = EOPNOTSUPP;
1814		break;
1815	}
1816
1817	return (error);
1818}
1819