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-boot", G_PART_ALIAS_APPLE_BOOT },
73 { "apple-hfs", G_PART_ALIAS_APPLE_HFS },
74 { "apple-label", G_PART_ALIAS_APPLE_LABEL },
75 { "apple-raid", G_PART_ALIAS_APPLE_RAID },
76 { "apple-raid-offline", G_PART_ALIAS_APPLE_RAID_OFFLINE },
77 { "apple-tv-recovery", G_PART_ALIAS_APPLE_TV_RECOVERY },
78 { "apple-ufs", G_PART_ALIAS_APPLE_UFS },
79 { "efi", G_PART_ALIAS_EFI },
80 { "freebsd", G_PART_ALIAS_FREEBSD },
81 { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
82 { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
83 { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
84 { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
85 { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
86 { "linux-data", G_PART_ALIAS_LINUX_DATA },
87 { "linux-lvm", G_PART_ALIAS_LINUX_LVM },
88 { "linux-raid", G_PART_ALIAS_LINUX_RAID },
89 { "linux-swap", G_PART_ALIAS_LINUX_SWAP },
90 { "ms-basic-data", G_PART_ALIAS_MS_BASIC_DATA },
91 { "ms-ldm-data", G_PART_ALIAS_MS_LDM_DATA },
92 { "ms-ldm-metadata", G_PART_ALIAS_MS_LDM_METADATA },
93 { "ms-reserved", G_PART_ALIAS_MS_RESERVED },
94 { "ntfs", G_PART_ALIAS_MS_NTFS },
95 { "netbsd-ccd", G_PART_ALIAS_NETBSD_CCD },
96 { "netbsd-cgd", G_PART_ALIAS_NETBSD_CGD },
97 { "netbsd-ffs", G_PART_ALIAS_NETBSD_FFS },
98 { "netbsd-lfs", G_PART_ALIAS_NETBSD_LFS },
99 { "netbsd-raid", G_PART_ALIAS_NETBSD_RAID },
100 { "netbsd-swap", G_PART_ALIAS_NETBSD_SWAP },
101 { "mbr", G_PART_ALIAS_MBR }
105 * The GEOM partitioning class.
107 static g_ctl_req_t g_part_ctlreq;
108 static g_ctl_destroy_geom_t g_part_destroy_geom;
109 static g_fini_t g_part_fini;
110 static g_init_t g_part_init;
111 static g_taste_t g_part_taste;
113 static g_access_t g_part_access;
114 static g_dumpconf_t g_part_dumpconf;
115 static g_orphan_t g_part_orphan;
116 static g_spoiled_t g_part_spoiled;
117 static g_start_t g_part_start;
119 static struct g_class g_part_class = {
121 .version = G_VERSION,
123 .ctlreq = g_part_ctlreq,
124 .destroy_geom = g_part_destroy_geom,
127 .taste = g_part_taste,
129 .access = g_part_access,
130 .dumpconf = g_part_dumpconf,
131 .orphan = g_part_orphan,
132 .spoiled = g_part_spoiled,
133 .start = g_part_start,
136 DECLARE_GEOM_CLASS(g_part_class, g_part);
142 static void g_part_wither(struct g_geom *, int);
145 g_part_alias_name(enum g_part_alias alias)
149 for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
150 if (g_part_alias_list[i].alias != alias)
152 return (g_part_alias_list[i].lexeme);
159 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
162 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
163 off_t chs, cylinders;
169 for (idx = 0; candidate_heads[idx] != 0; idx++) {
170 heads = candidate_heads[idx];
171 cylinders = blocks / heads / sectors;
172 if (cylinders < heads || cylinders < sectors)
174 if (cylinders > 1023)
176 chs = cylinders * heads * sectors;
177 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
185 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
188 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
190 u_int heads, sectors;
193 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 || sectors == 0 ||
194 g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
195 table->gpt_fixgeom = 0;
196 table->gpt_heads = 0;
197 table->gpt_sectors = 0;
199 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
200 sectors = candidate_sectors[idx];
201 g_part_geometry_heads(blocks, sectors, &chs, &heads);
205 * Prefer a geometry with sectors > 1, but only if
206 * it doesn't bump down the numbver of heads to 1.
208 if (chs > bestchs || (chs == bestchs && heads > 1 &&
209 table->gpt_sectors == 1)) {
211 table->gpt_heads = heads;
212 table->gpt_sectors = sectors;
216 * If we didn't find a geometry at all, then the disk is
217 * too big. This means we can use the maximum number of
221 table->gpt_heads = 255;
222 table->gpt_sectors = 63;
225 table->gpt_fixgeom = 1;
226 table->gpt_heads = heads;
227 table->gpt_sectors = sectors;
231 struct g_part_entry *
232 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
235 struct g_part_entry *entry, *last;
238 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
239 if (entry->gpe_index == index)
241 if (entry->gpe_index > index) {
248 entry = g_malloc(table->gpt_scheme->gps_entrysz,
250 entry->gpe_index = index;
252 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
254 LIST_INSERT_AFTER(last, entry, gpe_entry);
256 entry->gpe_offset = 0;
257 entry->gpe_start = start;
258 entry->gpe_end = end;
263 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
264 struct g_part_entry *entry)
266 struct g_consumer *cp;
267 struct g_provider *pp;
271 cp = LIST_FIRST(&gp->consumer);
274 offset = entry->gpe_start * pp->sectorsize;
275 if (entry->gpe_offset < offset)
276 entry->gpe_offset = offset;
278 if (entry->gpe_pp == NULL) {
279 sb = sbuf_new_auto();
280 G_PART_FULLNAME(table, entry, sb, gp->name);
282 entry->gpe_pp = g_new_providerf(gp, "%s", sbuf_data(sb));
284 entry->gpe_pp->private = entry; /* Close the circle. */
286 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */
287 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
289 entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
290 entry->gpe_pp->sectorsize = pp->sectorsize;
291 entry->gpe_pp->flags = pp->flags & G_PF_CANDELETE;
292 entry->gpe_pp->stripesize = pp->stripesize;
293 entry->gpe_pp->stripeoffset = pp->stripeoffset + entry->gpe_offset;
294 if (pp->stripesize > 0)
295 entry->gpe_pp->stripeoffset %= pp->stripesize;
296 g_error_provider(entry->gpe_pp, 0);
300 g_part_parm_geom(const char *name, struct g_geom **v)
304 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
305 name += strlen(_PATH_DEV);
306 LIST_FOREACH(gp, &g_part_class.geom, geom) {
307 if (!strcmp(name, gp->name))
317 g_part_parm_provider(const char *name, struct g_provider **v)
319 struct g_provider *pp;
321 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
322 name += strlen(_PATH_DEV);
323 pp = g_provider_by_name(name);
331 g_part_parm_quad(const char *p, quad_t *v)
336 q = strtoq(p, &x, 0);
337 if (*x != '\0' || q < 0)
344 g_part_parm_scheme(const char *p, struct g_part_scheme **v)
346 struct g_part_scheme *s;
348 TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
349 if (s == &g_part_null_scheme)
351 if (!strcasecmp(s->name, p))
361 g_part_parm_str(const char *p, const char **v)
371 g_part_parm_uint(const char *p, u_int *v)
376 l = strtol(p, &x, 0);
377 if (*x != '\0' || l < 0 || l > INT_MAX)
379 *v = (unsigned int)l;
384 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
386 struct g_part_scheme *iter, *scheme;
387 struct g_part_table *table;
391 scheme = (table != NULL) ? table->gpt_scheme : NULL;
392 pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
395 if (pri > 0) { /* error */
400 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
401 if (iter == &g_part_null_scheme)
403 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
406 table->gpt_scheme = iter;
407 table->gpt_depth = depth;
408 probe = G_PART_PROBE(table, cp);
409 if (probe <= 0 && probe > pri) {
412 if (gp->softc != NULL)
413 kobj_delete((kobj_t)gp->softc, M_GEOM);
418 kobj_delete((kobj_t)table, M_GEOM);
422 return ((scheme == NULL) ? ENXIO : 0);
426 * Control request functions.
430 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
433 struct g_provider *pp;
434 struct g_part_entry *delent, *last, *entry;
435 struct g_part_table *table;
442 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
445 pp = LIST_FIRST(&gp->consumer)->provider;
447 end = gpp->gpp_start + gpp->gpp_size - 1;
449 if (gpp->gpp_start < table->gpt_first ||
450 gpp->gpp_start > table->gpt_last) {
451 gctl_error(req, "%d start '%jd'", EINVAL,
452 (intmax_t)gpp->gpp_start);
455 if (end < gpp->gpp_start || end > table->gpt_last) {
456 gctl_error(req, "%d size '%jd'", EINVAL,
457 (intmax_t)gpp->gpp_size);
460 if (gpp->gpp_index > table->gpt_entries) {
461 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
465 delent = last = NULL;
466 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
467 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
468 if (entry->gpe_deleted) {
469 if (entry->gpe_index == index)
473 if (entry->gpe_index == index)
474 index = entry->gpe_index + 1;
475 if (entry->gpe_index < index)
477 if (entry->gpe_internal)
479 if (gpp->gpp_start >= entry->gpe_start &&
480 gpp->gpp_start <= entry->gpe_end) {
481 gctl_error(req, "%d start '%jd'", ENOSPC,
482 (intmax_t)gpp->gpp_start);
485 if (end >= entry->gpe_start && end <= entry->gpe_end) {
486 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
489 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
490 gctl_error(req, "%d size '%jd'", ENOSPC,
491 (intmax_t)gpp->gpp_size);
495 if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
496 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
499 if (index > table->gpt_entries) {
500 gctl_error(req, "%d index '%d'", ENOSPC, index);
504 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
505 M_WAITOK | M_ZERO) : delent;
506 entry->gpe_index = index;
507 entry->gpe_start = gpp->gpp_start;
508 entry->gpe_end = end;
509 error = G_PART_ADD(table, entry, gpp);
511 gctl_error(req, "%d", error);
516 if (delent == NULL) {
518 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
520 LIST_INSERT_AFTER(last, entry, gpe_entry);
521 entry->gpe_created = 1;
523 entry->gpe_deleted = 0;
524 entry->gpe_modified = 1;
526 g_part_new_provider(gp, table, entry);
528 /* Provide feedback if so requested. */
529 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
530 sb = sbuf_new_auto();
531 G_PART_FULLNAME(table, entry, sb, gp->name);
532 sbuf_cat(sb, " added\n");
534 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
541 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
544 struct g_part_table *table;
549 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
553 sz = table->gpt_scheme->gps_bootcodesz;
558 if (gpp->gpp_codesize > sz) {
563 error = G_PART_BOOTCODE(table, gpp);
567 /* Provide feedback if so requested. */
568 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
569 sb = sbuf_new_auto();
570 sbuf_printf(sb, "%s has bootcode\n", gp->name);
572 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
578 gctl_error(req, "%d", error);
583 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
585 struct g_consumer *cp;
587 struct g_provider *pp;
588 struct g_part_entry *entry, *tmp;
589 struct g_part_table *table;
594 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
598 if (!table->gpt_opened) {
599 gctl_error(req, "%d", EPERM);
605 cp = LIST_FIRST(&gp->consumer);
606 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
608 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
609 while (table->gpt_smhead != 0) {
610 i = ffs(table->gpt_smhead) - 1;
611 error = g_write_data(cp, i * pp->sectorsize, buf,
617 table->gpt_smhead &= ~(1 << i);
619 while (table->gpt_smtail != 0) {
620 i = ffs(table->gpt_smtail) - 1;
621 error = g_write_data(cp, pp->mediasize - (i + 1) *
622 pp->sectorsize, buf, pp->sectorsize);
627 table->gpt_smtail &= ~(1 << i);
632 if (table->gpt_scheme == &g_part_null_scheme) {
634 g_access(cp, -1, -1, -1);
635 g_part_wither(gp, ENXIO);
639 error = G_PART_WRITE(table, cp);
643 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
644 if (!entry->gpe_deleted) {
645 entry->gpe_created = 0;
646 entry->gpe_modified = 0;
649 LIST_REMOVE(entry, gpe_entry);
652 table->gpt_created = 0;
653 table->gpt_opened = 0;
656 g_access(cp, -1, -1, -1);
661 gctl_error(req, "%d", error);
666 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
668 struct g_consumer *cp;
670 struct g_provider *pp;
671 struct g_part_scheme *scheme;
672 struct g_part_table *null, *table;
676 pp = gpp->gpp_provider;
677 scheme = gpp->gpp_scheme;
678 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
681 /* Check that there isn't already a g_part geom on the provider. */
682 error = g_part_parm_geom(pp->name, &gp);
685 if (null->gpt_scheme != &g_part_null_scheme) {
686 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
692 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
693 (gpp->gpp_entries < scheme->gps_minent ||
694 gpp->gpp_entries > scheme->gps_maxent)) {
695 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
700 gp = g_new_geomf(&g_part_class, "%s", pp->name);
701 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
705 table->gpt_scheme = gpp->gpp_scheme;
706 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
707 gpp->gpp_entries : scheme->gps_minent;
708 LIST_INIT(&table->gpt_entry);
710 cp = g_new_consumer(gp);
711 error = g_attach(cp, pp);
713 error = g_access(cp, 1, 1, 1);
715 g_part_wither(gp, error);
716 gctl_error(req, "%d geom '%s'", error, pp->name);
719 table->gpt_opened = 1;
721 cp = LIST_FIRST(&gp->consumer);
722 table->gpt_opened = null->gpt_opened;
723 table->gpt_smhead = null->gpt_smhead;
724 table->gpt_smtail = null->gpt_smtail;
729 /* Make sure the provider has media. */
730 if (pp->mediasize == 0 || pp->sectorsize == 0) {
735 /* Make sure we can nest and if so, determine our depth. */
736 error = g_getattr("PART::isleaf", cp, &attr);
737 if (!error && attr) {
741 error = g_getattr("PART::depth", cp, &attr);
742 table->gpt_depth = (!error) ? attr + 1 : 0;
745 * Synthesize a disk geometry. Some partitioning schemes
746 * depend on it and since some file systems need it even
747 * when the partitition scheme doesn't, we do it here in
748 * scheme-independent code.
750 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
752 error = G_PART_CREATE(table, gpp);
758 table->gpt_created = 1;
760 kobj_delete((kobj_t)null, M_GEOM);
763 * Support automatic commit by filling in the gpp_geom
766 gpp->gpp_parms |= G_PART_PARM_GEOM;
769 /* Provide feedback if so requested. */
770 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
771 sb = sbuf_new_auto();
772 sbuf_printf(sb, "%s created\n", gp->name);
774 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
782 g_access(cp, -1, -1, -1);
783 g_part_wither(gp, error);
785 kobj_delete((kobj_t)gp->softc, M_GEOM);
788 gctl_error(req, "%d provider", error);
793 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
796 struct g_provider *pp;
797 struct g_part_entry *entry;
798 struct g_part_table *table;
802 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
807 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
808 if (entry->gpe_deleted || entry->gpe_internal)
810 if (entry->gpe_index == gpp->gpp_index)
814 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
820 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
821 gctl_error(req, "%d", EBUSY);
826 entry->gpe_pp = NULL;
830 g_wither_provider(pp, ENXIO);
832 /* Provide feedback if so requested. */
833 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
834 sb = sbuf_new_auto();
835 G_PART_FULLNAME(table, entry, sb, gp->name);
836 sbuf_cat(sb, " deleted\n");
838 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
842 if (entry->gpe_created) {
843 LIST_REMOVE(entry, gpe_entry);
846 entry->gpe_modified = 0;
847 entry->gpe_deleted = 1;
853 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
855 struct g_consumer *cp;
857 struct g_provider *pp;
858 struct g_part_entry *entry;
859 struct g_part_table *null, *table;
864 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
868 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
869 if (entry->gpe_deleted || entry->gpe_internal)
871 gctl_error(req, "%d", EBUSY);
875 error = G_PART_DESTROY(table, gpp);
877 gctl_error(req, "%d", error);
881 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
885 null->gpt_scheme = &g_part_null_scheme;
886 LIST_INIT(&null->gpt_entry);
888 cp = LIST_FIRST(&gp->consumer);
890 null->gpt_last = pp->mediasize / pp->sectorsize - 1;
892 null->gpt_depth = table->gpt_depth;
893 null->gpt_opened = table->gpt_opened;
894 null->gpt_smhead = table->gpt_smhead;
895 null->gpt_smtail = table->gpt_smtail;
897 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
898 LIST_REMOVE(entry, gpe_entry);
901 kobj_delete((kobj_t)table, M_GEOM);
903 /* Provide feedback if so requested. */
904 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
905 sb = sbuf_new_auto();
906 sbuf_printf(sb, "%s destroyed\n", gp->name);
908 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
915 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
918 struct g_part_entry *entry;
919 struct g_part_table *table;
924 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
929 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
930 if (entry->gpe_deleted || entry->gpe_internal)
932 if (entry->gpe_index == gpp->gpp_index)
936 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
940 error = G_PART_MODIFY(table, entry, gpp);
942 gctl_error(req, "%d", error);
946 if (!entry->gpe_created)
947 entry->gpe_modified = 1;
949 /* Provide feedback if so requested. */
950 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
951 sb = sbuf_new_auto();
952 G_PART_FULLNAME(table, entry, sb, gp->name);
953 sbuf_cat(sb, " modified\n");
955 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
962 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
964 gctl_error(req, "%d verb 'move'", ENOSYS);
969 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
971 gctl_error(req, "%d verb 'recover'", ENOSYS);
976 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
979 struct g_provider *pp;
980 struct g_part_entry *pe, *entry;
981 struct g_part_table *table;
987 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
991 /* check gpp_index */
992 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
993 if (entry->gpe_deleted || entry->gpe_internal)
995 if (entry->gpe_index == gpp->gpp_index)
999 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1003 /* check gpp_size */
1004 end = entry->gpe_start + gpp->gpp_size - 1;
1005 if (gpp->gpp_size < 1 || end > table->gpt_last) {
1006 gctl_error(req, "%d size '%jd'", EINVAL,
1007 (intmax_t)gpp->gpp_size);
1011 LIST_FOREACH(pe, &table->gpt_entry, gpe_entry) {
1012 if (pe->gpe_deleted || pe->gpe_internal || pe == entry)
1014 if (end >= pe->gpe_start && end <= pe->gpe_end) {
1015 gctl_error(req, "%d end '%jd'", ENOSPC,
1019 if (entry->gpe_start < pe->gpe_start && end > pe->gpe_end) {
1020 gctl_error(req, "%d size '%jd'", ENOSPC,
1021 (intmax_t)gpp->gpp_size);
1027 if ((g_debugflags & 16) == 0 &&
1028 (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)) {
1029 gctl_error(req, "%d", EBUSY);
1033 error = G_PART_RESIZE(table, entry, gpp);
1035 gctl_error(req, "%d", error);
1039 if (!entry->gpe_created)
1040 entry->gpe_modified = 1;
1042 /* update mediasize of changed provider */
1043 pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
1046 /* Provide feedback if so requested. */
1047 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1048 sb = sbuf_new_auto();
1049 G_PART_FULLNAME(table, entry, sb, gp->name);
1050 sbuf_cat(sb, " resized\n");
1052 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1059 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
1063 struct g_part_entry *entry;
1064 struct g_part_table *table;
1069 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1070 g_topology_assert();
1074 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1075 if (entry->gpe_deleted || entry->gpe_internal)
1077 if (entry->gpe_index == gpp->gpp_index)
1080 if (entry == NULL) {
1081 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1085 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
1087 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
1091 /* Provide feedback if so requested. */
1092 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1093 sb = sbuf_new_auto();
1094 G_PART_FULLNAME(table, entry, sb, gp->name);
1095 sbuf_printf(sb, " has %s %sset\n", gpp->gpp_attrib,
1098 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1105 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1107 struct g_consumer *cp;
1108 struct g_provider *pp;
1110 struct g_part_entry *entry, *tmp;
1111 struct g_part_table *table;
1115 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1116 g_topology_assert();
1119 if (!table->gpt_opened) {
1120 gctl_error(req, "%d", EPERM);
1124 cp = LIST_FIRST(&gp->consumer);
1125 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1126 entry->gpe_modified = 0;
1127 if (entry->gpe_created) {
1131 entry->gpe_pp = NULL;
1132 g_wither_provider(pp, ENXIO);
1134 entry->gpe_deleted = 1;
1136 if (entry->gpe_deleted) {
1137 LIST_REMOVE(entry, gpe_entry);
1142 g_topology_unlock();
1144 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1145 table->gpt_created) ? 1 : 0;
1148 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1149 if (entry->gpe_internal)
1154 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1155 LIST_REMOVE(entry, gpe_entry);
1158 error = g_part_probe(gp, cp, table->gpt_depth);
1161 g_access(cp, -1, -1, -1);
1162 g_part_wither(gp, error);
1168 * Synthesize a disk geometry. Some partitioning schemes
1169 * depend on it and since some file systems need it even
1170 * when the partitition scheme doesn't, we do it here in
1171 * scheme-independent code.
1174 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1177 error = G_PART_READ(table, cp);
1183 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1184 if (!entry->gpe_internal)
1185 g_part_new_provider(gp, table, entry);
1188 table->gpt_opened = 0;
1189 g_access(cp, -1, -1, -1);
1194 gctl_error(req, "%d", error);
1199 g_part_wither(struct g_geom *gp, int error)
1201 struct g_part_entry *entry;
1202 struct g_part_table *table;
1205 if (table != NULL) {
1206 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1207 LIST_REMOVE(entry, gpe_entry);
1210 if (gp->softc != NULL) {
1211 kobj_delete((kobj_t)gp->softc, M_GEOM);
1215 g_wither_geom(gp, error);
1223 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1225 struct g_part_parms gpp;
1226 struct g_part_table *table;
1227 struct gctl_req_arg *ap;
1229 enum g_part_ctl ctlreq;
1230 unsigned int i, mparms, oparms, parm;
1231 int auto_commit, close_on_error;
1232 int error, len, modifies;
1234 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1235 g_topology_assert();
1237 ctlreq = G_PART_CTL_NONE;
1240 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1243 if (!strcmp(verb, "add")) {
1244 ctlreq = G_PART_CTL_ADD;
1245 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1246 G_PART_PARM_START | G_PART_PARM_TYPE;
1247 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1251 if (!strcmp(verb, "bootcode")) {
1252 ctlreq = G_PART_CTL_BOOTCODE;
1253 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1257 if (!strcmp(verb, "commit")) {
1258 ctlreq = G_PART_CTL_COMMIT;
1259 mparms |= G_PART_PARM_GEOM;
1261 } else if (!strcmp(verb, "create")) {
1262 ctlreq = G_PART_CTL_CREATE;
1263 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1264 oparms |= G_PART_PARM_ENTRIES;
1268 if (!strcmp(verb, "delete")) {
1269 ctlreq = G_PART_CTL_DELETE;
1270 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1271 } else if (!strcmp(verb, "destroy")) {
1272 ctlreq = G_PART_CTL_DESTROY;
1273 mparms |= G_PART_PARM_GEOM;
1277 if (!strcmp(verb, "modify")) {
1278 ctlreq = G_PART_CTL_MODIFY;
1279 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1280 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1281 } else if (!strcmp(verb, "move")) {
1282 ctlreq = G_PART_CTL_MOVE;
1283 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1287 if (!strcmp(verb, "recover")) {
1288 ctlreq = G_PART_CTL_RECOVER;
1289 mparms |= G_PART_PARM_GEOM;
1290 } else if (!strcmp(verb, "resize")) {
1291 ctlreq = G_PART_CTL_RESIZE;
1292 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX |
1297 if (!strcmp(verb, "set")) {
1298 ctlreq = G_PART_CTL_SET;
1299 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1304 if (!strcmp(verb, "undo")) {
1305 ctlreq = G_PART_CTL_UNDO;
1306 mparms |= G_PART_PARM_GEOM;
1308 } else if (!strcmp(verb, "unset")) {
1309 ctlreq = G_PART_CTL_UNSET;
1310 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM |
1315 if (ctlreq == G_PART_CTL_NONE) {
1316 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1320 bzero(&gpp, sizeof(gpp));
1321 for (i = 0; i < req->narg; i++) {
1324 switch (ap->name[0]) {
1326 if (!strcmp(ap->name, "attrib"))
1327 parm = G_PART_PARM_ATTRIB;
1330 if (!strcmp(ap->name, "bootcode"))
1331 parm = G_PART_PARM_BOOTCODE;
1334 if (!strcmp(ap->name, "class"))
1338 if (!strcmp(ap->name, "entries"))
1339 parm = G_PART_PARM_ENTRIES;
1342 if (!strcmp(ap->name, "flags"))
1343 parm = G_PART_PARM_FLAGS;
1346 if (!strcmp(ap->name, "geom"))
1347 parm = G_PART_PARM_GEOM;
1350 if (!strcmp(ap->name, "index"))
1351 parm = G_PART_PARM_INDEX;
1354 if (!strcmp(ap->name, "label"))
1355 parm = G_PART_PARM_LABEL;
1358 if (!strcmp(ap->name, "output"))
1359 parm = G_PART_PARM_OUTPUT;
1362 if (!strcmp(ap->name, "provider"))
1363 parm = G_PART_PARM_PROVIDER;
1366 if (!strcmp(ap->name, "scheme"))
1367 parm = G_PART_PARM_SCHEME;
1368 else if (!strcmp(ap->name, "size"))
1369 parm = G_PART_PARM_SIZE;
1370 else if (!strcmp(ap->name, "start"))
1371 parm = G_PART_PARM_START;
1374 if (!strcmp(ap->name, "type"))
1375 parm = G_PART_PARM_TYPE;
1378 if (!strcmp(ap->name, "verb"))
1380 else if (!strcmp(ap->name, "version"))
1381 parm = G_PART_PARM_VERSION;
1384 if ((parm & (mparms | oparms)) == 0) {
1385 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1388 if (parm == G_PART_PARM_BOOTCODE)
1389 p = gctl_get_param(req, ap->name, &len);
1391 p = gctl_get_asciiparam(req, ap->name);
1393 gctl_error(req, "%d param '%s'", ENOATTR, ap->name);
1397 case G_PART_PARM_ATTRIB:
1398 error = g_part_parm_str(p, &gpp.gpp_attrib);
1400 case G_PART_PARM_BOOTCODE:
1401 gpp.gpp_codeptr = p;
1402 gpp.gpp_codesize = len;
1405 case G_PART_PARM_ENTRIES:
1406 error = g_part_parm_uint(p, &gpp.gpp_entries);
1408 case G_PART_PARM_FLAGS:
1411 error = g_part_parm_str(p, &gpp.gpp_flags);
1413 case G_PART_PARM_GEOM:
1414 error = g_part_parm_geom(p, &gpp.gpp_geom);
1416 case G_PART_PARM_INDEX:
1417 error = g_part_parm_uint(p, &gpp.gpp_index);
1419 case G_PART_PARM_LABEL:
1420 /* An empty label is always valid. */
1424 case G_PART_PARM_OUTPUT:
1425 error = 0; /* Write-only parameter */
1427 case G_PART_PARM_PROVIDER:
1428 error = g_part_parm_provider(p, &gpp.gpp_provider);
1430 case G_PART_PARM_SCHEME:
1431 error = g_part_parm_scheme(p, &gpp.gpp_scheme);
1433 case G_PART_PARM_SIZE:
1434 error = g_part_parm_quad(p, &gpp.gpp_size);
1436 case G_PART_PARM_START:
1437 error = g_part_parm_quad(p, &gpp.gpp_start);
1439 case G_PART_PARM_TYPE:
1440 error = g_part_parm_str(p, &gpp.gpp_type);
1442 case G_PART_PARM_VERSION:
1443 error = g_part_parm_uint(p, &gpp.gpp_version);
1450 gctl_error(req, "%d %s '%s'", error, ap->name, p);
1453 gpp.gpp_parms |= parm;
1455 if ((gpp.gpp_parms & mparms) != mparms) {
1456 parm = mparms - (gpp.gpp_parms & mparms);
1457 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1461 /* Obtain permissions if possible/necessary. */
1464 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1465 table = gpp.gpp_geom->softc;
1466 if (table != NULL && !table->gpt_opened) {
1467 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1470 gctl_error(req, "%d geom '%s'", error,
1471 gpp.gpp_geom->name);
1474 table->gpt_opened = 1;
1479 /* Allow the scheme to check or modify the parameters. */
1480 if (table != NULL) {
1481 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1483 gctl_error(req, "%d pre-check failed", error);
1487 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1490 case G_PART_CTL_NONE:
1491 panic("%s", __func__);
1492 case G_PART_CTL_ADD:
1493 error = g_part_ctl_add(req, &gpp);
1495 case G_PART_CTL_BOOTCODE:
1496 error = g_part_ctl_bootcode(req, &gpp);
1498 case G_PART_CTL_COMMIT:
1499 error = g_part_ctl_commit(req, &gpp);
1501 case G_PART_CTL_CREATE:
1502 error = g_part_ctl_create(req, &gpp);
1504 case G_PART_CTL_DELETE:
1505 error = g_part_ctl_delete(req, &gpp);
1507 case G_PART_CTL_DESTROY:
1508 error = g_part_ctl_destroy(req, &gpp);
1510 case G_PART_CTL_MODIFY:
1511 error = g_part_ctl_modify(req, &gpp);
1513 case G_PART_CTL_MOVE:
1514 error = g_part_ctl_move(req, &gpp);
1516 case G_PART_CTL_RECOVER:
1517 error = g_part_ctl_recover(req, &gpp);
1519 case G_PART_CTL_RESIZE:
1520 error = g_part_ctl_resize(req, &gpp);
1522 case G_PART_CTL_SET:
1523 error = g_part_ctl_setunset(req, &gpp, 1);
1525 case G_PART_CTL_UNDO:
1526 error = g_part_ctl_undo(req, &gpp);
1528 case G_PART_CTL_UNSET:
1529 error = g_part_ctl_setunset(req, &gpp, 0);
1533 /* Implement automatic commit. */
1535 auto_commit = (modifies &&
1536 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1537 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1539 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, (__func__));
1540 error = g_part_ctl_commit(req, &gpp);
1545 if (error && close_on_error) {
1546 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1547 table->gpt_opened = 0;
1552 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1556 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1557 g_topology_assert();
1559 g_part_wither(gp, EINVAL);
1563 static struct g_geom *
1564 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1566 struct g_consumer *cp;
1568 struct g_part_entry *entry;
1569 struct g_part_table *table;
1570 struct root_hold_token *rht;
1574 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1575 g_topology_assert();
1577 /* Skip providers that are already open for writing. */
1582 * Create a GEOM with consumer and hook it up to the provider.
1583 * With that we become part of the topology. Optain read access
1586 gp = g_new_geomf(mp, "%s", pp->name);
1587 cp = g_new_consumer(gp);
1588 error = g_attach(cp, pp);
1590 error = g_access(cp, 1, 0, 0);
1592 g_part_wither(gp, error);
1596 rht = root_mount_hold(mp->name);
1597 g_topology_unlock();
1600 * Short-circuit the whole probing galore when there's no
1603 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1608 /* Make sure we can nest and if so, determine our depth. */
1609 error = g_getattr("PART::isleaf", cp, &attr);
1610 if (!error && attr) {
1614 error = g_getattr("PART::depth", cp, &attr);
1615 depth = (!error) ? attr + 1 : 0;
1617 error = g_part_probe(gp, cp, depth);
1624 * Synthesize a disk geometry. Some partitioning schemes
1625 * depend on it and since some file systems need it even
1626 * when the partitition scheme doesn't, we do it here in
1627 * scheme-independent code.
1629 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1631 error = G_PART_READ(table, cp);
1636 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1637 if (!entry->gpe_internal)
1638 g_part_new_provider(gp, table, entry);
1641 root_mount_rel(rht);
1642 g_access(cp, -1, 0, 0);
1647 root_mount_rel(rht);
1648 g_access(cp, -1, 0, 0);
1649 g_part_wither(gp, error);
1658 g_part_access(struct g_provider *pp, int dr, int dw, int de)
1660 struct g_consumer *cp;
1662 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1665 cp = LIST_FIRST(&pp->geom->consumer);
1667 /* We always gain write-exclusive access. */
1668 return (g_access(cp, dr, dw, dw + de));
1672 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1673 struct g_consumer *cp, struct g_provider *pp)
1676 struct g_part_entry *entry;
1677 struct g_part_table *table;
1679 KASSERT(sb != NULL && gp != NULL, (__func__));
1682 if (indent == NULL) {
1683 KASSERT(cp == NULL && pp != NULL, (__func__));
1684 entry = pp->private;
1687 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
1688 (uintmax_t)entry->gpe_offset,
1689 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1691 * libdisk compatibility quirk - the scheme dumps the
1692 * slicer name and partition type in a way that is
1693 * compatible with libdisk. When libdisk is not used
1694 * anymore, this should go away.
1696 G_PART_DUMPCONF(table, entry, sb, indent);
1697 } else if (cp != NULL) { /* Consumer configuration. */
1698 KASSERT(pp == NULL, (__func__));
1700 } else if (pp != NULL) { /* Provider configuration. */
1701 entry = pp->private;
1704 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
1705 (uintmax_t)entry->gpe_start);
1706 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
1707 (uintmax_t)entry->gpe_end);
1708 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
1710 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1711 G_PART_TYPE(table, entry, buf, sizeof(buf)));
1712 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
1713 (uintmax_t)entry->gpe_offset);
1714 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
1715 (uintmax_t)pp->mediasize);
1716 G_PART_DUMPCONF(table, entry, sb, indent);
1717 } else { /* Geom configuration. */
1718 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
1719 table->gpt_scheme->name);
1720 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
1721 table->gpt_entries);
1722 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
1723 (uintmax_t)table->gpt_first);
1724 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
1725 (uintmax_t)table->gpt_last);
1726 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
1727 table->gpt_sectors);
1728 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
1730 G_PART_DUMPCONF(table, NULL, sb, indent);
1735 g_part_orphan(struct g_consumer *cp)
1737 struct g_provider *pp;
1738 struct g_part_table *table;
1741 KASSERT(pp != NULL, (__func__));
1742 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
1743 g_topology_assert();
1745 KASSERT(pp->error != 0, (__func__));
1746 table = cp->geom->softc;
1747 if (table != NULL && table->gpt_opened)
1748 g_access(cp, -1, -1, -1);
1749 g_part_wither(cp->geom, pp->error);
1753 g_part_spoiled(struct g_consumer *cp)
1756 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
1757 g_topology_assert();
1759 g_part_wither(cp->geom, ENXIO);
1763 g_part_start(struct bio *bp)
1766 struct g_consumer *cp;
1768 struct g_part_entry *entry;
1769 struct g_part_table *table;
1770 struct g_kerneldump *gkd;
1771 struct g_provider *pp;
1776 cp = LIST_FIRST(&gp->consumer);
1778 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
1781 entry = pp->private;
1782 if (entry == NULL) {
1783 g_io_deliver(bp, ENXIO);
1787 switch(bp->bio_cmd) {
1791 if (bp->bio_offset >= pp->mediasize) {
1792 g_io_deliver(bp, EIO);
1795 bp2 = g_clone_bio(bp);
1797 g_io_deliver(bp, ENOMEM);
1800 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
1801 bp2->bio_length = pp->mediasize - bp2->bio_offset;
1802 bp2->bio_done = g_std_done;
1803 bp2->bio_offset += entry->gpe_offset;
1804 g_io_request(bp2, cp);
1809 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
1811 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
1813 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
1815 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
1817 if (g_handleattr_str(bp, "PART::scheme",
1818 table->gpt_scheme->name))
1820 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
1822 * Check that the partition is suitable for kernel
1823 * dumps. Typically only swap partitions should be
1826 if (!G_PART_DUMPTO(table, entry)) {
1827 g_io_deliver(bp, ENODEV);
1828 printf("GEOM_PART: Partition '%s' not suitable"
1829 " for kernel dumps (wrong type?)\n",
1833 gkd = (struct g_kerneldump *)bp->bio_data;
1834 if (gkd->offset >= pp->mediasize) {
1835 g_io_deliver(bp, EIO);
1838 if (gkd->offset + gkd->length > pp->mediasize)
1839 gkd->length = pp->mediasize - gkd->offset;
1840 gkd->offset += entry->gpe_offset;
1844 g_io_deliver(bp, EOPNOTSUPP);
1848 bp2 = g_clone_bio(bp);
1850 g_io_deliver(bp, ENOMEM);
1853 bp2->bio_done = g_std_done;
1854 g_io_request(bp2, cp);
1858 g_part_init(struct g_class *mp)
1861 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
1865 g_part_fini(struct g_class *mp)
1868 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
1872 g_part_unload_event(void *arg, int flag)
1874 struct g_consumer *cp;
1876 struct g_provider *pp;
1877 struct g_part_scheme *scheme;
1878 struct g_part_table *table;
1882 if (flag == EV_CANCEL)
1887 scheme = (void *)(*xchg);
1889 g_topology_assert();
1891 LIST_FOREACH(gp, &g_part_class.geom, geom) {
1893 if (table->gpt_scheme != scheme)
1897 LIST_FOREACH(pp, &gp->provider, provider)
1898 acc += pp->acr + pp->acw + pp->ace;
1899 LIST_FOREACH(cp, &gp->consumer, consumer)
1900 acc += cp->acr + cp->acw + cp->ace;
1903 g_part_wither(gp, ENOSYS);
1909 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1915 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
1922 TAILQ_INSERT_TAIL(&g_part_schemes, scheme, scheme_list);
1924 error = g_retaste(&g_part_class);
1926 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
1929 arg = (uintptr_t)scheme;
1930 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,
1933 error = (arg == (uintptr_t)scheme) ? EDOOFUS : arg;