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);
483 if (index > table->gpt_entries) {
484 gctl_error(req, "%d index '%d'", ENOSPC, index);
488 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
489 M_WAITOK | M_ZERO) : delent;
490 entry->gpe_index = index;
491 entry->gpe_start = gpp->gpp_start;
492 entry->gpe_end = end;
493 error = G_PART_ADD(table, entry, gpp);
495 gctl_error(req, "%d", error);
500 if (delent == NULL) {
502 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
504 LIST_INSERT_AFTER(last, entry, gpe_entry);
505 entry->gpe_created = 1;
507 entry->gpe_deleted = 0;
508 entry->gpe_modified = 1;
510 g_part_new_provider(gp, table, entry);
512 /* Provide feedback if so requested. */
513 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
514 sb = sbuf_new_auto();
515 G_PART_FULLNAME(table, entry, sb, gp->name);
516 sbuf_cat(sb, " added\n");
518 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
525 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
528 struct g_part_table *table;
533 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
537 sz = table->gpt_scheme->gps_bootcodesz;
542 if (gpp->gpp_codesize > sz) {
547 error = G_PART_BOOTCODE(table, gpp);
551 /* Provide feedback if so requested. */
552 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
553 sb = sbuf_new_auto();
554 sbuf_printf(sb, "%s has bootcode\n", gp->name);
556 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
562 gctl_error(req, "%d", error);
567 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
569 struct g_consumer *cp;
571 struct g_provider *pp;
572 struct g_part_entry *entry, *tmp;
573 struct g_part_table *table;
578 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
582 if (!table->gpt_opened) {
583 gctl_error(req, "%d", EPERM);
589 cp = LIST_FIRST(&gp->consumer);
590 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
592 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
593 while (table->gpt_smhead != 0) {
594 i = ffs(table->gpt_smhead) - 1;
595 error = g_write_data(cp, i * pp->sectorsize, buf,
601 table->gpt_smhead &= ~(1 << i);
603 while (table->gpt_smtail != 0) {
604 i = ffs(table->gpt_smtail) - 1;
605 error = g_write_data(cp, pp->mediasize - (i + 1) *
606 pp->sectorsize, buf, pp->sectorsize);
611 table->gpt_smtail &= ~(1 << i);
616 if (table->gpt_scheme == &g_part_null_scheme) {
618 g_access(cp, -1, -1, -1);
619 g_part_wither(gp, ENXIO);
623 error = G_PART_WRITE(table, cp);
627 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
628 if (!entry->gpe_deleted) {
629 entry->gpe_created = 0;
630 entry->gpe_modified = 0;
633 LIST_REMOVE(entry, gpe_entry);
636 table->gpt_created = 0;
637 table->gpt_opened = 0;
640 g_access(cp, -1, -1, -1);
645 gctl_error(req, "%d", error);
650 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
652 struct g_consumer *cp;
654 struct g_provider *pp;
655 struct g_part_scheme *scheme;
656 struct g_part_table *null, *table;
660 pp = gpp->gpp_provider;
661 scheme = gpp->gpp_scheme;
662 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
665 /* Check that there isn't already a g_part geom on the provider. */
666 error = g_part_parm_geom(pp->name, &gp);
669 if (null->gpt_scheme != &g_part_null_scheme) {
670 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
676 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
677 (gpp->gpp_entries < scheme->gps_minent ||
678 gpp->gpp_entries > scheme->gps_maxent)) {
679 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
684 gp = g_new_geomf(&g_part_class, "%s", pp->name);
685 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
689 table->gpt_scheme = gpp->gpp_scheme;
690 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
691 gpp->gpp_entries : scheme->gps_minent;
692 LIST_INIT(&table->gpt_entry);
694 cp = g_new_consumer(gp);
695 error = g_attach(cp, pp);
697 error = g_access(cp, 1, 1, 1);
699 g_part_wither(gp, error);
700 gctl_error(req, "%d geom '%s'", error, pp->name);
703 table->gpt_opened = 1;
705 cp = LIST_FIRST(&gp->consumer);
706 table->gpt_opened = null->gpt_opened;
707 table->gpt_smhead = null->gpt_smhead;
708 table->gpt_smtail = null->gpt_smtail;
713 /* Make sure the provider has media. */
714 if (pp->mediasize == 0 || pp->sectorsize == 0) {
719 /* Make sure we can nest and if so, determine our depth. */
720 error = g_getattr("PART::isleaf", cp, &attr);
721 if (!error && attr) {
725 error = g_getattr("PART::depth", cp, &attr);
726 table->gpt_depth = (!error) ? attr + 1 : 0;
729 * Synthesize a disk geometry. Some partitioning schemes
730 * depend on it and since some file systems need it even
731 * when the partitition scheme doesn't, we do it here in
732 * scheme-independent code.
734 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
736 error = G_PART_CREATE(table, gpp);
742 table->gpt_created = 1;
744 kobj_delete((kobj_t)null, M_GEOM);
747 * Support automatic commit by filling in the gpp_geom
750 gpp->gpp_parms |= G_PART_PARM_GEOM;
753 /* Provide feedback if so requested. */
754 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
755 sb = sbuf_new_auto();
756 sbuf_printf(sb, "%s created\n", gp->name);
758 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
766 g_access(cp, -1, -1, -1);
767 g_part_wither(gp, error);
769 kobj_delete((kobj_t)gp->softc, M_GEOM);
772 gctl_error(req, "%d provider", error);
777 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
780 struct g_provider *pp;
781 struct g_part_entry *entry;
782 struct g_part_table *table;
786 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
791 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
792 if (entry->gpe_deleted || entry->gpe_internal)
794 if (entry->gpe_index == gpp->gpp_index)
798 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
804 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
805 gctl_error(req, "%d", EBUSY);
810 entry->gpe_pp = NULL;
813 if (entry->gpe_created) {
814 LIST_REMOVE(entry, gpe_entry);
817 entry->gpe_modified = 0;
818 entry->gpe_deleted = 1;
822 g_wither_provider(pp, ENXIO);
824 /* Provide feedback if so requested. */
825 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
826 sb = sbuf_new_auto();
827 G_PART_FULLNAME(table, entry, sb, gp->name);
828 sbuf_cat(sb, " deleted\n");
830 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
837 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
840 struct g_part_entry *entry;
841 struct g_part_table *null, *table;
846 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
850 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
851 if (entry->gpe_deleted || entry->gpe_internal)
853 gctl_error(req, "%d", EBUSY);
857 error = G_PART_DESTROY(table, gpp);
859 gctl_error(req, "%d", error);
863 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
867 null->gpt_scheme = &g_part_null_scheme;
868 LIST_INIT(&null->gpt_entry);
869 null->gpt_depth = table->gpt_depth;
870 null->gpt_opened = table->gpt_opened;
871 null->gpt_smhead = table->gpt_smhead;
872 null->gpt_smtail = table->gpt_smtail;
874 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
875 LIST_REMOVE(entry, gpe_entry);
878 kobj_delete((kobj_t)table, M_GEOM);
880 /* Provide feedback if so requested. */
881 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
882 sb = sbuf_new_auto();
883 sbuf_printf(sb, "%s destroyed\n", gp->name);
885 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
892 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
895 struct g_part_entry *entry;
896 struct g_part_table *table;
901 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
906 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
907 if (entry->gpe_deleted || entry->gpe_internal)
909 if (entry->gpe_index == gpp->gpp_index)
913 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
917 error = G_PART_MODIFY(table, entry, gpp);
919 gctl_error(req, "%d", error);
923 if (!entry->gpe_created)
924 entry->gpe_modified = 1;
926 /* Provide feedback if so requested. */
927 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
928 sb = sbuf_new_auto();
929 G_PART_FULLNAME(table, entry, sb, gp->name);
930 sbuf_cat(sb, " modified\n");
932 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
939 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
941 gctl_error(req, "%d verb 'move'", ENOSYS);
946 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
948 gctl_error(req, "%d verb 'recover'", ENOSYS);
953 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
955 gctl_error(req, "%d verb 'resize'", ENOSYS);
960 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
964 struct g_part_entry *entry;
965 struct g_part_table *table;
970 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
975 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
976 if (entry->gpe_deleted || entry->gpe_internal)
978 if (entry->gpe_index == gpp->gpp_index)
982 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
986 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
988 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
992 /* Provide feedback if so requested. */
993 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
994 sb = sbuf_new_auto();
995 G_PART_FULLNAME(table, entry, sb, gp->name);
996 sbuf_printf(sb, " has %s %sset\n", gpp->gpp_attrib,
999 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1006 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1008 struct g_consumer *cp;
1009 struct g_provider *pp;
1011 struct g_part_entry *entry, *tmp;
1012 struct g_part_table *table;
1016 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1017 g_topology_assert();
1020 if (!table->gpt_opened) {
1021 gctl_error(req, "%d", EPERM);
1025 cp = LIST_FIRST(&gp->consumer);
1026 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1027 entry->gpe_modified = 0;
1028 if (entry->gpe_created) {
1032 entry->gpe_pp = NULL;
1033 g_wither_provider(pp, ENXIO);
1035 entry->gpe_deleted = 1;
1037 if (entry->gpe_deleted) {
1038 LIST_REMOVE(entry, gpe_entry);
1043 g_topology_unlock();
1045 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1046 table->gpt_created) ? 1 : 0;
1049 if (!LIST_EMPTY(&table->gpt_entry)) {
1053 error = g_part_probe(gp, cp, table->gpt_depth);
1056 g_access(cp, -1, -1, -1);
1057 g_part_wither(gp, error);
1063 error = G_PART_READ(table, cp);
1069 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1070 if (!entry->gpe_internal)
1071 g_part_new_provider(gp, table, entry);
1074 table->gpt_opened = 0;
1075 g_access(cp, -1, -1, -1);
1080 gctl_error(req, "%d", error);
1085 g_part_wither(struct g_geom *gp, int error)
1087 struct g_part_entry *entry;
1088 struct g_part_table *table;
1091 if (table != NULL) {
1092 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1093 LIST_REMOVE(entry, gpe_entry);
1096 if (gp->softc != NULL) {
1097 kobj_delete((kobj_t)gp->softc, M_GEOM);
1101 g_wither_geom(gp, error);
1109 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1111 struct g_part_parms gpp;
1112 struct g_part_table *table;
1113 struct gctl_req_arg *ap;
1115 enum g_part_ctl ctlreq;
1116 unsigned int i, mparms, oparms, parm;
1117 int auto_commit, close_on_error;
1118 int error, len, modifies;
1120 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1121 g_topology_assert();
1123 ctlreq = G_PART_CTL_NONE;
1126 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1129 if (!strcmp(verb, "add")) {
1130 ctlreq = G_PART_CTL_ADD;
1131 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1132 G_PART_PARM_START | G_PART_PARM_TYPE;
1133 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1137 if (!strcmp(verb, "bootcode")) {
1138 ctlreq = G_PART_CTL_BOOTCODE;
1139 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1143 if (!strcmp(verb, "commit")) {
1144 ctlreq = G_PART_CTL_COMMIT;
1145 mparms |= G_PART_PARM_GEOM;
1147 } else if (!strcmp(verb, "create")) {
1148 ctlreq = G_PART_CTL_CREATE;
1149 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1150 oparms |= G_PART_PARM_ENTRIES;
1154 if (!strcmp(verb, "delete")) {
1155 ctlreq = G_PART_CTL_DELETE;
1156 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1157 } else if (!strcmp(verb, "destroy")) {
1158 ctlreq = G_PART_CTL_DESTROY;
1159 mparms |= G_PART_PARM_GEOM;
1163 if (!strcmp(verb, "modify")) {
1164 ctlreq = G_PART_CTL_MODIFY;
1165 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1166 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1167 } else if (!strcmp(verb, "move")) {
1168 ctlreq = G_PART_CTL_MOVE;
1169 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1173 if (!strcmp(verb, "recover")) {
1174 ctlreq = G_PART_CTL_RECOVER;
1175 mparms |= G_PART_PARM_GEOM;
1176 } else if (!strcmp(verb, "resize")) {
1177 ctlreq = G_PART_CTL_RESIZE;
1178 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1182 if (!strcmp(verb, "set")) {
1183 ctlreq = G_PART_CTL_SET;
1184 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1189 if (!strcmp(verb, "undo")) {
1190 ctlreq = G_PART_CTL_UNDO;
1191 mparms |= G_PART_PARM_GEOM;
1193 } else if (!strcmp(verb, "unset")) {
1194 ctlreq = G_PART_CTL_UNSET;
1195 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1200 if (ctlreq == G_PART_CTL_NONE) {
1201 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1205 bzero(&gpp, sizeof(gpp));
1206 for (i = 0; i < req->narg; i++) {
1209 switch (ap->name[0]) {
1211 if (!strcmp(ap->name, "attrib"))
1212 parm = G_PART_PARM_ATTRIB;
1215 if (!strcmp(ap->name, "bootcode"))
1216 parm = G_PART_PARM_BOOTCODE;
1219 if (!strcmp(ap->name, "class"))
1223 if (!strcmp(ap->name, "entries"))
1224 parm = G_PART_PARM_ENTRIES;
1227 if (!strcmp(ap->name, "flags"))
1228 parm = G_PART_PARM_FLAGS;
1231 if (!strcmp(ap->name, "geom"))
1232 parm = G_PART_PARM_GEOM;
1235 if (!strcmp(ap->name, "index"))
1236 parm = G_PART_PARM_INDEX;
1239 if (!strcmp(ap->name, "label"))
1240 parm = G_PART_PARM_LABEL;
1243 if (!strcmp(ap->name, "output"))
1244 parm = G_PART_PARM_OUTPUT;
1247 if (!strcmp(ap->name, "provider"))
1248 parm = G_PART_PARM_PROVIDER;
1251 if (!strcmp(ap->name, "scheme"))
1252 parm = G_PART_PARM_SCHEME;
1253 else if (!strcmp(ap->name, "size"))
1254 parm = G_PART_PARM_SIZE;
1255 else if (!strcmp(ap->name, "start"))
1256 parm = G_PART_PARM_START;
1259 if (!strcmp(ap->name, "type"))
1260 parm = G_PART_PARM_TYPE;
1263 if (!strcmp(ap->name, "verb"))
1265 else if (!strcmp(ap->name, "version"))
1266 parm = G_PART_PARM_VERSION;
1269 if ((parm & (mparms | oparms)) == 0) {
1270 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1273 if (parm == G_PART_PARM_BOOTCODE)
1274 p = gctl_get_param(req, ap->name, &len);
1276 p = gctl_get_asciiparam(req, ap->name);
1278 gctl_error(req, "%d param '%s'", ENOATTR, ap->name);
1282 case G_PART_PARM_ATTRIB:
1283 error = g_part_parm_str(p, &gpp.gpp_attrib);
1285 case G_PART_PARM_BOOTCODE:
1286 gpp.gpp_codeptr = p;
1287 gpp.gpp_codesize = len;
1290 case G_PART_PARM_ENTRIES:
1291 error = g_part_parm_uint(p, &gpp.gpp_entries);
1293 case G_PART_PARM_FLAGS:
1296 error = g_part_parm_str(p, &gpp.gpp_flags);
1298 case G_PART_PARM_GEOM:
1299 error = g_part_parm_geom(p, &gpp.gpp_geom);
1301 case G_PART_PARM_INDEX:
1302 error = g_part_parm_uint(p, &gpp.gpp_index);
1304 case G_PART_PARM_LABEL:
1305 /* An empty label is always valid. */
1309 case G_PART_PARM_OUTPUT:
1310 error = 0; /* Write-only parameter */
1312 case G_PART_PARM_PROVIDER:
1313 error = g_part_parm_provider(p, &gpp.gpp_provider);
1315 case G_PART_PARM_SCHEME:
1316 error = g_part_parm_scheme(p, &gpp.gpp_scheme);
1318 case G_PART_PARM_SIZE:
1319 error = g_part_parm_quad(p, &gpp.gpp_size);
1321 case G_PART_PARM_START:
1322 error = g_part_parm_quad(p, &gpp.gpp_start);
1324 case G_PART_PARM_TYPE:
1325 error = g_part_parm_str(p, &gpp.gpp_type);
1327 case G_PART_PARM_VERSION:
1328 error = g_part_parm_uint(p, &gpp.gpp_version);
1335 gctl_error(req, "%d %s '%s'", error, ap->name, p);
1338 gpp.gpp_parms |= parm;
1340 if ((gpp.gpp_parms & mparms) != mparms) {
1341 parm = mparms - (gpp.gpp_parms & mparms);
1342 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1346 /* Obtain permissions if possible/necessary. */
1349 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1350 table = gpp.gpp_geom->softc;
1351 if (table != NULL && !table->gpt_opened) {
1352 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1355 gctl_error(req, "%d geom '%s'", error,
1356 gpp.gpp_geom->name);
1359 table->gpt_opened = 1;
1364 /* Allow the scheme to check or modify the parameters. */
1365 if (table != NULL) {
1366 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1368 gctl_error(req, "%d pre-check failed", error);
1372 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1375 case G_PART_CTL_NONE:
1376 panic("%s", __func__);
1377 case G_PART_CTL_ADD:
1378 error = g_part_ctl_add(req, &gpp);
1380 case G_PART_CTL_BOOTCODE:
1381 error = g_part_ctl_bootcode(req, &gpp);
1383 case G_PART_CTL_COMMIT:
1384 error = g_part_ctl_commit(req, &gpp);
1386 case G_PART_CTL_CREATE:
1387 error = g_part_ctl_create(req, &gpp);
1389 case G_PART_CTL_DELETE:
1390 error = g_part_ctl_delete(req, &gpp);
1392 case G_PART_CTL_DESTROY:
1393 error = g_part_ctl_destroy(req, &gpp);
1395 case G_PART_CTL_MODIFY:
1396 error = g_part_ctl_modify(req, &gpp);
1398 case G_PART_CTL_MOVE:
1399 error = g_part_ctl_move(req, &gpp);
1401 case G_PART_CTL_RECOVER:
1402 error = g_part_ctl_recover(req, &gpp);
1404 case G_PART_CTL_RESIZE:
1405 error = g_part_ctl_resize(req, &gpp);
1407 case G_PART_CTL_SET:
1408 error = g_part_ctl_setunset(req, &gpp, 1);
1410 case G_PART_CTL_UNDO:
1411 error = g_part_ctl_undo(req, &gpp);
1413 case G_PART_CTL_UNSET:
1414 error = g_part_ctl_setunset(req, &gpp, 0);
1418 /* Implement automatic commit. */
1420 auto_commit = (modifies &&
1421 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1422 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1424 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, (__func__));
1425 error = g_part_ctl_commit(req, &gpp);
1430 if (error && close_on_error) {
1431 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1432 table->gpt_opened = 0;
1437 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1441 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1442 g_topology_assert();
1444 g_part_wither(gp, EINVAL);
1448 static struct g_geom *
1449 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1451 struct g_consumer *cp;
1453 struct g_part_entry *entry;
1454 struct g_part_table *table;
1455 struct root_hold_token *rht;
1459 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1460 g_topology_assert();
1462 /* Skip providers that are already open for writing. */
1467 * Create a GEOM with consumer and hook it up to the provider.
1468 * With that we become part of the topology. Optain read access
1471 gp = g_new_geomf(mp, "%s", pp->name);
1472 cp = g_new_consumer(gp);
1473 error = g_attach(cp, pp);
1475 error = g_access(cp, 1, 0, 0);
1477 g_part_wither(gp, error);
1481 rht = root_mount_hold(mp->name);
1482 g_topology_unlock();
1485 * Short-circuit the whole probing galore when there's no
1488 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1493 /* Make sure we can nest and if so, determine our depth. */
1494 error = g_getattr("PART::isleaf", cp, &attr);
1495 if (!error && attr) {
1499 error = g_getattr("PART::depth", cp, &attr);
1500 depth = (!error) ? attr + 1 : 0;
1502 error = g_part_probe(gp, cp, depth);
1509 * Synthesize a disk geometry. Some partitioning schemes
1510 * depend on it and since some file systems need it even
1511 * when the partitition scheme doesn't, we do it here in
1512 * scheme-independent code.
1514 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1516 error = G_PART_READ(table, cp);
1521 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1522 if (!entry->gpe_internal)
1523 g_part_new_provider(gp, table, entry);
1526 root_mount_rel(rht);
1527 g_access(cp, -1, 0, 0);
1532 root_mount_rel(rht);
1533 g_access(cp, -1, 0, 0);
1534 g_part_wither(gp, error);
1543 g_part_access(struct g_provider *pp, int dr, int dw, int de)
1545 struct g_consumer *cp;
1547 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1550 cp = LIST_FIRST(&pp->geom->consumer);
1552 /* We always gain write-exclusive access. */
1553 return (g_access(cp, dr, dw, dw + de));
1557 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1558 struct g_consumer *cp, struct g_provider *pp)
1561 struct g_part_entry *entry;
1562 struct g_part_table *table;
1564 KASSERT(sb != NULL && gp != NULL, (__func__));
1567 if (indent == NULL) {
1568 KASSERT(cp == NULL && pp != NULL, (__func__));
1569 entry = pp->private;
1572 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
1573 (uintmax_t)entry->gpe_offset,
1574 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1576 * libdisk compatibility quirk - the scheme dumps the
1577 * slicer name and partition type in a way that is
1578 * compatible with libdisk. When libdisk is not used
1579 * anymore, this should go away.
1581 G_PART_DUMPCONF(table, entry, sb, indent);
1582 } else if (cp != NULL) { /* Consumer configuration. */
1583 KASSERT(pp == NULL, (__func__));
1585 } else if (pp != NULL) { /* Provider configuration. */
1586 entry = pp->private;
1589 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
1590 (uintmax_t)entry->gpe_start);
1591 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
1592 (uintmax_t)entry->gpe_end);
1593 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
1595 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1596 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1597 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
1598 (uintmax_t)entry->gpe_offset);
1599 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
1600 (uintmax_t)pp->mediasize);
1601 G_PART_DUMPCONF(table, entry, sb, indent);
1602 } else { /* Geom configuration. */
1603 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
1604 table->gpt_scheme->name);
1605 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
1606 table->gpt_entries);
1607 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
1608 (uintmax_t)table->gpt_first);
1609 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
1610 (uintmax_t)table->gpt_last);
1611 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
1612 table->gpt_sectors);
1613 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
1615 G_PART_DUMPCONF(table, NULL, sb, indent);
1620 g_part_orphan(struct g_consumer *cp)
1622 struct g_provider *pp;
1625 KASSERT(pp != NULL, (__func__));
1626 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
1627 g_topology_assert();
1629 KASSERT(pp->error != 0, (__func__));
1630 g_part_wither(cp->geom, pp->error);
1634 g_part_spoiled(struct g_consumer *cp)
1637 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
1638 g_topology_assert();
1640 g_part_wither(cp->geom, ENXIO);
1644 g_part_start(struct bio *bp)
1647 struct g_consumer *cp;
1649 struct g_part_entry *entry;
1650 struct g_part_table *table;
1651 struct g_kerneldump *gkd;
1652 struct g_provider *pp;
1657 cp = LIST_FIRST(&gp->consumer);
1659 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
1662 entry = pp->private;
1663 if (entry == NULL) {
1664 g_io_deliver(bp, ENXIO);
1668 switch(bp->bio_cmd) {
1672 if (bp->bio_offset >= pp->mediasize) {
1673 g_io_deliver(bp, EIO);
1676 bp2 = g_clone_bio(bp);
1678 g_io_deliver(bp, ENOMEM);
1681 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
1682 bp2->bio_length = pp->mediasize - bp2->bio_offset;
1683 bp2->bio_done = g_std_done;
1684 bp2->bio_offset += entry->gpe_offset;
1685 g_io_request(bp2, cp);
1690 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
1692 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
1694 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
1696 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
1698 if (g_handleattr_str(bp, "PART::scheme",
1699 table->gpt_scheme->name))
1701 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
1703 * Check that the partition is suitable for kernel
1704 * dumps. Typically only swap partitions should be
1707 if (!G_PART_DUMPTO(table, entry)) {
1708 g_io_deliver(bp, ENODEV);
1709 printf("GEOM_PART: Partition '%s' not suitable"
1710 " for kernel dumps (wrong type?)\n",
1714 gkd = (struct g_kerneldump *)bp->bio_data;
1715 if (gkd->offset >= pp->mediasize) {
1716 g_io_deliver(bp, EIO);
1719 if (gkd->offset + gkd->length > pp->mediasize)
1720 gkd->length = pp->mediasize - gkd->offset;
1721 gkd->offset += entry->gpe_offset;
1725 g_io_deliver(bp, EOPNOTSUPP);
1729 bp2 = g_clone_bio(bp);
1731 g_io_deliver(bp, ENOMEM);
1734 bp2->bio_done = g_std_done;
1735 g_io_request(bp2, cp);
1739 g_part_init(struct g_class *mp)
1742 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
1746 g_part_fini(struct g_class *mp)
1749 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
1753 g_part_unload_event(void *arg, int flag)
1755 struct g_consumer *cp;
1757 struct g_provider *pp;
1758 struct g_part_scheme *scheme;
1759 struct g_part_table *table;
1763 if (flag == EV_CANCEL)
1768 scheme = (void *)(*xchg);
1770 g_topology_assert();
1772 LIST_FOREACH(gp, &g_part_class.geom, geom) {
1774 if (table->gpt_scheme != scheme)
1778 LIST_FOREACH(pp, &gp->provider, provider)
1779 acc += pp->acr + pp->acw + pp->ace;
1780 LIST_FOREACH(cp, &gp->consumer, consumer)
1781 acc += cp->acr + cp->acw + cp->ace;
1784 g_part_wither(gp, ENOSYS);
1790 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1796 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
1803 TAILQ_INSERT_TAIL(&g_part_schemes, scheme, scheme_list);
1805 error = g_retaste(&g_part_class);
1807 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1810 arg = (uintptr_t)scheme;
1811 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,
1814 error = (arg == (uintptr_t)scheme) ? EDOOFUS : arg;