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