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>
33 #include <sys/diskmbr.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
37 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.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"
52 static kobj_method_t g_part_null_methods[] = {
56 static struct g_part_scheme g_part_null_scheme = {
59 sizeof(struct g_part_table),
62 TAILQ_HEAD(, g_part_scheme) g_part_schemes =
63 TAILQ_HEAD_INITIALIZER(g_part_schemes);
65 struct g_part_alias_list {
67 enum g_part_alias alias;
68 } g_part_alias_list[G_PART_ALIAS_COUNT] = {
69 { "efi", G_PART_ALIAS_EFI },
70 { "freebsd", G_PART_ALIAS_FREEBSD },
71 { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
72 { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
73 { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
74 { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
75 { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
76 { "mbr", G_PART_ALIAS_MBR }
80 * The GEOM partitioning class.
82 static g_ctl_req_t g_part_ctlreq;
83 static g_ctl_destroy_geom_t g_part_destroy_geom;
84 static g_fini_t g_part_fini;
85 static g_init_t g_part_init;
86 static g_taste_t g_part_taste;
88 static g_access_t g_part_access;
89 static g_dumpconf_t g_part_dumpconf;
90 static g_ioctl_t g_part_ioctl;
91 static g_orphan_t g_part_orphan;
92 static g_spoiled_t g_part_spoiled;
93 static g_start_t g_part_start;
95 static struct g_class g_part_class = {
99 .ctlreq = g_part_ctlreq,
100 .destroy_geom = g_part_destroy_geom,
103 .taste = g_part_taste,
105 .access = g_part_access,
106 .dumpconf = g_part_dumpconf,
107 .ioctl = g_part_ioctl,
108 .orphan = g_part_orphan,
109 .spoiled = g_part_spoiled,
110 .start = g_part_start,
113 DECLARE_GEOM_CLASS(g_part_class, g_part);
119 static void g_part_wither(struct g_geom *, int);
122 g_part_alias_name(enum g_part_alias alias)
126 for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
127 if (g_part_alias_list[i].alias != alias)
129 return (g_part_alias_list[i].lexeme);
136 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
139 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
140 off_t chs, cylinders;
146 for (idx = 0; candidate_heads[idx] != 0; idx++) {
147 heads = candidate_heads[idx];
148 cylinders = blocks / heads / sectors;
149 if (cylinders < heads || cylinders < sectors)
151 if (cylinders > 1023)
153 chs = cylinders * heads * sectors;
154 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
162 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
165 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
167 u_int heads, sectors;
170 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 || sectors == 0 ||
171 g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
172 table->gpt_fixgeom = 0;
173 table->gpt_heads = 0;
174 table->gpt_sectors = 0;
176 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
177 sectors = candidate_sectors[idx];
178 g_part_geometry_heads(blocks, sectors, &chs, &heads);
182 * Prefer a geometry with sectors > 1, but only if
183 * it doesn't bump down the numbver of heads to 1.
185 if (chs > bestchs || (chs == bestchs && heads > 1 &&
186 table->gpt_sectors == 1)) {
188 table->gpt_heads = heads;
189 table->gpt_sectors = sectors;
193 * If we didn't find a geometry at all, then the disk is
194 * too big. This means we can use the maximum number of
198 table->gpt_heads = 255;
199 table->gpt_sectors = 63;
202 table->gpt_fixgeom = 1;
203 table->gpt_heads = heads;
204 table->gpt_sectors = sectors;
208 struct g_part_entry *
209 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
212 struct g_part_entry *entry, *last;
215 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
216 if (entry->gpe_index == index)
218 if (entry->gpe_index > index) {
225 entry = g_malloc(table->gpt_scheme->gps_entrysz,
227 entry->gpe_index = index;
229 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
231 LIST_INSERT_AFTER(last, entry, gpe_entry);
233 entry->gpe_offset = 0;
234 entry->gpe_start = start;
235 entry->gpe_end = end;
240 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
241 struct g_part_entry *entry)
244 struct g_consumer *cp;
245 struct g_provider *pp;
248 cp = LIST_FIRST(&gp->consumer);
251 offset = entry->gpe_start * pp->sectorsize;
252 if (entry->gpe_offset < offset)
253 entry->gpe_offset = offset;
255 if (entry->gpe_pp == NULL) {
256 entry->gpe_pp = g_new_providerf(gp, "%s%s", gp->name,
257 G_PART_NAME(table, entry, buf, sizeof(buf)));
258 entry->gpe_pp->private = entry; /* Close the circle. */
260 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */
261 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
263 entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
264 entry->gpe_pp->sectorsize = pp->sectorsize;
265 entry->gpe_pp->flags = pp->flags & G_PF_CANDELETE;
266 if (pp->stripesize > 0) {
267 entry->gpe_pp->stripesize = pp->stripesize;
268 entry->gpe_pp->stripeoffset = (pp->stripeoffset +
269 entry->gpe_offset) % pp->stripesize;
271 g_error_provider(entry->gpe_pp, 0);
275 g_part_parm_geom(const char *p, struct g_geom **v)
279 LIST_FOREACH(gp, &g_part_class.geom, geom) {
280 if (!strcmp(p, gp->name))
290 g_part_parm_provider(const char *p, struct g_provider **v)
292 struct g_provider *pp;
294 pp = g_provider_by_name(p);
302 g_part_parm_quad(const char *p, quad_t *v)
307 q = strtoq(p, &x, 0);
308 if (*x != '\0' || q < 0)
315 g_part_parm_scheme(const char *p, struct g_part_scheme **v)
317 struct g_part_scheme *s;
319 TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
320 if (s == &g_part_null_scheme)
322 if (!strcasecmp(s->name, p))
332 g_part_parm_str(const char *p, const char **v)
342 g_part_parm_uint(const char *p, u_int *v)
347 l = strtol(p, &x, 0);
348 if (*x != '\0' || l < 0 || l > INT_MAX)
350 *v = (unsigned int)l;
355 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
357 struct g_part_scheme *iter, *scheme;
358 struct g_part_table *table;
362 scheme = (table != NULL) ? table->gpt_scheme : NULL;
363 pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
366 if (pri > 0) { /* error */
371 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
372 if (iter == &g_part_null_scheme)
374 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
377 table->gpt_scheme = iter;
378 table->gpt_depth = depth;
379 probe = G_PART_PROBE(table, cp);
380 if (probe <= 0 && probe > pri) {
383 if (gp->softc != NULL)
384 kobj_delete((kobj_t)gp->softc, M_GEOM);
389 kobj_delete((kobj_t)table, M_GEOM);
393 return ((scheme == NULL) ? ENXIO : 0);
397 * Control request functions.
401 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
405 struct g_provider *pp;
406 struct g_part_entry *delent, *last, *entry;
407 struct g_part_table *table;
414 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
417 pp = LIST_FIRST(&gp->consumer)->provider;
419 end = gpp->gpp_start + gpp->gpp_size - 1;
421 if (gpp->gpp_start < table->gpt_first ||
422 gpp->gpp_start > table->gpt_last) {
423 gctl_error(req, "%d start '%jd'", EINVAL,
424 (intmax_t)gpp->gpp_start);
427 if (end < gpp->gpp_start || end > table->gpt_last) {
428 gctl_error(req, "%d size '%jd'", EINVAL,
429 (intmax_t)gpp->gpp_size);
432 if (gpp->gpp_index > table->gpt_entries) {
433 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
437 delent = last = NULL;
438 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
439 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
440 if (entry->gpe_deleted) {
441 if (entry->gpe_index == index)
445 if (entry->gpe_index == index)
446 index = entry->gpe_index + 1;
447 if (entry->gpe_index < index)
449 if (entry->gpe_internal)
451 if (gpp->gpp_start >= entry->gpe_start &&
452 gpp->gpp_start <= entry->gpe_end) {
453 gctl_error(req, "%d start '%jd'", ENOSPC,
454 (intmax_t)gpp->gpp_start);
457 if (end >= entry->gpe_start && end <= entry->gpe_end) {
458 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
461 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
462 gctl_error(req, "%d size '%jd'", ENOSPC,
463 (intmax_t)gpp->gpp_size);
467 if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
468 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
472 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
473 M_WAITOK | M_ZERO) : delent;
474 entry->gpe_index = index;
475 entry->gpe_start = gpp->gpp_start;
476 entry->gpe_end = end;
477 error = G_PART_ADD(table, entry, gpp);
479 gctl_error(req, "%d", error);
484 if (delent == NULL) {
486 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
488 LIST_INSERT_AFTER(last, entry, gpe_entry);
489 entry->gpe_created = 1;
491 entry->gpe_deleted = 0;
492 entry->gpe_modified = 1;
494 g_part_new_provider(gp, table, entry);
496 /* Provide feedback if so requested. */
497 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
498 sb = sbuf_new_auto();
499 sbuf_printf(sb, "%s%s added\n", gp->name,
500 G_PART_NAME(table, entry, buf, sizeof(buf)));
502 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
509 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
512 struct g_part_table *table;
517 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
521 sz = table->gpt_scheme->gps_bootcodesz;
526 if (gpp->gpp_codesize > sz) {
531 error = G_PART_BOOTCODE(table, gpp);
535 /* Provide feedback if so requested. */
536 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
537 sb = sbuf_new_auto();
538 sbuf_printf(sb, "%s has bootcode\n", gp->name);
540 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
546 gctl_error(req, "%d", error);
551 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
553 struct g_consumer *cp;
555 struct g_provider *pp;
556 struct g_part_entry *entry, *tmp;
557 struct g_part_table *table;
562 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
566 if (!table->gpt_opened) {
567 gctl_error(req, "%d", EPERM);
573 cp = LIST_FIRST(&gp->consumer);
574 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
576 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
577 while (table->gpt_smhead != 0) {
578 i = ffs(table->gpt_smhead) - 1;
579 error = g_write_data(cp, i * pp->sectorsize, buf,
585 table->gpt_smhead &= ~(1 << i);
587 while (table->gpt_smtail != 0) {
588 i = ffs(table->gpt_smtail) - 1;
589 error = g_write_data(cp, pp->mediasize - (i + 1) *
590 pp->sectorsize, buf, pp->sectorsize);
595 table->gpt_smtail &= ~(1 << i);
600 if (table->gpt_scheme == &g_part_null_scheme) {
602 g_access(cp, -1, -1, -1);
603 g_part_wither(gp, ENXIO);
607 error = G_PART_WRITE(table, cp);
611 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
612 if (!entry->gpe_deleted) {
613 entry->gpe_created = 0;
614 entry->gpe_modified = 0;
617 LIST_REMOVE(entry, gpe_entry);
620 table->gpt_created = 0;
621 table->gpt_opened = 0;
624 g_access(cp, -1, -1, -1);
629 gctl_error(req, "%d", error);
634 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
636 struct g_consumer *cp;
638 struct g_provider *pp;
639 struct g_part_scheme *scheme;
640 struct g_part_table *null, *table;
644 pp = gpp->gpp_provider;
645 scheme = gpp->gpp_scheme;
646 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
649 /* Check that there isn't already a g_part geom on the provider. */
650 error = g_part_parm_geom(pp->name, &gp);
653 if (null->gpt_scheme != &g_part_null_scheme) {
654 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
660 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
661 (gpp->gpp_entries < scheme->gps_minent ||
662 gpp->gpp_entries > scheme->gps_maxent)) {
663 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
668 gp = g_new_geomf(&g_part_class, "%s", pp->name);
669 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
673 table->gpt_scheme = gpp->gpp_scheme;
674 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
675 gpp->gpp_entries : scheme->gps_minent;
676 LIST_INIT(&table->gpt_entry);
678 cp = g_new_consumer(gp);
679 error = g_attach(cp, pp);
681 error = g_access(cp, 1, 1, 1);
683 g_part_wither(gp, error);
684 gctl_error(req, "%d geom '%s'", error, pp->name);
687 table->gpt_opened = 1;
689 cp = LIST_FIRST(&gp->consumer);
690 table->gpt_opened = null->gpt_opened;
691 table->gpt_smhead = null->gpt_smhead;
692 table->gpt_smtail = null->gpt_smtail;
697 /* Make sure the provider has media. */
698 if (pp->mediasize == 0 || pp->sectorsize == 0) {
703 /* Make sure we can nest and if so, determine our depth. */
704 error = g_getattr("PART::isleaf", cp, &attr);
705 if (!error && attr) {
709 error = g_getattr("PART::depth", cp, &attr);
710 table->gpt_depth = (!error) ? attr + 1 : 0;
713 * Synthesize a disk geometry. Some partitioning schemes
714 * depend on it and since some file systems need it even
715 * when the partitition scheme doesn't, we do it here in
716 * scheme-independent code.
718 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
720 error = G_PART_CREATE(table, gpp);
726 table->gpt_created = 1;
728 kobj_delete((kobj_t)null, M_GEOM);
731 * Support automatic commit by filling in the gpp_geom
734 gpp->gpp_parms |= G_PART_PARM_GEOM;
737 /* Provide feedback if so requested. */
738 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
739 sb = sbuf_new_auto();
740 sbuf_printf(sb, "%s created\n", gp->name);
742 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
750 g_access(cp, -1, -1, -1);
751 g_part_wither(gp, error);
753 kobj_delete((kobj_t)gp->softc, M_GEOM);
756 gctl_error(req, "%d provider", error);
761 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
765 struct g_provider *pp;
766 struct g_part_entry *entry;
767 struct g_part_table *table;
771 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
776 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
777 if (entry->gpe_deleted || entry->gpe_internal)
779 if (entry->gpe_index == gpp->gpp_index)
783 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
789 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
790 gctl_error(req, "%d", EBUSY);
795 entry->gpe_pp = NULL;
798 if (entry->gpe_created) {
799 LIST_REMOVE(entry, gpe_entry);
802 entry->gpe_modified = 0;
803 entry->gpe_deleted = 1;
807 g_wither_provider(pp, ENXIO);
809 /* Provide feedback if so requested. */
810 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
811 sb = sbuf_new_auto();
812 sbuf_printf(sb, "%s%s deleted\n", gp->name,
813 G_PART_NAME(table, entry, buf, sizeof(buf)));
815 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
822 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
825 struct g_part_entry *entry;
826 struct g_part_table *null, *table;
831 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
835 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
836 if (entry->gpe_deleted || entry->gpe_internal)
838 gctl_error(req, "%d", EBUSY);
842 error = G_PART_DESTROY(table, gpp);
844 gctl_error(req, "%d", error);
848 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
852 null->gpt_scheme = &g_part_null_scheme;
853 LIST_INIT(&null->gpt_entry);
854 null->gpt_depth = table->gpt_depth;
855 null->gpt_opened = table->gpt_opened;
856 null->gpt_smhead = table->gpt_smhead;
857 null->gpt_smtail = table->gpt_smtail;
859 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
860 LIST_REMOVE(entry, gpe_entry);
863 kobj_delete((kobj_t)table, M_GEOM);
865 /* Provide feedback if so requested. */
866 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
867 sb = sbuf_new_auto();
868 sbuf_printf(sb, "%s destroyed\n", gp->name);
870 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
877 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
881 struct g_part_entry *entry;
882 struct g_part_table *table;
887 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
892 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
893 if (entry->gpe_deleted || entry->gpe_internal)
895 if (entry->gpe_index == gpp->gpp_index)
899 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
903 error = G_PART_MODIFY(table, entry, gpp);
905 gctl_error(req, "%d", error);
909 if (!entry->gpe_created)
910 entry->gpe_modified = 1;
912 /* Provide feedback if so requested. */
913 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
914 sb = sbuf_new_auto();
915 sbuf_printf(sb, "%s%s modified\n", gp->name,
916 G_PART_NAME(table, entry, buf, sizeof(buf)));
918 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
925 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
927 gctl_error(req, "%d verb 'move'", ENOSYS);
932 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
934 gctl_error(req, "%d verb 'recover'", ENOSYS);
939 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
941 gctl_error(req, "%d verb 'resize'", ENOSYS);
946 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
951 struct g_part_entry *entry;
952 struct g_part_table *table;
957 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
962 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
963 if (entry->gpe_deleted || entry->gpe_internal)
965 if (entry->gpe_index == gpp->gpp_index)
969 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
973 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
975 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
979 /* Provide feedback if so requested. */
980 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
981 sb = sbuf_new_auto();
982 sbuf_printf(sb, "%s%s has %s %sset\n", gp->name,
983 G_PART_NAME(table, entry, buf, sizeof(buf)),
984 gpp->gpp_attrib, (set) ? "" : "un");
986 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
993 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
995 struct g_consumer *cp;
996 struct g_provider *pp;
998 struct g_part_entry *entry, *tmp;
999 struct g_part_table *table;
1003 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1004 g_topology_assert();
1007 if (!table->gpt_opened) {
1008 gctl_error(req, "%d", EPERM);
1012 cp = LIST_FIRST(&gp->consumer);
1013 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1014 entry->gpe_modified = 0;
1015 if (entry->gpe_created) {
1019 entry->gpe_pp = NULL;
1020 g_wither_provider(pp, ENXIO);
1022 entry->gpe_deleted = 1;
1024 if (entry->gpe_deleted) {
1025 LIST_REMOVE(entry, gpe_entry);
1030 g_topology_unlock();
1032 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1033 table->gpt_created) ? 1 : 0;
1036 if (!LIST_EMPTY(&table->gpt_entry)) {
1040 error = g_part_probe(gp, cp, table->gpt_depth);
1043 g_access(cp, -1, -1, -1);
1044 g_part_wither(gp, error);
1050 error = G_PART_READ(table, cp);
1056 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1057 if (!entry->gpe_internal)
1058 g_part_new_provider(gp, table, entry);
1061 table->gpt_opened = 0;
1062 g_access(cp, -1, -1, -1);
1067 gctl_error(req, "%d", error);
1072 g_part_wither(struct g_geom *gp, int error)
1074 struct g_part_entry *entry;
1075 struct g_part_table *table;
1078 if (table != NULL) {
1079 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1080 LIST_REMOVE(entry, gpe_entry);
1083 if (gp->softc != NULL) {
1084 kobj_delete((kobj_t)gp->softc, M_GEOM);
1088 g_wither_geom(gp, error);
1096 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1098 struct g_part_parms gpp;
1099 struct g_part_table *table;
1100 struct gctl_req_arg *ap;
1102 enum g_part_ctl ctlreq;
1103 unsigned int i, mparms, oparms, parm;
1104 int auto_commit, close_on_error;
1105 int error, len, modifies;
1107 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1108 g_topology_assert();
1110 ctlreq = G_PART_CTL_NONE;
1113 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1116 if (!strcmp(verb, "add")) {
1117 ctlreq = G_PART_CTL_ADD;
1118 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1119 G_PART_PARM_START | G_PART_PARM_TYPE;
1120 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1124 if (!strcmp(verb, "bootcode")) {
1125 ctlreq = G_PART_CTL_BOOTCODE;
1126 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1130 if (!strcmp(verb, "commit")) {
1131 ctlreq = G_PART_CTL_COMMIT;
1132 mparms |= G_PART_PARM_GEOM;
1134 } else if (!strcmp(verb, "create")) {
1135 ctlreq = G_PART_CTL_CREATE;
1136 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1137 oparms |= G_PART_PARM_ENTRIES;
1141 if (!strcmp(verb, "delete")) {
1142 ctlreq = G_PART_CTL_DELETE;
1143 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1144 } else if (!strcmp(verb, "destroy")) {
1145 ctlreq = G_PART_CTL_DESTROY;
1146 mparms |= G_PART_PARM_GEOM;
1150 if (!strcmp(verb, "modify")) {
1151 ctlreq = G_PART_CTL_MODIFY;
1152 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1153 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1154 } else if (!strcmp(verb, "move")) {
1155 ctlreq = G_PART_CTL_MOVE;
1156 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1160 if (!strcmp(verb, "recover")) {
1161 ctlreq = G_PART_CTL_RECOVER;
1162 mparms |= G_PART_PARM_GEOM;
1163 } else if (!strcmp(verb, "resize")) {
1164 ctlreq = G_PART_CTL_RESIZE;
1165 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1169 if (!strcmp(verb, "set")) {
1170 ctlreq = G_PART_CTL_SET;
1171 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1176 if (!strcmp(verb, "undo")) {
1177 ctlreq = G_PART_CTL_UNDO;
1178 mparms |= G_PART_PARM_GEOM;
1180 } else if (!strcmp(verb, "unset")) {
1181 ctlreq = G_PART_CTL_UNSET;
1182 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1187 if (ctlreq == G_PART_CTL_NONE) {
1188 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1192 bzero(&gpp, sizeof(gpp));
1193 for (i = 0; i < req->narg; i++) {
1196 switch (ap->name[0]) {
1198 if (!strcmp(ap->name, "attrib"))
1199 parm = G_PART_PARM_ATTRIB;
1202 if (!strcmp(ap->name, "bootcode"))
1203 parm = G_PART_PARM_BOOTCODE;
1206 if (!strcmp(ap->name, "class"))
1210 if (!strcmp(ap->name, "entries"))
1211 parm = G_PART_PARM_ENTRIES;
1214 if (!strcmp(ap->name, "flags"))
1215 parm = G_PART_PARM_FLAGS;
1218 if (!strcmp(ap->name, "geom"))
1219 parm = G_PART_PARM_GEOM;
1222 if (!strcmp(ap->name, "index"))
1223 parm = G_PART_PARM_INDEX;
1226 if (!strcmp(ap->name, "label"))
1227 parm = G_PART_PARM_LABEL;
1230 if (!strcmp(ap->name, "output"))
1231 parm = G_PART_PARM_OUTPUT;
1234 if (!strcmp(ap->name, "provider"))
1235 parm = G_PART_PARM_PROVIDER;
1238 if (!strcmp(ap->name, "scheme"))
1239 parm = G_PART_PARM_SCHEME;
1240 else if (!strcmp(ap->name, "size"))
1241 parm = G_PART_PARM_SIZE;
1242 else if (!strcmp(ap->name, "start"))
1243 parm = G_PART_PARM_START;
1246 if (!strcmp(ap->name, "type"))
1247 parm = G_PART_PARM_TYPE;
1250 if (!strcmp(ap->name, "verb"))
1252 else if (!strcmp(ap->name, "version"))
1253 parm = G_PART_PARM_VERSION;
1256 if ((parm & (mparms | oparms)) == 0) {
1257 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1260 if (parm == G_PART_PARM_BOOTCODE)
1261 p = gctl_get_param(req, ap->name, &len);
1263 p = gctl_get_asciiparam(req, ap->name);
1265 gctl_error(req, "%d param '%s'", ENOATTR, ap->name);
1269 case G_PART_PARM_ATTRIB:
1270 error = g_part_parm_str(p, &gpp.gpp_attrib);
1272 case G_PART_PARM_BOOTCODE:
1273 gpp.gpp_codeptr = p;
1274 gpp.gpp_codesize = len;
1277 case G_PART_PARM_ENTRIES:
1278 error = g_part_parm_uint(p, &gpp.gpp_entries);
1280 case G_PART_PARM_FLAGS:
1283 error = g_part_parm_str(p, &gpp.gpp_flags);
1285 case G_PART_PARM_GEOM:
1286 error = g_part_parm_geom(p, &gpp.gpp_geom);
1288 case G_PART_PARM_INDEX:
1289 error = g_part_parm_uint(p, &gpp.gpp_index);
1291 case G_PART_PARM_LABEL:
1292 /* An empty label is always valid. */
1296 case G_PART_PARM_OUTPUT:
1297 error = 0; /* Write-only parameter */
1299 case G_PART_PARM_PROVIDER:
1300 error = g_part_parm_provider(p, &gpp.gpp_provider);
1302 case G_PART_PARM_SCHEME:
1303 error = g_part_parm_scheme(p, &gpp.gpp_scheme);
1305 case G_PART_PARM_SIZE:
1306 error = g_part_parm_quad(p, &gpp.gpp_size);
1308 case G_PART_PARM_START:
1309 error = g_part_parm_quad(p, &gpp.gpp_start);
1311 case G_PART_PARM_TYPE:
1312 error = g_part_parm_str(p, &gpp.gpp_type);
1314 case G_PART_PARM_VERSION:
1315 error = g_part_parm_uint(p, &gpp.gpp_version);
1322 gctl_error(req, "%d %s '%s'", error, ap->name, p);
1325 gpp.gpp_parms |= parm;
1327 if ((gpp.gpp_parms & mparms) != mparms) {
1328 parm = mparms - (gpp.gpp_parms & mparms);
1329 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1333 /* Obtain permissions if possible/necessary. */
1336 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1337 table = gpp.gpp_geom->softc;
1338 if (table != NULL && !table->gpt_opened) {
1339 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1342 gctl_error(req, "%d geom '%s'", error,
1343 gpp.gpp_geom->name);
1346 table->gpt_opened = 1;
1351 /* Allow the scheme to check or modify the parameters. */
1352 if (table != NULL) {
1353 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1355 gctl_error(req, "%d pre-check failed", error);
1359 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1362 case G_PART_CTL_NONE:
1363 panic("%s", __func__);
1364 case G_PART_CTL_ADD:
1365 error = g_part_ctl_add(req, &gpp);
1367 case G_PART_CTL_BOOTCODE:
1368 error = g_part_ctl_bootcode(req, &gpp);
1370 case G_PART_CTL_COMMIT:
1371 error = g_part_ctl_commit(req, &gpp);
1373 case G_PART_CTL_CREATE:
1374 error = g_part_ctl_create(req, &gpp);
1376 case G_PART_CTL_DELETE:
1377 error = g_part_ctl_delete(req, &gpp);
1379 case G_PART_CTL_DESTROY:
1380 error = g_part_ctl_destroy(req, &gpp);
1382 case G_PART_CTL_MODIFY:
1383 error = g_part_ctl_modify(req, &gpp);
1385 case G_PART_CTL_MOVE:
1386 error = g_part_ctl_move(req, &gpp);
1388 case G_PART_CTL_RECOVER:
1389 error = g_part_ctl_recover(req, &gpp);
1391 case G_PART_CTL_RESIZE:
1392 error = g_part_ctl_resize(req, &gpp);
1394 case G_PART_CTL_SET:
1395 error = g_part_ctl_setunset(req, &gpp, 1);
1397 case G_PART_CTL_UNDO:
1398 error = g_part_ctl_undo(req, &gpp);
1400 case G_PART_CTL_UNSET:
1401 error = g_part_ctl_setunset(req, &gpp, 0);
1405 /* Implement automatic commit. */
1407 auto_commit = (modifies &&
1408 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1409 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1411 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, (__func__));
1412 error = g_part_ctl_commit(req, &gpp);
1417 if (error && close_on_error) {
1418 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1419 table->gpt_opened = 0;
1424 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1428 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1429 g_topology_assert();
1431 g_part_wither(gp, EINVAL);
1435 static struct g_geom *
1436 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1438 struct g_consumer *cp;
1440 struct g_part_entry *entry;
1441 struct g_part_table *table;
1442 struct root_hold_token *rht;
1446 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1447 g_topology_assert();
1450 * Create a GEOM with consumer and hook it up to the provider.
1451 * With that we become part of the topology. Optain read access
1454 gp = g_new_geomf(mp, "%s", pp->name);
1455 cp = g_new_consumer(gp);
1456 error = g_attach(cp, pp);
1458 error = g_access(cp, 1, 0, 0);
1460 g_part_wither(gp, error);
1464 rht = root_mount_hold(mp->name);
1465 g_topology_unlock();
1468 * Short-circuit the whole probing galore when there's no
1471 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1476 /* Make sure we can nest and if so, determine our depth. */
1477 error = g_getattr("PART::isleaf", cp, &attr);
1478 if (!error && attr) {
1482 error = g_getattr("PART::depth", cp, &attr);
1483 depth = (!error) ? attr + 1 : 0;
1485 error = g_part_probe(gp, cp, depth);
1492 * Synthesize a disk geometry. Some partitioning schemes
1493 * depend on it and since some file systems need it even
1494 * when the partitition scheme doesn't, we do it here in
1495 * scheme-independent code.
1497 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1499 error = G_PART_READ(table, cp);
1504 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1505 if (!entry->gpe_internal)
1506 g_part_new_provider(gp, table, entry);
1509 root_mount_rel(rht);
1510 g_access(cp, -1, 0, 0);
1515 root_mount_rel(rht);
1516 g_access(cp, -1, 0, 0);
1517 g_part_wither(gp, error);
1526 g_part_access(struct g_provider *pp, int dr, int dw, int de)
1528 struct g_consumer *cp;
1530 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1533 cp = LIST_FIRST(&pp->geom->consumer);
1535 /* We always gain write-exclusive access. */
1536 return (g_access(cp, dr, dw, dw + de));
1540 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1541 struct g_consumer *cp, struct g_provider *pp)
1544 struct g_part_entry *entry;
1545 struct g_part_table *table;
1547 KASSERT(sb != NULL && gp != NULL, (__func__));
1550 if (indent == NULL) {
1551 KASSERT(cp == NULL && pp != NULL, (__func__));
1552 entry = pp->private;
1555 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
1556 (uintmax_t)entry->gpe_offset,
1557 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1559 * libdisk compatibility quirk - the scheme dumps the
1560 * slicer name and partition type in a way that is
1561 * compatible with libdisk. When libdisk is not used
1562 * anymore, this should go away.
1564 G_PART_DUMPCONF(table, entry, sb, indent);
1565 } else if (cp != NULL) { /* Consumer configuration. */
1566 KASSERT(pp == NULL, (__func__));
1568 } else if (pp != NULL) { /* Provider configuration. */
1569 entry = pp->private;
1572 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
1573 (uintmax_t)entry->gpe_start);
1574 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
1575 (uintmax_t)entry->gpe_end);
1576 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
1578 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1579 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1580 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
1581 (uintmax_t)entry->gpe_offset);
1582 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
1583 (uintmax_t)pp->mediasize);
1584 G_PART_DUMPCONF(table, entry, sb, indent);
1585 } else { /* Geom configuration. */
1586 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
1587 table->gpt_scheme->name);
1588 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
1589 table->gpt_entries);
1590 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
1591 (uintmax_t)table->gpt_first);
1592 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
1593 (uintmax_t)table->gpt_last);
1594 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
1595 table->gpt_sectors);
1596 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
1598 G_PART_DUMPCONF(table, NULL, sb, indent);
1603 g_part_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag,
1607 struct g_part_table *table;
1608 struct g_part_entry *entry;
1613 entry = pp->private;
1616 case DIOCGPROVIDERALIAS:
1617 error = G_PART_DEVALIAS(table, entry, data, MAXPATHLEN);
1628 g_part_orphan(struct g_consumer *cp)
1630 struct g_provider *pp;
1633 KASSERT(pp != NULL, (__func__));
1634 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
1635 g_topology_assert();
1637 KASSERT(pp->error != 0, (__func__));
1638 g_part_wither(cp->geom, pp->error);
1642 g_part_spoiled(struct g_consumer *cp)
1645 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
1646 g_topology_assert();
1648 g_part_wither(cp->geom, ENXIO);
1652 g_part_start(struct bio *bp)
1655 struct g_consumer *cp;
1657 struct g_part_entry *entry;
1658 struct g_part_table *table;
1659 struct g_kerneldump *gkd;
1660 struct g_provider *pp;
1665 cp = LIST_FIRST(&gp->consumer);
1667 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
1670 entry = pp->private;
1671 if (entry == NULL) {
1672 g_io_deliver(bp, ENXIO);
1676 switch(bp->bio_cmd) {
1680 if (bp->bio_offset >= pp->mediasize) {
1681 g_io_deliver(bp, EIO);
1684 bp2 = g_clone_bio(bp);
1686 g_io_deliver(bp, ENOMEM);
1689 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
1690 bp2->bio_length = pp->mediasize - bp2->bio_offset;
1691 bp2->bio_done = g_std_done;
1692 bp2->bio_offset += entry->gpe_offset;
1693 g_io_request(bp2, cp);
1698 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
1700 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
1702 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
1704 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
1706 if (g_handleattr_str(bp, "PART::scheme",
1707 table->gpt_scheme->name))
1709 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
1711 * Check that the partition is suitable for kernel
1712 * dumps. Typically only swap partitions should be
1715 if (!G_PART_DUMPTO(table, entry)) {
1716 g_io_deliver(bp, ENXIO);
1719 gkd = (struct g_kerneldump *)bp->bio_data;
1720 if (gkd->offset >= pp->mediasize) {
1721 g_io_deliver(bp, EIO);
1724 if (gkd->offset + gkd->length > pp->mediasize)
1725 gkd->length = pp->mediasize - gkd->offset;
1726 gkd->offset += entry->gpe_offset;
1730 g_io_deliver(bp, EOPNOTSUPP);
1734 bp2 = g_clone_bio(bp);
1736 g_io_deliver(bp, ENOMEM);
1739 bp2->bio_done = g_std_done;
1740 g_io_request(bp2, cp);
1744 g_part_init(struct g_class *mp)
1747 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
1751 g_part_fini(struct g_class *mp)
1754 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
1758 g_part_unload_event(void *arg, int flag)
1760 struct g_consumer *cp;
1762 struct g_provider *pp;
1763 struct g_part_scheme *scheme;
1764 struct g_part_table *table;
1768 if (flag == EV_CANCEL)
1773 scheme = (void *)(*xchg);
1775 g_topology_assert();
1777 LIST_FOREACH(gp, &g_part_class.geom, geom) {
1779 if (table->gpt_scheme != scheme)
1783 LIST_FOREACH(pp, &gp->provider, provider)
1784 acc += pp->acr + pp->acw + pp->ace;
1785 LIST_FOREACH(cp, &gp->consumer, consumer)
1786 acc += cp->acr + cp->acw + cp->ace;
1789 g_part_wither(gp, ENOSYS);
1795 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1801 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
1808 TAILQ_INSERT_TAIL(&g_part_schemes, scheme, scheme_list);
1810 error = g_retaste(&g_part_class);
1812 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1815 arg = (uintptr_t)scheme;
1816 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,
1819 error = (arg == (uintptr_t)scheme) ? EDOOFUS : arg;