2 * Copyright (c) 2002, 2005, 2006, 2007 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/part/g_part.h>
48 #include "g_part_if.h"
50 static kobj_method_t g_part_null_methods[] = {
54 static struct g_part_scheme g_part_null_scheme = {
57 sizeof(struct g_part_table),
59 G_PART_SCHEME_DECLARE(g_part_null_scheme);
61 SET_DECLARE(g_part_scheme_set, struct g_part_scheme);
63 struct g_part_alias_list {
65 enum g_part_alias alias;
66 } g_part_alias_list[G_PART_ALIAS_COUNT] = {
67 { "efi", G_PART_ALIAS_EFI },
68 { "freebsd", G_PART_ALIAS_FREEBSD },
69 { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
70 { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
71 { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
72 { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
73 { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
74 { "mbr", G_PART_ALIAS_MBR }
78 * The GEOM partitioning class.
80 static g_ctl_req_t g_part_ctlreq;
81 static g_ctl_destroy_geom_t g_part_destroy_geom;
82 static g_taste_t g_part_taste;
84 static g_access_t g_part_access;
85 static g_dumpconf_t g_part_dumpconf;
86 static g_orphan_t g_part_orphan;
87 static g_spoiled_t g_part_spoiled;
88 static g_start_t g_part_start;
90 static struct g_class g_part_class = {
94 .ctlreq = g_part_ctlreq,
95 .destroy_geom = g_part_destroy_geom,
96 .taste = g_part_taste,
98 .access = g_part_access,
99 .dumpconf = g_part_dumpconf,
100 .orphan = g_part_orphan,
101 .spoiled = g_part_spoiled,
102 .start = g_part_start,
105 DECLARE_GEOM_CLASS(g_part_class, g_part);
125 static void g_part_wither(struct g_geom *, int);
128 g_part_alias_name(enum g_part_alias alias)
132 for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
133 if (g_part_alias_list[i].alias != alias)
135 return (g_part_alias_list[i].lexeme);
142 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
145 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
146 off_t chs, cylinders;
152 for (idx = 0; candidate_heads[idx] != 0; idx++) {
153 heads = candidate_heads[idx];
154 cylinders = blocks / heads / sectors;
155 if (cylinders < heads || cylinders < sectors)
157 if (cylinders > 1023)
159 chs = cylinders * heads * sectors;
160 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
168 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
171 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
173 u_int heads, sectors;
176 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 ||
177 sectors < 1 || sectors > 63 ||
178 g_getattr("GEOM::fwheads", cp, &heads) != 0 ||
179 heads < 1 || heads > 255) {
180 table->gpt_fixgeom = 0;
181 table->gpt_heads = 0;
182 table->gpt_sectors = 0;
184 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
185 sectors = candidate_sectors[idx];
186 g_part_geometry_heads(blocks, sectors, &chs, &heads);
190 * Prefer a geometry with sectors > 1, but only if
191 * it doesn't bump down the numbver of heads to 1.
193 if (chs > bestchs || (chs == bestchs && heads > 1 &&
194 table->gpt_sectors == 1)) {
196 table->gpt_heads = heads;
197 table->gpt_sectors = sectors;
201 * If we didn't find a geometry at all, then the disk is
202 * too big. This means we can use the maximum number of
206 table->gpt_heads = 255;
207 table->gpt_sectors = 63;
210 table->gpt_fixgeom = 1;
211 table->gpt_heads = heads;
212 table->gpt_sectors = sectors;
216 struct g_part_entry *
217 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
220 struct g_part_entry *entry, *last;
223 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
224 if (entry->gpe_index == index)
226 if (entry->gpe_index > index) {
233 entry = g_malloc(table->gpt_scheme->gps_entrysz,
235 entry->gpe_index = index;
237 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
239 LIST_INSERT_AFTER(last, entry, gpe_entry);
241 entry->gpe_start = start;
242 entry->gpe_end = end;
247 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
248 struct g_part_entry *entry)
251 struct g_consumer *cp;
252 struct g_provider *pp;
254 cp = LIST_FIRST(&gp->consumer);
257 entry->gpe_offset = entry->gpe_start * pp->sectorsize;
259 if (entry->gpe_pp == NULL) {
260 entry->gpe_pp = g_new_providerf(gp, "%s%s", gp->name,
261 G_PART_NAME(table, entry, buf, sizeof(buf)));
262 entry->gpe_pp->private = entry; /* Close the circle. */
264 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */
265 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
267 entry->gpe_pp->sectorsize = pp->sectorsize;
268 entry->gpe_pp->flags = pp->flags & G_PF_CANDELETE;
269 if (pp->stripesize > 0) {
270 entry->gpe_pp->stripesize = pp->stripesize;
271 entry->gpe_pp->stripeoffset = (pp->stripeoffset +
272 entry->gpe_offset) % pp->stripesize;
274 g_error_provider(entry->gpe_pp, 0);
278 g_part_parm_geom(const char *p, struct g_geom **v)
282 LIST_FOREACH(gp, &g_part_class.geom, geom) {
283 if (!strcmp(p, gp->name))
293 g_part_parm_provider(const char *p, struct g_provider **v)
295 struct g_provider *pp;
297 pp = g_provider_by_name(p);
305 g_part_parm_quad(const char *p, quad_t *v)
310 q = strtoq(p, &x, 0);
311 if (*x != '\0' || q < 0)
318 g_part_parm_scheme(const char *p, struct g_part_scheme **v)
320 struct g_part_scheme **iter, *s;
323 SET_FOREACH(iter, g_part_scheme_set) {
324 if ((*iter)->name == NULL)
326 if (!strcasecmp((*iter)->name, p)) {
338 g_part_parm_str(const char *p, const char **v)
348 g_part_parm_uint(const char *p, u_int *v)
353 l = strtol(p, &x, 0);
354 if (*x != '\0' || l < 0 || l > INT_MAX)
356 *v = (unsigned int)l;
361 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
363 struct g_part_scheme **iter, *scheme;
364 struct g_part_table *table;
368 scheme = (table != NULL) ? table->gpt_scheme : &g_part_null_scheme;
369 pri = (scheme != &g_part_null_scheme) ? G_PART_PROBE(table, cp) :
373 if (pri > 0) { /* error */
374 scheme = &g_part_null_scheme;
378 SET_FOREACH(iter, g_part_scheme_set) {
379 if ((*iter) == &g_part_null_scheme)
381 table = (void *)kobj_create((kobj_class_t)(*iter), M_GEOM,
384 table->gpt_scheme = *iter;
385 table->gpt_depth = depth;
386 probe = G_PART_PROBE(table, cp);
387 if (probe <= 0 && probe > pri) {
390 if (gp->softc != NULL)
391 kobj_delete((kobj_t)gp->softc, M_GEOM);
396 kobj_delete((kobj_t)table, M_GEOM);
400 return ((scheme == &g_part_null_scheme) ? ENXIO : 0);
404 * Control request functions.
408 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
412 struct g_provider *pp;
413 struct g_part_entry *delent, *last, *entry;
414 struct g_part_table *table;
421 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
424 pp = LIST_FIRST(&gp->consumer)->provider;
426 end = gpp->gpp_start + gpp->gpp_size - 1;
428 if (gpp->gpp_start < table->gpt_first ||
429 gpp->gpp_start > table->gpt_last) {
430 gctl_error(req, "%d start '%jd'", EINVAL,
431 (intmax_t)gpp->gpp_start);
434 if (end < gpp->gpp_start || end > table->gpt_last) {
435 gctl_error(req, "%d size '%jd'", EINVAL,
436 (intmax_t)gpp->gpp_size);
439 if (gpp->gpp_index > table->gpt_entries) {
440 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
444 delent = last = NULL;
445 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
446 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
447 if (entry->gpe_deleted) {
448 if (entry->gpe_index == index)
452 if (entry->gpe_index == index) {
453 index = entry->gpe_index + 1;
456 if (entry->gpe_internal)
458 if (gpp->gpp_start >= entry->gpe_start &&
459 gpp->gpp_start <= entry->gpe_end) {
460 gctl_error(req, "%d start '%jd'", ENOSPC,
461 (intmax_t)gpp->gpp_start);
464 if (end >= entry->gpe_start && end <= entry->gpe_end) {
465 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
468 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
469 gctl_error(req, "%d size '%jd'", ENOSPC,
470 (intmax_t)gpp->gpp_size);
474 if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
475 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
479 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
480 M_WAITOK | M_ZERO) : delent;
481 entry->gpe_index = index;
482 entry->gpe_start = gpp->gpp_start;
483 entry->gpe_end = end;
484 error = G_PART_ADD(table, entry, gpp);
486 gctl_error(req, "%d", error);
491 if (delent == NULL) {
493 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
495 LIST_INSERT_AFTER(last, entry, gpe_entry);
496 entry->gpe_created = 1;
498 entry->gpe_deleted = 0;
499 entry->gpe_modified = 1;
501 g_part_new_provider(gp, table, entry);
503 /* Provide feedback if so requested. */
504 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
505 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
506 sbuf_printf(sb, "%s%s added\n", gp->name,
507 G_PART_NAME(table, entry, buf, sizeof(buf)));
509 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
516 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
518 struct g_consumer *cp;
520 struct g_provider *pp;
521 struct g_part_entry *entry, *tmp;
522 struct g_part_table *table;
527 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
531 if (!table->gpt_opened) {
532 gctl_error(req, "%d", EPERM);
536 cp = LIST_FIRST(&gp->consumer);
537 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
539 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
540 while (table->gpt_smhead != 0) {
541 i = ffs(table->gpt_smhead) - 1;
542 error = g_write_data(cp, i * pp->sectorsize, buf,
548 table->gpt_smhead &= ~(1 << i);
550 while (table->gpt_smtail != 0) {
551 i = ffs(table->gpt_smtail) - 1;
552 error = g_write_data(cp, pp->mediasize - (i + 1) *
553 pp->sectorsize, buf, pp->sectorsize);
558 table->gpt_smtail &= ~(1 << i);
563 if (table->gpt_scheme == &g_part_null_scheme) {
564 g_access(cp, -1, -1, -1);
565 g_part_wither(gp, ENXIO);
569 error = G_PART_WRITE(table, cp);
573 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
574 if (!entry->gpe_deleted) {
575 entry->gpe_created = 0;
576 entry->gpe_modified = 0;
579 LIST_REMOVE(entry, gpe_entry);
582 table->gpt_created = 0;
583 table->gpt_opened = 0;
584 g_access(cp, -1, -1, -1);
588 gctl_error(req, "%d", error);
593 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
595 struct g_consumer *cp;
597 struct g_provider *pp;
598 struct g_part_scheme *scheme;
599 struct g_part_table *null, *table;
603 pp = gpp->gpp_provider;
604 scheme = gpp->gpp_scheme;
605 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
608 /* Check that there isn't already a g_part geom on the provider. */
609 error = g_part_parm_geom(pp->name, &gp);
612 if (null->gpt_scheme != &g_part_null_scheme) {
613 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
619 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
620 (gpp->gpp_entries < scheme->gps_minent ||
621 gpp->gpp_entries > scheme->gps_maxent)) {
622 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
627 gp = g_new_geomf(&g_part_class, "%s", pp->name);
628 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
632 table->gpt_scheme = gpp->gpp_scheme;
633 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
634 gpp->gpp_entries : scheme->gps_minent;
635 LIST_INIT(&table->gpt_entry);
637 cp = g_new_consumer(gp);
638 error = g_attach(cp, pp);
640 error = g_access(cp, 1, 1, 1);
642 g_part_wither(gp, error);
643 gctl_error(req, "%d geom '%s'", error, pp->name);
646 table->gpt_opened = 1;
648 cp = LIST_FIRST(&gp->consumer);
649 table->gpt_opened = null->gpt_opened;
650 table->gpt_smhead = null->gpt_smhead;
651 table->gpt_smtail = null->gpt_smtail;
656 /* Make sure the provider has media. */
657 if (pp->mediasize == 0 || pp->sectorsize == 0) {
662 /* Make sure we can nest and if so, determine our depth. */
663 error = g_getattr("PART::isleaf", cp, &attr);
664 if (!error && attr) {
668 error = g_getattr("PART::depth", cp, &attr);
669 table->gpt_depth = (!error) ? attr + 1 : 0;
671 /* If we're nested, get the absolute sector offset on disk. */
672 if (table->gpt_depth) {
673 error = g_getattr("PART::offset", cp, &attr);
676 table->gpt_offset = attr;
680 * Synthesize a disk geometry. Some partitioning schemes
681 * depend on it and since some file systems need it even
682 * when the partitition scheme doesn't, we do it here in
683 * scheme-independent code.
685 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
687 error = G_PART_CREATE(table, gpp);
693 table->gpt_created = 1;
695 kobj_delete((kobj_t)null, M_GEOM);
698 * Support automatic commit by filling in the gpp_geom
701 gpp->gpp_parms |= G_PART_PARM_GEOM;
704 /* Provide feedback if so requested. */
705 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
706 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
707 sbuf_printf(sb, "%s created\n", gp->name);
709 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
717 g_access(cp, -1, -1, -1);
718 g_part_wither(gp, error);
720 kobj_delete((kobj_t)gp->softc, M_GEOM);
723 gctl_error(req, "%d provider", error);
728 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
732 struct g_provider *pp;
733 struct g_part_entry *entry;
734 struct g_part_table *table;
738 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
743 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
744 if (entry->gpe_deleted || entry->gpe_internal)
746 if (entry->gpe_index == gpp->gpp_index)
750 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
756 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
757 gctl_error(req, "%d", EBUSY);
762 entry->gpe_pp = NULL;
765 if (entry->gpe_created) {
766 LIST_REMOVE(entry, gpe_entry);
769 entry->gpe_modified = 0;
770 entry->gpe_deleted = 1;
774 g_wither_provider(pp, ENXIO);
776 /* Provide feedback if so requested. */
777 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
778 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
779 sbuf_printf(sb, "%s%s deleted\n", gp->name,
780 G_PART_NAME(table, entry, buf, sizeof(buf)));
782 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
789 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
792 struct g_part_entry *entry;
793 struct g_part_table *null, *table;
798 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
802 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
803 if (entry->gpe_deleted || entry->gpe_internal)
805 gctl_error(req, "%d", EBUSY);
809 error = G_PART_DESTROY(table, gpp);
811 gctl_error(req, "%d", error);
815 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
819 null->gpt_scheme = &g_part_null_scheme;
820 LIST_INIT(&null->gpt_entry);
821 null->gpt_depth = table->gpt_depth;
822 null->gpt_opened = table->gpt_opened;
823 null->gpt_smhead = table->gpt_smhead;
824 null->gpt_smtail = table->gpt_smtail;
826 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
827 LIST_REMOVE(entry, gpe_entry);
830 kobj_delete((kobj_t)table, M_GEOM);
832 /* Provide feedback if so requested. */
833 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
834 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
835 sbuf_printf(sb, "%s destroyed\n", gp->name);
837 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
844 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
848 struct g_part_entry *entry;
849 struct g_part_table *table;
854 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
859 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
860 if (entry->gpe_deleted || entry->gpe_internal)
862 if (entry->gpe_index == gpp->gpp_index)
866 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
870 error = G_PART_MODIFY(table, entry, gpp);
872 gctl_error(req, "%d", error);
876 if (!entry->gpe_created)
877 entry->gpe_modified = 1;
879 /* Provide feedback if so requested. */
880 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
881 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
882 sbuf_printf(sb, "%s%s modified\n", gp->name,
883 G_PART_NAME(table, entry, buf, sizeof(buf)));
885 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
892 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
894 gctl_error(req, "%d verb 'move'", ENOSYS);
899 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
901 gctl_error(req, "%d verb 'recover'", ENOSYS);
906 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
908 gctl_error(req, "%d verb 'resize'", ENOSYS);
913 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
915 struct g_consumer *cp;
916 struct g_provider *pp;
918 struct g_part_entry *entry, *tmp;
919 struct g_part_table *table;
923 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
927 if (!table->gpt_opened) {
928 gctl_error(req, "%d", EPERM);
932 cp = LIST_FIRST(&gp->consumer);
933 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
934 entry->gpe_modified = 0;
935 if (entry->gpe_created) {
939 entry->gpe_pp = NULL;
940 g_wither_provider(pp, ENXIO);
942 entry->gpe_deleted = 1;
944 if (entry->gpe_deleted) {
945 LIST_REMOVE(entry, gpe_entry);
952 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
953 table->gpt_created) ? 1 : 0;
956 if (!LIST_EMPTY(&table->gpt_entry)) {
960 error = g_part_probe(gp, cp, table->gpt_depth);
963 g_access(cp, -1, -1, -1);
964 g_part_wither(gp, error);
970 error = G_PART_READ(table, cp);
976 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
977 if (!entry->gpe_internal)
978 g_part_new_provider(gp, table, entry);
981 table->gpt_opened = 0;
982 g_access(cp, -1, -1, -1);
987 gctl_error(req, "%d", error);
992 g_part_wither(struct g_geom *gp, int error)
994 struct g_part_entry *entry;
995 struct g_part_table *table;
999 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1000 LIST_REMOVE(entry, gpe_entry);
1003 if (gp->softc != NULL) {
1004 kobj_delete((kobj_t)gp->softc, M_GEOM);
1008 g_wither_geom(gp, error);
1016 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1018 struct g_part_parms gpp;
1019 struct g_part_table *table;
1020 struct gctl_req_arg *ap;
1022 enum g_part_ctl ctlreq;
1023 unsigned int i, mparms, oparms, parm;
1024 int auto_commit, close_on_error;
1025 int error, modifies;
1027 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1028 g_topology_assert();
1030 ctlreq = G_PART_CTL_NONE;
1033 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1036 if (!strcmp(verb, "add")) {
1037 ctlreq = G_PART_CTL_ADD;
1038 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1039 G_PART_PARM_START | G_PART_PARM_TYPE;
1040 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1044 if (!strcmp(verb, "commit")) {
1045 ctlreq = G_PART_CTL_COMMIT;
1046 mparms |= G_PART_PARM_GEOM;
1048 } else if (!strcmp(verb, "create")) {
1049 ctlreq = G_PART_CTL_CREATE;
1050 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1051 oparms |= G_PART_PARM_ENTRIES;
1055 if (!strcmp(verb, "delete")) {
1056 ctlreq = G_PART_CTL_DELETE;
1057 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1058 } else if (!strcmp(verb, "destroy")) {
1059 ctlreq = G_PART_CTL_DESTROY;
1060 mparms |= G_PART_PARM_GEOM;
1064 if (!strcmp(verb, "modify")) {
1065 ctlreq = G_PART_CTL_MODIFY;
1066 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1067 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1068 } else if (!strcmp(verb, "move")) {
1069 ctlreq = G_PART_CTL_MOVE;
1070 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1074 if (!strcmp(verb, "recover")) {
1075 ctlreq = G_PART_CTL_RECOVER;
1076 mparms |= G_PART_PARM_GEOM;
1077 } else if (!strcmp(verb, "resize")) {
1078 ctlreq = G_PART_CTL_RESIZE;
1079 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1083 if (!strcmp(verb, "undo")) {
1084 ctlreq = G_PART_CTL_UNDO;
1085 mparms |= G_PART_PARM_GEOM;
1090 if (ctlreq == G_PART_CTL_NONE) {
1091 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1095 bzero(&gpp, sizeof(gpp));
1096 for (i = 0; i < req->narg; i++) {
1099 switch (ap->name[0]) {
1101 if (!strcmp(ap->name, "class"))
1105 if (!strcmp(ap->name, "entries"))
1106 parm = G_PART_PARM_ENTRIES;
1109 if (!strcmp(ap->name, "flags"))
1110 parm = G_PART_PARM_FLAGS;
1113 if (!strcmp(ap->name, "geom"))
1114 parm = G_PART_PARM_GEOM;
1117 if (!strcmp(ap->name, "index"))
1118 parm = G_PART_PARM_INDEX;
1121 if (!strcmp(ap->name, "label"))
1122 parm = G_PART_PARM_LABEL;
1125 if (!strcmp(ap->name, "output"))
1126 parm = G_PART_PARM_OUTPUT;
1129 if (!strcmp(ap->name, "provider"))
1130 parm = G_PART_PARM_PROVIDER;
1133 if (!strcmp(ap->name, "scheme"))
1134 parm = G_PART_PARM_SCHEME;
1135 else if (!strcmp(ap->name, "size"))
1136 parm = G_PART_PARM_SIZE;
1137 else if (!strcmp(ap->name, "start"))
1138 parm = G_PART_PARM_START;
1141 if (!strcmp(ap->name, "type"))
1142 parm = G_PART_PARM_TYPE;
1145 if (!strcmp(ap->name, "verb"))
1147 else if (!strcmp(ap->name, "version"))
1148 parm = G_PART_PARM_VERSION;
1151 if ((parm & (mparms | oparms)) == 0) {
1152 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1155 p = gctl_get_asciiparam(req, ap->name);
1157 gctl_error(req, "%d param '%s'", ENOATTR, ap->name);
1161 case G_PART_PARM_ENTRIES:
1162 error = g_part_parm_uint(p, &gpp.gpp_entries);
1164 case G_PART_PARM_FLAGS:
1167 error = g_part_parm_str(p, &gpp.gpp_flags);
1169 case G_PART_PARM_GEOM:
1170 error = g_part_parm_geom(p, &gpp.gpp_geom);
1172 case G_PART_PARM_INDEX:
1173 error = g_part_parm_uint(p, &gpp.gpp_index);
1175 case G_PART_PARM_LABEL:
1176 /* An empty label is always valid. */
1180 case G_PART_PARM_OUTPUT:
1181 error = 0; /* Write-only parameter */
1183 case G_PART_PARM_PROVIDER:
1184 error = g_part_parm_provider(p, &gpp.gpp_provider);
1186 case G_PART_PARM_SCHEME:
1187 error = g_part_parm_scheme(p, &gpp.gpp_scheme);
1189 case G_PART_PARM_SIZE:
1190 error = g_part_parm_quad(p, &gpp.gpp_size);
1192 case G_PART_PARM_START:
1193 error = g_part_parm_quad(p, &gpp.gpp_start);
1195 case G_PART_PARM_TYPE:
1196 error = g_part_parm_str(p, &gpp.gpp_type);
1198 case G_PART_PARM_VERSION:
1199 error = g_part_parm_uint(p, &gpp.gpp_version);
1206 gctl_error(req, "%d %s '%s'", error, ap->name, p);
1209 gpp.gpp_parms |= parm;
1211 if ((gpp.gpp_parms & mparms) != mparms) {
1212 parm = mparms - (gpp.gpp_parms & mparms);
1213 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1217 /* Obtain permissions if possible/necessary. */
1219 table = NULL; /* Suppress uninit. warning. */
1220 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1221 table = gpp.gpp_geom->softc;
1222 if (table != NULL && !table->gpt_opened) {
1223 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1226 gctl_error(req, "%d geom '%s'", error,
1227 gpp.gpp_geom->name);
1230 table->gpt_opened = 1;
1235 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1237 case G_PART_CTL_NONE:
1238 panic("%s", __func__);
1239 case G_PART_CTL_ADD:
1240 error = g_part_ctl_add(req, &gpp);
1242 case G_PART_CTL_COMMIT:
1243 error = g_part_ctl_commit(req, &gpp);
1245 case G_PART_CTL_CREATE:
1246 error = g_part_ctl_create(req, &gpp);
1248 case G_PART_CTL_DELETE:
1249 error = g_part_ctl_delete(req, &gpp);
1251 case G_PART_CTL_DESTROY:
1252 error = g_part_ctl_destroy(req, &gpp);
1254 case G_PART_CTL_MODIFY:
1255 error = g_part_ctl_modify(req, &gpp);
1257 case G_PART_CTL_MOVE:
1258 error = g_part_ctl_move(req, &gpp);
1260 case G_PART_CTL_RECOVER:
1261 error = g_part_ctl_recover(req, &gpp);
1263 case G_PART_CTL_RESIZE:
1264 error = g_part_ctl_resize(req, &gpp);
1266 case G_PART_CTL_UNDO:
1267 error = g_part_ctl_undo(req, &gpp);
1271 /* Implement automatic commit. */
1273 auto_commit = (modifies &&
1274 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1275 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1277 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, (__func__));
1278 error = g_part_ctl_commit(req, &gpp);
1282 if (error && close_on_error) {
1283 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1284 table->gpt_opened = 0;
1289 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1293 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1294 g_topology_assert();
1296 g_part_wither(gp, EINVAL);
1300 static struct g_geom *
1301 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1303 struct g_consumer *cp;
1305 struct g_part_entry *entry;
1306 struct g_part_table *table;
1310 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1311 g_topology_assert();
1314 * Create a GEOM with consumer and hook it up to the provider.
1315 * With that we become part of the topology. Optain read access
1318 gp = g_new_geomf(mp, "%s", pp->name);
1319 cp = g_new_consumer(gp);
1320 error = g_attach(cp, pp);
1322 error = g_access(cp, 1, 0, 0);
1324 g_part_wither(gp, error);
1328 g_topology_unlock();
1331 * Short-circuit the whole probing galore when there's no
1334 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1339 /* Make sure we can nest and if so, determine our depth. */
1340 error = g_getattr("PART::isleaf", cp, &attr);
1341 if (!error && attr) {
1345 error = g_getattr("PART::depth", cp, &attr);
1346 depth = (!error) ? attr + 1 : 0;
1348 error = g_part_probe(gp, cp, depth);
1354 /* If we're nested, get the absolute sector offset on disk. */
1355 if (table->gpt_depth) {
1356 error = g_getattr("PART::offset", cp, &attr);
1359 table->gpt_offset = attr;
1363 * Synthesize a disk geometry. Some partitioning schemes
1364 * depend on it and since some file systems need it even
1365 * when the partitition scheme doesn't, we do it here in
1366 * scheme-independent code.
1368 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1370 error = G_PART_READ(table, cp);
1375 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1376 if (!entry->gpe_internal)
1377 g_part_new_provider(gp, table, entry);
1380 g_access(cp, -1, 0, 0);
1385 g_access(cp, -1, 0, 0);
1386 g_part_wither(gp, error);
1395 g_part_access(struct g_provider *pp, int dr, int dw, int de)
1397 struct g_consumer *cp;
1399 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1402 cp = LIST_FIRST(&pp->geom->consumer);
1404 /* We always gain write-exclusive access. */
1405 return (g_access(cp, dr, dw, dw + de));
1409 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1410 struct g_consumer *cp, struct g_provider *pp)
1413 struct g_part_entry *entry;
1414 struct g_part_table *table;
1416 KASSERT(sb != NULL && gp != NULL, (__func__));
1419 if (indent == NULL) {
1420 KASSERT(cp == NULL && pp != NULL, (__func__));
1421 entry = pp->private;
1424 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
1425 (uintmax_t)entry->gpe_offset,
1426 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1427 } else if (cp != NULL) { /* Consumer configuration. */
1428 KASSERT(pp == NULL, (__func__));
1430 } else if (pp != NULL) { /* Provider configuration. */
1431 entry = pp->private;
1434 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
1436 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1437 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1438 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
1439 (uintmax_t)entry->gpe_offset);
1440 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
1441 (uintmax_t)pp->mediasize);
1442 G_PART_DUMPCONF(table, entry, sb, indent);
1443 } else { /* Geom configuration. */
1444 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
1445 table->gpt_scheme->name);
1446 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
1447 table->gpt_entries);
1448 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
1449 (uintmax_t)table->gpt_first);
1450 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
1451 (uintmax_t)table->gpt_last);
1452 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
1453 table->gpt_sectors);
1454 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
1456 G_PART_DUMPCONF(table, NULL, sb, indent);
1461 g_part_orphan(struct g_consumer *cp)
1463 struct g_provider *pp;
1466 KASSERT(pp != NULL, (__func__));
1467 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
1468 g_topology_assert();
1470 KASSERT(pp->error != 0, (__func__));
1471 g_part_wither(cp->geom, pp->error);
1475 g_part_spoiled(struct g_consumer *cp)
1478 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
1479 g_topology_assert();
1481 g_part_wither(cp->geom, ENXIO);
1485 g_part_start(struct bio *bp)
1488 struct g_consumer *cp;
1490 struct g_part_entry *entry;
1491 struct g_part_table *table;
1492 struct g_kerneldump *gkd;
1493 struct g_provider *pp;
1498 cp = LIST_FIRST(&gp->consumer);
1500 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
1503 entry = pp->private;
1504 if (entry == NULL) {
1505 g_io_deliver(bp, ENXIO);
1509 switch(bp->bio_cmd) {
1513 if (bp->bio_offset >= pp->mediasize) {
1514 g_io_deliver(bp, EIO);
1517 bp2 = g_clone_bio(bp);
1519 g_io_deliver(bp, ENOMEM);
1522 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
1523 bp2->bio_length = pp->mediasize - bp2->bio_offset;
1524 bp2->bio_done = g_std_done;
1525 bp2->bio_offset += entry->gpe_offset;
1526 g_io_request(bp2, cp);
1531 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
1533 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
1535 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
1537 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
1539 if (g_handleattr_int(bp, "PART::offset",
1540 table->gpt_offset + entry->gpe_start))
1542 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
1544 * Check that the partition is suitable for kernel
1545 * dumps. Typically only swap partitions should be
1548 if (!G_PART_DUMPTO(table, entry)) {
1549 g_io_deliver(bp, ENXIO);
1552 gkd = (struct g_kerneldump *)bp->bio_data;
1553 if (gkd->offset >= pp->mediasize) {
1554 g_io_deliver(bp, EIO);
1557 if (gkd->offset + gkd->length > pp->mediasize)
1558 gkd->length = pp->mediasize - gkd->offset;
1559 gkd->offset += entry->gpe_offset;
1563 g_io_deliver(bp, EOPNOTSUPP);
1567 bp2 = g_clone_bio(bp);
1569 g_io_deliver(bp, ENOMEM);
1572 bp2->bio_done = g_std_done;
1573 g_io_request(bp2, cp);