2 * Copyright (c) 2002, 2005-2009 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
35 #include <sys/limits.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/queue.h>
41 #include <sys/sysctl.h>
42 #include <sys/systm.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>
49 #include "g_part_if.h"
52 #define _PATH_DEV "/dev/"
55 static kobj_method_t g_part_null_methods[] = {
59 static struct g_part_scheme g_part_null_scheme = {
62 sizeof(struct g_part_table),
65 TAILQ_HEAD(, g_part_scheme) g_part_schemes =
66 TAILQ_HEAD_INITIALIZER(g_part_schemes);
68 struct g_part_alias_list {
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 { "bios-boot", G_PART_ALIAS_BIOS_BOOT },
80 { "ebr", G_PART_ALIAS_EBR },
81 { "efi", G_PART_ALIAS_EFI },
82 { "fat16", G_PART_ALIAS_MS_FAT16 },
83 { "fat32", G_PART_ALIAS_MS_FAT32 },
84 { "freebsd", G_PART_ALIAS_FREEBSD },
85 { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
86 { "freebsd-nandfs", G_PART_ALIAS_FREEBSD_NANDFS },
87 { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
88 { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
89 { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
90 { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
91 { "linux-data", G_PART_ALIAS_LINUX_DATA },
92 { "linux-lvm", G_PART_ALIAS_LINUX_LVM },
93 { "linux-raid", G_PART_ALIAS_LINUX_RAID },
94 { "linux-swap", G_PART_ALIAS_LINUX_SWAP },
95 { "mbr", G_PART_ALIAS_MBR },
96 { "ms-basic-data", G_PART_ALIAS_MS_BASIC_DATA },
97 { "ms-ldm-data", G_PART_ALIAS_MS_LDM_DATA },
98 { "ms-ldm-metadata", G_PART_ALIAS_MS_LDM_METADATA },
99 { "ms-reserved", G_PART_ALIAS_MS_RESERVED },
100 { "ntfs", G_PART_ALIAS_MS_NTFS },
101 { "netbsd-ccd", G_PART_ALIAS_NETBSD_CCD },
102 { "netbsd-cgd", G_PART_ALIAS_NETBSD_CGD },
103 { "netbsd-ffs", G_PART_ALIAS_NETBSD_FFS },
104 { "netbsd-lfs", G_PART_ALIAS_NETBSD_LFS },
105 { "netbsd-raid", G_PART_ALIAS_NETBSD_RAID },
106 { "netbsd-swap", G_PART_ALIAS_NETBSD_SWAP },
107 { "vmware-vmfs", G_PART_ALIAS_VMFS },
108 { "vmware-vmkdiag", G_PART_ALIAS_VMKDIAG },
109 { "vmware-reserved", G_PART_ALIAS_VMRESERVED },
110 { "vmware-vsanhdr", G_PART_ALIAS_VMVSANHDR },
113 SYSCTL_DECL(_kern_geom);
114 SYSCTL_NODE(_kern_geom, OID_AUTO, part, CTLFLAG_RW, 0,
116 static u_int check_integrity = 1;
117 TUNABLE_INT("kern.geom.part.check_integrity", &check_integrity);
118 SYSCTL_UINT(_kern_geom_part, OID_AUTO, check_integrity,
119 CTLFLAG_RW | CTLFLAG_TUN, &check_integrity, 1,
120 "Enable integrity checking");
123 * The GEOM partitioning class.
125 static g_ctl_req_t g_part_ctlreq;
126 static g_ctl_destroy_geom_t g_part_destroy_geom;
127 static g_fini_t g_part_fini;
128 static g_init_t g_part_init;
129 static g_taste_t g_part_taste;
131 static g_access_t g_part_access;
132 static g_dumpconf_t g_part_dumpconf;
133 static g_orphan_t g_part_orphan;
134 static g_spoiled_t g_part_spoiled;
135 static g_start_t g_part_start;
136 static g_resize_t g_part_resize;
138 static struct g_class g_part_class = {
140 .version = G_VERSION,
142 .ctlreq = g_part_ctlreq,
143 .destroy_geom = g_part_destroy_geom,
146 .taste = g_part_taste,
148 .access = g_part_access,
149 .dumpconf = g_part_dumpconf,
150 .orphan = g_part_orphan,
151 .spoiled = g_part_spoiled,
152 .start = g_part_start,
153 .resize = g_part_resize
156 DECLARE_GEOM_CLASS(g_part_class, g_part);
157 MODULE_VERSION(g_part, 0);
163 static void g_part_wither(struct g_geom *, int);
166 g_part_alias_name(enum g_part_alias alias)
170 for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
171 if (g_part_alias_list[i].alias != alias)
173 return (g_part_alias_list[i].lexeme);
180 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
183 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
184 off_t chs, cylinders;
190 for (idx = 0; candidate_heads[idx] != 0; idx++) {
191 heads = candidate_heads[idx];
192 cylinders = blocks / heads / sectors;
193 if (cylinders < heads || cylinders < sectors)
195 if (cylinders > 1023)
197 chs = cylinders * heads * sectors;
198 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
206 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
209 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
211 u_int heads, sectors;
214 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 || sectors == 0 ||
215 g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
216 table->gpt_fixgeom = 0;
217 table->gpt_heads = 0;
218 table->gpt_sectors = 0;
220 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
221 sectors = candidate_sectors[idx];
222 g_part_geometry_heads(blocks, sectors, &chs, &heads);
226 * Prefer a geometry with sectors > 1, but only if
227 * it doesn't bump down the number of heads to 1.
229 if (chs > bestchs || (chs == bestchs && heads > 1 &&
230 table->gpt_sectors == 1)) {
232 table->gpt_heads = heads;
233 table->gpt_sectors = sectors;
237 * If we didn't find a geometry at all, then the disk is
238 * too big. This means we can use the maximum number of
242 table->gpt_heads = 255;
243 table->gpt_sectors = 63;
246 table->gpt_fixgeom = 1;
247 table->gpt_heads = heads;
248 table->gpt_sectors = sectors;
252 #define DPRINTF(...) if (bootverbose) { \
253 printf("GEOM_PART: " __VA_ARGS__); \
257 g_part_check_integrity(struct g_part_table *table, struct g_consumer *cp)
259 struct g_part_entry *e1, *e2;
260 struct g_provider *pp;
266 if (table->gpt_last < table->gpt_first) {
267 DPRINTF("last LBA is below first LBA: %jd < %jd\n",
268 (intmax_t)table->gpt_last, (intmax_t)table->gpt_first);
271 if (table->gpt_last > pp->mediasize / pp->sectorsize - 1) {
272 DPRINTF("last LBA extends beyond mediasize: "
273 "%jd > %jd\n", (intmax_t)table->gpt_last,
274 (intmax_t)pp->mediasize / pp->sectorsize - 1);
277 LIST_FOREACH(e1, &table->gpt_entry, gpe_entry) {
278 if (e1->gpe_deleted || e1->gpe_internal)
280 if (e1->gpe_start < table->gpt_first) {
281 DPRINTF("partition %d has start offset below first "
282 "LBA: %jd < %jd\n", e1->gpe_index,
283 (intmax_t)e1->gpe_start,
284 (intmax_t)table->gpt_first);
287 if (e1->gpe_start > table->gpt_last) {
288 DPRINTF("partition %d has start offset beyond last "
289 "LBA: %jd > %jd\n", e1->gpe_index,
290 (intmax_t)e1->gpe_start,
291 (intmax_t)table->gpt_last);
294 if (e1->gpe_end < e1->gpe_start) {
295 DPRINTF("partition %d has end offset below start "
296 "offset: %jd < %jd\n", e1->gpe_index,
297 (intmax_t)e1->gpe_end,
298 (intmax_t)e1->gpe_start);
301 if (e1->gpe_end > table->gpt_last) {
302 DPRINTF("partition %d has end offset beyond last "
303 "LBA: %jd > %jd\n", e1->gpe_index,
304 (intmax_t)e1->gpe_end,
305 (intmax_t)table->gpt_last);
308 if (pp->stripesize > 0) {
309 offset = e1->gpe_start * pp->sectorsize;
310 if (e1->gpe_offset > offset)
311 offset = e1->gpe_offset;
312 if ((offset + pp->stripeoffset) % pp->stripesize) {
313 DPRINTF("partition %d is not aligned on %u "
314 "bytes\n", e1->gpe_index, pp->stripesize);
315 /* Don't treat this as a critical failure */
319 while ((e2 = LIST_NEXT(e2, gpe_entry)) != NULL) {
320 if (e2->gpe_deleted || e2->gpe_internal)
322 if (e1->gpe_start >= e2->gpe_start &&
323 e1->gpe_start <= e2->gpe_end) {
324 DPRINTF("partition %d has start offset inside "
325 "partition %d: start[%d] %jd >= start[%d] "
326 "%jd <= end[%d] %jd\n",
327 e1->gpe_index, e2->gpe_index,
328 e2->gpe_index, (intmax_t)e2->gpe_start,
329 e1->gpe_index, (intmax_t)e1->gpe_start,
330 e2->gpe_index, (intmax_t)e2->gpe_end);
333 if (e1->gpe_end >= e2->gpe_start &&
334 e1->gpe_end <= e2->gpe_end) {
335 DPRINTF("partition %d has end offset inside "
336 "partition %d: start[%d] %jd >= end[%d] "
337 "%jd <= end[%d] %jd\n",
338 e1->gpe_index, e2->gpe_index,
339 e2->gpe_index, (intmax_t)e2->gpe_start,
340 e1->gpe_index, (intmax_t)e1->gpe_end,
341 e2->gpe_index, (intmax_t)e2->gpe_end);
344 if (e1->gpe_start < e2->gpe_start &&
345 e1->gpe_end > e2->gpe_end) {
346 DPRINTF("partition %d contains partition %d: "
347 "start[%d] %jd > start[%d] %jd, end[%d] "
348 "%jd < end[%d] %jd\n",
349 e1->gpe_index, e2->gpe_index,
350 e1->gpe_index, (intmax_t)e1->gpe_start,
351 e2->gpe_index, (intmax_t)e2->gpe_start,
352 e2->gpe_index, (intmax_t)e2->gpe_end,
353 e1->gpe_index, (intmax_t)e1->gpe_end);
359 printf("GEOM_PART: integrity check failed (%s, %s)\n",
360 pp->name, table->gpt_scheme->name);
361 if (check_integrity != 0)
363 table->gpt_corrupt = 1;
369 struct g_part_entry *
370 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
373 struct g_part_entry *entry, *last;
376 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
377 if (entry->gpe_index == index)
379 if (entry->gpe_index > index) {
386 entry = g_malloc(table->gpt_scheme->gps_entrysz,
388 entry->gpe_index = index;
390 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
392 LIST_INSERT_AFTER(last, entry, gpe_entry);
394 entry->gpe_offset = 0;
395 entry->gpe_start = start;
396 entry->gpe_end = end;
401 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
402 struct g_part_entry *entry)
404 struct g_consumer *cp;
405 struct g_provider *pp;
409 cp = LIST_FIRST(&gp->consumer);
412 offset = entry->gpe_start * pp->sectorsize;
413 if (entry->gpe_offset < offset)
414 entry->gpe_offset = offset;
416 if (entry->gpe_pp == NULL) {
417 sb = sbuf_new_auto();
418 G_PART_FULLNAME(table, entry, sb, gp->name);
420 entry->gpe_pp = g_new_providerf(gp, "%s", sbuf_data(sb));
422 entry->gpe_pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
423 entry->gpe_pp->private = entry; /* Close the circle. */
425 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */
426 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
428 entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
429 entry->gpe_pp->sectorsize = pp->sectorsize;
430 entry->gpe_pp->stripesize = pp->stripesize;
431 entry->gpe_pp->stripeoffset = pp->stripeoffset + entry->gpe_offset;
432 if (pp->stripesize > 0)
433 entry->gpe_pp->stripeoffset %= pp->stripesize;
434 entry->gpe_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
435 g_error_provider(entry->gpe_pp, 0);
438 static struct g_geom*
439 g_part_find_geom(const char *name)
442 LIST_FOREACH(gp, &g_part_class.geom, geom) {
443 if (!strcmp(name, gp->name))
450 g_part_parm_geom(struct gctl_req *req, const char *name, struct g_geom **v)
455 gname = gctl_get_asciiparam(req, name);
458 if (strncmp(gname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
459 gname += sizeof(_PATH_DEV) - 1;
460 gp = g_part_find_geom(gname);
462 gctl_error(req, "%d %s '%s'", EINVAL, name, gname);
465 if ((gp->flags & G_GEOM_WITHER) != 0) {
466 gctl_error(req, "%d %s", ENXIO, gname);
474 g_part_parm_provider(struct gctl_req *req, const char *name,
475 struct g_provider **v)
477 struct g_provider *pp;
480 pname = gctl_get_asciiparam(req, name);
483 if (strncmp(pname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
484 pname += sizeof(_PATH_DEV) - 1;
485 pp = g_provider_by_name(pname);
487 gctl_error(req, "%d %s '%s'", EINVAL, name, pname);
495 g_part_parm_quad(struct gctl_req *req, const char *name, quad_t *v)
501 p = gctl_get_asciiparam(req, name);
504 q = strtoq(p, &x, 0);
505 if (*x != '\0' || q < 0) {
506 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
514 g_part_parm_scheme(struct gctl_req *req, const char *name,
515 struct g_part_scheme **v)
517 struct g_part_scheme *s;
520 p = gctl_get_asciiparam(req, name);
523 TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
524 if (s == &g_part_null_scheme)
526 if (!strcasecmp(s->name, p))
530 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
538 g_part_parm_str(struct gctl_req *req, const char *name, const char **v)
542 p = gctl_get_asciiparam(req, name);
545 /* An empty label is always valid. */
546 if (strcmp(name, "label") != 0 && p[0] == '\0') {
547 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
555 g_part_parm_intmax(struct gctl_req *req, const char *name, u_int *v)
560 p = gctl_get_param(req, name, &size);
563 if (size != sizeof(*p) || *p < 0 || *p > INT_MAX) {
564 gctl_error(req, "%d %s '%jd'", EINVAL, name, *p);
572 g_part_parm_uint32(struct gctl_req *req, const char *name, u_int *v)
577 p = gctl_get_param(req, name, &size);
580 if (size != sizeof(*p) || *p > INT_MAX) {
581 gctl_error(req, "%d %s '%u'", EINVAL, name, (unsigned int)*p);
589 g_part_parm_bootcode(struct gctl_req *req, const char *name, const void **v,
595 p = gctl_get_param(req, name, &size);
604 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
606 struct g_part_scheme *iter, *scheme;
607 struct g_part_table *table;
611 scheme = (table != NULL) ? table->gpt_scheme : NULL;
612 pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
615 if (pri > 0) { /* error */
620 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
621 if (iter == &g_part_null_scheme)
623 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
626 table->gpt_scheme = iter;
627 table->gpt_depth = depth;
628 probe = G_PART_PROBE(table, cp);
629 if (probe <= 0 && probe > pri) {
632 if (gp->softc != NULL)
633 kobj_delete((kobj_t)gp->softc, M_GEOM);
638 kobj_delete((kobj_t)table, M_GEOM);
642 return ((scheme == NULL) ? ENXIO : 0);
646 * Control request functions.
650 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
653 struct g_provider *pp;
654 struct g_part_entry *delent, *last, *entry;
655 struct g_part_table *table;
662 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
665 pp = LIST_FIRST(&gp->consumer)->provider;
667 end = gpp->gpp_start + gpp->gpp_size - 1;
669 if (gpp->gpp_start < table->gpt_first ||
670 gpp->gpp_start > table->gpt_last) {
671 gctl_error(req, "%d start '%jd'", EINVAL,
672 (intmax_t)gpp->gpp_start);
675 if (end < gpp->gpp_start || end > table->gpt_last) {
676 gctl_error(req, "%d size '%jd'", EINVAL,
677 (intmax_t)gpp->gpp_size);
680 if (gpp->gpp_index > table->gpt_entries) {
681 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
685 delent = last = NULL;
686 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
687 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
688 if (entry->gpe_deleted) {
689 if (entry->gpe_index == index)
693 if (entry->gpe_index == index)
694 index = entry->gpe_index + 1;
695 if (entry->gpe_index < index)
697 if (entry->gpe_internal)
699 if (gpp->gpp_start >= entry->gpe_start &&
700 gpp->gpp_start <= entry->gpe_end) {
701 gctl_error(req, "%d start '%jd'", ENOSPC,
702 (intmax_t)gpp->gpp_start);
705 if (end >= entry->gpe_start && end <= entry->gpe_end) {
706 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
709 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
710 gctl_error(req, "%d size '%jd'", ENOSPC,
711 (intmax_t)gpp->gpp_size);
715 if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
716 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
719 if (index > table->gpt_entries) {
720 gctl_error(req, "%d index '%d'", ENOSPC, index);
724 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
725 M_WAITOK | M_ZERO) : delent;
726 entry->gpe_index = index;
727 entry->gpe_start = gpp->gpp_start;
728 entry->gpe_end = end;
729 error = G_PART_ADD(table, entry, gpp);
731 gctl_error(req, "%d", error);
736 if (delent == NULL) {
738 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
740 LIST_INSERT_AFTER(last, entry, gpe_entry);
741 entry->gpe_created = 1;
743 entry->gpe_deleted = 0;
744 entry->gpe_modified = 1;
746 g_part_new_provider(gp, table, entry);
748 /* Provide feedback if so requested. */
749 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
750 sb = sbuf_new_auto();
751 G_PART_FULLNAME(table, entry, sb, gp->name);
752 if (pp->stripesize > 0 && entry->gpe_pp->stripeoffset != 0)
753 sbuf_printf(sb, " added, but partition is not "
754 "aligned on %u bytes\n", pp->stripesize);
756 sbuf_cat(sb, " added\n");
758 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
765 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
768 struct g_part_table *table;
773 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
777 sz = table->gpt_scheme->gps_bootcodesz;
782 if (gpp->gpp_codesize > sz) {
787 error = G_PART_BOOTCODE(table, gpp);
791 /* Provide feedback if so requested. */
792 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
793 sb = sbuf_new_auto();
794 sbuf_printf(sb, "bootcode written to %s\n", gp->name);
796 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
802 gctl_error(req, "%d", error);
807 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
809 struct g_consumer *cp;
811 struct g_provider *pp;
812 struct g_part_entry *entry, *tmp;
813 struct g_part_table *table;
818 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
822 if (!table->gpt_opened) {
823 gctl_error(req, "%d", EPERM);
829 cp = LIST_FIRST(&gp->consumer);
830 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
832 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
833 while (table->gpt_smhead != 0) {
834 i = ffs(table->gpt_smhead) - 1;
835 error = g_write_data(cp, i * pp->sectorsize, buf,
841 table->gpt_smhead &= ~(1 << i);
843 while (table->gpt_smtail != 0) {
844 i = ffs(table->gpt_smtail) - 1;
845 error = g_write_data(cp, pp->mediasize - (i + 1) *
846 pp->sectorsize, buf, pp->sectorsize);
851 table->gpt_smtail &= ~(1 << i);
856 if (table->gpt_scheme == &g_part_null_scheme) {
858 g_access(cp, -1, -1, -1);
859 g_part_wither(gp, ENXIO);
863 error = G_PART_WRITE(table, cp);
867 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
868 if (!entry->gpe_deleted) {
869 entry->gpe_created = 0;
870 entry->gpe_modified = 0;
873 LIST_REMOVE(entry, gpe_entry);
876 table->gpt_created = 0;
877 table->gpt_opened = 0;
880 g_access(cp, -1, -1, -1);
885 gctl_error(req, "%d", error);
890 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
892 struct g_consumer *cp;
894 struct g_provider *pp;
895 struct g_part_scheme *scheme;
896 struct g_part_table *null, *table;
900 pp = gpp->gpp_provider;
901 scheme = gpp->gpp_scheme;
902 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
905 /* Check that there isn't already a g_part geom on the provider. */
906 gp = g_part_find_geom(pp->name);
909 if (null->gpt_scheme != &g_part_null_scheme) {
910 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
916 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
917 (gpp->gpp_entries < scheme->gps_minent ||
918 gpp->gpp_entries > scheme->gps_maxent)) {
919 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
924 gp = g_new_geomf(&g_part_class, "%s", pp->name);
925 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
929 table->gpt_scheme = gpp->gpp_scheme;
930 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
931 gpp->gpp_entries : scheme->gps_minent;
932 LIST_INIT(&table->gpt_entry);
934 cp = g_new_consumer(gp);
935 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
936 error = g_attach(cp, pp);
938 error = g_access(cp, 1, 1, 1);
940 g_part_wither(gp, error);
941 gctl_error(req, "%d geom '%s'", error, pp->name);
944 table->gpt_opened = 1;
946 cp = LIST_FIRST(&gp->consumer);
947 table->gpt_opened = null->gpt_opened;
948 table->gpt_smhead = null->gpt_smhead;
949 table->gpt_smtail = null->gpt_smtail;
954 /* Make sure the provider has media. */
955 if (pp->mediasize == 0 || pp->sectorsize == 0) {
960 /* Make sure we can nest and if so, determine our depth. */
961 error = g_getattr("PART::isleaf", cp, &attr);
962 if (!error && attr) {
966 error = g_getattr("PART::depth", cp, &attr);
967 table->gpt_depth = (!error) ? attr + 1 : 0;
970 * Synthesize a disk geometry. Some partitioning schemes
971 * depend on it and since some file systems need it even
972 * when the partitition scheme doesn't, we do it here in
973 * scheme-independent code.
975 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
977 error = G_PART_CREATE(table, gpp);
983 table->gpt_created = 1;
985 kobj_delete((kobj_t)null, M_GEOM);
988 * Support automatic commit by filling in the gpp_geom
991 gpp->gpp_parms |= G_PART_PARM_GEOM;
994 /* Provide feedback if so requested. */
995 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
996 sb = sbuf_new_auto();
997 sbuf_printf(sb, "%s created\n", gp->name);
999 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1007 g_access(cp, -1, -1, -1);
1008 g_part_wither(gp, error);
1010 kobj_delete((kobj_t)gp->softc, M_GEOM);
1013 gctl_error(req, "%d provider", error);
1018 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
1021 struct g_provider *pp;
1022 struct g_part_entry *entry;
1023 struct g_part_table *table;
1027 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1028 g_topology_assert();
1032 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1033 if (entry->gpe_deleted || entry->gpe_internal)
1035 if (entry->gpe_index == gpp->gpp_index)
1038 if (entry == NULL) {
1039 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1045 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
1046 gctl_error(req, "%d", EBUSY);
1051 entry->gpe_pp = NULL;
1055 g_wither_provider(pp, ENXIO);
1057 /* Provide feedback if so requested. */
1058 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1059 sb = sbuf_new_auto();
1060 G_PART_FULLNAME(table, entry, sb, gp->name);
1061 sbuf_cat(sb, " deleted\n");
1063 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1067 if (entry->gpe_created) {
1068 LIST_REMOVE(entry, gpe_entry);
1071 entry->gpe_modified = 0;
1072 entry->gpe_deleted = 1;
1078 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
1080 struct g_consumer *cp;
1082 struct g_provider *pp;
1083 struct g_part_entry *entry, *tmp;
1084 struct g_part_table *null, *table;
1089 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1090 g_topology_assert();
1093 /* Check for busy providers. */
1094 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1095 if (entry->gpe_deleted || entry->gpe_internal)
1097 if (gpp->gpp_force) {
1101 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1104 gctl_error(req, "%d", EBUSY);
1108 if (gpp->gpp_force) {
1109 /* Destroy all providers. */
1110 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1114 g_wither_provider(pp, ENXIO);
1116 LIST_REMOVE(entry, gpe_entry);
1121 error = G_PART_DESTROY(table, gpp);
1123 gctl_error(req, "%d", error);
1127 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
1131 null->gpt_scheme = &g_part_null_scheme;
1132 LIST_INIT(&null->gpt_entry);
1134 cp = LIST_FIRST(&gp->consumer);
1136 null->gpt_last = pp->mediasize / pp->sectorsize - 1;
1138 null->gpt_depth = table->gpt_depth;
1139 null->gpt_opened = table->gpt_opened;
1140 null->gpt_smhead = table->gpt_smhead;
1141 null->gpt_smtail = table->gpt_smtail;
1143 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1144 LIST_REMOVE(entry, gpe_entry);
1147 kobj_delete((kobj_t)table, M_GEOM);
1149 /* Provide feedback if so requested. */
1150 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1151 sb = sbuf_new_auto();
1152 sbuf_printf(sb, "%s destroyed\n", gp->name);
1154 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1161 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
1164 struct g_part_entry *entry;
1165 struct g_part_table *table;
1170 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1171 g_topology_assert();
1175 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1176 if (entry->gpe_deleted || entry->gpe_internal)
1178 if (entry->gpe_index == gpp->gpp_index)
1181 if (entry == NULL) {
1182 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1186 error = G_PART_MODIFY(table, entry, gpp);
1188 gctl_error(req, "%d", error);
1192 if (!entry->gpe_created)
1193 entry->gpe_modified = 1;
1195 /* Provide feedback if so requested. */
1196 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1197 sb = sbuf_new_auto();
1198 G_PART_FULLNAME(table, entry, sb, gp->name);
1199 sbuf_cat(sb, " modified\n");
1201 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1208 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
1210 gctl_error(req, "%d verb 'move'", ENOSYS);
1215 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
1217 struct g_part_table *table;
1220 int error, recovered;
1223 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1224 g_topology_assert();
1226 error = recovered = 0;
1228 if (table->gpt_corrupt) {
1229 error = G_PART_RECOVER(table);
1231 error = g_part_check_integrity(table,
1232 LIST_FIRST(&gp->consumer));
1234 gctl_error(req, "%d recovering '%s' failed",
1240 /* Provide feedback if so requested. */
1241 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1242 sb = sbuf_new_auto();
1244 sbuf_printf(sb, "%s recovered\n", gp->name);
1246 sbuf_printf(sb, "%s recovering is not needed\n",
1249 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1256 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
1259 struct g_provider *pp;
1260 struct g_part_entry *pe, *entry;
1261 struct g_part_table *table;
1268 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1269 g_topology_assert();
1272 /* check gpp_index */
1273 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1274 if (entry->gpe_deleted || entry->gpe_internal)
1276 if (entry->gpe_index == gpp->gpp_index)
1279 if (entry == NULL) {
1280 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1284 /* check gpp_size */
1285 end = entry->gpe_start + gpp->gpp_size - 1;
1286 if (gpp->gpp_size < 1 || end > table->gpt_last) {
1287 gctl_error(req, "%d size '%jd'", EINVAL,
1288 (intmax_t)gpp->gpp_size);
1292 LIST_FOREACH(pe, &table->gpt_entry, gpe_entry) {
1293 if (pe->gpe_deleted || pe->gpe_internal || pe == entry)
1295 if (end >= pe->gpe_start && end <= pe->gpe_end) {
1296 gctl_error(req, "%d end '%jd'", ENOSPC,
1300 if (entry->gpe_start < pe->gpe_start && end > pe->gpe_end) {
1301 gctl_error(req, "%d size '%jd'", ENOSPC,
1302 (intmax_t)gpp->gpp_size);
1308 if ((g_debugflags & 16) == 0 &&
1309 (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)) {
1310 if (entry->gpe_end - entry->gpe_start + 1 > gpp->gpp_size) {
1311 /* Deny shrinking of an opened partition. */
1312 gctl_error(req, "%d", EBUSY);
1317 error = G_PART_RESIZE(table, entry, gpp);
1319 gctl_error(req, "%d%s", error, error != EBUSY ? "":
1320 " resizing will lead to unexpected shrinking"
1321 " due to alignment");
1325 if (!entry->gpe_created)
1326 entry->gpe_modified = 1;
1328 /* update mediasize of changed provider */
1329 mediasize = (entry->gpe_end - entry->gpe_start + 1) *
1331 g_resize_provider(pp, mediasize);
1333 /* Provide feedback if so requested. */
1334 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1335 sb = sbuf_new_auto();
1336 G_PART_FULLNAME(table, entry, sb, gp->name);
1337 sbuf_cat(sb, " resized\n");
1339 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1346 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
1350 struct g_part_entry *entry;
1351 struct g_part_table *table;
1356 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1357 g_topology_assert();
1361 if (gpp->gpp_parms & G_PART_PARM_INDEX) {
1362 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1363 if (entry->gpe_deleted || entry->gpe_internal)
1365 if (entry->gpe_index == gpp->gpp_index)
1368 if (entry == NULL) {
1369 gctl_error(req, "%d index '%d'", ENOENT,
1376 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
1378 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
1382 /* Provide feedback if so requested. */
1383 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1384 sb = sbuf_new_auto();
1385 sbuf_printf(sb, "%s %sset on ", gpp->gpp_attrib,
1388 G_PART_FULLNAME(table, entry, sb, gp->name);
1390 sbuf_cat(sb, gp->name);
1393 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1400 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1402 struct g_consumer *cp;
1403 struct g_provider *pp;
1405 struct g_part_entry *entry, *tmp;
1406 struct g_part_table *table;
1410 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1411 g_topology_assert();
1414 if (!table->gpt_opened) {
1415 gctl_error(req, "%d", EPERM);
1419 cp = LIST_FIRST(&gp->consumer);
1420 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1421 entry->gpe_modified = 0;
1422 if (entry->gpe_created) {
1426 entry->gpe_pp = NULL;
1427 g_wither_provider(pp, ENXIO);
1429 entry->gpe_deleted = 1;
1431 if (entry->gpe_deleted) {
1432 LIST_REMOVE(entry, gpe_entry);
1437 g_topology_unlock();
1439 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1440 table->gpt_created) ? 1 : 0;
1443 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1444 if (entry->gpe_internal)
1449 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1450 LIST_REMOVE(entry, gpe_entry);
1453 error = g_part_probe(gp, cp, table->gpt_depth);
1456 g_access(cp, -1, -1, -1);
1457 g_part_wither(gp, error);
1463 * Synthesize a disk geometry. Some partitioning schemes
1464 * depend on it and since some file systems need it even
1465 * when the partitition scheme doesn't, we do it here in
1466 * scheme-independent code.
1469 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1472 error = G_PART_READ(table, cp);
1475 error = g_part_check_integrity(table, cp);
1480 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1481 if (!entry->gpe_internal)
1482 g_part_new_provider(gp, table, entry);
1485 table->gpt_opened = 0;
1486 g_access(cp, -1, -1, -1);
1491 gctl_error(req, "%d", error);
1496 g_part_wither(struct g_geom *gp, int error)
1498 struct g_part_entry *entry;
1499 struct g_part_table *table;
1502 if (table != NULL) {
1503 G_PART_DESTROY(table, NULL);
1504 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1505 LIST_REMOVE(entry, gpe_entry);
1508 if (gp->softc != NULL) {
1509 kobj_delete((kobj_t)gp->softc, M_GEOM);
1513 g_wither_geom(gp, error);
1521 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1523 struct g_part_parms gpp;
1524 struct g_part_table *table;
1525 struct gctl_req_arg *ap;
1526 enum g_part_ctl ctlreq;
1527 unsigned int i, mparms, oparms, parm;
1528 int auto_commit, close_on_error;
1529 int error, modifies;
1531 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1532 g_topology_assert();
1534 ctlreq = G_PART_CTL_NONE;
1537 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1540 if (!strcmp(verb, "add")) {
1541 ctlreq = G_PART_CTL_ADD;
1542 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1543 G_PART_PARM_START | G_PART_PARM_TYPE;
1544 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1548 if (!strcmp(verb, "bootcode")) {
1549 ctlreq = G_PART_CTL_BOOTCODE;
1550 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1554 if (!strcmp(verb, "commit")) {
1555 ctlreq = G_PART_CTL_COMMIT;
1556 mparms |= G_PART_PARM_GEOM;
1558 } else if (!strcmp(verb, "create")) {
1559 ctlreq = G_PART_CTL_CREATE;
1560 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1561 oparms |= G_PART_PARM_ENTRIES;
1565 if (!strcmp(verb, "delete")) {
1566 ctlreq = G_PART_CTL_DELETE;
1567 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1568 } else if (!strcmp(verb, "destroy")) {
1569 ctlreq = G_PART_CTL_DESTROY;
1570 mparms |= G_PART_PARM_GEOM;
1571 oparms |= G_PART_PARM_FORCE;
1575 if (!strcmp(verb, "modify")) {
1576 ctlreq = G_PART_CTL_MODIFY;
1577 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1578 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1579 } else if (!strcmp(verb, "move")) {
1580 ctlreq = G_PART_CTL_MOVE;
1581 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1585 if (!strcmp(verb, "recover")) {
1586 ctlreq = G_PART_CTL_RECOVER;
1587 mparms |= G_PART_PARM_GEOM;
1588 } else if (!strcmp(verb, "resize")) {
1589 ctlreq = G_PART_CTL_RESIZE;
1590 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX |
1595 if (!strcmp(verb, "set")) {
1596 ctlreq = G_PART_CTL_SET;
1597 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1598 oparms |= G_PART_PARM_INDEX;
1602 if (!strcmp(verb, "undo")) {
1603 ctlreq = G_PART_CTL_UNDO;
1604 mparms |= G_PART_PARM_GEOM;
1606 } else if (!strcmp(verb, "unset")) {
1607 ctlreq = G_PART_CTL_UNSET;
1608 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1609 oparms |= G_PART_PARM_INDEX;
1613 if (ctlreq == G_PART_CTL_NONE) {
1614 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1618 bzero(&gpp, sizeof(gpp));
1619 for (i = 0; i < req->narg; i++) {
1622 switch (ap->name[0]) {
1624 if (!strcmp(ap->name, "arg0")) {
1626 (G_PART_PARM_GEOM | G_PART_PARM_PROVIDER);
1628 if (!strcmp(ap->name, "attrib"))
1629 parm = G_PART_PARM_ATTRIB;
1632 if (!strcmp(ap->name, "bootcode"))
1633 parm = G_PART_PARM_BOOTCODE;
1636 if (!strcmp(ap->name, "class"))
1640 if (!strcmp(ap->name, "entries"))
1641 parm = G_PART_PARM_ENTRIES;
1644 if (!strcmp(ap->name, "flags"))
1645 parm = G_PART_PARM_FLAGS;
1646 else if (!strcmp(ap->name, "force"))
1647 parm = G_PART_PARM_FORCE;
1650 if (!strcmp(ap->name, "index"))
1651 parm = G_PART_PARM_INDEX;
1654 if (!strcmp(ap->name, "label"))
1655 parm = G_PART_PARM_LABEL;
1658 if (!strcmp(ap->name, "output"))
1659 parm = G_PART_PARM_OUTPUT;
1662 if (!strcmp(ap->name, "scheme"))
1663 parm = G_PART_PARM_SCHEME;
1664 else if (!strcmp(ap->name, "size"))
1665 parm = G_PART_PARM_SIZE;
1666 else if (!strcmp(ap->name, "start"))
1667 parm = G_PART_PARM_START;
1670 if (!strcmp(ap->name, "type"))
1671 parm = G_PART_PARM_TYPE;
1674 if (!strcmp(ap->name, "verb"))
1676 else if (!strcmp(ap->name, "version"))
1677 parm = G_PART_PARM_VERSION;
1680 if ((parm & (mparms | oparms)) == 0) {
1681 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1685 case G_PART_PARM_ATTRIB:
1686 error = g_part_parm_str(req, ap->name,
1689 case G_PART_PARM_BOOTCODE:
1690 error = g_part_parm_bootcode(req, ap->name,
1691 &gpp.gpp_codeptr, &gpp.gpp_codesize);
1693 case G_PART_PARM_ENTRIES:
1694 error = g_part_parm_intmax(req, ap->name,
1697 case G_PART_PARM_FLAGS:
1698 error = g_part_parm_str(req, ap->name, &gpp.gpp_flags);
1700 case G_PART_PARM_FORCE:
1701 error = g_part_parm_uint32(req, ap->name,
1704 case G_PART_PARM_GEOM:
1705 error = g_part_parm_geom(req, ap->name, &gpp.gpp_geom);
1707 case G_PART_PARM_INDEX:
1708 error = g_part_parm_intmax(req, ap->name,
1711 case G_PART_PARM_LABEL:
1712 error = g_part_parm_str(req, ap->name, &gpp.gpp_label);
1714 case G_PART_PARM_OUTPUT:
1715 error = 0; /* Write-only parameter */
1717 case G_PART_PARM_PROVIDER:
1718 error = g_part_parm_provider(req, ap->name,
1721 case G_PART_PARM_SCHEME:
1722 error = g_part_parm_scheme(req, ap->name,
1725 case G_PART_PARM_SIZE:
1726 error = g_part_parm_quad(req, ap->name, &gpp.gpp_size);
1728 case G_PART_PARM_START:
1729 error = g_part_parm_quad(req, ap->name,
1732 case G_PART_PARM_TYPE:
1733 error = g_part_parm_str(req, ap->name, &gpp.gpp_type);
1735 case G_PART_PARM_VERSION:
1736 error = g_part_parm_uint32(req, ap->name,
1741 gctl_error(req, "%d %s", error, ap->name);
1745 if (error == ENOATTR) {
1746 gctl_error(req, "%d param '%s'", error,
1751 gpp.gpp_parms |= parm;
1753 if ((gpp.gpp_parms & mparms) != mparms) {
1754 parm = mparms - (gpp.gpp_parms & mparms);
1755 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1759 /* Obtain permissions if possible/necessary. */
1762 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1763 table = gpp.gpp_geom->softc;
1764 if (table != NULL && table->gpt_corrupt &&
1765 ctlreq != G_PART_CTL_DESTROY &&
1766 ctlreq != G_PART_CTL_RECOVER) {
1767 gctl_error(req, "%d table '%s' is corrupt",
1768 EPERM, gpp.gpp_geom->name);
1771 if (table != NULL && !table->gpt_opened) {
1772 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1775 gctl_error(req, "%d geom '%s'", error,
1776 gpp.gpp_geom->name);
1779 table->gpt_opened = 1;
1784 /* Allow the scheme to check or modify the parameters. */
1785 if (table != NULL) {
1786 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1788 gctl_error(req, "%d pre-check failed", error);
1792 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1795 case G_PART_CTL_NONE:
1796 panic("%s", __func__);
1797 case G_PART_CTL_ADD:
1798 error = g_part_ctl_add(req, &gpp);
1800 case G_PART_CTL_BOOTCODE:
1801 error = g_part_ctl_bootcode(req, &gpp);
1803 case G_PART_CTL_COMMIT:
1804 error = g_part_ctl_commit(req, &gpp);
1806 case G_PART_CTL_CREATE:
1807 error = g_part_ctl_create(req, &gpp);
1809 case G_PART_CTL_DELETE:
1810 error = g_part_ctl_delete(req, &gpp);
1812 case G_PART_CTL_DESTROY:
1813 error = g_part_ctl_destroy(req, &gpp);
1815 case G_PART_CTL_MODIFY:
1816 error = g_part_ctl_modify(req, &gpp);
1818 case G_PART_CTL_MOVE:
1819 error = g_part_ctl_move(req, &gpp);
1821 case G_PART_CTL_RECOVER:
1822 error = g_part_ctl_recover(req, &gpp);
1824 case G_PART_CTL_RESIZE:
1825 error = g_part_ctl_resize(req, &gpp);
1827 case G_PART_CTL_SET:
1828 error = g_part_ctl_setunset(req, &gpp, 1);
1830 case G_PART_CTL_UNDO:
1831 error = g_part_ctl_undo(req, &gpp);
1833 case G_PART_CTL_UNSET:
1834 error = g_part_ctl_setunset(req, &gpp, 0);
1838 /* Implement automatic commit. */
1840 auto_commit = (modifies &&
1841 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1842 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1844 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, ("%s",
1846 error = g_part_ctl_commit(req, &gpp);
1851 if (error && close_on_error) {
1852 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1853 table->gpt_opened = 0;
1858 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1862 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1863 g_topology_assert();
1865 g_part_wither(gp, EINVAL);
1869 static struct g_geom *
1870 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1872 struct g_consumer *cp;
1874 struct g_part_entry *entry;
1875 struct g_part_table *table;
1876 struct root_hold_token *rht;
1880 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1881 g_topology_assert();
1883 /* Skip providers that are already open for writing. */
1888 * Create a GEOM with consumer and hook it up to the provider.
1889 * With that we become part of the topology. Optain read access
1892 gp = g_new_geomf(mp, "%s", pp->name);
1893 cp = g_new_consumer(gp);
1894 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1895 error = g_attach(cp, pp);
1897 error = g_access(cp, 1, 0, 0);
1901 g_destroy_consumer(cp);
1906 rht = root_mount_hold(mp->name);
1907 g_topology_unlock();
1910 * Short-circuit the whole probing galore when there's no
1913 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1918 /* Make sure we can nest and if so, determine our depth. */
1919 error = g_getattr("PART::isleaf", cp, &attr);
1920 if (!error && attr) {
1924 error = g_getattr("PART::depth", cp, &attr);
1925 depth = (!error) ? attr + 1 : 0;
1927 error = g_part_probe(gp, cp, depth);
1934 * Synthesize a disk geometry. Some partitioning schemes
1935 * depend on it and since some file systems need it even
1936 * when the partitition scheme doesn't, we do it here in
1937 * scheme-independent code.
1939 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1941 error = G_PART_READ(table, cp);
1944 error = g_part_check_integrity(table, cp);
1949 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1950 if (!entry->gpe_internal)
1951 g_part_new_provider(gp, table, entry);
1954 root_mount_rel(rht);
1955 g_access(cp, -1, 0, 0);
1960 root_mount_rel(rht);
1961 g_access(cp, -1, 0, 0);
1963 g_destroy_consumer(cp);
1973 g_part_access(struct g_provider *pp, int dr, int dw, int de)
1975 struct g_consumer *cp;
1977 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1980 cp = LIST_FIRST(&pp->geom->consumer);
1982 /* We always gain write-exclusive access. */
1983 return (g_access(cp, dr, dw, dw + de));
1987 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1988 struct g_consumer *cp, struct g_provider *pp)
1991 struct g_part_entry *entry;
1992 struct g_part_table *table;
1994 KASSERT(sb != NULL && gp != NULL, ("%s", __func__));
1997 if (indent == NULL) {
1998 KASSERT(cp == NULL && pp != NULL, ("%s", __func__));
1999 entry = pp->private;
2002 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
2003 (uintmax_t)entry->gpe_offset,
2004 G_PART_TYPE(table, entry, buf, sizeof(buf)));
2006 * libdisk compatibility quirk - the scheme dumps the
2007 * slicer name and partition type in a way that is
2008 * compatible with libdisk. When libdisk is not used
2009 * anymore, this should go away.
2011 G_PART_DUMPCONF(table, entry, sb, indent);
2012 } else if (cp != NULL) { /* Consumer configuration. */
2013 KASSERT(pp == NULL, ("%s", __func__));
2015 } else if (pp != NULL) { /* Provider configuration. */
2016 entry = pp->private;
2019 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
2020 (uintmax_t)entry->gpe_start);
2021 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
2022 (uintmax_t)entry->gpe_end);
2023 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
2025 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
2026 G_PART_TYPE(table, entry, buf, sizeof(buf)));
2027 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
2028 (uintmax_t)entry->gpe_offset);
2029 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
2030 (uintmax_t)pp->mediasize);
2031 G_PART_DUMPCONF(table, entry, sb, indent);
2032 } else { /* Geom configuration. */
2033 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
2034 table->gpt_scheme->name);
2035 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
2036 table->gpt_entries);
2037 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
2038 (uintmax_t)table->gpt_first);
2039 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
2040 (uintmax_t)table->gpt_last);
2041 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
2042 table->gpt_sectors);
2043 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
2045 sbuf_printf(sb, "%s<state>%s</state>\n", indent,
2046 table->gpt_corrupt ? "CORRUPT": "OK");
2047 sbuf_printf(sb, "%s<modified>%s</modified>\n", indent,
2048 table->gpt_opened ? "true": "false");
2049 G_PART_DUMPCONF(table, NULL, sb, indent);
2054 g_part_resize(struct g_consumer *cp)
2056 struct g_part_table *table;
2058 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2059 g_topology_assert();
2061 table = cp->geom->softc;
2062 if (table->gpt_opened == 0) {
2063 if (g_access(cp, 1, 1, 1) != 0)
2065 table->gpt_opened = 1;
2067 if (G_PART_RESIZE(table, NULL, NULL) == 0)
2068 printf("GEOM_PART: %s was automatically resized.\n"
2069 " Use `gpart commit %s` to save changes or "
2070 "`gpart undo %s` to revert them.\n", cp->geom->name,
2071 cp->geom->name, cp->geom->name);
2072 if (g_part_check_integrity(table, cp) != 0) {
2073 g_access(cp, -1, -1, -1);
2074 table->gpt_opened = 0;
2075 g_part_wither(table->gpt_gp, ENXIO);
2080 g_part_orphan(struct g_consumer *cp)
2082 struct g_provider *pp;
2083 struct g_part_table *table;
2086 KASSERT(pp != NULL, ("%s", __func__));
2087 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
2088 g_topology_assert();
2090 KASSERT(pp->error != 0, ("%s", __func__));
2091 table = cp->geom->softc;
2092 if (table != NULL && table->gpt_opened)
2093 g_access(cp, -1, -1, -1);
2094 g_part_wither(cp->geom, pp->error);
2098 g_part_spoiled(struct g_consumer *cp)
2101 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2102 g_topology_assert();
2104 cp->flags |= G_CF_ORPHAN;
2105 g_part_wither(cp->geom, ENXIO);
2109 g_part_start(struct bio *bp)
2112 struct g_consumer *cp;
2114 struct g_part_entry *entry;
2115 struct g_part_table *table;
2116 struct g_kerneldump *gkd;
2117 struct g_provider *pp;
2123 cp = LIST_FIRST(&gp->consumer);
2125 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
2128 entry = pp->private;
2129 if (entry == NULL) {
2130 g_io_deliver(bp, ENXIO);
2134 switch(bp->bio_cmd) {
2138 if (bp->bio_offset >= pp->mediasize) {
2139 g_io_deliver(bp, EIO);
2142 bp2 = g_clone_bio(bp);
2144 g_io_deliver(bp, ENOMEM);
2147 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
2148 bp2->bio_length = pp->mediasize - bp2->bio_offset;
2149 bp2->bio_done = g_std_done;
2150 bp2->bio_offset += entry->gpe_offset;
2151 g_io_request(bp2, cp);
2156 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
2158 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
2160 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
2162 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
2164 if (g_handleattr_str(bp, "PART::scheme",
2165 table->gpt_scheme->name))
2167 if (g_handleattr_str(bp, "PART::type",
2168 G_PART_TYPE(table, entry, buf, sizeof(buf))))
2170 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
2172 * Check that the partition is suitable for kernel
2173 * dumps. Typically only swap partitions should be
2174 * used. If the request comes from the nested scheme
2175 * we allow dumping there as well.
2177 if ((bp->bio_from == NULL ||
2178 bp->bio_from->geom->class != &g_part_class) &&
2179 G_PART_DUMPTO(table, entry) == 0) {
2180 g_io_deliver(bp, ENODEV);
2181 printf("GEOM_PART: Partition '%s' not suitable"
2182 " for kernel dumps (wrong type?)\n",
2186 gkd = (struct g_kerneldump *)bp->bio_data;
2187 if (gkd->offset >= pp->mediasize) {
2188 g_io_deliver(bp, EIO);
2191 if (gkd->offset + gkd->length > pp->mediasize)
2192 gkd->length = pp->mediasize - gkd->offset;
2193 gkd->offset += entry->gpe_offset;
2197 g_io_deliver(bp, EOPNOTSUPP);
2201 bp2 = g_clone_bio(bp);
2203 g_io_deliver(bp, ENOMEM);
2206 bp2->bio_done = g_std_done;
2207 g_io_request(bp2, cp);
2211 g_part_init(struct g_class *mp)
2214 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
2218 g_part_fini(struct g_class *mp)
2221 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
2225 g_part_unload_event(void *arg, int flag)
2227 struct g_consumer *cp;
2229 struct g_provider *pp;
2230 struct g_part_scheme *scheme;
2231 struct g_part_table *table;
2235 if (flag == EV_CANCEL)
2240 scheme = (void *)(*xchg);
2242 g_topology_assert();
2244 LIST_FOREACH(gp, &g_part_class.geom, geom) {
2246 if (table->gpt_scheme != scheme)
2250 LIST_FOREACH(pp, &gp->provider, provider)
2251 acc += pp->acr + pp->acw + pp->ace;
2252 LIST_FOREACH(cp, &gp->consumer, consumer)
2253 acc += cp->acr + cp->acw + cp->ace;
2256 g_part_wither(gp, ENOSYS);
2262 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
2268 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
2270 struct g_part_scheme *iter;
2277 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
2278 if (scheme == iter) {
2279 printf("GEOM_PART: scheme %s is already "
2280 "registered!\n", scheme->name);
2285 TAILQ_INSERT_TAIL(&g_part_schemes, scheme,
2287 g_retaste(&g_part_class);
2291 arg = (uintptr_t)scheme;
2292 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,