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