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 },
106 { "vmware-vmfs", G_PART_ALIAS_VMFS },
107 { "vmware-vmkdiag", G_PART_ALIAS_VMKDIAG },
108 { "vmware-reserved", G_PART_ALIAS_VMRESERVED },
111 SYSCTL_DECL(_kern_geom);
112 SYSCTL_NODE(_kern_geom, OID_AUTO, part, CTLFLAG_RW, 0,
114 static u_int check_integrity = 0;
115 TUNABLE_INT("kern.geom.part.check_integrity", &check_integrity);
116 SYSCTL_UINT(_kern_geom_part, OID_AUTO, check_integrity,
117 CTLFLAG_RW | CTLFLAG_TUN, &check_integrity, 0,
118 "Enable integrity checking");
121 * The GEOM partitioning class.
123 static g_ctl_req_t g_part_ctlreq;
124 static g_ctl_destroy_geom_t g_part_destroy_geom;
125 static g_fini_t g_part_fini;
126 static g_init_t g_part_init;
127 static g_taste_t g_part_taste;
129 static g_access_t g_part_access;
130 static g_dumpconf_t g_part_dumpconf;
131 static g_orphan_t g_part_orphan;
132 static g_spoiled_t g_part_spoiled;
133 static g_start_t g_part_start;
135 static struct g_class g_part_class = {
137 .version = G_VERSION,
139 .ctlreq = g_part_ctlreq,
140 .destroy_geom = g_part_destroy_geom,
143 .taste = g_part_taste,
145 .access = g_part_access,
146 .dumpconf = g_part_dumpconf,
147 .orphan = g_part_orphan,
148 .spoiled = g_part_spoiled,
149 .start = g_part_start,
152 DECLARE_GEOM_CLASS(g_part_class, g_part);
153 MODULE_VERSION(g_part, 0);
159 static void g_part_wither(struct g_geom *, int);
162 g_part_alias_name(enum g_part_alias alias)
166 for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
167 if (g_part_alias_list[i].alias != alias)
169 return (g_part_alias_list[i].lexeme);
176 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
179 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
180 off_t chs, cylinders;
186 for (idx = 0; candidate_heads[idx] != 0; idx++) {
187 heads = candidate_heads[idx];
188 cylinders = blocks / heads / sectors;
189 if (cylinders < heads || cylinders < sectors)
191 if (cylinders > 1023)
193 chs = cylinders * heads * sectors;
194 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
202 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
205 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
207 u_int heads, sectors;
210 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 || sectors == 0 ||
211 g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
212 table->gpt_fixgeom = 0;
213 table->gpt_heads = 0;
214 table->gpt_sectors = 0;
216 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
217 sectors = candidate_sectors[idx];
218 g_part_geometry_heads(blocks, sectors, &chs, &heads);
222 * Prefer a geometry with sectors > 1, but only if
223 * it doesn't bump down the numbver of heads to 1.
225 if (chs > bestchs || (chs == bestchs && heads > 1 &&
226 table->gpt_sectors == 1)) {
228 table->gpt_heads = heads;
229 table->gpt_sectors = sectors;
233 * If we didn't find a geometry at all, then the disk is
234 * too big. This means we can use the maximum number of
238 table->gpt_heads = 255;
239 table->gpt_sectors = 63;
242 table->gpt_fixgeom = 1;
243 table->gpt_heads = heads;
244 table->gpt_sectors = sectors;
248 #define DPRINTF(...) if (bootverbose) { \
249 printf("GEOM_PART: " __VA_ARGS__); \
253 g_part_check_integrity(struct g_part_table *table, struct g_consumer *cp)
255 struct g_part_entry *e1, *e2;
256 struct g_provider *pp;
262 if (table->gpt_last < table->gpt_first) {
263 DPRINTF("last LBA is below first LBA: %jd < %jd\n",
264 (intmax_t)table->gpt_last, (intmax_t)table->gpt_first);
267 if (table->gpt_last > pp->mediasize / pp->sectorsize - 1) {
268 DPRINTF("last LBA extends beyond mediasize: "
269 "%jd > %jd\n", (intmax_t)table->gpt_last,
270 (intmax_t)pp->mediasize / pp->sectorsize - 1);
273 LIST_FOREACH(e1, &table->gpt_entry, gpe_entry) {
274 if (e1->gpe_deleted || e1->gpe_internal)
276 if (e1->gpe_start < table->gpt_first) {
277 DPRINTF("partition %d has start offset below first "
278 "LBA: %jd < %jd\n", e1->gpe_index,
279 (intmax_t)e1->gpe_start,
280 (intmax_t)table->gpt_first);
283 if (e1->gpe_start > table->gpt_last) {
284 DPRINTF("partition %d has start offset beyond last "
285 "LBA: %jd > %jd\n", e1->gpe_index,
286 (intmax_t)e1->gpe_start,
287 (intmax_t)table->gpt_last);
290 if (e1->gpe_end < e1->gpe_start) {
291 DPRINTF("partition %d has end offset below start "
292 "offset: %jd < %jd\n", e1->gpe_index,
293 (intmax_t)e1->gpe_end,
294 (intmax_t)e1->gpe_start);
297 if (e1->gpe_end > table->gpt_last) {
298 DPRINTF("partition %d has end offset beyond last "
299 "LBA: %jd > %jd\n", e1->gpe_index,
300 (intmax_t)e1->gpe_end,
301 (intmax_t)table->gpt_last);
304 if (pp->stripesize > 0) {
305 offset = e1->gpe_start * pp->sectorsize;
306 if (e1->gpe_offset > offset)
307 offset = e1->gpe_offset;
308 if ((offset + pp->stripeoffset) % pp->stripesize) {
309 DPRINTF("partition %d is not aligned on %u "
310 "bytes\n", e1->gpe_index, pp->stripesize);
311 /* Don't treat this as a critical failure */
315 while ((e2 = LIST_NEXT(e2, gpe_entry)) != NULL) {
316 if (e2->gpe_deleted || e2->gpe_internal)
318 if (e1->gpe_start >= e2->gpe_start &&
319 e1->gpe_start <= e2->gpe_end) {
320 DPRINTF("partition %d has start offset inside "
321 "partition %d: start[%d] %jd >= start[%d] "
322 "%jd <= end[%d] %jd\n",
323 e1->gpe_index, e2->gpe_index,
324 e2->gpe_index, (intmax_t)e2->gpe_start,
325 e1->gpe_index, (intmax_t)e1->gpe_start,
326 e2->gpe_index, (intmax_t)e2->gpe_end);
329 if (e1->gpe_end >= e2->gpe_start &&
330 e1->gpe_end <= e2->gpe_end) {
331 DPRINTF("partition %d has end offset inside "
332 "partition %d: start[%d] %jd >= end[%d] "
333 "%jd <= end[%d] %jd\n",
334 e1->gpe_index, e2->gpe_index,
335 e2->gpe_index, (intmax_t)e2->gpe_start,
336 e1->gpe_index, (intmax_t)e1->gpe_end,
337 e2->gpe_index, (intmax_t)e2->gpe_end);
340 if (e1->gpe_start < e2->gpe_start &&
341 e1->gpe_end > e2->gpe_end) {
342 DPRINTF("partition %d contains partition %d: "
343 "start[%d] %jd > start[%d] %jd, end[%d] "
344 "%jd < end[%d] %jd\n",
345 e1->gpe_index, e2->gpe_index,
346 e1->gpe_index, (intmax_t)e1->gpe_start,
347 e2->gpe_index, (intmax_t)e2->gpe_start,
348 e2->gpe_index, (intmax_t)e2->gpe_end,
349 e1->gpe_index, (intmax_t)e1->gpe_end);
355 printf("GEOM_PART: integrity check failed (%s, %s)\n",
356 pp->name, table->gpt_scheme->name);
357 if (check_integrity != 0)
359 table->gpt_corrupt = 1;
365 struct g_part_entry *
366 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
369 struct g_part_entry *entry, *last;
372 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
373 if (entry->gpe_index == index)
375 if (entry->gpe_index > index) {
382 entry = g_malloc(table->gpt_scheme->gps_entrysz,
384 entry->gpe_index = index;
386 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
388 LIST_INSERT_AFTER(last, entry, gpe_entry);
390 entry->gpe_offset = 0;
391 entry->gpe_start = start;
392 entry->gpe_end = end;
397 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
398 struct g_part_entry *entry)
400 struct g_consumer *cp;
401 struct g_provider *pp;
405 cp = LIST_FIRST(&gp->consumer);
408 offset = entry->gpe_start * pp->sectorsize;
409 if (entry->gpe_offset < offset)
410 entry->gpe_offset = offset;
412 if (entry->gpe_pp == NULL) {
413 sb = sbuf_new_auto();
414 G_PART_FULLNAME(table, entry, sb, gp->name);
416 entry->gpe_pp = g_new_providerf(gp, "%s", sbuf_data(sb));
418 entry->gpe_pp->private = entry; /* Close the circle. */
420 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */
421 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
423 entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
424 entry->gpe_pp->sectorsize = pp->sectorsize;
425 entry->gpe_pp->flags = pp->flags & G_PF_CANDELETE;
426 entry->gpe_pp->stripesize = pp->stripesize;
427 entry->gpe_pp->stripeoffset = pp->stripeoffset + entry->gpe_offset;
428 if (pp->stripesize > 0)
429 entry->gpe_pp->stripeoffset %= pp->stripesize;
430 g_error_provider(entry->gpe_pp, 0);
433 static struct g_geom*
434 g_part_find_geom(const char *name)
437 LIST_FOREACH(gp, &g_part_class.geom, geom) {
438 if (!strcmp(name, gp->name))
445 g_part_parm_geom(const char *name, struct g_geom **v)
449 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
450 name += strlen(_PATH_DEV);
451 gp = g_part_find_geom(name);
454 if ((gp->flags & G_GEOM_WITHER) != 0)
461 g_part_parm_provider(const char *name, struct g_provider **v)
463 struct g_provider *pp;
465 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
466 name += strlen(_PATH_DEV);
467 pp = g_provider_by_name(name);
475 g_part_parm_quad(const char *p, quad_t *v)
480 q = strtoq(p, &x, 0);
481 if (*x != '\0' || q < 0)
488 g_part_parm_scheme(const char *p, struct g_part_scheme **v)
490 struct g_part_scheme *s;
492 TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
493 if (s == &g_part_null_scheme)
495 if (!strcasecmp(s->name, p))
505 g_part_parm_str(const char *p, const char **v)
515 g_part_parm_uint(const char *p, u_int *v)
520 l = strtol(p, &x, 0);
521 if (*x != '\0' || l < 0 || l > INT_MAX)
523 *v = (unsigned int)l;
528 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
530 struct g_part_scheme *iter, *scheme;
531 struct g_part_table *table;
535 scheme = (table != NULL) ? table->gpt_scheme : NULL;
536 pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
539 if (pri > 0) { /* error */
544 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
545 if (iter == &g_part_null_scheme)
547 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
550 table->gpt_scheme = iter;
551 table->gpt_depth = depth;
552 probe = G_PART_PROBE(table, cp);
553 if (probe <= 0 && probe > pri) {
556 if (gp->softc != NULL)
557 kobj_delete((kobj_t)gp->softc, M_GEOM);
562 kobj_delete((kobj_t)table, M_GEOM);
566 return ((scheme == NULL) ? ENXIO : 0);
570 * Control request functions.
574 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
577 struct g_provider *pp;
578 struct g_part_entry *delent, *last, *entry;
579 struct g_part_table *table;
586 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
589 pp = LIST_FIRST(&gp->consumer)->provider;
591 end = gpp->gpp_start + gpp->gpp_size - 1;
593 if (gpp->gpp_start < table->gpt_first ||
594 gpp->gpp_start > table->gpt_last) {
595 gctl_error(req, "%d start '%jd'", EINVAL,
596 (intmax_t)gpp->gpp_start);
599 if (end < gpp->gpp_start || end > table->gpt_last) {
600 gctl_error(req, "%d size '%jd'", EINVAL,
601 (intmax_t)gpp->gpp_size);
604 if (gpp->gpp_index > table->gpt_entries) {
605 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
609 delent = last = NULL;
610 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
611 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
612 if (entry->gpe_deleted) {
613 if (entry->gpe_index == index)
617 if (entry->gpe_index == index)
618 index = entry->gpe_index + 1;
619 if (entry->gpe_index < index)
621 if (entry->gpe_internal)
623 if (gpp->gpp_start >= entry->gpe_start &&
624 gpp->gpp_start <= entry->gpe_end) {
625 gctl_error(req, "%d start '%jd'", ENOSPC,
626 (intmax_t)gpp->gpp_start);
629 if (end >= entry->gpe_start && end <= entry->gpe_end) {
630 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
633 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
634 gctl_error(req, "%d size '%jd'", ENOSPC,
635 (intmax_t)gpp->gpp_size);
639 if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
640 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
643 if (index > table->gpt_entries) {
644 gctl_error(req, "%d index '%d'", ENOSPC, index);
648 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
649 M_WAITOK | M_ZERO) : delent;
650 entry->gpe_index = index;
651 entry->gpe_start = gpp->gpp_start;
652 entry->gpe_end = end;
653 error = G_PART_ADD(table, entry, gpp);
655 gctl_error(req, "%d", error);
660 if (delent == NULL) {
662 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
664 LIST_INSERT_AFTER(last, entry, gpe_entry);
665 entry->gpe_created = 1;
667 entry->gpe_deleted = 0;
668 entry->gpe_modified = 1;
670 g_part_new_provider(gp, table, entry);
672 /* Provide feedback if so requested. */
673 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
674 sb = sbuf_new_auto();
675 G_PART_FULLNAME(table, entry, sb, gp->name);
676 if (pp->stripesize > 0 && entry->gpe_pp->stripeoffset != 0)
677 sbuf_printf(sb, " added, but partition is not "
678 "aligned on %u bytes\n", pp->stripesize);
680 sbuf_cat(sb, " added\n");
682 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
689 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
692 struct g_part_table *table;
697 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
701 sz = table->gpt_scheme->gps_bootcodesz;
706 if (gpp->gpp_codesize > sz) {
711 error = G_PART_BOOTCODE(table, gpp);
715 /* Provide feedback if so requested. */
716 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
717 sb = sbuf_new_auto();
718 sbuf_printf(sb, "bootcode written to %s\n", gp->name);
720 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
726 gctl_error(req, "%d", error);
731 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
733 struct g_consumer *cp;
735 struct g_provider *pp;
736 struct g_part_entry *entry, *tmp;
737 struct g_part_table *table;
742 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
746 if (!table->gpt_opened) {
747 gctl_error(req, "%d", EPERM);
753 cp = LIST_FIRST(&gp->consumer);
754 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
756 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
757 while (table->gpt_smhead != 0) {
758 i = ffs(table->gpt_smhead) - 1;
759 error = g_write_data(cp, i * pp->sectorsize, buf,
765 table->gpt_smhead &= ~(1 << i);
767 while (table->gpt_smtail != 0) {
768 i = ffs(table->gpt_smtail) - 1;
769 error = g_write_data(cp, pp->mediasize - (i + 1) *
770 pp->sectorsize, buf, pp->sectorsize);
775 table->gpt_smtail &= ~(1 << i);
780 if (table->gpt_scheme == &g_part_null_scheme) {
782 g_access(cp, -1, -1, -1);
783 g_part_wither(gp, ENXIO);
787 error = G_PART_WRITE(table, cp);
791 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
792 if (!entry->gpe_deleted) {
793 entry->gpe_created = 0;
794 entry->gpe_modified = 0;
797 LIST_REMOVE(entry, gpe_entry);
800 table->gpt_created = 0;
801 table->gpt_opened = 0;
804 g_access(cp, -1, -1, -1);
809 gctl_error(req, "%d", error);
814 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
816 struct g_consumer *cp;
818 struct g_provider *pp;
819 struct g_part_scheme *scheme;
820 struct g_part_table *null, *table;
824 pp = gpp->gpp_provider;
825 scheme = gpp->gpp_scheme;
826 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
829 /* Check that there isn't already a g_part geom on the provider. */
830 gp = g_part_find_geom(pp->name);
833 if (null->gpt_scheme != &g_part_null_scheme) {
834 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
840 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
841 (gpp->gpp_entries < scheme->gps_minent ||
842 gpp->gpp_entries > scheme->gps_maxent)) {
843 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
848 gp = g_new_geomf(&g_part_class, "%s", pp->name);
849 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
853 table->gpt_scheme = gpp->gpp_scheme;
854 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
855 gpp->gpp_entries : scheme->gps_minent;
856 LIST_INIT(&table->gpt_entry);
858 cp = g_new_consumer(gp);
859 error = g_attach(cp, pp);
861 error = g_access(cp, 1, 1, 1);
863 g_part_wither(gp, error);
864 gctl_error(req, "%d geom '%s'", error, pp->name);
867 table->gpt_opened = 1;
869 cp = LIST_FIRST(&gp->consumer);
870 table->gpt_opened = null->gpt_opened;
871 table->gpt_smhead = null->gpt_smhead;
872 table->gpt_smtail = null->gpt_smtail;
877 /* Make sure the provider has media. */
878 if (pp->mediasize == 0 || pp->sectorsize == 0) {
883 /* Make sure we can nest and if so, determine our depth. */
884 error = g_getattr("PART::isleaf", cp, &attr);
885 if (!error && attr) {
889 error = g_getattr("PART::depth", cp, &attr);
890 table->gpt_depth = (!error) ? attr + 1 : 0;
893 * Synthesize a disk geometry. Some partitioning schemes
894 * depend on it and since some file systems need it even
895 * when the partitition scheme doesn't, we do it here in
896 * scheme-independent code.
898 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
900 error = G_PART_CREATE(table, gpp);
906 table->gpt_created = 1;
908 kobj_delete((kobj_t)null, M_GEOM);
911 * Support automatic commit by filling in the gpp_geom
914 gpp->gpp_parms |= G_PART_PARM_GEOM;
917 /* Provide feedback if so requested. */
918 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
919 sb = sbuf_new_auto();
920 sbuf_printf(sb, "%s created\n", gp->name);
922 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
930 g_access(cp, -1, -1, -1);
931 g_part_wither(gp, error);
933 kobj_delete((kobj_t)gp->softc, M_GEOM);
936 gctl_error(req, "%d provider", error);
941 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
944 struct g_provider *pp;
945 struct g_part_entry *entry;
946 struct g_part_table *table;
950 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
955 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
956 if (entry->gpe_deleted || entry->gpe_internal)
958 if (entry->gpe_index == gpp->gpp_index)
962 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
968 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
969 gctl_error(req, "%d", EBUSY);
974 entry->gpe_pp = NULL;
978 g_wither_provider(pp, ENXIO);
980 /* Provide feedback if so requested. */
981 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
982 sb = sbuf_new_auto();
983 G_PART_FULLNAME(table, entry, sb, gp->name);
984 sbuf_cat(sb, " deleted\n");
986 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
990 if (entry->gpe_created) {
991 LIST_REMOVE(entry, gpe_entry);
994 entry->gpe_modified = 0;
995 entry->gpe_deleted = 1;
1001 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
1003 struct g_consumer *cp;
1005 struct g_provider *pp;
1006 struct g_part_entry *entry, *tmp;
1007 struct g_part_table *null, *table;
1012 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1013 g_topology_assert();
1016 /* Check for busy providers. */
1017 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1018 if (entry->gpe_deleted || entry->gpe_internal)
1020 if (gpp->gpp_force) {
1024 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1027 gctl_error(req, "%d", EBUSY);
1031 if (gpp->gpp_force) {
1032 /* Destroy all providers. */
1033 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1037 g_wither_provider(pp, ENXIO);
1039 LIST_REMOVE(entry, gpe_entry);
1044 error = G_PART_DESTROY(table, gpp);
1046 gctl_error(req, "%d", error);
1050 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
1054 null->gpt_scheme = &g_part_null_scheme;
1055 LIST_INIT(&null->gpt_entry);
1057 cp = LIST_FIRST(&gp->consumer);
1059 null->gpt_last = pp->mediasize / pp->sectorsize - 1;
1061 null->gpt_depth = table->gpt_depth;
1062 null->gpt_opened = table->gpt_opened;
1063 null->gpt_smhead = table->gpt_smhead;
1064 null->gpt_smtail = table->gpt_smtail;
1066 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1067 LIST_REMOVE(entry, gpe_entry);
1070 kobj_delete((kobj_t)table, M_GEOM);
1072 /* Provide feedback if so requested. */
1073 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1074 sb = sbuf_new_auto();
1075 sbuf_printf(sb, "%s destroyed\n", gp->name);
1077 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1084 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
1087 struct g_part_entry *entry;
1088 struct g_part_table *table;
1093 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1094 g_topology_assert();
1098 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1099 if (entry->gpe_deleted || entry->gpe_internal)
1101 if (entry->gpe_index == gpp->gpp_index)
1104 if (entry == NULL) {
1105 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1109 error = G_PART_MODIFY(table, entry, gpp);
1111 gctl_error(req, "%d", error);
1115 if (!entry->gpe_created)
1116 entry->gpe_modified = 1;
1118 /* Provide feedback if so requested. */
1119 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1120 sb = sbuf_new_auto();
1121 G_PART_FULLNAME(table, entry, sb, gp->name);
1122 sbuf_cat(sb, " modified\n");
1124 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1131 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
1133 gctl_error(req, "%d verb 'move'", ENOSYS);
1138 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
1140 struct g_part_table *table;
1143 int error, recovered;
1146 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1147 g_topology_assert();
1149 error = recovered = 0;
1151 if (table->gpt_corrupt) {
1152 error = G_PART_RECOVER(table);
1154 error = g_part_check_integrity(table,
1155 LIST_FIRST(&gp->consumer));
1157 gctl_error(req, "%d recovering '%s' failed",
1163 /* Provide feedback if so requested. */
1164 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1165 sb = sbuf_new_auto();
1167 sbuf_printf(sb, "%s recovered\n", gp->name);
1169 sbuf_printf(sb, "%s recovering is not needed\n",
1172 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1179 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
1182 struct g_provider *pp;
1183 struct g_part_entry *pe, *entry;
1184 struct g_part_table *table;
1190 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1191 g_topology_assert();
1194 /* check gpp_index */
1195 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1196 if (entry->gpe_deleted || entry->gpe_internal)
1198 if (entry->gpe_index == gpp->gpp_index)
1201 if (entry == NULL) {
1202 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1206 /* check gpp_size */
1207 end = entry->gpe_start + gpp->gpp_size - 1;
1208 if (gpp->gpp_size < 1 || end > table->gpt_last) {
1209 gctl_error(req, "%d size '%jd'", EINVAL,
1210 (intmax_t)gpp->gpp_size);
1214 LIST_FOREACH(pe, &table->gpt_entry, gpe_entry) {
1215 if (pe->gpe_deleted || pe->gpe_internal || pe == entry)
1217 if (end >= pe->gpe_start && end <= pe->gpe_end) {
1218 gctl_error(req, "%d end '%jd'", ENOSPC,
1222 if (entry->gpe_start < pe->gpe_start && end > pe->gpe_end) {
1223 gctl_error(req, "%d size '%jd'", ENOSPC,
1224 (intmax_t)gpp->gpp_size);
1230 if ((g_debugflags & 16) == 0 &&
1231 (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)) {
1232 gctl_error(req, "%d", EBUSY);
1236 error = G_PART_RESIZE(table, entry, gpp);
1238 gctl_error(req, "%d", error);
1242 if (!entry->gpe_created)
1243 entry->gpe_modified = 1;
1245 /* update mediasize of changed provider */
1246 pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
1249 /* Provide feedback if so requested. */
1250 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1251 sb = sbuf_new_auto();
1252 G_PART_FULLNAME(table, entry, sb, gp->name);
1253 sbuf_cat(sb, " resized\n");
1255 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1262 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
1266 struct g_part_entry *entry;
1267 struct g_part_table *table;
1272 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1273 g_topology_assert();
1277 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1278 if (entry->gpe_deleted || entry->gpe_internal)
1280 if (entry->gpe_index == gpp->gpp_index)
1283 if (entry == NULL) {
1284 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1288 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
1290 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
1294 /* Provide feedback if so requested. */
1295 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1296 sb = sbuf_new_auto();
1297 sbuf_printf(sb, "%s %sset on ", gpp->gpp_attrib,
1299 G_PART_FULLNAME(table, entry, sb, gp->name);
1300 sbuf_printf(sb, "\n");
1302 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1309 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1311 struct g_consumer *cp;
1312 struct g_provider *pp;
1314 struct g_part_entry *entry, *tmp;
1315 struct g_part_table *table;
1319 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1320 g_topology_assert();
1323 if (!table->gpt_opened) {
1324 gctl_error(req, "%d", EPERM);
1328 cp = LIST_FIRST(&gp->consumer);
1329 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1330 entry->gpe_modified = 0;
1331 if (entry->gpe_created) {
1335 entry->gpe_pp = NULL;
1336 g_wither_provider(pp, ENXIO);
1338 entry->gpe_deleted = 1;
1340 if (entry->gpe_deleted) {
1341 LIST_REMOVE(entry, gpe_entry);
1346 g_topology_unlock();
1348 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1349 table->gpt_created) ? 1 : 0;
1352 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1353 if (entry->gpe_internal)
1358 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1359 LIST_REMOVE(entry, gpe_entry);
1362 error = g_part_probe(gp, cp, table->gpt_depth);
1365 g_access(cp, -1, -1, -1);
1366 g_part_wither(gp, error);
1372 * Synthesize a disk geometry. Some partitioning schemes
1373 * depend on it and since some file systems need it even
1374 * when the partitition scheme doesn't, we do it here in
1375 * scheme-independent code.
1378 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1381 error = G_PART_READ(table, cp);
1384 error = g_part_check_integrity(table, cp);
1389 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1390 if (!entry->gpe_internal)
1391 g_part_new_provider(gp, table, entry);
1394 table->gpt_opened = 0;
1395 g_access(cp, -1, -1, -1);
1400 gctl_error(req, "%d", error);
1405 g_part_wither(struct g_geom *gp, int error)
1407 struct g_part_entry *entry;
1408 struct g_part_table *table;
1411 if (table != NULL) {
1412 G_PART_DESTROY(table, NULL);
1413 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1414 LIST_REMOVE(entry, gpe_entry);
1417 if (gp->softc != NULL) {
1418 kobj_delete((kobj_t)gp->softc, M_GEOM);
1422 g_wither_geom(gp, error);
1430 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1432 struct g_part_parms gpp;
1433 struct g_part_table *table;
1434 struct gctl_req_arg *ap;
1436 enum g_part_ctl ctlreq;
1437 unsigned int i, mparms, oparms, parm;
1438 int auto_commit, close_on_error;
1439 int error, len, modifies;
1441 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1442 g_topology_assert();
1444 ctlreq = G_PART_CTL_NONE;
1447 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1450 if (!strcmp(verb, "add")) {
1451 ctlreq = G_PART_CTL_ADD;
1452 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1453 G_PART_PARM_START | G_PART_PARM_TYPE;
1454 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1458 if (!strcmp(verb, "bootcode")) {
1459 ctlreq = G_PART_CTL_BOOTCODE;
1460 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1464 if (!strcmp(verb, "commit")) {
1465 ctlreq = G_PART_CTL_COMMIT;
1466 mparms |= G_PART_PARM_GEOM;
1468 } else if (!strcmp(verb, "create")) {
1469 ctlreq = G_PART_CTL_CREATE;
1470 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1471 oparms |= G_PART_PARM_ENTRIES;
1475 if (!strcmp(verb, "delete")) {
1476 ctlreq = G_PART_CTL_DELETE;
1477 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1478 } else if (!strcmp(verb, "destroy")) {
1479 ctlreq = G_PART_CTL_DESTROY;
1480 mparms |= G_PART_PARM_GEOM;
1481 oparms |= G_PART_PARM_FORCE;
1485 if (!strcmp(verb, "modify")) {
1486 ctlreq = G_PART_CTL_MODIFY;
1487 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1488 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1489 } else if (!strcmp(verb, "move")) {
1490 ctlreq = G_PART_CTL_MOVE;
1491 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1495 if (!strcmp(verb, "recover")) {
1496 ctlreq = G_PART_CTL_RECOVER;
1497 mparms |= G_PART_PARM_GEOM;
1498 } else if (!strcmp(verb, "resize")) {
1499 ctlreq = G_PART_CTL_RESIZE;
1500 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX |
1505 if (!strcmp(verb, "set")) {
1506 ctlreq = G_PART_CTL_SET;
1507 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1512 if (!strcmp(verb, "undo")) {
1513 ctlreq = G_PART_CTL_UNDO;
1514 mparms |= G_PART_PARM_GEOM;
1516 } else if (!strcmp(verb, "unset")) {
1517 ctlreq = G_PART_CTL_UNSET;
1518 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1523 if (ctlreq == G_PART_CTL_NONE) {
1524 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1528 bzero(&gpp, sizeof(gpp));
1529 for (i = 0; i < req->narg; i++) {
1532 switch (ap->name[0]) {
1534 if (!strcmp(ap->name, "attrib"))
1535 parm = G_PART_PARM_ATTRIB;
1538 if (!strcmp(ap->name, "bootcode"))
1539 parm = G_PART_PARM_BOOTCODE;
1542 if (!strcmp(ap->name, "class"))
1546 if (!strcmp(ap->name, "entries"))
1547 parm = G_PART_PARM_ENTRIES;
1550 if (!strcmp(ap->name, "flags"))
1551 parm = G_PART_PARM_FLAGS;
1552 else if (!strcmp(ap->name, "force"))
1553 parm = G_PART_PARM_FORCE;
1556 if (!strcmp(ap->name, "geom"))
1557 parm = G_PART_PARM_GEOM;
1560 if (!strcmp(ap->name, "index"))
1561 parm = G_PART_PARM_INDEX;
1564 if (!strcmp(ap->name, "label"))
1565 parm = G_PART_PARM_LABEL;
1568 if (!strcmp(ap->name, "output"))
1569 parm = G_PART_PARM_OUTPUT;
1572 if (!strcmp(ap->name, "provider"))
1573 parm = G_PART_PARM_PROVIDER;
1576 if (!strcmp(ap->name, "scheme"))
1577 parm = G_PART_PARM_SCHEME;
1578 else if (!strcmp(ap->name, "size"))
1579 parm = G_PART_PARM_SIZE;
1580 else if (!strcmp(ap->name, "start"))
1581 parm = G_PART_PARM_START;
1584 if (!strcmp(ap->name, "type"))
1585 parm = G_PART_PARM_TYPE;
1588 if (!strcmp(ap->name, "verb"))
1590 else if (!strcmp(ap->name, "version"))
1591 parm = G_PART_PARM_VERSION;
1594 if ((parm & (mparms | oparms)) == 0) {
1595 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1598 if (parm == G_PART_PARM_BOOTCODE)
1599 p = gctl_get_param(req, ap->name, &len);
1601 p = gctl_get_asciiparam(req, ap->name);
1603 gctl_error(req, "%d param '%s'", ENOATTR, ap->name);
1607 case G_PART_PARM_ATTRIB:
1608 error = g_part_parm_str(p, &gpp.gpp_attrib);
1610 case G_PART_PARM_BOOTCODE:
1611 gpp.gpp_codeptr = p;
1612 gpp.gpp_codesize = len;
1615 case G_PART_PARM_ENTRIES:
1616 error = g_part_parm_uint(p, &gpp.gpp_entries);
1618 case G_PART_PARM_FLAGS:
1621 error = g_part_parm_str(p, &gpp.gpp_flags);
1623 case G_PART_PARM_FORCE:
1624 error = g_part_parm_uint(p, &gpp.gpp_force);
1626 case G_PART_PARM_GEOM:
1627 error = g_part_parm_geom(p, &gpp.gpp_geom);
1629 case G_PART_PARM_INDEX:
1630 error = g_part_parm_uint(p, &gpp.gpp_index);
1632 case G_PART_PARM_LABEL:
1633 /* An empty label is always valid. */
1637 case G_PART_PARM_OUTPUT:
1638 error = 0; /* Write-only parameter */
1640 case G_PART_PARM_PROVIDER:
1641 error = g_part_parm_provider(p, &gpp.gpp_provider);
1643 case G_PART_PARM_SCHEME:
1644 error = g_part_parm_scheme(p, &gpp.gpp_scheme);
1646 case G_PART_PARM_SIZE:
1647 error = g_part_parm_quad(p, &gpp.gpp_size);
1649 case G_PART_PARM_START:
1650 error = g_part_parm_quad(p, &gpp.gpp_start);
1652 case G_PART_PARM_TYPE:
1653 error = g_part_parm_str(p, &gpp.gpp_type);
1655 case G_PART_PARM_VERSION:
1656 error = g_part_parm_uint(p, &gpp.gpp_version);
1663 gctl_error(req, "%d %s '%s'", error, ap->name, p);
1666 gpp.gpp_parms |= parm;
1668 if ((gpp.gpp_parms & mparms) != mparms) {
1669 parm = mparms - (gpp.gpp_parms & mparms);
1670 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1674 /* Obtain permissions if possible/necessary. */
1677 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1678 table = gpp.gpp_geom->softc;
1679 if (table != NULL && table->gpt_corrupt &&
1680 ctlreq != G_PART_CTL_DESTROY &&
1681 ctlreq != G_PART_CTL_RECOVER) {
1682 gctl_error(req, "%d table '%s' is corrupt",
1683 EPERM, gpp.gpp_geom->name);
1686 if (table != NULL && !table->gpt_opened) {
1687 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1690 gctl_error(req, "%d geom '%s'", error,
1691 gpp.gpp_geom->name);
1694 table->gpt_opened = 1;
1699 /* Allow the scheme to check or modify the parameters. */
1700 if (table != NULL) {
1701 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1703 gctl_error(req, "%d pre-check failed", error);
1707 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1710 case G_PART_CTL_NONE:
1711 panic("%s", __func__);
1712 case G_PART_CTL_ADD:
1713 error = g_part_ctl_add(req, &gpp);
1715 case G_PART_CTL_BOOTCODE:
1716 error = g_part_ctl_bootcode(req, &gpp);
1718 case G_PART_CTL_COMMIT:
1719 error = g_part_ctl_commit(req, &gpp);
1721 case G_PART_CTL_CREATE:
1722 error = g_part_ctl_create(req, &gpp);
1724 case G_PART_CTL_DELETE:
1725 error = g_part_ctl_delete(req, &gpp);
1727 case G_PART_CTL_DESTROY:
1728 error = g_part_ctl_destroy(req, &gpp);
1730 case G_PART_CTL_MODIFY:
1731 error = g_part_ctl_modify(req, &gpp);
1733 case G_PART_CTL_MOVE:
1734 error = g_part_ctl_move(req, &gpp);
1736 case G_PART_CTL_RECOVER:
1737 error = g_part_ctl_recover(req, &gpp);
1739 case G_PART_CTL_RESIZE:
1740 error = g_part_ctl_resize(req, &gpp);
1742 case G_PART_CTL_SET:
1743 error = g_part_ctl_setunset(req, &gpp, 1);
1745 case G_PART_CTL_UNDO:
1746 error = g_part_ctl_undo(req, &gpp);
1748 case G_PART_CTL_UNSET:
1749 error = g_part_ctl_setunset(req, &gpp, 0);
1753 /* Implement automatic commit. */
1755 auto_commit = (modifies &&
1756 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1757 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1759 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, (__func__));
1760 error = g_part_ctl_commit(req, &gpp);
1765 if (error && close_on_error) {
1766 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1767 table->gpt_opened = 0;
1772 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1776 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1777 g_topology_assert();
1779 g_part_wither(gp, EINVAL);
1783 static struct g_geom *
1784 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1786 struct g_consumer *cp;
1788 struct g_part_entry *entry;
1789 struct g_part_table *table;
1790 struct root_hold_token *rht;
1794 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1795 g_topology_assert();
1797 /* Skip providers that are already open for writing. */
1802 * Create a GEOM with consumer and hook it up to the provider.
1803 * With that we become part of the topology. Optain read access
1806 gp = g_new_geomf(mp, "%s", pp->name);
1807 cp = g_new_consumer(gp);
1808 error = g_attach(cp, pp);
1810 error = g_access(cp, 1, 0, 0);
1814 g_destroy_consumer(cp);
1819 rht = root_mount_hold(mp->name);
1820 g_topology_unlock();
1823 * Short-circuit the whole probing galore when there's no
1826 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1831 /* Make sure we can nest and if so, determine our depth. */
1832 error = g_getattr("PART::isleaf", cp, &attr);
1833 if (!error && attr) {
1837 error = g_getattr("PART::depth", cp, &attr);
1838 depth = (!error) ? attr + 1 : 0;
1840 error = g_part_probe(gp, cp, depth);
1847 * Synthesize a disk geometry. Some partitioning schemes
1848 * depend on it and since some file systems need it even
1849 * when the partitition scheme doesn't, we do it here in
1850 * scheme-independent code.
1852 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1854 error = G_PART_READ(table, cp);
1857 error = g_part_check_integrity(table, cp);
1862 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1863 if (!entry->gpe_internal)
1864 g_part_new_provider(gp, table, entry);
1867 root_mount_rel(rht);
1868 g_access(cp, -1, 0, 0);
1873 root_mount_rel(rht);
1874 g_access(cp, -1, 0, 0);
1876 g_destroy_consumer(cp);
1886 g_part_access(struct g_provider *pp, int dr, int dw, int de)
1888 struct g_consumer *cp;
1890 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1893 cp = LIST_FIRST(&pp->geom->consumer);
1895 /* We always gain write-exclusive access. */
1896 return (g_access(cp, dr, dw, dw + de));
1900 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1901 struct g_consumer *cp, struct g_provider *pp)
1904 struct g_part_entry *entry;
1905 struct g_part_table *table;
1907 KASSERT(sb != NULL && gp != NULL, (__func__));
1910 if (indent == NULL) {
1911 KASSERT(cp == NULL && pp != NULL, (__func__));
1912 entry = pp->private;
1915 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
1916 (uintmax_t)entry->gpe_offset,
1917 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1919 * libdisk compatibility quirk - the scheme dumps the
1920 * slicer name and partition type in a way that is
1921 * compatible with libdisk. When libdisk is not used
1922 * anymore, this should go away.
1924 G_PART_DUMPCONF(table, entry, sb, indent);
1925 } else if (cp != NULL) { /* Consumer configuration. */
1926 KASSERT(pp == NULL, (__func__));
1928 } else if (pp != NULL) { /* Provider configuration. */
1929 entry = pp->private;
1932 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
1933 (uintmax_t)entry->gpe_start);
1934 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
1935 (uintmax_t)entry->gpe_end);
1936 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
1938 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1939 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1940 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
1941 (uintmax_t)entry->gpe_offset);
1942 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
1943 (uintmax_t)pp->mediasize);
1944 G_PART_DUMPCONF(table, entry, sb, indent);
1945 } else { /* Geom configuration. */
1946 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
1947 table->gpt_scheme->name);
1948 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
1949 table->gpt_entries);
1950 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
1951 (uintmax_t)table->gpt_first);
1952 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
1953 (uintmax_t)table->gpt_last);
1954 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
1955 table->gpt_sectors);
1956 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
1958 sbuf_printf(sb, "%s<state>%s</state>\n", indent,
1959 table->gpt_corrupt ? "CORRUPT": "OK");
1960 sbuf_printf(sb, "%s<modified>%s</modified>\n", indent,
1961 table->gpt_opened ? "true": "false");
1962 G_PART_DUMPCONF(table, NULL, sb, indent);
1967 g_part_orphan(struct g_consumer *cp)
1969 struct g_provider *pp;
1970 struct g_part_table *table;
1973 KASSERT(pp != NULL, (__func__));
1974 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
1975 g_topology_assert();
1977 KASSERT(pp->error != 0, (__func__));
1978 table = cp->geom->softc;
1979 if (table != NULL && table->gpt_opened)
1980 g_access(cp, -1, -1, -1);
1981 g_part_wither(cp->geom, pp->error);
1985 g_part_spoiled(struct g_consumer *cp)
1988 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
1989 g_topology_assert();
1991 g_part_wither(cp->geom, ENXIO);
1995 g_part_start(struct bio *bp)
1998 struct g_consumer *cp;
2000 struct g_part_entry *entry;
2001 struct g_part_table *table;
2002 struct g_kerneldump *gkd;
2003 struct g_provider *pp;
2008 cp = LIST_FIRST(&gp->consumer);
2010 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
2013 entry = pp->private;
2014 if (entry == NULL) {
2015 g_io_deliver(bp, ENXIO);
2019 switch(bp->bio_cmd) {
2023 if (bp->bio_offset >= pp->mediasize) {
2024 g_io_deliver(bp, EIO);
2027 bp2 = g_clone_bio(bp);
2029 g_io_deliver(bp, ENOMEM);
2032 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
2033 bp2->bio_length = pp->mediasize - bp2->bio_offset;
2034 bp2->bio_done = g_std_done;
2035 bp2->bio_offset += entry->gpe_offset;
2036 g_io_request(bp2, cp);
2041 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
2043 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
2045 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
2047 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
2049 if (g_handleattr_str(bp, "PART::scheme",
2050 table->gpt_scheme->name))
2052 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
2054 * Check that the partition is suitable for kernel
2055 * dumps. Typically only swap partitions should be
2056 * used. If the request comes from the nested scheme
2057 * we allow dumping there as well.
2059 if ((bp->bio_from == NULL ||
2060 bp->bio_from->geom->class != &g_part_class) &&
2061 G_PART_DUMPTO(table, entry) == 0) {
2062 g_io_deliver(bp, ENODEV);
2063 printf("GEOM_PART: Partition '%s' not suitable"
2064 " for kernel dumps (wrong type?)\n",
2068 gkd = (struct g_kerneldump *)bp->bio_data;
2069 if (gkd->offset >= pp->mediasize) {
2070 g_io_deliver(bp, EIO);
2073 if (gkd->offset + gkd->length > pp->mediasize)
2074 gkd->length = pp->mediasize - gkd->offset;
2075 gkd->offset += entry->gpe_offset;
2079 g_io_deliver(bp, EOPNOTSUPP);
2083 bp2 = g_clone_bio(bp);
2085 g_io_deliver(bp, ENOMEM);
2088 bp2->bio_done = g_std_done;
2089 g_io_request(bp2, cp);
2093 g_part_init(struct g_class *mp)
2096 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
2100 g_part_fini(struct g_class *mp)
2103 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
2107 g_part_unload_event(void *arg, int flag)
2109 struct g_consumer *cp;
2111 struct g_provider *pp;
2112 struct g_part_scheme *scheme;
2113 struct g_part_table *table;
2117 if (flag == EV_CANCEL)
2122 scheme = (void *)(*xchg);
2124 g_topology_assert();
2126 LIST_FOREACH(gp, &g_part_class.geom, geom) {
2128 if (table->gpt_scheme != scheme)
2132 LIST_FOREACH(pp, &gp->provider, provider)
2133 acc += pp->acr + pp->acw + pp->ace;
2134 LIST_FOREACH(cp, &gp->consumer, consumer)
2135 acc += cp->acr + cp->acw + cp->ace;
2138 g_part_wither(gp, ENOSYS);
2144 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
2150 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
2152 struct g_part_scheme *iter;
2159 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
2160 if (scheme == iter) {
2161 printf("GEOM_PART: scheme %s is already "
2162 "registered!\n", scheme->name);
2167 TAILQ_INSERT_TAIL(&g_part_schemes, scheme,
2169 g_retaste(&g_part_class);
2173 arg = (uintptr_t)scheme;
2174 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,