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/diskmbr.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/queue.h>
42 #include <sys/sysctl.h>
43 #include <sys/systm.h>
45 #include <geom/geom.h>
46 #include <geom/geom_ctl.h>
47 #include <geom/geom_int.h>
48 #include <geom/part/g_part.h>
50 #include "g_part_if.h"
53 #define _PATH_DEV "/dev/"
56 static kobj_method_t g_part_null_methods[] = {
60 static struct g_part_scheme g_part_null_scheme = {
63 sizeof(struct g_part_table),
66 TAILQ_HEAD(, g_part_scheme) g_part_schemes =
67 TAILQ_HEAD_INITIALIZER(g_part_schemes);
69 struct g_part_alias_list {
71 enum g_part_alias alias;
72 } g_part_alias_list[G_PART_ALIAS_COUNT] = {
73 { "apple-boot", G_PART_ALIAS_APPLE_BOOT },
74 { "apple-hfs", G_PART_ALIAS_APPLE_HFS },
75 { "apple-label", G_PART_ALIAS_APPLE_LABEL },
76 { "apple-raid", G_PART_ALIAS_APPLE_RAID },
77 { "apple-raid-offline", G_PART_ALIAS_APPLE_RAID_OFFLINE },
78 { "apple-tv-recovery", G_PART_ALIAS_APPLE_TV_RECOVERY },
79 { "apple-ufs", G_PART_ALIAS_APPLE_UFS },
80 { "bios-boot", G_PART_ALIAS_BIOS_BOOT },
81 { "ebr", G_PART_ALIAS_EBR },
82 { "efi", G_PART_ALIAS_EFI },
83 { "fat32", G_PART_ALIAS_MS_FAT32 },
84 { "freebsd", G_PART_ALIAS_FREEBSD },
85 { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
86 { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
87 { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
88 { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
89 { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
90 { "linux-data", G_PART_ALIAS_LINUX_DATA },
91 { "linux-lvm", G_PART_ALIAS_LINUX_LVM },
92 { "linux-raid", G_PART_ALIAS_LINUX_RAID },
93 { "linux-swap", G_PART_ALIAS_LINUX_SWAP },
94 { "mbr", G_PART_ALIAS_MBR },
95 { "ms-basic-data", G_PART_ALIAS_MS_BASIC_DATA },
96 { "ms-ldm-data", G_PART_ALIAS_MS_LDM_DATA },
97 { "ms-ldm-metadata", G_PART_ALIAS_MS_LDM_METADATA },
98 { "ms-reserved", G_PART_ALIAS_MS_RESERVED },
99 { "ntfs", G_PART_ALIAS_MS_NTFS },
100 { "netbsd-ccd", G_PART_ALIAS_NETBSD_CCD },
101 { "netbsd-cgd", G_PART_ALIAS_NETBSD_CGD },
102 { "netbsd-ffs", G_PART_ALIAS_NETBSD_FFS },
103 { "netbsd-lfs", G_PART_ALIAS_NETBSD_LFS },
104 { "netbsd-raid", G_PART_ALIAS_NETBSD_RAID },
105 { "netbsd-swap", G_PART_ALIAS_NETBSD_SWAP },
108 SYSCTL_DECL(_kern_geom);
109 SYSCTL_NODE(_kern_geom, OID_AUTO, part, CTLFLAG_RW, 0, "GEOM_PART stuff");
110 static u_int check_integrity = 1;
111 TUNABLE_INT("kern.geom.part.check_integrity", &check_integrity);
112 SYSCTL_UINT(_kern_geom_part, OID_AUTO, check_integrity, CTLFLAG_RW,
113 &check_integrity, 1, "Enable integrity checking");
116 * The GEOM partitioning class.
118 static g_ctl_req_t g_part_ctlreq;
119 static g_ctl_destroy_geom_t g_part_destroy_geom;
120 static g_fini_t g_part_fini;
121 static g_init_t g_part_init;
122 static g_taste_t g_part_taste;
124 static g_access_t g_part_access;
125 static g_dumpconf_t g_part_dumpconf;
126 static g_orphan_t g_part_orphan;
127 static g_spoiled_t g_part_spoiled;
128 static g_start_t g_part_start;
130 static struct g_class g_part_class = {
132 .version = G_VERSION,
134 .ctlreq = g_part_ctlreq,
135 .destroy_geom = g_part_destroy_geom,
138 .taste = g_part_taste,
140 .access = g_part_access,
141 .dumpconf = g_part_dumpconf,
142 .orphan = g_part_orphan,
143 .spoiled = g_part_spoiled,
144 .start = g_part_start,
147 DECLARE_GEOM_CLASS(g_part_class, g_part);
153 static void g_part_wither(struct g_geom *, int);
156 g_part_alias_name(enum g_part_alias alias)
160 for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
161 if (g_part_alias_list[i].alias != alias)
163 return (g_part_alias_list[i].lexeme);
170 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
173 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
174 off_t chs, cylinders;
180 for (idx = 0; candidate_heads[idx] != 0; idx++) {
181 heads = candidate_heads[idx];
182 cylinders = blocks / heads / sectors;
183 if (cylinders < heads || cylinders < sectors)
185 if (cylinders > 1023)
187 chs = cylinders * heads * sectors;
188 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
196 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
199 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
201 u_int heads, sectors;
204 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 || sectors == 0 ||
205 g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
206 table->gpt_fixgeom = 0;
207 table->gpt_heads = 0;
208 table->gpt_sectors = 0;
210 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
211 sectors = candidate_sectors[idx];
212 g_part_geometry_heads(blocks, sectors, &chs, &heads);
216 * Prefer a geometry with sectors > 1, but only if
217 * it doesn't bump down the numbver of heads to 1.
219 if (chs > bestchs || (chs == bestchs && heads > 1 &&
220 table->gpt_sectors == 1)) {
222 table->gpt_heads = heads;
223 table->gpt_sectors = sectors;
227 * If we didn't find a geometry at all, then the disk is
228 * too big. This means we can use the maximum number of
232 table->gpt_heads = 255;
233 table->gpt_sectors = 63;
236 table->gpt_fixgeom = 1;
237 table->gpt_heads = heads;
238 table->gpt_sectors = sectors;
242 #define DPRINTF(...) if (bootverbose) { \
243 printf("GEOM_PART: " __VA_ARGS__); \
247 g_part_check_integrity(struct g_part_table *table, struct g_consumer *cp)
249 struct g_part_entry *e1, *e2;
250 struct g_provider *pp;
256 if (table->gpt_last < table->gpt_first) {
257 DPRINTF("last LBA is below first LBA: %jd < %jd\n",
258 (intmax_t)table->gpt_last, (intmax_t)table->gpt_first);
261 if (table->gpt_last > pp->mediasize / pp->sectorsize - 1) {
262 DPRINTF("last LBA extends beyond mediasize: "
263 "%jd > %jd\n", (intmax_t)table->gpt_last,
264 (intmax_t)pp->mediasize / pp->sectorsize - 1);
267 LIST_FOREACH(e1, &table->gpt_entry, gpe_entry) {
268 if (e1->gpe_deleted || e1->gpe_internal)
270 if (e1->gpe_start < table->gpt_first) {
271 DPRINTF("partition %d has start offset below first "
272 "LBA: %jd < %jd\n", e1->gpe_index,
273 (intmax_t)e1->gpe_start,
274 (intmax_t)table->gpt_first);
277 if (e1->gpe_start > table->gpt_last) {
278 DPRINTF("partition %d has start offset beyond last "
279 "LBA: %jd > %jd\n", e1->gpe_index,
280 (intmax_t)e1->gpe_start,
281 (intmax_t)table->gpt_last);
284 if (e1->gpe_end < e1->gpe_start) {
285 DPRINTF("partition %d has end offset below start "
286 "offset: %jd < %jd\n", e1->gpe_index,
287 (intmax_t)e1->gpe_end,
288 (intmax_t)e1->gpe_start);
291 if (e1->gpe_end > table->gpt_last) {
292 DPRINTF("partition %d has end offset beyond last "
293 "LBA: %jd > %jd\n", e1->gpe_index,
294 (intmax_t)e1->gpe_end,
295 (intmax_t)table->gpt_last);
298 if (pp->stripesize > 0) {
299 offset = e1->gpe_start * pp->sectorsize;
300 if (e1->gpe_offset > offset)
301 offset = e1->gpe_offset;
302 if ((offset + pp->stripeoffset) % pp->stripesize) {
303 DPRINTF("partition %d is not aligned on %u "
304 "bytes\n", e1->gpe_index, pp->stripesize);
305 /* Don't treat this as a critical failure */
309 while ((e2 = LIST_NEXT(e2, gpe_entry)) != NULL) {
310 if (e2->gpe_deleted || e2->gpe_internal)
312 if (e1->gpe_start >= e2->gpe_start &&
313 e1->gpe_start <= e2->gpe_end) {
314 DPRINTF("partition %d has start offset inside "
315 "partition %d: start[%d] %jd >= start[%d] "
316 "%jd <= end[%d] %jd\n",
317 e1->gpe_index, e2->gpe_index,
318 e2->gpe_index, (intmax_t)e2->gpe_start,
319 e1->gpe_index, (intmax_t)e1->gpe_start,
320 e2->gpe_index, (intmax_t)e2->gpe_end);
323 if (e1->gpe_end >= e2->gpe_start &&
324 e1->gpe_end <= e2->gpe_end) {
325 DPRINTF("partition %d has end offset inside "
326 "partition %d: start[%d] %jd >= end[%d] "
327 "%jd <= end[%d] %jd\n",
328 e1->gpe_index, e2->gpe_index,
329 e2->gpe_index, (intmax_t)e2->gpe_start,
330 e1->gpe_index, (intmax_t)e1->gpe_end,
331 e2->gpe_index, (intmax_t)e2->gpe_end);
334 if (e1->gpe_start < e2->gpe_start &&
335 e1->gpe_end > e2->gpe_end) {
336 DPRINTF("partition %d contains partition %d: "
337 "start[%d] %jd > start[%d] %jd, end[%d] "
338 "%jd < end[%d] %jd\n",
339 e1->gpe_index, e2->gpe_index,
340 e1->gpe_index, (intmax_t)e1->gpe_start,
341 e2->gpe_index, (intmax_t)e2->gpe_start,
342 e2->gpe_index, (intmax_t)e2->gpe_end,
343 e1->gpe_index, (intmax_t)e1->gpe_end);
349 printf("GEOM_PART: integrity check failed (%s, %s)\n",
350 pp->name, table->gpt_scheme->name);
351 if (check_integrity != 0)
353 table->gpt_corrupt = 1;
359 struct g_part_entry *
360 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
363 struct g_part_entry *entry, *last;
366 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
367 if (entry->gpe_index == index)
369 if (entry->gpe_index > index) {
376 entry = g_malloc(table->gpt_scheme->gps_entrysz,
378 entry->gpe_index = index;
380 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
382 LIST_INSERT_AFTER(last, entry, gpe_entry);
384 entry->gpe_offset = 0;
385 entry->gpe_start = start;
386 entry->gpe_end = end;
391 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
392 struct g_part_entry *entry)
394 struct g_consumer *cp;
395 struct g_provider *pp;
399 cp = LIST_FIRST(&gp->consumer);
402 offset = entry->gpe_start * pp->sectorsize;
403 if (entry->gpe_offset < offset)
404 entry->gpe_offset = offset;
406 if (entry->gpe_pp == NULL) {
407 sb = sbuf_new_auto();
408 G_PART_FULLNAME(table, entry, sb, gp->name);
410 entry->gpe_pp = g_new_providerf(gp, "%s", sbuf_data(sb));
412 entry->gpe_pp->private = entry; /* Close the circle. */
414 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */
415 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
417 entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
418 entry->gpe_pp->sectorsize = pp->sectorsize;
419 entry->gpe_pp->flags = pp->flags & G_PF_CANDELETE;
420 entry->gpe_pp->stripesize = pp->stripesize;
421 entry->gpe_pp->stripeoffset = pp->stripeoffset + entry->gpe_offset;
422 if (pp->stripesize > 0)
423 entry->gpe_pp->stripeoffset %= pp->stripesize;
424 g_error_provider(entry->gpe_pp, 0);
427 static struct g_geom*
428 g_part_find_geom(const char *name)
431 LIST_FOREACH(gp, &g_part_class.geom, geom) {
432 if (!strcmp(name, gp->name))
439 g_part_parm_geom(struct gctl_req *req, const char *name, struct g_geom **v)
444 gname = gctl_get_asciiparam(req, name);
447 if (strncmp(gname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
448 gname += sizeof(_PATH_DEV) - 1;
449 gp = g_part_find_geom(gname);
451 gctl_error(req, "%d %s '%s'", EINVAL, name, gname);
454 if ((gp->flags & G_GEOM_WITHER) != 0) {
455 gctl_error(req, "%d %s", ENXIO, gname);
463 g_part_parm_provider(struct gctl_req *req, const char *name,
464 struct g_provider **v)
466 struct g_provider *pp;
469 pname = gctl_get_asciiparam(req, name);
472 if (strncmp(pname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
473 pname += sizeof(_PATH_DEV) - 1;
474 pp = g_provider_by_name(pname);
476 gctl_error(req, "%d %s '%s'", EINVAL, name, pname);
484 g_part_parm_quad(struct gctl_req *req, const char *name, quad_t *v)
490 p = gctl_get_asciiparam(req, name);
493 q = strtoq(p, &x, 0);
494 if (*x != '\0' || q < 0) {
495 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
503 g_part_parm_scheme(struct gctl_req *req, const char *name,
504 struct g_part_scheme **v)
506 struct g_part_scheme *s;
509 p = gctl_get_asciiparam(req, name);
512 TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
513 if (s == &g_part_null_scheme)
515 if (!strcasecmp(s->name, p))
519 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
527 g_part_parm_str(struct gctl_req *req, const char *name, const char **v)
531 p = gctl_get_asciiparam(req, name);
534 /* An empty label is always valid. */
535 if (strcmp(name, "label") != 0 && p[0] == '\0') {
536 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
544 g_part_parm_intmax(struct gctl_req *req, const char *name, u_int *v)
549 p = gctl_get_param(req, name, &size);
552 if (size != sizeof(*p) || *p < 0 || *p > INT_MAX) {
553 gctl_error(req, "%d %s '%jd'", EINVAL, name, *p);
561 g_part_parm_uint32(struct gctl_req *req, const char *name, u_int *v)
566 p = gctl_get_param(req, name, &size);
569 if (size != sizeof(*p) || *p > INT_MAX) {
570 gctl_error(req, "%d %s '%u'", EINVAL, name, (unsigned int)*p);
578 g_part_parm_bootcode(struct gctl_req *req, const char *name, const void **v,
584 p = gctl_get_param(req, name, &size);
593 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
595 struct g_part_scheme *iter, *scheme;
596 struct g_part_table *table;
600 scheme = (table != NULL) ? table->gpt_scheme : NULL;
601 pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
604 if (pri > 0) { /* error */
609 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
610 if (iter == &g_part_null_scheme)
612 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
615 table->gpt_scheme = iter;
616 table->gpt_depth = depth;
617 probe = G_PART_PROBE(table, cp);
618 if (probe <= 0 && probe > pri) {
621 if (gp->softc != NULL)
622 kobj_delete((kobj_t)gp->softc, M_GEOM);
627 kobj_delete((kobj_t)table, M_GEOM);
631 return ((scheme == NULL) ? ENXIO : 0);
635 * Control request functions.
639 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
642 struct g_provider *pp;
643 struct g_part_entry *delent, *last, *entry;
644 struct g_part_table *table;
651 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
654 pp = LIST_FIRST(&gp->consumer)->provider;
656 end = gpp->gpp_start + gpp->gpp_size - 1;
658 if (gpp->gpp_start < table->gpt_first ||
659 gpp->gpp_start > table->gpt_last) {
660 gctl_error(req, "%d start '%jd'", EINVAL,
661 (intmax_t)gpp->gpp_start);
664 if (end < gpp->gpp_start || end > table->gpt_last) {
665 gctl_error(req, "%d size '%jd'", EINVAL,
666 (intmax_t)gpp->gpp_size);
669 if (gpp->gpp_index > table->gpt_entries) {
670 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
674 delent = last = NULL;
675 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
676 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
677 if (entry->gpe_deleted) {
678 if (entry->gpe_index == index)
682 if (entry->gpe_index == index)
683 index = entry->gpe_index + 1;
684 if (entry->gpe_index < index)
686 if (entry->gpe_internal)
688 if (gpp->gpp_start >= entry->gpe_start &&
689 gpp->gpp_start <= entry->gpe_end) {
690 gctl_error(req, "%d start '%jd'", ENOSPC,
691 (intmax_t)gpp->gpp_start);
694 if (end >= entry->gpe_start && end <= entry->gpe_end) {
695 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
698 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
699 gctl_error(req, "%d size '%jd'", ENOSPC,
700 (intmax_t)gpp->gpp_size);
704 if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
705 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
708 if (index > table->gpt_entries) {
709 gctl_error(req, "%d index '%d'", ENOSPC, index);
713 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
714 M_WAITOK | M_ZERO) : delent;
715 entry->gpe_index = index;
716 entry->gpe_start = gpp->gpp_start;
717 entry->gpe_end = end;
718 error = G_PART_ADD(table, entry, gpp);
720 gctl_error(req, "%d", error);
725 if (delent == NULL) {
727 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
729 LIST_INSERT_AFTER(last, entry, gpe_entry);
730 entry->gpe_created = 1;
732 entry->gpe_deleted = 0;
733 entry->gpe_modified = 1;
735 g_part_new_provider(gp, table, entry);
737 /* Provide feedback if so requested. */
738 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
739 sb = sbuf_new_auto();
740 G_PART_FULLNAME(table, entry, sb, gp->name);
741 if (pp->stripesize > 0 && entry->gpe_pp->stripeoffset != 0)
742 sbuf_printf(sb, " added, but partition is not "
743 "aligned on %u bytes\n", pp->stripesize);
745 sbuf_cat(sb, " added\n");
747 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
754 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
757 struct g_part_table *table;
762 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
766 sz = table->gpt_scheme->gps_bootcodesz;
771 if (gpp->gpp_codesize > sz) {
776 error = G_PART_BOOTCODE(table, gpp);
780 /* Provide feedback if so requested. */
781 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
782 sb = sbuf_new_auto();
783 sbuf_printf(sb, "bootcode written to %s\n", gp->name);
785 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
791 gctl_error(req, "%d", error);
796 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
798 struct g_consumer *cp;
800 struct g_provider *pp;
801 struct g_part_entry *entry, *tmp;
802 struct g_part_table *table;
807 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
811 if (!table->gpt_opened) {
812 gctl_error(req, "%d", EPERM);
818 cp = LIST_FIRST(&gp->consumer);
819 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
821 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
822 while (table->gpt_smhead != 0) {
823 i = ffs(table->gpt_smhead) - 1;
824 error = g_write_data(cp, i * pp->sectorsize, buf,
830 table->gpt_smhead &= ~(1 << i);
832 while (table->gpt_smtail != 0) {
833 i = ffs(table->gpt_smtail) - 1;
834 error = g_write_data(cp, pp->mediasize - (i + 1) *
835 pp->sectorsize, buf, pp->sectorsize);
840 table->gpt_smtail &= ~(1 << i);
845 if (table->gpt_scheme == &g_part_null_scheme) {
847 g_access(cp, -1, -1, -1);
848 g_part_wither(gp, ENXIO);
852 error = G_PART_WRITE(table, cp);
856 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
857 if (!entry->gpe_deleted) {
858 entry->gpe_created = 0;
859 entry->gpe_modified = 0;
862 LIST_REMOVE(entry, gpe_entry);
865 table->gpt_created = 0;
866 table->gpt_opened = 0;
869 g_access(cp, -1, -1, -1);
874 gctl_error(req, "%d", error);
879 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
881 struct g_consumer *cp;
883 struct g_provider *pp;
884 struct g_part_scheme *scheme;
885 struct g_part_table *null, *table;
889 pp = gpp->gpp_provider;
890 scheme = gpp->gpp_scheme;
891 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
894 /* Check that there isn't already a g_part geom on the provider. */
895 gp = g_part_find_geom(pp->name);
898 if (null->gpt_scheme != &g_part_null_scheme) {
899 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
905 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
906 (gpp->gpp_entries < scheme->gps_minent ||
907 gpp->gpp_entries > scheme->gps_maxent)) {
908 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
913 gp = g_new_geomf(&g_part_class, "%s", pp->name);
914 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
918 table->gpt_scheme = gpp->gpp_scheme;
919 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
920 gpp->gpp_entries : scheme->gps_minent;
921 LIST_INIT(&table->gpt_entry);
923 cp = g_new_consumer(gp);
924 error = g_attach(cp, pp);
926 error = g_access(cp, 1, 1, 1);
928 g_part_wither(gp, error);
929 gctl_error(req, "%d geom '%s'", error, pp->name);
932 table->gpt_opened = 1;
934 cp = LIST_FIRST(&gp->consumer);
935 table->gpt_opened = null->gpt_opened;
936 table->gpt_smhead = null->gpt_smhead;
937 table->gpt_smtail = null->gpt_smtail;
942 /* Make sure the provider has media. */
943 if (pp->mediasize == 0 || pp->sectorsize == 0) {
948 /* Make sure we can nest and if so, determine our depth. */
949 error = g_getattr("PART::isleaf", cp, &attr);
950 if (!error && attr) {
954 error = g_getattr("PART::depth", cp, &attr);
955 table->gpt_depth = (!error) ? attr + 1 : 0;
958 * Synthesize a disk geometry. Some partitioning schemes
959 * depend on it and since some file systems need it even
960 * when the partitition scheme doesn't, we do it here in
961 * scheme-independent code.
963 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
965 error = G_PART_CREATE(table, gpp);
971 table->gpt_created = 1;
973 kobj_delete((kobj_t)null, M_GEOM);
976 * Support automatic commit by filling in the gpp_geom
979 gpp->gpp_parms |= G_PART_PARM_GEOM;
982 /* Provide feedback if so requested. */
983 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
984 sb = sbuf_new_auto();
985 sbuf_printf(sb, "%s created\n", gp->name);
987 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
995 g_access(cp, -1, -1, -1);
996 g_part_wither(gp, error);
998 kobj_delete((kobj_t)gp->softc, M_GEOM);
1001 gctl_error(req, "%d provider", error);
1006 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
1009 struct g_provider *pp;
1010 struct g_part_entry *entry;
1011 struct g_part_table *table;
1015 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1016 g_topology_assert();
1020 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1021 if (entry->gpe_deleted || entry->gpe_internal)
1023 if (entry->gpe_index == gpp->gpp_index)
1026 if (entry == NULL) {
1027 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1033 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
1034 gctl_error(req, "%d", EBUSY);
1039 entry->gpe_pp = NULL;
1043 g_wither_provider(pp, ENXIO);
1045 /* Provide feedback if so requested. */
1046 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1047 sb = sbuf_new_auto();
1048 G_PART_FULLNAME(table, entry, sb, gp->name);
1049 sbuf_cat(sb, " deleted\n");
1051 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1055 if (entry->gpe_created) {
1056 LIST_REMOVE(entry, gpe_entry);
1059 entry->gpe_modified = 0;
1060 entry->gpe_deleted = 1;
1066 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
1068 struct g_consumer *cp;
1070 struct g_provider *pp;
1071 struct g_part_entry *entry, *tmp;
1072 struct g_part_table *null, *table;
1077 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1078 g_topology_assert();
1081 /* Check for busy providers. */
1082 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1083 if (entry->gpe_deleted || entry->gpe_internal)
1085 if (gpp->gpp_force) {
1089 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1092 gctl_error(req, "%d", EBUSY);
1096 if (gpp->gpp_force) {
1097 /* Destroy all providers. */
1098 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1102 g_wither_provider(pp, ENXIO);
1104 LIST_REMOVE(entry, gpe_entry);
1109 error = G_PART_DESTROY(table, gpp);
1111 gctl_error(req, "%d", error);
1115 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
1119 null->gpt_scheme = &g_part_null_scheme;
1120 LIST_INIT(&null->gpt_entry);
1122 cp = LIST_FIRST(&gp->consumer);
1124 null->gpt_last = pp->mediasize / pp->sectorsize - 1;
1126 null->gpt_depth = table->gpt_depth;
1127 null->gpt_opened = table->gpt_opened;
1128 null->gpt_smhead = table->gpt_smhead;
1129 null->gpt_smtail = table->gpt_smtail;
1131 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1132 LIST_REMOVE(entry, gpe_entry);
1135 kobj_delete((kobj_t)table, M_GEOM);
1137 /* Provide feedback if so requested. */
1138 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1139 sb = sbuf_new_auto();
1140 sbuf_printf(sb, "%s destroyed\n", gp->name);
1142 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1149 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
1152 struct g_part_entry *entry;
1153 struct g_part_table *table;
1158 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1159 g_topology_assert();
1163 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1164 if (entry->gpe_deleted || entry->gpe_internal)
1166 if (entry->gpe_index == gpp->gpp_index)
1169 if (entry == NULL) {
1170 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1174 error = G_PART_MODIFY(table, entry, gpp);
1176 gctl_error(req, "%d", error);
1180 if (!entry->gpe_created)
1181 entry->gpe_modified = 1;
1183 /* Provide feedback if so requested. */
1184 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1185 sb = sbuf_new_auto();
1186 G_PART_FULLNAME(table, entry, sb, gp->name);
1187 sbuf_cat(sb, " modified\n");
1189 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1196 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
1198 gctl_error(req, "%d verb 'move'", ENOSYS);
1203 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
1205 struct g_part_table *table;
1208 int error, recovered;
1211 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1212 g_topology_assert();
1214 error = recovered = 0;
1216 if (table->gpt_corrupt) {
1217 error = G_PART_RECOVER(table);
1219 gctl_error(req, "%d recovering '%s' failed",
1225 /* Provide feedback if so requested. */
1226 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1227 sb = sbuf_new_auto();
1229 sbuf_printf(sb, "%s recovered\n", gp->name);
1231 sbuf_printf(sb, "%s recovering is not needed\n",
1234 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1241 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
1244 struct g_provider *pp;
1245 struct g_part_entry *pe, *entry;
1246 struct g_part_table *table;
1252 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1253 g_topology_assert();
1256 /* check gpp_index */
1257 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1258 if (entry->gpe_deleted || entry->gpe_internal)
1260 if (entry->gpe_index == gpp->gpp_index)
1263 if (entry == NULL) {
1264 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1268 /* check gpp_size */
1269 end = entry->gpe_start + gpp->gpp_size - 1;
1270 if (gpp->gpp_size < 1 || end > table->gpt_last) {
1271 gctl_error(req, "%d size '%jd'", EINVAL,
1272 (intmax_t)gpp->gpp_size);
1276 LIST_FOREACH(pe, &table->gpt_entry, gpe_entry) {
1277 if (pe->gpe_deleted || pe->gpe_internal || pe == entry)
1279 if (end >= pe->gpe_start && end <= pe->gpe_end) {
1280 gctl_error(req, "%d end '%jd'", ENOSPC,
1284 if (entry->gpe_start < pe->gpe_start && end > pe->gpe_end) {
1285 gctl_error(req, "%d size '%jd'", ENOSPC,
1286 (intmax_t)gpp->gpp_size);
1292 if ((g_debugflags & 16) == 0 &&
1293 (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)) {
1294 gctl_error(req, "%d", EBUSY);
1298 error = G_PART_RESIZE(table, entry, gpp);
1300 gctl_error(req, "%d", error);
1304 if (!entry->gpe_created)
1305 entry->gpe_modified = 1;
1307 /* update mediasize of changed provider */
1308 pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
1311 /* Provide feedback if so requested. */
1312 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1313 sb = sbuf_new_auto();
1314 G_PART_FULLNAME(table, entry, sb, gp->name);
1315 sbuf_cat(sb, " resized\n");
1317 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1324 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
1328 struct g_part_entry *entry;
1329 struct g_part_table *table;
1334 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1335 g_topology_assert();
1339 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1340 if (entry->gpe_deleted || entry->gpe_internal)
1342 if (entry->gpe_index == gpp->gpp_index)
1345 if (entry == NULL) {
1346 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1350 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
1352 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
1356 /* Provide feedback if so requested. */
1357 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1358 sb = sbuf_new_auto();
1359 sbuf_printf(sb, "%s %sset on ", gpp->gpp_attrib,
1361 G_PART_FULLNAME(table, entry, sb, gp->name);
1362 sbuf_printf(sb, "\n");
1364 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1371 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1373 struct g_consumer *cp;
1374 struct g_provider *pp;
1376 struct g_part_entry *entry, *tmp;
1377 struct g_part_table *table;
1381 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1382 g_topology_assert();
1385 if (!table->gpt_opened) {
1386 gctl_error(req, "%d", EPERM);
1390 cp = LIST_FIRST(&gp->consumer);
1391 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1392 entry->gpe_modified = 0;
1393 if (entry->gpe_created) {
1397 entry->gpe_pp = NULL;
1398 g_wither_provider(pp, ENXIO);
1400 entry->gpe_deleted = 1;
1402 if (entry->gpe_deleted) {
1403 LIST_REMOVE(entry, gpe_entry);
1408 g_topology_unlock();
1410 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1411 table->gpt_created) ? 1 : 0;
1414 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1415 if (entry->gpe_internal)
1420 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1421 LIST_REMOVE(entry, gpe_entry);
1424 error = g_part_probe(gp, cp, table->gpt_depth);
1427 g_access(cp, -1, -1, -1);
1428 g_part_wither(gp, error);
1434 * Synthesize a disk geometry. Some partitioning schemes
1435 * depend on it and since some file systems need it even
1436 * when the partitition scheme doesn't, we do it here in
1437 * scheme-independent code.
1440 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1443 error = G_PART_READ(table, cp);
1446 error = g_part_check_integrity(table, cp);
1451 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1452 if (!entry->gpe_internal)
1453 g_part_new_provider(gp, table, entry);
1456 table->gpt_opened = 0;
1457 g_access(cp, -1, -1, -1);
1462 gctl_error(req, "%d", error);
1467 g_part_wither(struct g_geom *gp, int error)
1469 struct g_part_entry *entry;
1470 struct g_part_table *table;
1473 if (table != NULL) {
1474 G_PART_DESTROY(table, NULL);
1475 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1476 LIST_REMOVE(entry, gpe_entry);
1479 if (gp->softc != NULL) {
1480 kobj_delete((kobj_t)gp->softc, M_GEOM);
1484 g_wither_geom(gp, error);
1492 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1494 struct g_part_parms gpp;
1495 struct g_part_table *table;
1496 struct gctl_req_arg *ap;
1497 enum g_part_ctl ctlreq;
1498 unsigned int i, mparms, oparms, parm;
1499 int auto_commit, close_on_error;
1500 int error, modifies;
1502 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1503 g_topology_assert();
1505 ctlreq = G_PART_CTL_NONE;
1508 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1511 if (!strcmp(verb, "add")) {
1512 ctlreq = G_PART_CTL_ADD;
1513 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1514 G_PART_PARM_START | G_PART_PARM_TYPE;
1515 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1519 if (!strcmp(verb, "bootcode")) {
1520 ctlreq = G_PART_CTL_BOOTCODE;
1521 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1525 if (!strcmp(verb, "commit")) {
1526 ctlreq = G_PART_CTL_COMMIT;
1527 mparms |= G_PART_PARM_GEOM;
1529 } else if (!strcmp(verb, "create")) {
1530 ctlreq = G_PART_CTL_CREATE;
1531 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1532 oparms |= G_PART_PARM_ENTRIES;
1536 if (!strcmp(verb, "delete")) {
1537 ctlreq = G_PART_CTL_DELETE;
1538 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1539 } else if (!strcmp(verb, "destroy")) {
1540 ctlreq = G_PART_CTL_DESTROY;
1541 mparms |= G_PART_PARM_GEOM;
1542 oparms |= G_PART_PARM_FORCE;
1546 if (!strcmp(verb, "modify")) {
1547 ctlreq = G_PART_CTL_MODIFY;
1548 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1549 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1550 } else if (!strcmp(verb, "move")) {
1551 ctlreq = G_PART_CTL_MOVE;
1552 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1556 if (!strcmp(verb, "recover")) {
1557 ctlreq = G_PART_CTL_RECOVER;
1558 mparms |= G_PART_PARM_GEOM;
1559 } else if (!strcmp(verb, "resize")) {
1560 ctlreq = G_PART_CTL_RESIZE;
1561 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX |
1566 if (!strcmp(verb, "set")) {
1567 ctlreq = G_PART_CTL_SET;
1568 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1573 if (!strcmp(verb, "undo")) {
1574 ctlreq = G_PART_CTL_UNDO;
1575 mparms |= G_PART_PARM_GEOM;
1577 } else if (!strcmp(verb, "unset")) {
1578 ctlreq = G_PART_CTL_UNSET;
1579 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1584 if (ctlreq == G_PART_CTL_NONE) {
1585 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1589 bzero(&gpp, sizeof(gpp));
1590 for (i = 0; i < req->narg; i++) {
1593 switch (ap->name[0]) {
1595 if (!strcmp(ap->name, "arg0")) {
1597 (G_PART_PARM_GEOM | G_PART_PARM_PROVIDER);
1599 if (!strcmp(ap->name, "attrib"))
1600 parm = G_PART_PARM_ATTRIB;
1603 if (!strcmp(ap->name, "bootcode"))
1604 parm = G_PART_PARM_BOOTCODE;
1607 if (!strcmp(ap->name, "class"))
1611 if (!strcmp(ap->name, "entries"))
1612 parm = G_PART_PARM_ENTRIES;
1615 if (!strcmp(ap->name, "flags"))
1616 parm = G_PART_PARM_FLAGS;
1617 else if (!strcmp(ap->name, "force"))
1618 parm = G_PART_PARM_FORCE;
1621 if (!strcmp(ap->name, "index"))
1622 parm = G_PART_PARM_INDEX;
1625 if (!strcmp(ap->name, "label"))
1626 parm = G_PART_PARM_LABEL;
1629 if (!strcmp(ap->name, "output"))
1630 parm = G_PART_PARM_OUTPUT;
1633 if (!strcmp(ap->name, "scheme"))
1634 parm = G_PART_PARM_SCHEME;
1635 else if (!strcmp(ap->name, "size"))
1636 parm = G_PART_PARM_SIZE;
1637 else if (!strcmp(ap->name, "start"))
1638 parm = G_PART_PARM_START;
1641 if (!strcmp(ap->name, "type"))
1642 parm = G_PART_PARM_TYPE;
1645 if (!strcmp(ap->name, "verb"))
1647 else if (!strcmp(ap->name, "version"))
1648 parm = G_PART_PARM_VERSION;
1651 if ((parm & (mparms | oparms)) == 0) {
1652 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1656 case G_PART_PARM_ATTRIB:
1657 error = g_part_parm_str(req, ap->name,
1660 case G_PART_PARM_BOOTCODE:
1661 error = g_part_parm_bootcode(req, ap->name,
1662 &gpp.gpp_codeptr, &gpp.gpp_codesize);
1664 case G_PART_PARM_ENTRIES:
1665 error = g_part_parm_intmax(req, ap->name,
1668 case G_PART_PARM_FLAGS:
1669 error = g_part_parm_str(req, ap->name, &gpp.gpp_flags);
1671 case G_PART_PARM_FORCE:
1672 error = g_part_parm_uint32(req, ap->name,
1675 case G_PART_PARM_GEOM:
1676 error = g_part_parm_geom(req, ap->name, &gpp.gpp_geom);
1678 case G_PART_PARM_INDEX:
1679 error = g_part_parm_intmax(req, ap->name,
1682 case G_PART_PARM_LABEL:
1683 error = g_part_parm_str(req, ap->name, &gpp.gpp_label);
1685 case G_PART_PARM_OUTPUT:
1686 error = 0; /* Write-only parameter */
1688 case G_PART_PARM_PROVIDER:
1689 error = g_part_parm_provider(req, ap->name,
1692 case G_PART_PARM_SCHEME:
1693 error = g_part_parm_scheme(req, ap->name,
1696 case G_PART_PARM_SIZE:
1697 error = g_part_parm_quad(req, ap->name, &gpp.gpp_size);
1699 case G_PART_PARM_START:
1700 error = g_part_parm_quad(req, ap->name,
1703 case G_PART_PARM_TYPE:
1704 error = g_part_parm_str(req, ap->name, &gpp.gpp_type);
1706 case G_PART_PARM_VERSION:
1707 error = g_part_parm_uint32(req, ap->name,
1712 gctl_error(req, "%d %s", error, ap->name);
1716 if (error == ENOATTR) {
1717 gctl_error(req, "%d param '%s'", error,
1722 gpp.gpp_parms |= parm;
1724 if ((gpp.gpp_parms & mparms) != mparms) {
1725 parm = mparms - (gpp.gpp_parms & mparms);
1726 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1730 /* Obtain permissions if possible/necessary. */
1733 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1734 table = gpp.gpp_geom->softc;
1735 if (table != NULL && table->gpt_corrupt &&
1736 ctlreq != G_PART_CTL_DESTROY &&
1737 ctlreq != G_PART_CTL_RECOVER) {
1738 gctl_error(req, "%d table '%s' is corrupt",
1739 EPERM, gpp.gpp_geom->name);
1742 if (table != NULL && !table->gpt_opened) {
1743 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1746 gctl_error(req, "%d geom '%s'", error,
1747 gpp.gpp_geom->name);
1750 table->gpt_opened = 1;
1755 /* Allow the scheme to check or modify the parameters. */
1756 if (table != NULL) {
1757 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1759 gctl_error(req, "%d pre-check failed", error);
1763 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1766 case G_PART_CTL_NONE:
1767 panic("%s", __func__);
1768 case G_PART_CTL_ADD:
1769 error = g_part_ctl_add(req, &gpp);
1771 case G_PART_CTL_BOOTCODE:
1772 error = g_part_ctl_bootcode(req, &gpp);
1774 case G_PART_CTL_COMMIT:
1775 error = g_part_ctl_commit(req, &gpp);
1777 case G_PART_CTL_CREATE:
1778 error = g_part_ctl_create(req, &gpp);
1780 case G_PART_CTL_DELETE:
1781 error = g_part_ctl_delete(req, &gpp);
1783 case G_PART_CTL_DESTROY:
1784 error = g_part_ctl_destroy(req, &gpp);
1786 case G_PART_CTL_MODIFY:
1787 error = g_part_ctl_modify(req, &gpp);
1789 case G_PART_CTL_MOVE:
1790 error = g_part_ctl_move(req, &gpp);
1792 case G_PART_CTL_RECOVER:
1793 error = g_part_ctl_recover(req, &gpp);
1795 case G_PART_CTL_RESIZE:
1796 error = g_part_ctl_resize(req, &gpp);
1798 case G_PART_CTL_SET:
1799 error = g_part_ctl_setunset(req, &gpp, 1);
1801 case G_PART_CTL_UNDO:
1802 error = g_part_ctl_undo(req, &gpp);
1804 case G_PART_CTL_UNSET:
1805 error = g_part_ctl_setunset(req, &gpp, 0);
1809 /* Implement automatic commit. */
1811 auto_commit = (modifies &&
1812 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1813 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1815 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, ("%s",
1817 error = g_part_ctl_commit(req, &gpp);
1822 if (error && close_on_error) {
1823 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1824 table->gpt_opened = 0;
1829 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1833 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1834 g_topology_assert();
1836 g_part_wither(gp, EINVAL);
1840 static struct g_geom *
1841 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1843 struct g_consumer *cp;
1845 struct g_part_entry *entry;
1846 struct g_part_table *table;
1847 struct root_hold_token *rht;
1851 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1852 g_topology_assert();
1854 /* Skip providers that are already open for writing. */
1859 * Create a GEOM with consumer and hook it up to the provider.
1860 * With that we become part of the topology. Optain read access
1863 gp = g_new_geomf(mp, "%s", pp->name);
1864 cp = g_new_consumer(gp);
1865 error = g_attach(cp, pp);
1867 error = g_access(cp, 1, 0, 0);
1869 g_part_wither(gp, error);
1873 rht = root_mount_hold(mp->name);
1874 g_topology_unlock();
1877 * Short-circuit the whole probing galore when there's no
1880 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1885 /* Make sure we can nest and if so, determine our depth. */
1886 error = g_getattr("PART::isleaf", cp, &attr);
1887 if (!error && attr) {
1891 error = g_getattr("PART::depth", cp, &attr);
1892 depth = (!error) ? attr + 1 : 0;
1894 error = g_part_probe(gp, cp, depth);
1901 * Synthesize a disk geometry. Some partitioning schemes
1902 * depend on it and since some file systems need it even
1903 * when the partitition scheme doesn't, we do it here in
1904 * scheme-independent code.
1906 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1908 error = G_PART_READ(table, cp);
1911 error = g_part_check_integrity(table, cp);
1916 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1917 if (!entry->gpe_internal)
1918 g_part_new_provider(gp, table, entry);
1921 root_mount_rel(rht);
1922 g_access(cp, -1, 0, 0);
1927 root_mount_rel(rht);
1928 g_access(cp, -1, 0, 0);
1929 g_part_wither(gp, error);
1938 g_part_access(struct g_provider *pp, int dr, int dw, int de)
1940 struct g_consumer *cp;
1942 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1945 cp = LIST_FIRST(&pp->geom->consumer);
1947 /* We always gain write-exclusive access. */
1948 return (g_access(cp, dr, dw, dw + de));
1952 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1953 struct g_consumer *cp, struct g_provider *pp)
1956 struct g_part_entry *entry;
1957 struct g_part_table *table;
1959 KASSERT(sb != NULL && gp != NULL, ("%s", __func__));
1962 if (indent == NULL) {
1963 KASSERT(cp == NULL && pp != NULL, ("%s", __func__));
1964 entry = pp->private;
1967 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
1968 (uintmax_t)entry->gpe_offset,
1969 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1971 * libdisk compatibility quirk - the scheme dumps the
1972 * slicer name and partition type in a way that is
1973 * compatible with libdisk. When libdisk is not used
1974 * anymore, this should go away.
1976 G_PART_DUMPCONF(table, entry, sb, indent);
1977 } else if (cp != NULL) { /* Consumer configuration. */
1978 KASSERT(pp == NULL, ("%s", __func__));
1980 } else if (pp != NULL) { /* Provider configuration. */
1981 entry = pp->private;
1984 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
1985 (uintmax_t)entry->gpe_start);
1986 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
1987 (uintmax_t)entry->gpe_end);
1988 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
1990 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1991 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1992 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
1993 (uintmax_t)entry->gpe_offset);
1994 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
1995 (uintmax_t)pp->mediasize);
1996 G_PART_DUMPCONF(table, entry, sb, indent);
1997 } else { /* Geom configuration. */
1998 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
1999 table->gpt_scheme->name);
2000 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
2001 table->gpt_entries);
2002 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
2003 (uintmax_t)table->gpt_first);
2004 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
2005 (uintmax_t)table->gpt_last);
2006 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
2007 table->gpt_sectors);
2008 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
2010 sbuf_printf(sb, "%s<state>%s</state>\n", indent,
2011 table->gpt_corrupt ? "CORRUPT": "OK");
2012 sbuf_printf(sb, "%s<modified>%s</modified>\n", indent,
2013 table->gpt_opened ? "true": "false");
2014 G_PART_DUMPCONF(table, NULL, sb, indent);
2019 g_part_orphan(struct g_consumer *cp)
2021 struct g_provider *pp;
2022 struct g_part_table *table;
2025 KASSERT(pp != NULL, ("%s", __func__));
2026 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
2027 g_topology_assert();
2029 KASSERT(pp->error != 0, ("%s", __func__));
2030 table = cp->geom->softc;
2031 if (table != NULL && table->gpt_opened)
2032 g_access(cp, -1, -1, -1);
2033 g_part_wither(cp->geom, pp->error);
2037 g_part_spoiled(struct g_consumer *cp)
2040 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2041 g_topology_assert();
2043 g_part_wither(cp->geom, ENXIO);
2047 g_part_start(struct bio *bp)
2050 struct g_consumer *cp;
2052 struct g_part_entry *entry;
2053 struct g_part_table *table;
2054 struct g_kerneldump *gkd;
2055 struct g_provider *pp;
2060 cp = LIST_FIRST(&gp->consumer);
2062 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
2065 entry = pp->private;
2066 if (entry == NULL) {
2067 g_io_deliver(bp, ENXIO);
2071 switch(bp->bio_cmd) {
2075 if (bp->bio_offset >= pp->mediasize) {
2076 g_io_deliver(bp, EIO);
2079 bp2 = g_clone_bio(bp);
2081 g_io_deliver(bp, ENOMEM);
2084 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
2085 bp2->bio_length = pp->mediasize - bp2->bio_offset;
2086 bp2->bio_done = g_std_done;
2087 bp2->bio_offset += entry->gpe_offset;
2088 g_io_request(bp2, cp);
2093 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
2095 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
2097 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
2099 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
2101 if (g_handleattr_str(bp, "PART::scheme",
2102 table->gpt_scheme->name))
2104 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
2106 * Check that the partition is suitable for kernel
2107 * dumps. Typically only swap partitions should be
2110 if (!G_PART_DUMPTO(table, entry)) {
2111 g_io_deliver(bp, ENODEV);
2112 printf("GEOM_PART: Partition '%s' not suitable"
2113 " for kernel dumps (wrong type?)\n",
2117 gkd = (struct g_kerneldump *)bp->bio_data;
2118 if (gkd->offset >= pp->mediasize) {
2119 g_io_deliver(bp, EIO);
2122 if (gkd->offset + gkd->length > pp->mediasize)
2123 gkd->length = pp->mediasize - gkd->offset;
2124 gkd->offset += entry->gpe_offset;
2128 g_io_deliver(bp, EOPNOTSUPP);
2132 bp2 = g_clone_bio(bp);
2134 g_io_deliver(bp, ENOMEM);
2137 bp2->bio_done = g_std_done;
2138 g_io_request(bp2, cp);
2142 g_part_init(struct g_class *mp)
2145 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
2149 g_part_fini(struct g_class *mp)
2152 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
2156 g_part_unload_event(void *arg, int flag)
2158 struct g_consumer *cp;
2160 struct g_provider *pp;
2161 struct g_part_scheme *scheme;
2162 struct g_part_table *table;
2166 if (flag == EV_CANCEL)
2171 scheme = (void *)(*xchg);
2173 g_topology_assert();
2175 LIST_FOREACH(gp, &g_part_class.geom, geom) {
2177 if (table->gpt_scheme != scheme)
2181 LIST_FOREACH(pp, &gp->provider, provider)
2182 acc += pp->acr + pp->acw + pp->ace;
2183 LIST_FOREACH(cp, &gp->consumer, consumer)
2184 acc += cp->acr + cp->acw + cp->ace;
2187 g_part_wither(gp, ENOSYS);
2193 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
2199 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
2206 TAILQ_INSERT_TAIL(&g_part_schemes, scheme, scheme_list);
2208 error = g_retaste(&g_part_class);
2210 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
2213 arg = (uintptr_t)scheme;
2214 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,
2217 error = (arg == (uintptr_t)scheme) ? EDOOFUS : arg;