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/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-hfs", G_PART_ALIAS_APPLE_HFS },
73 { "efi", G_PART_ALIAS_EFI },
74 { "freebsd", G_PART_ALIAS_FREEBSD },
75 { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
76 { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
77 { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
78 { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
79 { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
80 { "mbr", G_PART_ALIAS_MBR }
84 * The GEOM partitioning class.
86 static g_ctl_req_t g_part_ctlreq;
87 static g_ctl_destroy_geom_t g_part_destroy_geom;
88 static g_fini_t g_part_fini;
89 static g_init_t g_part_init;
90 static g_taste_t g_part_taste;
92 static g_access_t g_part_access;
93 static g_dumpconf_t g_part_dumpconf;
94 static g_orphan_t g_part_orphan;
95 static g_spoiled_t g_part_spoiled;
96 static g_start_t g_part_start;
98 static struct g_class g_part_class = {
100 .version = G_VERSION,
102 .ctlreq = g_part_ctlreq,
103 .destroy_geom = g_part_destroy_geom,
106 .taste = g_part_taste,
108 .access = g_part_access,
109 .dumpconf = g_part_dumpconf,
110 .orphan = g_part_orphan,
111 .spoiled = g_part_spoiled,
112 .start = g_part_start,
115 DECLARE_GEOM_CLASS(g_part_class, g_part);
121 static void g_part_wither(struct g_geom *, int);
124 g_part_alias_name(enum g_part_alias alias)
128 for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
129 if (g_part_alias_list[i].alias != alias)
131 return (g_part_alias_list[i].lexeme);
138 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
141 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
142 off_t chs, cylinders;
148 for (idx = 0; candidate_heads[idx] != 0; idx++) {
149 heads = candidate_heads[idx];
150 cylinders = blocks / heads / sectors;
151 if (cylinders < heads || cylinders < sectors)
153 if (cylinders > 1023)
155 chs = cylinders * heads * sectors;
156 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
164 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
167 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
169 u_int heads, sectors;
172 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 || sectors == 0 ||
173 g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
174 table->gpt_fixgeom = 0;
175 table->gpt_heads = 0;
176 table->gpt_sectors = 0;
178 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
179 sectors = candidate_sectors[idx];
180 g_part_geometry_heads(blocks, sectors, &chs, &heads);
184 * Prefer a geometry with sectors > 1, but only if
185 * it doesn't bump down the numbver of heads to 1.
187 if (chs > bestchs || (chs == bestchs && heads > 1 &&
188 table->gpt_sectors == 1)) {
190 table->gpt_heads = heads;
191 table->gpt_sectors = sectors;
195 * If we didn't find a geometry at all, then the disk is
196 * too big. This means we can use the maximum number of
200 table->gpt_heads = 255;
201 table->gpt_sectors = 63;
204 table->gpt_fixgeom = 1;
205 table->gpt_heads = heads;
206 table->gpt_sectors = sectors;
210 struct g_part_entry *
211 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
214 struct g_part_entry *entry, *last;
217 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
218 if (entry->gpe_index == index)
220 if (entry->gpe_index > index) {
227 entry = g_malloc(table->gpt_scheme->gps_entrysz,
229 entry->gpe_index = index;
231 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
233 LIST_INSERT_AFTER(last, entry, gpe_entry);
235 entry->gpe_offset = 0;
236 entry->gpe_start = start;
237 entry->gpe_end = end;
242 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
243 struct g_part_entry *entry)
245 struct g_consumer *cp;
246 struct g_provider *pp;
250 cp = LIST_FIRST(&gp->consumer);
253 offset = entry->gpe_start * pp->sectorsize;
254 if (entry->gpe_offset < offset)
255 entry->gpe_offset = offset;
257 if (entry->gpe_pp == NULL) {
258 sb = sbuf_new_auto();
259 G_PART_FULLNAME(table, entry, sb, gp->name);
261 entry->gpe_pp = g_new_providerf(gp, "%s", sbuf_data(sb));
263 entry->gpe_pp->private = entry; /* Close the circle. */
265 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */
266 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
268 entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
269 entry->gpe_pp->sectorsize = pp->sectorsize;
270 entry->gpe_pp->flags = pp->flags & G_PF_CANDELETE;
271 if (pp->stripesize > 0) {
272 entry->gpe_pp->stripesize = pp->stripesize;
273 entry->gpe_pp->stripeoffset = (pp->stripeoffset +
274 entry->gpe_offset) % pp->stripesize;
276 g_error_provider(entry->gpe_pp, 0);
280 g_part_parm_geom(const char *rawname, struct g_geom **v)
285 if (strncmp(rawname, _PATH_DEV, strlen(_PATH_DEV)) == 0)
286 pname = rawname + strlen(_PATH_DEV);
289 LIST_FOREACH(gp, &g_part_class.geom, geom) {
290 if (!strcmp(pname, gp->name))
300 g_part_parm_provider(const char *pname, struct g_provider **v)
302 struct g_provider *pp;
304 if (strncmp(pname, _PATH_DEV, strlen(_PATH_DEV)) == 0)
305 pp = g_provider_by_name(pname + strlen(_PATH_DEV));
307 pp = g_provider_by_name(pname);
315 g_part_parm_quad(const char *p, quad_t *v)
320 q = strtoq(p, &x, 0);
321 if (*x != '\0' || q < 0)
328 g_part_parm_scheme(const char *p, struct g_part_scheme **v)
330 struct g_part_scheme *s;
332 TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
333 if (s == &g_part_null_scheme)
335 if (!strcasecmp(s->name, p))
345 g_part_parm_str(const char *p, const char **v)
355 g_part_parm_uint(const char *p, u_int *v)
360 l = strtol(p, &x, 0);
361 if (*x != '\0' || l < 0 || l > INT_MAX)
363 *v = (unsigned int)l;
368 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
370 struct g_part_scheme *iter, *scheme;
371 struct g_part_table *table;
375 scheme = (table != NULL) ? table->gpt_scheme : NULL;
376 pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
379 if (pri > 0) { /* error */
384 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
385 if (iter == &g_part_null_scheme)
387 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
390 table->gpt_scheme = iter;
391 table->gpt_depth = depth;
392 probe = G_PART_PROBE(table, cp);
393 if (probe <= 0 && probe > pri) {
396 if (gp->softc != NULL)
397 kobj_delete((kobj_t)gp->softc, M_GEOM);
402 kobj_delete((kobj_t)table, M_GEOM);
406 return ((scheme == NULL) ? ENXIO : 0);
410 * Control request functions.
414 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
417 struct g_provider *pp;
418 struct g_part_entry *delent, *last, *entry;
419 struct g_part_table *table;
426 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
429 pp = LIST_FIRST(&gp->consumer)->provider;
431 end = gpp->gpp_start + gpp->gpp_size - 1;
433 if (gpp->gpp_start < table->gpt_first ||
434 gpp->gpp_start > table->gpt_last) {
435 gctl_error(req, "%d start '%jd'", EINVAL,
436 (intmax_t)gpp->gpp_start);
439 if (end < gpp->gpp_start || end > table->gpt_last) {
440 gctl_error(req, "%d size '%jd'", EINVAL,
441 (intmax_t)gpp->gpp_size);
444 if (gpp->gpp_index > table->gpt_entries) {
445 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
449 delent = last = NULL;
450 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
451 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
452 if (entry->gpe_deleted) {
453 if (entry->gpe_index == index)
457 if (entry->gpe_index == index)
458 index = entry->gpe_index + 1;
459 if (entry->gpe_index < index)
461 if (entry->gpe_internal)
463 if (gpp->gpp_start >= entry->gpe_start &&
464 gpp->gpp_start <= entry->gpe_end) {
465 gctl_error(req, "%d start '%jd'", ENOSPC,
466 (intmax_t)gpp->gpp_start);
469 if (end >= entry->gpe_start && end <= entry->gpe_end) {
470 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
473 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
474 gctl_error(req, "%d size '%jd'", ENOSPC,
475 (intmax_t)gpp->gpp_size);
479 if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
480 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
484 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
485 M_WAITOK | M_ZERO) : delent;
486 entry->gpe_index = index;
487 entry->gpe_start = gpp->gpp_start;
488 entry->gpe_end = end;
489 error = G_PART_ADD(table, entry, gpp);
491 gctl_error(req, "%d", error);
496 if (delent == NULL) {
498 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
500 LIST_INSERT_AFTER(last, entry, gpe_entry);
501 entry->gpe_created = 1;
503 entry->gpe_deleted = 0;
504 entry->gpe_modified = 1;
506 g_part_new_provider(gp, table, entry);
508 /* Provide feedback if so requested. */
509 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
510 sb = sbuf_new_auto();
511 G_PART_FULLNAME(table, entry, sb, gp->name);
512 sbuf_cat(sb, " added\n");
514 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
521 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
524 struct g_part_table *table;
529 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
533 sz = table->gpt_scheme->gps_bootcodesz;
538 if (gpp->gpp_codesize > sz) {
543 error = G_PART_BOOTCODE(table, gpp);
547 /* Provide feedback if so requested. */
548 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
549 sb = sbuf_new_auto();
550 sbuf_printf(sb, "%s has bootcode\n", gp->name);
552 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
558 gctl_error(req, "%d", error);
563 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
565 struct g_consumer *cp;
567 struct g_provider *pp;
568 struct g_part_entry *entry, *tmp;
569 struct g_part_table *table;
574 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
578 if (!table->gpt_opened) {
579 gctl_error(req, "%d", EPERM);
585 cp = LIST_FIRST(&gp->consumer);
586 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
588 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
589 while (table->gpt_smhead != 0) {
590 i = ffs(table->gpt_smhead) - 1;
591 error = g_write_data(cp, i * pp->sectorsize, buf,
597 table->gpt_smhead &= ~(1 << i);
599 while (table->gpt_smtail != 0) {
600 i = ffs(table->gpt_smtail) - 1;
601 error = g_write_data(cp, pp->mediasize - (i + 1) *
602 pp->sectorsize, buf, pp->sectorsize);
607 table->gpt_smtail &= ~(1 << i);
612 if (table->gpt_scheme == &g_part_null_scheme) {
614 g_access(cp, -1, -1, -1);
615 g_part_wither(gp, ENXIO);
619 error = G_PART_WRITE(table, cp);
623 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
624 if (!entry->gpe_deleted) {
625 entry->gpe_created = 0;
626 entry->gpe_modified = 0;
629 LIST_REMOVE(entry, gpe_entry);
632 table->gpt_created = 0;
633 table->gpt_opened = 0;
636 g_access(cp, -1, -1, -1);
641 gctl_error(req, "%d", error);
646 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
648 struct g_consumer *cp;
650 struct g_provider *pp;
651 struct g_part_scheme *scheme;
652 struct g_part_table *null, *table;
656 pp = gpp->gpp_provider;
657 scheme = gpp->gpp_scheme;
658 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
661 /* Check that there isn't already a g_part geom on the provider. */
662 error = g_part_parm_geom(pp->name, &gp);
665 if (null->gpt_scheme != &g_part_null_scheme) {
666 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
672 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
673 (gpp->gpp_entries < scheme->gps_minent ||
674 gpp->gpp_entries > scheme->gps_maxent)) {
675 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
680 gp = g_new_geomf(&g_part_class, "%s", pp->name);
681 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
685 table->gpt_scheme = gpp->gpp_scheme;
686 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
687 gpp->gpp_entries : scheme->gps_minent;
688 LIST_INIT(&table->gpt_entry);
690 cp = g_new_consumer(gp);
691 error = g_attach(cp, pp);
693 error = g_access(cp, 1, 1, 1);
695 g_part_wither(gp, error);
696 gctl_error(req, "%d geom '%s'", error, pp->name);
699 table->gpt_opened = 1;
701 cp = LIST_FIRST(&gp->consumer);
702 table->gpt_opened = null->gpt_opened;
703 table->gpt_smhead = null->gpt_smhead;
704 table->gpt_smtail = null->gpt_smtail;
709 /* Make sure the provider has media. */
710 if (pp->mediasize == 0 || pp->sectorsize == 0) {
715 /* Make sure we can nest and if so, determine our depth. */
716 error = g_getattr("PART::isleaf", cp, &attr);
717 if (!error && attr) {
721 error = g_getattr("PART::depth", cp, &attr);
722 table->gpt_depth = (!error) ? attr + 1 : 0;
725 * Synthesize a disk geometry. Some partitioning schemes
726 * depend on it and since some file systems need it even
727 * when the partitition scheme doesn't, we do it here in
728 * scheme-independent code.
730 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
732 error = G_PART_CREATE(table, gpp);
738 table->gpt_created = 1;
740 kobj_delete((kobj_t)null, M_GEOM);
743 * Support automatic commit by filling in the gpp_geom
746 gpp->gpp_parms |= G_PART_PARM_GEOM;
749 /* Provide feedback if so requested. */
750 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
751 sb = sbuf_new_auto();
752 sbuf_printf(sb, "%s created\n", gp->name);
754 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
762 g_access(cp, -1, -1, -1);
763 g_part_wither(gp, error);
765 kobj_delete((kobj_t)gp->softc, M_GEOM);
768 gctl_error(req, "%d provider", error);
773 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
776 struct g_provider *pp;
777 struct g_part_entry *entry;
778 struct g_part_table *table;
782 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
787 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
788 if (entry->gpe_deleted || entry->gpe_internal)
790 if (entry->gpe_index == gpp->gpp_index)
794 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
800 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
801 gctl_error(req, "%d", EBUSY);
806 entry->gpe_pp = NULL;
809 if (entry->gpe_created) {
810 LIST_REMOVE(entry, gpe_entry);
813 entry->gpe_modified = 0;
814 entry->gpe_deleted = 1;
818 g_wither_provider(pp, ENXIO);
820 /* Provide feedback if so requested. */
821 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
822 sb = sbuf_new_auto();
823 G_PART_FULLNAME(table, entry, sb, gp->name);
824 sbuf_cat(sb, " deleted\n");
826 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
833 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
836 struct g_part_entry *entry;
837 struct g_part_table *null, *table;
842 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
846 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
847 if (entry->gpe_deleted || entry->gpe_internal)
849 gctl_error(req, "%d", EBUSY);
853 error = G_PART_DESTROY(table, gpp);
855 gctl_error(req, "%d", error);
859 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
863 null->gpt_scheme = &g_part_null_scheme;
864 LIST_INIT(&null->gpt_entry);
865 null->gpt_depth = table->gpt_depth;
866 null->gpt_opened = table->gpt_opened;
867 null->gpt_smhead = table->gpt_smhead;
868 null->gpt_smtail = table->gpt_smtail;
870 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
871 LIST_REMOVE(entry, gpe_entry);
874 kobj_delete((kobj_t)table, M_GEOM);
876 /* Provide feedback if so requested. */
877 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
878 sb = sbuf_new_auto();
879 sbuf_printf(sb, "%s destroyed\n", gp->name);
881 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
888 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
891 struct g_part_entry *entry;
892 struct g_part_table *table;
897 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
902 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
903 if (entry->gpe_deleted || entry->gpe_internal)
905 if (entry->gpe_index == gpp->gpp_index)
909 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
913 error = G_PART_MODIFY(table, entry, gpp);
915 gctl_error(req, "%d", error);
919 if (!entry->gpe_created)
920 entry->gpe_modified = 1;
922 /* Provide feedback if so requested. */
923 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
924 sb = sbuf_new_auto();
925 G_PART_FULLNAME(table, entry, sb, gp->name);
926 sbuf_cat(sb, " modified\n");
928 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
935 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
937 gctl_error(req, "%d verb 'move'", ENOSYS);
942 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
944 gctl_error(req, "%d verb 'recover'", ENOSYS);
949 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
951 gctl_error(req, "%d verb 'resize'", ENOSYS);
956 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
960 struct g_part_entry *entry;
961 struct g_part_table *table;
966 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
971 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
972 if (entry->gpe_deleted || entry->gpe_internal)
974 if (entry->gpe_index == gpp->gpp_index)
978 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
982 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
984 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
988 /* Provide feedback if so requested. */
989 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
990 sb = sbuf_new_auto();
991 G_PART_FULLNAME(table, entry, sb, gp->name);
992 sbuf_printf(sb, " has %s %sset\n", gpp->gpp_attrib,
995 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1002 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1004 struct g_consumer *cp;
1005 struct g_provider *pp;
1007 struct g_part_entry *entry, *tmp;
1008 struct g_part_table *table;
1012 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1013 g_topology_assert();
1016 if (!table->gpt_opened) {
1017 gctl_error(req, "%d", EPERM);
1021 cp = LIST_FIRST(&gp->consumer);
1022 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1023 entry->gpe_modified = 0;
1024 if (entry->gpe_created) {
1028 entry->gpe_pp = NULL;
1029 g_wither_provider(pp, ENXIO);
1031 entry->gpe_deleted = 1;
1033 if (entry->gpe_deleted) {
1034 LIST_REMOVE(entry, gpe_entry);
1039 g_topology_unlock();
1041 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1042 table->gpt_created) ? 1 : 0;
1045 if (!LIST_EMPTY(&table->gpt_entry)) {
1049 error = g_part_probe(gp, cp, table->gpt_depth);
1052 g_access(cp, -1, -1, -1);
1053 g_part_wither(gp, error);
1059 error = G_PART_READ(table, cp);
1065 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1066 if (!entry->gpe_internal)
1067 g_part_new_provider(gp, table, entry);
1070 table->gpt_opened = 0;
1071 g_access(cp, -1, -1, -1);
1076 gctl_error(req, "%d", error);
1081 g_part_wither(struct g_geom *gp, int error)
1083 struct g_part_entry *entry;
1084 struct g_part_table *table;
1087 if (table != NULL) {
1088 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1089 LIST_REMOVE(entry, gpe_entry);
1092 if (gp->softc != NULL) {
1093 kobj_delete((kobj_t)gp->softc, M_GEOM);
1097 g_wither_geom(gp, error);
1105 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1107 struct g_part_parms gpp;
1108 struct g_part_table *table;
1109 struct gctl_req_arg *ap;
1111 enum g_part_ctl ctlreq;
1112 unsigned int i, mparms, oparms, parm;
1113 int auto_commit, close_on_error;
1114 int error, len, modifies;
1116 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1117 g_topology_assert();
1119 ctlreq = G_PART_CTL_NONE;
1122 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1125 if (!strcmp(verb, "add")) {
1126 ctlreq = G_PART_CTL_ADD;
1127 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1128 G_PART_PARM_START | G_PART_PARM_TYPE;
1129 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1133 if (!strcmp(verb, "bootcode")) {
1134 ctlreq = G_PART_CTL_BOOTCODE;
1135 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1139 if (!strcmp(verb, "commit")) {
1140 ctlreq = G_PART_CTL_COMMIT;
1141 mparms |= G_PART_PARM_GEOM;
1143 } else if (!strcmp(verb, "create")) {
1144 ctlreq = G_PART_CTL_CREATE;
1145 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1146 oparms |= G_PART_PARM_ENTRIES;
1150 if (!strcmp(verb, "delete")) {
1151 ctlreq = G_PART_CTL_DELETE;
1152 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1153 } else if (!strcmp(verb, "destroy")) {
1154 ctlreq = G_PART_CTL_DESTROY;
1155 mparms |= G_PART_PARM_GEOM;
1159 if (!strcmp(verb, "modify")) {
1160 ctlreq = G_PART_CTL_MODIFY;
1161 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1162 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1163 } else if (!strcmp(verb, "move")) {
1164 ctlreq = G_PART_CTL_MOVE;
1165 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1169 if (!strcmp(verb, "recover")) {
1170 ctlreq = G_PART_CTL_RECOVER;
1171 mparms |= G_PART_PARM_GEOM;
1172 } else if (!strcmp(verb, "resize")) {
1173 ctlreq = G_PART_CTL_RESIZE;
1174 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1178 if (!strcmp(verb, "set")) {
1179 ctlreq = G_PART_CTL_SET;
1180 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1185 if (!strcmp(verb, "undo")) {
1186 ctlreq = G_PART_CTL_UNDO;
1187 mparms |= G_PART_PARM_GEOM;
1189 } else if (!strcmp(verb, "unset")) {
1190 ctlreq = G_PART_CTL_UNSET;
1191 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1196 if (ctlreq == G_PART_CTL_NONE) {
1197 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1201 bzero(&gpp, sizeof(gpp));
1202 for (i = 0; i < req->narg; i++) {
1205 switch (ap->name[0]) {
1207 if (!strcmp(ap->name, "attrib"))
1208 parm = G_PART_PARM_ATTRIB;
1211 if (!strcmp(ap->name, "bootcode"))
1212 parm = G_PART_PARM_BOOTCODE;
1215 if (!strcmp(ap->name, "class"))
1219 if (!strcmp(ap->name, "entries"))
1220 parm = G_PART_PARM_ENTRIES;
1223 if (!strcmp(ap->name, "flags"))
1224 parm = G_PART_PARM_FLAGS;
1227 if (!strcmp(ap->name, "geom"))
1228 parm = G_PART_PARM_GEOM;
1231 if (!strcmp(ap->name, "index"))
1232 parm = G_PART_PARM_INDEX;
1235 if (!strcmp(ap->name, "label"))
1236 parm = G_PART_PARM_LABEL;
1239 if (!strcmp(ap->name, "output"))
1240 parm = G_PART_PARM_OUTPUT;
1243 if (!strcmp(ap->name, "provider"))
1244 parm = G_PART_PARM_PROVIDER;
1247 if (!strcmp(ap->name, "scheme"))
1248 parm = G_PART_PARM_SCHEME;
1249 else if (!strcmp(ap->name, "size"))
1250 parm = G_PART_PARM_SIZE;
1251 else if (!strcmp(ap->name, "start"))
1252 parm = G_PART_PARM_START;
1255 if (!strcmp(ap->name, "type"))
1256 parm = G_PART_PARM_TYPE;
1259 if (!strcmp(ap->name, "verb"))
1261 else if (!strcmp(ap->name, "version"))
1262 parm = G_PART_PARM_VERSION;
1265 if ((parm & (mparms | oparms)) == 0) {
1266 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1269 if (parm == G_PART_PARM_BOOTCODE)
1270 p = gctl_get_param(req, ap->name, &len);
1272 p = gctl_get_asciiparam(req, ap->name);
1274 gctl_error(req, "%d param '%s'", ENOATTR, ap->name);
1278 case G_PART_PARM_ATTRIB:
1279 error = g_part_parm_str(p, &gpp.gpp_attrib);
1281 case G_PART_PARM_BOOTCODE:
1282 gpp.gpp_codeptr = p;
1283 gpp.gpp_codesize = len;
1286 case G_PART_PARM_ENTRIES:
1287 error = g_part_parm_uint(p, &gpp.gpp_entries);
1289 case G_PART_PARM_FLAGS:
1292 error = g_part_parm_str(p, &gpp.gpp_flags);
1294 case G_PART_PARM_GEOM:
1295 error = g_part_parm_geom(p, &gpp.gpp_geom);
1297 case G_PART_PARM_INDEX:
1298 error = g_part_parm_uint(p, &gpp.gpp_index);
1300 case G_PART_PARM_LABEL:
1301 /* An empty label is always valid. */
1305 case G_PART_PARM_OUTPUT:
1306 error = 0; /* Write-only parameter */
1308 case G_PART_PARM_PROVIDER:
1309 error = g_part_parm_provider(p, &gpp.gpp_provider);
1311 case G_PART_PARM_SCHEME:
1312 error = g_part_parm_scheme(p, &gpp.gpp_scheme);
1314 case G_PART_PARM_SIZE:
1315 error = g_part_parm_quad(p, &gpp.gpp_size);
1317 case G_PART_PARM_START:
1318 error = g_part_parm_quad(p, &gpp.gpp_start);
1320 case G_PART_PARM_TYPE:
1321 error = g_part_parm_str(p, &gpp.gpp_type);
1323 case G_PART_PARM_VERSION:
1324 error = g_part_parm_uint(p, &gpp.gpp_version);
1331 gctl_error(req, "%d %s '%s'", error, ap->name, p);
1334 gpp.gpp_parms |= parm;
1336 if ((gpp.gpp_parms & mparms) != mparms) {
1337 parm = mparms - (gpp.gpp_parms & mparms);
1338 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1342 /* Obtain permissions if possible/necessary. */
1345 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1346 table = gpp.gpp_geom->softc;
1347 if (table != NULL && !table->gpt_opened) {
1348 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1351 gctl_error(req, "%d geom '%s'", error,
1352 gpp.gpp_geom->name);
1355 table->gpt_opened = 1;
1360 /* Allow the scheme to check or modify the parameters. */
1361 if (table != NULL) {
1362 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1364 gctl_error(req, "%d pre-check failed", error);
1368 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1371 case G_PART_CTL_NONE:
1372 panic("%s", __func__);
1373 case G_PART_CTL_ADD:
1374 error = g_part_ctl_add(req, &gpp);
1376 case G_PART_CTL_BOOTCODE:
1377 error = g_part_ctl_bootcode(req, &gpp);
1379 case G_PART_CTL_COMMIT:
1380 error = g_part_ctl_commit(req, &gpp);
1382 case G_PART_CTL_CREATE:
1383 error = g_part_ctl_create(req, &gpp);
1385 case G_PART_CTL_DELETE:
1386 error = g_part_ctl_delete(req, &gpp);
1388 case G_PART_CTL_DESTROY:
1389 error = g_part_ctl_destroy(req, &gpp);
1391 case G_PART_CTL_MODIFY:
1392 error = g_part_ctl_modify(req, &gpp);
1394 case G_PART_CTL_MOVE:
1395 error = g_part_ctl_move(req, &gpp);
1397 case G_PART_CTL_RECOVER:
1398 error = g_part_ctl_recover(req, &gpp);
1400 case G_PART_CTL_RESIZE:
1401 error = g_part_ctl_resize(req, &gpp);
1403 case G_PART_CTL_SET:
1404 error = g_part_ctl_setunset(req, &gpp, 1);
1406 case G_PART_CTL_UNDO:
1407 error = g_part_ctl_undo(req, &gpp);
1409 case G_PART_CTL_UNSET:
1410 error = g_part_ctl_setunset(req, &gpp, 0);
1414 /* Implement automatic commit. */
1416 auto_commit = (modifies &&
1417 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1418 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1420 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, (__func__));
1421 error = g_part_ctl_commit(req, &gpp);
1426 if (error && close_on_error) {
1427 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1428 table->gpt_opened = 0;
1433 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1437 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1438 g_topology_assert();
1440 g_part_wither(gp, EINVAL);
1444 static struct g_geom *
1445 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1447 struct g_consumer *cp;
1449 struct g_part_entry *entry;
1450 struct g_part_table *table;
1451 struct root_hold_token *rht;
1455 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1456 g_topology_assert();
1459 * Create a GEOM with consumer and hook it up to the provider.
1460 * With that we become part of the topology. Optain read access
1463 gp = g_new_geomf(mp, "%s", pp->name);
1464 cp = g_new_consumer(gp);
1465 error = g_attach(cp, pp);
1467 error = g_access(cp, 1, 0, 0);
1469 g_part_wither(gp, error);
1473 rht = root_mount_hold(mp->name);
1474 g_topology_unlock();
1477 * Short-circuit the whole probing galore when there's no
1480 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1485 /* Make sure we can nest and if so, determine our depth. */
1486 error = g_getattr("PART::isleaf", cp, &attr);
1487 if (!error && attr) {
1491 error = g_getattr("PART::depth", cp, &attr);
1492 depth = (!error) ? attr + 1 : 0;
1494 error = g_part_probe(gp, cp, depth);
1501 * Synthesize a disk geometry. Some partitioning schemes
1502 * depend on it and since some file systems need it even
1503 * when the partitition scheme doesn't, we do it here in
1504 * scheme-independent code.
1506 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1508 error = G_PART_READ(table, cp);
1513 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1514 if (!entry->gpe_internal)
1515 g_part_new_provider(gp, table, entry);
1518 root_mount_rel(rht);
1519 g_access(cp, -1, 0, 0);
1524 root_mount_rel(rht);
1525 g_access(cp, -1, 0, 0);
1526 g_part_wither(gp, error);
1535 g_part_access(struct g_provider *pp, int dr, int dw, int de)
1537 struct g_consumer *cp;
1539 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1542 cp = LIST_FIRST(&pp->geom->consumer);
1544 /* We always gain write-exclusive access. */
1545 return (g_access(cp, dr, dw, dw + de));
1549 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1550 struct g_consumer *cp, struct g_provider *pp)
1553 struct g_part_entry *entry;
1554 struct g_part_table *table;
1556 KASSERT(sb != NULL && gp != NULL, (__func__));
1559 if (indent == NULL) {
1560 KASSERT(cp == NULL && pp != NULL, (__func__));
1561 entry = pp->private;
1564 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
1565 (uintmax_t)entry->gpe_offset,
1566 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1568 * libdisk compatibility quirk - the scheme dumps the
1569 * slicer name and partition type in a way that is
1570 * compatible with libdisk. When libdisk is not used
1571 * anymore, this should go away.
1573 G_PART_DUMPCONF(table, entry, sb, indent);
1574 } else if (cp != NULL) { /* Consumer configuration. */
1575 KASSERT(pp == NULL, (__func__));
1577 } else if (pp != NULL) { /* Provider configuration. */
1578 entry = pp->private;
1581 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
1582 (uintmax_t)entry->gpe_start);
1583 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
1584 (uintmax_t)entry->gpe_end);
1585 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
1587 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1588 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1589 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
1590 (uintmax_t)entry->gpe_offset);
1591 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
1592 (uintmax_t)pp->mediasize);
1593 G_PART_DUMPCONF(table, entry, sb, indent);
1594 } else { /* Geom configuration. */
1595 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
1596 table->gpt_scheme->name);
1597 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
1598 table->gpt_entries);
1599 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
1600 (uintmax_t)table->gpt_first);
1601 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
1602 (uintmax_t)table->gpt_last);
1603 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
1604 table->gpt_sectors);
1605 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
1607 G_PART_DUMPCONF(table, NULL, sb, indent);
1612 g_part_orphan(struct g_consumer *cp)
1614 struct g_provider *pp;
1617 KASSERT(pp != NULL, (__func__));
1618 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
1619 g_topology_assert();
1621 KASSERT(pp->error != 0, (__func__));
1622 g_part_wither(cp->geom, pp->error);
1626 g_part_spoiled(struct g_consumer *cp)
1629 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
1630 g_topology_assert();
1632 g_part_wither(cp->geom, ENXIO);
1636 g_part_start(struct bio *bp)
1639 struct g_consumer *cp;
1641 struct g_part_entry *entry;
1642 struct g_part_table *table;
1643 struct g_kerneldump *gkd;
1644 struct g_provider *pp;
1649 cp = LIST_FIRST(&gp->consumer);
1651 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
1654 entry = pp->private;
1655 if (entry == NULL) {
1656 g_io_deliver(bp, ENXIO);
1660 switch(bp->bio_cmd) {
1664 if (bp->bio_offset >= pp->mediasize) {
1665 g_io_deliver(bp, EIO);
1668 bp2 = g_clone_bio(bp);
1670 g_io_deliver(bp, ENOMEM);
1673 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
1674 bp2->bio_length = pp->mediasize - bp2->bio_offset;
1675 bp2->bio_done = g_std_done;
1676 bp2->bio_offset += entry->gpe_offset;
1677 g_io_request(bp2, cp);
1682 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
1684 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
1686 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
1688 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
1690 if (g_handleattr_str(bp, "PART::scheme",
1691 table->gpt_scheme->name))
1693 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
1695 * Check that the partition is suitable for kernel
1696 * dumps. Typically only swap partitions should be
1699 if (!G_PART_DUMPTO(table, entry)) {
1700 g_io_deliver(bp, ENODEV);
1701 printf("GEOM_PART: Partition '%s' not suitable"
1702 " for kernel dumps (wrong type?)\n",
1706 gkd = (struct g_kerneldump *)bp->bio_data;
1707 if (gkd->offset >= pp->mediasize) {
1708 g_io_deliver(bp, EIO);
1711 if (gkd->offset + gkd->length > pp->mediasize)
1712 gkd->length = pp->mediasize - gkd->offset;
1713 gkd->offset += entry->gpe_offset;
1717 g_io_deliver(bp, EOPNOTSUPP);
1721 bp2 = g_clone_bio(bp);
1723 g_io_deliver(bp, ENOMEM);
1726 bp2->bio_done = g_std_done;
1727 g_io_request(bp2, cp);
1731 g_part_init(struct g_class *mp)
1734 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
1738 g_part_fini(struct g_class *mp)
1741 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
1745 g_part_unload_event(void *arg, int flag)
1747 struct g_consumer *cp;
1749 struct g_provider *pp;
1750 struct g_part_scheme *scheme;
1751 struct g_part_table *table;
1755 if (flag == EV_CANCEL)
1760 scheme = (void *)(*xchg);
1762 g_topology_assert();
1764 LIST_FOREACH(gp, &g_part_class.geom, geom) {
1766 if (table->gpt_scheme != scheme)
1770 LIST_FOREACH(pp, &gp->provider, provider)
1771 acc += pp->acr + pp->acw + pp->ace;
1772 LIST_FOREACH(cp, &gp->consumer, consumer)
1773 acc += cp->acr + cp->acw + cp->ace;
1776 g_part_wither(gp, ENOSYS);
1782 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1788 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
1795 TAILQ_INSERT_TAIL(&g_part_schemes, scheme, scheme_list);
1797 error = g_retaste(&g_part_class);
1799 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1802 arg = (uintptr_t)scheme;
1803 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,
1806 error = (arg == (uintptr_t)scheme) ? EDOOFUS : arg;