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/endian.h>
33 #include <sys/kernel.h>
35 #include <sys/limits.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/queue.h>
41 #include <sys/sysctl.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-core-storage", G_PART_ALIAS_APPLE_CORE_STORAGE },
74 { "apple-hfs", G_PART_ALIAS_APPLE_HFS },
75 { "apple-label", G_PART_ALIAS_APPLE_LABEL },
76 { "apple-raid", G_PART_ALIAS_APPLE_RAID },
77 { "apple-raid-offline", G_PART_ALIAS_APPLE_RAID_OFFLINE },
78 { "apple-tv-recovery", G_PART_ALIAS_APPLE_TV_RECOVERY },
79 { "apple-ufs", G_PART_ALIAS_APPLE_UFS },
80 { "bios-boot", G_PART_ALIAS_BIOS_BOOT },
81 { "ebr", G_PART_ALIAS_EBR },
82 { "efi", G_PART_ALIAS_EFI },
83 { "fat16", G_PART_ALIAS_MS_FAT16 },
84 { "fat32", G_PART_ALIAS_MS_FAT32 },
85 { "freebsd", G_PART_ALIAS_FREEBSD },
86 { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
87 { "freebsd-nandfs", G_PART_ALIAS_FREEBSD_NANDFS },
88 { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
89 { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
90 { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
91 { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
92 { "linux-data", G_PART_ALIAS_LINUX_DATA },
93 { "linux-lvm", G_PART_ALIAS_LINUX_LVM },
94 { "linux-raid", G_PART_ALIAS_LINUX_RAID },
95 { "linux-swap", G_PART_ALIAS_LINUX_SWAP },
96 { "mbr", G_PART_ALIAS_MBR },
97 { "ms-basic-data", G_PART_ALIAS_MS_BASIC_DATA },
98 { "ms-ldm-data", G_PART_ALIAS_MS_LDM_DATA },
99 { "ms-ldm-metadata", G_PART_ALIAS_MS_LDM_METADATA },
100 { "ms-reserved", G_PART_ALIAS_MS_RESERVED },
101 { "ntfs", G_PART_ALIAS_MS_NTFS },
102 { "netbsd-ccd", G_PART_ALIAS_NETBSD_CCD },
103 { "netbsd-cgd", G_PART_ALIAS_NETBSD_CGD },
104 { "netbsd-ffs", G_PART_ALIAS_NETBSD_FFS },
105 { "netbsd-lfs", G_PART_ALIAS_NETBSD_LFS },
106 { "netbsd-raid", G_PART_ALIAS_NETBSD_RAID },
107 { "netbsd-swap", G_PART_ALIAS_NETBSD_SWAP },
108 { "vmware-vmfs", G_PART_ALIAS_VMFS },
109 { "vmware-vmkdiag", G_PART_ALIAS_VMKDIAG },
110 { "vmware-reserved", G_PART_ALIAS_VMRESERVED },
111 { "vmware-vsanhdr", G_PART_ALIAS_VMVSANHDR },
112 { "dragonfly-label32", G_PART_ALIAS_DFBSD },
113 { "dragonfly-label64", G_PART_ALIAS_DFBSD64 },
114 { "dragonfly-swap", G_PART_ALIAS_DFBSD_SWAP },
115 { "dragonfly-ufs", G_PART_ALIAS_DFBSD_UFS },
116 { "dragonfly-vinum", G_PART_ALIAS_DFBSD_VINUM },
117 { "dragonfly-ccd", G_PART_ALIAS_DFBSD_CCD },
118 { "dragonfly-legacy", G_PART_ALIAS_DFBSD_LEGACY },
119 { "dragonfly-hammer", G_PART_ALIAS_DFBSD_HAMMER },
120 { "dragonfly-hammer2", G_PART_ALIAS_DFBSD_HAMMER2 },
121 { "prep-boot", G_PART_ALIAS_PREP_BOOT },
124 SYSCTL_DECL(_kern_geom);
125 SYSCTL_NODE(_kern_geom, OID_AUTO, part, CTLFLAG_RW, 0,
127 static u_int check_integrity = 1;
128 TUNABLE_INT("kern.geom.part.check_integrity", &check_integrity);
129 SYSCTL_UINT(_kern_geom_part, OID_AUTO, check_integrity,
130 CTLFLAG_RW | CTLFLAG_TUN, &check_integrity, 1,
131 "Enable integrity checking");
134 * The GEOM partitioning class.
136 static g_ctl_req_t g_part_ctlreq;
137 static g_ctl_destroy_geom_t g_part_destroy_geom;
138 static g_fini_t g_part_fini;
139 static g_init_t g_part_init;
140 static g_taste_t g_part_taste;
142 static g_access_t g_part_access;
143 static g_dumpconf_t g_part_dumpconf;
144 static g_orphan_t g_part_orphan;
145 static g_spoiled_t g_part_spoiled;
146 static g_start_t g_part_start;
147 static g_resize_t g_part_resize;
149 static struct g_class g_part_class = {
151 .version = G_VERSION,
153 .ctlreq = g_part_ctlreq,
154 .destroy_geom = g_part_destroy_geom,
157 .taste = g_part_taste,
159 .access = g_part_access,
160 .dumpconf = g_part_dumpconf,
161 .orphan = g_part_orphan,
162 .spoiled = g_part_spoiled,
163 .start = g_part_start,
164 .resize = g_part_resize
167 DECLARE_GEOM_CLASS(g_part_class, g_part);
168 MODULE_VERSION(g_part, 0);
174 static void g_part_wither(struct g_geom *, int);
177 g_part_alias_name(enum g_part_alias alias)
181 for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
182 if (g_part_alias_list[i].alias != alias)
184 return (g_part_alias_list[i].lexeme);
191 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
194 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
195 off_t chs, cylinders;
201 for (idx = 0; candidate_heads[idx] != 0; idx++) {
202 heads = candidate_heads[idx];
203 cylinders = blocks / heads / sectors;
204 if (cylinders < heads || cylinders < sectors)
206 if (cylinders > 1023)
208 chs = cylinders * heads * sectors;
209 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
217 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
220 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
222 u_int heads, sectors;
225 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 || sectors == 0 ||
226 g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
227 table->gpt_fixgeom = 0;
228 table->gpt_heads = 0;
229 table->gpt_sectors = 0;
231 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
232 sectors = candidate_sectors[idx];
233 g_part_geometry_heads(blocks, sectors, &chs, &heads);
237 * Prefer a geometry with sectors > 1, but only if
238 * it doesn't bump down the number of heads to 1.
240 if (chs > bestchs || (chs == bestchs && heads > 1 &&
241 table->gpt_sectors == 1)) {
243 table->gpt_heads = heads;
244 table->gpt_sectors = sectors;
248 * If we didn't find a geometry at all, then the disk is
249 * too big. This means we can use the maximum number of
253 table->gpt_heads = 255;
254 table->gpt_sectors = 63;
257 table->gpt_fixgeom = 1;
258 table->gpt_heads = heads;
259 table->gpt_sectors = sectors;
263 #define DPRINTF(...) if (bootverbose) { \
264 printf("GEOM_PART: " __VA_ARGS__); \
268 g_part_check_integrity(struct g_part_table *table, struct g_consumer *cp)
270 struct g_part_entry *e1, *e2;
271 struct g_provider *pp;
277 if (table->gpt_last < table->gpt_first) {
278 DPRINTF("last LBA is below first LBA: %jd < %jd\n",
279 (intmax_t)table->gpt_last, (intmax_t)table->gpt_first);
282 if (table->gpt_last > pp->mediasize / pp->sectorsize - 1) {
283 DPRINTF("last LBA extends beyond mediasize: "
284 "%jd > %jd\n", (intmax_t)table->gpt_last,
285 (intmax_t)pp->mediasize / pp->sectorsize - 1);
288 LIST_FOREACH(e1, &table->gpt_entry, gpe_entry) {
289 if (e1->gpe_deleted || e1->gpe_internal)
291 if (e1->gpe_start < table->gpt_first) {
292 DPRINTF("partition %d has start offset below first "
293 "LBA: %jd < %jd\n", e1->gpe_index,
294 (intmax_t)e1->gpe_start,
295 (intmax_t)table->gpt_first);
298 if (e1->gpe_start > table->gpt_last) {
299 DPRINTF("partition %d has start offset beyond last "
300 "LBA: %jd > %jd\n", e1->gpe_index,
301 (intmax_t)e1->gpe_start,
302 (intmax_t)table->gpt_last);
305 if (e1->gpe_end < e1->gpe_start) {
306 DPRINTF("partition %d has end offset below start "
307 "offset: %jd < %jd\n", e1->gpe_index,
308 (intmax_t)e1->gpe_end,
309 (intmax_t)e1->gpe_start);
312 if (e1->gpe_end > table->gpt_last) {
313 DPRINTF("partition %d has end offset beyond last "
314 "LBA: %jd > %jd\n", e1->gpe_index,
315 (intmax_t)e1->gpe_end,
316 (intmax_t)table->gpt_last);
319 if (pp->stripesize > 0) {
320 offset = e1->gpe_start * pp->sectorsize;
321 if (e1->gpe_offset > offset)
322 offset = e1->gpe_offset;
323 if ((offset + pp->stripeoffset) % pp->stripesize) {
324 DPRINTF("partition %d on (%s, %s) is not "
325 "aligned on %u bytes\n", e1->gpe_index,
326 pp->name, table->gpt_scheme->name,
328 /* Don't treat this as a critical failure */
332 while ((e2 = LIST_NEXT(e2, gpe_entry)) != NULL) {
333 if (e2->gpe_deleted || e2->gpe_internal)
335 if (e1->gpe_start >= e2->gpe_start &&
336 e1->gpe_start <= e2->gpe_end) {
337 DPRINTF("partition %d has start offset inside "
338 "partition %d: start[%d] %jd >= start[%d] "
339 "%jd <= end[%d] %jd\n",
340 e1->gpe_index, e2->gpe_index,
341 e2->gpe_index, (intmax_t)e2->gpe_start,
342 e1->gpe_index, (intmax_t)e1->gpe_start,
343 e2->gpe_index, (intmax_t)e2->gpe_end);
346 if (e1->gpe_end >= e2->gpe_start &&
347 e1->gpe_end <= e2->gpe_end) {
348 DPRINTF("partition %d has end offset inside "
349 "partition %d: start[%d] %jd >= end[%d] "
350 "%jd <= end[%d] %jd\n",
351 e1->gpe_index, e2->gpe_index,
352 e2->gpe_index, (intmax_t)e2->gpe_start,
353 e1->gpe_index, (intmax_t)e1->gpe_end,
354 e2->gpe_index, (intmax_t)e2->gpe_end);
357 if (e1->gpe_start < e2->gpe_start &&
358 e1->gpe_end > e2->gpe_end) {
359 DPRINTF("partition %d contains partition %d: "
360 "start[%d] %jd > start[%d] %jd, end[%d] "
361 "%jd < end[%d] %jd\n",
362 e1->gpe_index, e2->gpe_index,
363 e1->gpe_index, (intmax_t)e1->gpe_start,
364 e2->gpe_index, (intmax_t)e2->gpe_start,
365 e2->gpe_index, (intmax_t)e2->gpe_end,
366 e1->gpe_index, (intmax_t)e1->gpe_end);
372 printf("GEOM_PART: integrity check failed (%s, %s)\n",
373 pp->name, table->gpt_scheme->name);
374 if (check_integrity != 0)
376 table->gpt_corrupt = 1;
382 struct g_part_entry *
383 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
386 struct g_part_entry *entry, *last;
389 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
390 if (entry->gpe_index == index)
392 if (entry->gpe_index > index) {
399 entry = g_malloc(table->gpt_scheme->gps_entrysz,
401 entry->gpe_index = index;
403 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
405 LIST_INSERT_AFTER(last, entry, gpe_entry);
407 entry->gpe_offset = 0;
408 entry->gpe_start = start;
409 entry->gpe_end = end;
414 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
415 struct g_part_entry *entry)
417 struct g_consumer *cp;
418 struct g_provider *pp;
422 cp = LIST_FIRST(&gp->consumer);
425 offset = entry->gpe_start * pp->sectorsize;
426 if (entry->gpe_offset < offset)
427 entry->gpe_offset = offset;
429 if (entry->gpe_pp == NULL) {
430 sb = sbuf_new_auto();
431 G_PART_FULLNAME(table, entry, sb, gp->name);
433 entry->gpe_pp = g_new_providerf(gp, "%s", sbuf_data(sb));
435 entry->gpe_pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
436 entry->gpe_pp->private = entry; /* Close the circle. */
438 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */
439 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
441 entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
442 entry->gpe_pp->sectorsize = pp->sectorsize;
443 entry->gpe_pp->stripesize = pp->stripesize;
444 entry->gpe_pp->stripeoffset = pp->stripeoffset + entry->gpe_offset;
445 if (pp->stripesize > 0)
446 entry->gpe_pp->stripeoffset %= pp->stripesize;
447 entry->gpe_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
448 g_error_provider(entry->gpe_pp, 0);
451 static struct g_geom*
452 g_part_find_geom(const char *name)
455 LIST_FOREACH(gp, &g_part_class.geom, geom) {
456 if ((gp->flags & G_GEOM_WITHER) == 0 &&
457 strcmp(name, gp->name) == 0)
464 g_part_parm_geom(struct gctl_req *req, const char *name, struct g_geom **v)
469 gname = gctl_get_asciiparam(req, name);
472 if (strncmp(gname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
473 gname += sizeof(_PATH_DEV) - 1;
474 gp = g_part_find_geom(gname);
476 gctl_error(req, "%d %s '%s'", EINVAL, name, gname);
484 g_part_parm_provider(struct gctl_req *req, const char *name,
485 struct g_provider **v)
487 struct g_provider *pp;
490 pname = gctl_get_asciiparam(req, name);
493 if (strncmp(pname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
494 pname += sizeof(_PATH_DEV) - 1;
495 pp = g_provider_by_name(pname);
497 gctl_error(req, "%d %s '%s'", EINVAL, name, pname);
505 g_part_parm_quad(struct gctl_req *req, const char *name, quad_t *v)
511 p = gctl_get_asciiparam(req, name);
514 q = strtoq(p, &x, 0);
515 if (*x != '\0' || q < 0) {
516 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
524 g_part_parm_scheme(struct gctl_req *req, const char *name,
525 struct g_part_scheme **v)
527 struct g_part_scheme *s;
530 p = gctl_get_asciiparam(req, name);
533 TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
534 if (s == &g_part_null_scheme)
536 if (!strcasecmp(s->name, p))
540 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
548 g_part_parm_str(struct gctl_req *req, const char *name, const char **v)
552 p = gctl_get_asciiparam(req, name);
555 /* An empty label is always valid. */
556 if (strcmp(name, "label") != 0 && p[0] == '\0') {
557 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
565 g_part_parm_intmax(struct gctl_req *req, const char *name, u_int *v)
570 p = gctl_get_param(req, name, &size);
573 if (size != sizeof(*p) || *p < 0 || *p > INT_MAX) {
574 gctl_error(req, "%d %s '%jd'", EINVAL, name, *p);
582 g_part_parm_uint32(struct gctl_req *req, const char *name, u_int *v)
587 p = gctl_get_param(req, name, &size);
590 if (size != sizeof(*p) || *p > INT_MAX) {
591 gctl_error(req, "%d %s '%u'", EINVAL, name, (unsigned int)*p);
599 g_part_parm_bootcode(struct gctl_req *req, const char *name, const void **v,
605 p = gctl_get_param(req, name, &size);
614 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
616 struct g_part_scheme *iter, *scheme;
617 struct g_part_table *table;
621 scheme = (table != NULL) ? table->gpt_scheme : NULL;
622 pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
625 if (pri > 0) { /* error */
630 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
631 if (iter == &g_part_null_scheme)
633 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
636 table->gpt_scheme = iter;
637 table->gpt_depth = depth;
638 probe = G_PART_PROBE(table, cp);
639 if (probe <= 0 && probe > pri) {
642 if (gp->softc != NULL)
643 kobj_delete((kobj_t)gp->softc, M_GEOM);
648 kobj_delete((kobj_t)table, M_GEOM);
652 return ((scheme == NULL) ? ENXIO : 0);
656 * Control request functions.
660 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
663 struct g_provider *pp;
664 struct g_part_entry *delent, *last, *entry;
665 struct g_part_table *table;
672 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
675 pp = LIST_FIRST(&gp->consumer)->provider;
677 end = gpp->gpp_start + gpp->gpp_size - 1;
679 if (gpp->gpp_start < table->gpt_first ||
680 gpp->gpp_start > table->gpt_last) {
681 gctl_error(req, "%d start '%jd'", EINVAL,
682 (intmax_t)gpp->gpp_start);
685 if (end < gpp->gpp_start || end > table->gpt_last) {
686 gctl_error(req, "%d size '%jd'", EINVAL,
687 (intmax_t)gpp->gpp_size);
690 if (gpp->gpp_index > table->gpt_entries) {
691 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
695 delent = last = NULL;
696 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
697 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
698 if (entry->gpe_deleted) {
699 if (entry->gpe_index == index)
703 if (entry->gpe_index == index)
704 index = entry->gpe_index + 1;
705 if (entry->gpe_index < index)
707 if (entry->gpe_internal)
709 if (gpp->gpp_start >= entry->gpe_start &&
710 gpp->gpp_start <= entry->gpe_end) {
711 gctl_error(req, "%d start '%jd'", ENOSPC,
712 (intmax_t)gpp->gpp_start);
715 if (end >= entry->gpe_start && end <= entry->gpe_end) {
716 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
719 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
720 gctl_error(req, "%d size '%jd'", ENOSPC,
721 (intmax_t)gpp->gpp_size);
725 if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
726 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
729 if (index > table->gpt_entries) {
730 gctl_error(req, "%d index '%d'", ENOSPC, index);
734 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
735 M_WAITOK | M_ZERO) : delent;
736 entry->gpe_index = index;
737 entry->gpe_start = gpp->gpp_start;
738 entry->gpe_end = end;
739 error = G_PART_ADD(table, entry, gpp);
741 gctl_error(req, "%d", error);
746 if (delent == NULL) {
748 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
750 LIST_INSERT_AFTER(last, entry, gpe_entry);
751 entry->gpe_created = 1;
753 entry->gpe_deleted = 0;
754 entry->gpe_modified = 1;
756 g_part_new_provider(gp, table, entry);
758 /* Provide feedback if so requested. */
759 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
760 sb = sbuf_new_auto();
761 G_PART_FULLNAME(table, entry, sb, gp->name);
762 if (pp->stripesize > 0 && entry->gpe_pp->stripeoffset != 0)
763 sbuf_printf(sb, " added, but partition is not "
764 "aligned on %u bytes\n", pp->stripesize);
766 sbuf_cat(sb, " added\n");
768 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
775 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
778 struct g_part_table *table;
783 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
787 sz = table->gpt_scheme->gps_bootcodesz;
792 if (gpp->gpp_codesize > sz) {
797 error = G_PART_BOOTCODE(table, gpp);
801 /* Provide feedback if so requested. */
802 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
803 sb = sbuf_new_auto();
804 sbuf_printf(sb, "bootcode written to %s\n", gp->name);
806 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
812 gctl_error(req, "%d", error);
817 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
819 struct g_consumer *cp;
821 struct g_provider *pp;
822 struct g_part_entry *entry, *tmp;
823 struct g_part_table *table;
828 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
832 if (!table->gpt_opened) {
833 gctl_error(req, "%d", EPERM);
839 cp = LIST_FIRST(&gp->consumer);
840 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
842 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
843 while (table->gpt_smhead != 0) {
844 i = ffs(table->gpt_smhead) - 1;
845 error = g_write_data(cp, i * pp->sectorsize, buf,
851 table->gpt_smhead &= ~(1 << i);
853 while (table->gpt_smtail != 0) {
854 i = ffs(table->gpt_smtail) - 1;
855 error = g_write_data(cp, pp->mediasize - (i + 1) *
856 pp->sectorsize, buf, pp->sectorsize);
861 table->gpt_smtail &= ~(1 << i);
866 if (table->gpt_scheme == &g_part_null_scheme) {
868 g_access(cp, -1, -1, -1);
869 g_part_wither(gp, ENXIO);
873 error = G_PART_WRITE(table, cp);
877 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
878 if (!entry->gpe_deleted) {
879 entry->gpe_created = 0;
880 entry->gpe_modified = 0;
883 LIST_REMOVE(entry, gpe_entry);
886 table->gpt_created = 0;
887 table->gpt_opened = 0;
890 g_access(cp, -1, -1, -1);
895 gctl_error(req, "%d", error);
900 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
902 struct g_consumer *cp;
904 struct g_provider *pp;
905 struct g_part_scheme *scheme;
906 struct g_part_table *null, *table;
910 pp = gpp->gpp_provider;
911 scheme = gpp->gpp_scheme;
912 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
915 /* Check that there isn't already a g_part geom on the provider. */
916 gp = g_part_find_geom(pp->name);
919 if (null->gpt_scheme != &g_part_null_scheme) {
920 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
926 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
927 (gpp->gpp_entries < scheme->gps_minent ||
928 gpp->gpp_entries > scheme->gps_maxent)) {
929 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
934 gp = g_new_geomf(&g_part_class, "%s", pp->name);
935 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
939 table->gpt_scheme = gpp->gpp_scheme;
940 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
941 gpp->gpp_entries : scheme->gps_minent;
942 LIST_INIT(&table->gpt_entry);
944 cp = g_new_consumer(gp);
945 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
946 error = g_attach(cp, pp);
948 error = g_access(cp, 1, 1, 1);
950 g_part_wither(gp, error);
951 gctl_error(req, "%d geom '%s'", error, pp->name);
954 table->gpt_opened = 1;
956 cp = LIST_FIRST(&gp->consumer);
957 table->gpt_opened = null->gpt_opened;
958 table->gpt_smhead = null->gpt_smhead;
959 table->gpt_smtail = null->gpt_smtail;
964 /* Make sure the provider has media. */
965 if (pp->mediasize == 0 || pp->sectorsize == 0) {
970 /* Make sure we can nest and if so, determine our depth. */
971 error = g_getattr("PART::isleaf", cp, &attr);
972 if (!error && attr) {
976 error = g_getattr("PART::depth", cp, &attr);
977 table->gpt_depth = (!error) ? attr + 1 : 0;
980 * Synthesize a disk geometry. Some partitioning schemes
981 * depend on it and since some file systems need it even
982 * when the partitition scheme doesn't, we do it here in
983 * scheme-independent code.
985 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
987 error = G_PART_CREATE(table, gpp);
993 table->gpt_created = 1;
995 kobj_delete((kobj_t)null, M_GEOM);
998 * Support automatic commit by filling in the gpp_geom
1001 gpp->gpp_parms |= G_PART_PARM_GEOM;
1004 /* Provide feedback if so requested. */
1005 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1006 sb = sbuf_new_auto();
1007 sbuf_printf(sb, "%s created\n", gp->name);
1009 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1017 g_access(cp, -1, -1, -1);
1018 g_part_wither(gp, error);
1020 kobj_delete((kobj_t)gp->softc, M_GEOM);
1023 gctl_error(req, "%d provider", error);
1028 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
1031 struct g_provider *pp;
1032 struct g_part_entry *entry;
1033 struct g_part_table *table;
1037 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1038 g_topology_assert();
1042 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1043 if (entry->gpe_deleted || entry->gpe_internal)
1045 if (entry->gpe_index == gpp->gpp_index)
1048 if (entry == NULL) {
1049 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1055 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
1056 gctl_error(req, "%d", EBUSY);
1061 entry->gpe_pp = NULL;
1065 g_wither_provider(pp, ENXIO);
1067 /* Provide feedback if so requested. */
1068 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1069 sb = sbuf_new_auto();
1070 G_PART_FULLNAME(table, entry, sb, gp->name);
1071 sbuf_cat(sb, " deleted\n");
1073 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1077 if (entry->gpe_created) {
1078 LIST_REMOVE(entry, gpe_entry);
1081 entry->gpe_modified = 0;
1082 entry->gpe_deleted = 1;
1088 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
1090 struct g_consumer *cp;
1092 struct g_provider *pp;
1093 struct g_part_entry *entry, *tmp;
1094 struct g_part_table *null, *table;
1099 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1100 g_topology_assert();
1103 /* Check for busy providers. */
1104 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1105 if (entry->gpe_deleted || entry->gpe_internal)
1107 if (gpp->gpp_force) {
1111 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1114 gctl_error(req, "%d", EBUSY);
1118 if (gpp->gpp_force) {
1119 /* Destroy all providers. */
1120 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1124 g_wither_provider(pp, ENXIO);
1126 LIST_REMOVE(entry, gpe_entry);
1131 error = G_PART_DESTROY(table, gpp);
1133 gctl_error(req, "%d", error);
1137 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
1141 null->gpt_scheme = &g_part_null_scheme;
1142 LIST_INIT(&null->gpt_entry);
1144 cp = LIST_FIRST(&gp->consumer);
1146 null->gpt_last = pp->mediasize / pp->sectorsize - 1;
1148 null->gpt_depth = table->gpt_depth;
1149 null->gpt_opened = table->gpt_opened;
1150 null->gpt_smhead = table->gpt_smhead;
1151 null->gpt_smtail = table->gpt_smtail;
1153 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1154 LIST_REMOVE(entry, gpe_entry);
1157 kobj_delete((kobj_t)table, M_GEOM);
1159 /* Provide feedback if so requested. */
1160 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1161 sb = sbuf_new_auto();
1162 sbuf_printf(sb, "%s destroyed\n", gp->name);
1164 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1171 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
1174 struct g_part_entry *entry;
1175 struct g_part_table *table;
1180 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1181 g_topology_assert();
1185 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1186 if (entry->gpe_deleted || entry->gpe_internal)
1188 if (entry->gpe_index == gpp->gpp_index)
1191 if (entry == NULL) {
1192 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1196 error = G_PART_MODIFY(table, entry, gpp);
1198 gctl_error(req, "%d", error);
1202 if (!entry->gpe_created)
1203 entry->gpe_modified = 1;
1205 /* Provide feedback if so requested. */
1206 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1207 sb = sbuf_new_auto();
1208 G_PART_FULLNAME(table, entry, sb, gp->name);
1209 sbuf_cat(sb, " modified\n");
1211 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1218 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
1220 gctl_error(req, "%d verb 'move'", ENOSYS);
1225 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
1227 struct g_part_table *table;
1230 int error, recovered;
1233 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1234 g_topology_assert();
1236 error = recovered = 0;
1238 if (table->gpt_corrupt) {
1239 error = G_PART_RECOVER(table);
1241 error = g_part_check_integrity(table,
1242 LIST_FIRST(&gp->consumer));
1244 gctl_error(req, "%d recovering '%s' failed",
1250 /* Provide feedback if so requested. */
1251 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1252 sb = sbuf_new_auto();
1254 sbuf_printf(sb, "%s recovered\n", gp->name);
1256 sbuf_printf(sb, "%s recovering is not needed\n",
1259 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1266 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
1269 struct g_provider *pp;
1270 struct g_part_entry *pe, *entry;
1271 struct g_part_table *table;
1278 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1279 g_topology_assert();
1282 /* check gpp_index */
1283 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1284 if (entry->gpe_deleted || entry->gpe_internal)
1286 if (entry->gpe_index == gpp->gpp_index)
1289 if (entry == NULL) {
1290 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1294 /* check gpp_size */
1295 end = entry->gpe_start + gpp->gpp_size - 1;
1296 if (gpp->gpp_size < 1 || end > table->gpt_last) {
1297 gctl_error(req, "%d size '%jd'", EINVAL,
1298 (intmax_t)gpp->gpp_size);
1302 LIST_FOREACH(pe, &table->gpt_entry, gpe_entry) {
1303 if (pe->gpe_deleted || pe->gpe_internal || pe == entry)
1305 if (end >= pe->gpe_start && end <= pe->gpe_end) {
1306 gctl_error(req, "%d end '%jd'", ENOSPC,
1310 if (entry->gpe_start < pe->gpe_start && end > pe->gpe_end) {
1311 gctl_error(req, "%d size '%jd'", ENOSPC,
1312 (intmax_t)gpp->gpp_size);
1318 if ((g_debugflags & 16) == 0 &&
1319 (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)) {
1320 if (entry->gpe_end - entry->gpe_start + 1 > gpp->gpp_size) {
1321 /* Deny shrinking of an opened partition. */
1322 gctl_error(req, "%d", EBUSY);
1327 error = G_PART_RESIZE(table, entry, gpp);
1329 gctl_error(req, "%d%s", error, error != EBUSY ? "":
1330 " resizing will lead to unexpected shrinking"
1331 " due to alignment");
1335 if (!entry->gpe_created)
1336 entry->gpe_modified = 1;
1338 /* update mediasize of changed provider */
1339 mediasize = (entry->gpe_end - entry->gpe_start + 1) *
1341 g_resize_provider(pp, mediasize);
1343 /* Provide feedback if so requested. */
1344 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1345 sb = sbuf_new_auto();
1346 G_PART_FULLNAME(table, entry, sb, gp->name);
1347 sbuf_cat(sb, " resized\n");
1349 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1356 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
1360 struct g_part_entry *entry;
1361 struct g_part_table *table;
1366 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1367 g_topology_assert();
1371 if (gpp->gpp_parms & G_PART_PARM_INDEX) {
1372 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1373 if (entry->gpe_deleted || entry->gpe_internal)
1375 if (entry->gpe_index == gpp->gpp_index)
1378 if (entry == NULL) {
1379 gctl_error(req, "%d index '%d'", ENOENT,
1386 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
1388 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
1392 /* Provide feedback if so requested. */
1393 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1394 sb = sbuf_new_auto();
1395 sbuf_printf(sb, "%s %sset on ", gpp->gpp_attrib,
1398 G_PART_FULLNAME(table, entry, sb, gp->name);
1400 sbuf_cat(sb, gp->name);
1403 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1410 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1412 struct g_consumer *cp;
1413 struct g_provider *pp;
1415 struct g_part_entry *entry, *tmp;
1416 struct g_part_table *table;
1420 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1421 g_topology_assert();
1424 if (!table->gpt_opened) {
1425 gctl_error(req, "%d", EPERM);
1429 cp = LIST_FIRST(&gp->consumer);
1430 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1431 entry->gpe_modified = 0;
1432 if (entry->gpe_created) {
1436 entry->gpe_pp = NULL;
1437 g_wither_provider(pp, ENXIO);
1439 entry->gpe_deleted = 1;
1441 if (entry->gpe_deleted) {
1442 LIST_REMOVE(entry, gpe_entry);
1447 g_topology_unlock();
1449 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1450 table->gpt_created) ? 1 : 0;
1453 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1454 if (entry->gpe_internal)
1459 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1460 LIST_REMOVE(entry, gpe_entry);
1463 error = g_part_probe(gp, cp, table->gpt_depth);
1466 g_access(cp, -1, -1, -1);
1467 g_part_wither(gp, error);
1473 * Synthesize a disk geometry. Some partitioning schemes
1474 * depend on it and since some file systems need it even
1475 * when the partitition scheme doesn't, we do it here in
1476 * scheme-independent code.
1479 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1482 error = G_PART_READ(table, cp);
1485 error = g_part_check_integrity(table, cp);
1490 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1491 if (!entry->gpe_internal)
1492 g_part_new_provider(gp, table, entry);
1495 table->gpt_opened = 0;
1496 g_access(cp, -1, -1, -1);
1501 gctl_error(req, "%d", error);
1506 g_part_wither(struct g_geom *gp, int error)
1508 struct g_part_entry *entry;
1509 struct g_part_table *table;
1512 if (table != NULL) {
1513 G_PART_DESTROY(table, NULL);
1514 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1515 LIST_REMOVE(entry, gpe_entry);
1518 if (gp->softc != NULL) {
1519 kobj_delete((kobj_t)gp->softc, M_GEOM);
1523 g_wither_geom(gp, error);
1531 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1533 struct g_part_parms gpp;
1534 struct g_part_table *table;
1535 struct gctl_req_arg *ap;
1536 enum g_part_ctl ctlreq;
1537 unsigned int i, mparms, oparms, parm;
1538 int auto_commit, close_on_error;
1539 int error, modifies;
1541 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1542 g_topology_assert();
1544 ctlreq = G_PART_CTL_NONE;
1547 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1550 if (!strcmp(verb, "add")) {
1551 ctlreq = G_PART_CTL_ADD;
1552 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1553 G_PART_PARM_START | G_PART_PARM_TYPE;
1554 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1558 if (!strcmp(verb, "bootcode")) {
1559 ctlreq = G_PART_CTL_BOOTCODE;
1560 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1564 if (!strcmp(verb, "commit")) {
1565 ctlreq = G_PART_CTL_COMMIT;
1566 mparms |= G_PART_PARM_GEOM;
1568 } else if (!strcmp(verb, "create")) {
1569 ctlreq = G_PART_CTL_CREATE;
1570 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1571 oparms |= G_PART_PARM_ENTRIES;
1575 if (!strcmp(verb, "delete")) {
1576 ctlreq = G_PART_CTL_DELETE;
1577 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1578 } else if (!strcmp(verb, "destroy")) {
1579 ctlreq = G_PART_CTL_DESTROY;
1580 mparms |= G_PART_PARM_GEOM;
1581 oparms |= G_PART_PARM_FORCE;
1585 if (!strcmp(verb, "modify")) {
1586 ctlreq = G_PART_CTL_MODIFY;
1587 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1588 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1589 } else if (!strcmp(verb, "move")) {
1590 ctlreq = G_PART_CTL_MOVE;
1591 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1595 if (!strcmp(verb, "recover")) {
1596 ctlreq = G_PART_CTL_RECOVER;
1597 mparms |= G_PART_PARM_GEOM;
1598 } else if (!strcmp(verb, "resize")) {
1599 ctlreq = G_PART_CTL_RESIZE;
1600 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX |
1605 if (!strcmp(verb, "set")) {
1606 ctlreq = G_PART_CTL_SET;
1607 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1608 oparms |= G_PART_PARM_INDEX;
1612 if (!strcmp(verb, "undo")) {
1613 ctlreq = G_PART_CTL_UNDO;
1614 mparms |= G_PART_PARM_GEOM;
1616 } else if (!strcmp(verb, "unset")) {
1617 ctlreq = G_PART_CTL_UNSET;
1618 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1619 oparms |= G_PART_PARM_INDEX;
1623 if (ctlreq == G_PART_CTL_NONE) {
1624 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1628 bzero(&gpp, sizeof(gpp));
1629 for (i = 0; i < req->narg; i++) {
1632 switch (ap->name[0]) {
1634 if (!strcmp(ap->name, "arg0")) {
1636 (G_PART_PARM_GEOM | G_PART_PARM_PROVIDER);
1638 if (!strcmp(ap->name, "attrib"))
1639 parm = G_PART_PARM_ATTRIB;
1642 if (!strcmp(ap->name, "bootcode"))
1643 parm = G_PART_PARM_BOOTCODE;
1646 if (!strcmp(ap->name, "class"))
1650 if (!strcmp(ap->name, "entries"))
1651 parm = G_PART_PARM_ENTRIES;
1654 if (!strcmp(ap->name, "flags"))
1655 parm = G_PART_PARM_FLAGS;
1656 else if (!strcmp(ap->name, "force"))
1657 parm = G_PART_PARM_FORCE;
1660 if (!strcmp(ap->name, "index"))
1661 parm = G_PART_PARM_INDEX;
1664 if (!strcmp(ap->name, "label"))
1665 parm = G_PART_PARM_LABEL;
1668 if (!strcmp(ap->name, "output"))
1669 parm = G_PART_PARM_OUTPUT;
1672 if (!strcmp(ap->name, "scheme"))
1673 parm = G_PART_PARM_SCHEME;
1674 else if (!strcmp(ap->name, "size"))
1675 parm = G_PART_PARM_SIZE;
1676 else if (!strcmp(ap->name, "start"))
1677 parm = G_PART_PARM_START;
1680 if (!strcmp(ap->name, "type"))
1681 parm = G_PART_PARM_TYPE;
1684 if (!strcmp(ap->name, "verb"))
1686 else if (!strcmp(ap->name, "version"))
1687 parm = G_PART_PARM_VERSION;
1690 if ((parm & (mparms | oparms)) == 0) {
1691 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1695 case G_PART_PARM_ATTRIB:
1696 error = g_part_parm_str(req, ap->name,
1699 case G_PART_PARM_BOOTCODE:
1700 error = g_part_parm_bootcode(req, ap->name,
1701 &gpp.gpp_codeptr, &gpp.gpp_codesize);
1703 case G_PART_PARM_ENTRIES:
1704 error = g_part_parm_intmax(req, ap->name,
1707 case G_PART_PARM_FLAGS:
1708 error = g_part_parm_str(req, ap->name, &gpp.gpp_flags);
1710 case G_PART_PARM_FORCE:
1711 error = g_part_parm_uint32(req, ap->name,
1714 case G_PART_PARM_GEOM:
1715 error = g_part_parm_geom(req, ap->name, &gpp.gpp_geom);
1717 case G_PART_PARM_INDEX:
1718 error = g_part_parm_intmax(req, ap->name,
1721 case G_PART_PARM_LABEL:
1722 error = g_part_parm_str(req, ap->name, &gpp.gpp_label);
1724 case G_PART_PARM_OUTPUT:
1725 error = 0; /* Write-only parameter */
1727 case G_PART_PARM_PROVIDER:
1728 error = g_part_parm_provider(req, ap->name,
1731 case G_PART_PARM_SCHEME:
1732 error = g_part_parm_scheme(req, ap->name,
1735 case G_PART_PARM_SIZE:
1736 error = g_part_parm_quad(req, ap->name, &gpp.gpp_size);
1738 case G_PART_PARM_START:
1739 error = g_part_parm_quad(req, ap->name,
1742 case G_PART_PARM_TYPE:
1743 error = g_part_parm_str(req, ap->name, &gpp.gpp_type);
1745 case G_PART_PARM_VERSION:
1746 error = g_part_parm_uint32(req, ap->name,
1751 gctl_error(req, "%d %s", error, ap->name);
1755 if (error == ENOATTR) {
1756 gctl_error(req, "%d param '%s'", error,
1761 gpp.gpp_parms |= parm;
1763 if ((gpp.gpp_parms & mparms) != mparms) {
1764 parm = mparms - (gpp.gpp_parms & mparms);
1765 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1769 /* Obtain permissions if possible/necessary. */
1772 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1773 table = gpp.gpp_geom->softc;
1774 if (table != NULL && table->gpt_corrupt &&
1775 ctlreq != G_PART_CTL_DESTROY &&
1776 ctlreq != G_PART_CTL_RECOVER) {
1777 gctl_error(req, "%d table '%s' is corrupt",
1778 EPERM, gpp.gpp_geom->name);
1781 if (table != NULL && !table->gpt_opened) {
1782 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1785 gctl_error(req, "%d geom '%s'", error,
1786 gpp.gpp_geom->name);
1789 table->gpt_opened = 1;
1794 /* Allow the scheme to check or modify the parameters. */
1795 if (table != NULL) {
1796 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1798 gctl_error(req, "%d pre-check failed", error);
1802 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1805 case G_PART_CTL_NONE:
1806 panic("%s", __func__);
1807 case G_PART_CTL_ADD:
1808 error = g_part_ctl_add(req, &gpp);
1810 case G_PART_CTL_BOOTCODE:
1811 error = g_part_ctl_bootcode(req, &gpp);
1813 case G_PART_CTL_COMMIT:
1814 error = g_part_ctl_commit(req, &gpp);
1816 case G_PART_CTL_CREATE:
1817 error = g_part_ctl_create(req, &gpp);
1819 case G_PART_CTL_DELETE:
1820 error = g_part_ctl_delete(req, &gpp);
1822 case G_PART_CTL_DESTROY:
1823 error = g_part_ctl_destroy(req, &gpp);
1825 case G_PART_CTL_MODIFY:
1826 error = g_part_ctl_modify(req, &gpp);
1828 case G_PART_CTL_MOVE:
1829 error = g_part_ctl_move(req, &gpp);
1831 case G_PART_CTL_RECOVER:
1832 error = g_part_ctl_recover(req, &gpp);
1834 case G_PART_CTL_RESIZE:
1835 error = g_part_ctl_resize(req, &gpp);
1837 case G_PART_CTL_SET:
1838 error = g_part_ctl_setunset(req, &gpp, 1);
1840 case G_PART_CTL_UNDO:
1841 error = g_part_ctl_undo(req, &gpp);
1843 case G_PART_CTL_UNSET:
1844 error = g_part_ctl_setunset(req, &gpp, 0);
1848 /* Implement automatic commit. */
1850 auto_commit = (modifies &&
1851 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1852 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1854 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, ("%s",
1856 error = g_part_ctl_commit(req, &gpp);
1861 if (error && close_on_error) {
1862 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1863 table->gpt_opened = 0;
1868 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1872 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1873 g_topology_assert();
1875 g_part_wither(gp, EINVAL);
1879 static struct g_geom *
1880 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1882 struct g_consumer *cp;
1884 struct g_part_entry *entry;
1885 struct g_part_table *table;
1886 struct root_hold_token *rht;
1890 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1891 g_topology_assert();
1893 /* Skip providers that are already open for writing. */
1898 * Create a GEOM with consumer and hook it up to the provider.
1899 * With that we become part of the topology. Optain read access
1902 gp = g_new_geomf(mp, "%s", pp->name);
1903 cp = g_new_consumer(gp);
1904 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1905 error = g_attach(cp, pp);
1907 error = g_access(cp, 1, 0, 0);
1911 g_destroy_consumer(cp);
1916 rht = root_mount_hold(mp->name);
1917 g_topology_unlock();
1920 * Short-circuit the whole probing galore when there's no
1923 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1928 /* Make sure we can nest and if so, determine our depth. */
1929 error = g_getattr("PART::isleaf", cp, &attr);
1930 if (!error && attr) {
1934 error = g_getattr("PART::depth", cp, &attr);
1935 depth = (!error) ? attr + 1 : 0;
1937 error = g_part_probe(gp, cp, depth);
1944 * Synthesize a disk geometry. Some partitioning schemes
1945 * depend on it and since some file systems need it even
1946 * when the partitition scheme doesn't, we do it here in
1947 * scheme-independent code.
1949 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1951 error = G_PART_READ(table, cp);
1954 error = g_part_check_integrity(table, cp);
1959 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1960 if (!entry->gpe_internal)
1961 g_part_new_provider(gp, table, entry);
1964 root_mount_rel(rht);
1965 g_access(cp, -1, 0, 0);
1970 root_mount_rel(rht);
1971 g_access(cp, -1, 0, 0);
1973 g_destroy_consumer(cp);
1983 g_part_access(struct g_provider *pp, int dr, int dw, int de)
1985 struct g_consumer *cp;
1987 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1990 cp = LIST_FIRST(&pp->geom->consumer);
1992 /* We always gain write-exclusive access. */
1993 return (g_access(cp, dr, dw, dw + de));
1997 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1998 struct g_consumer *cp, struct g_provider *pp)
2001 struct g_part_entry *entry;
2002 struct g_part_table *table;
2004 KASSERT(sb != NULL && gp != NULL, ("%s", __func__));
2007 if (indent == NULL) {
2008 KASSERT(cp == NULL && pp != NULL, ("%s", __func__));
2009 entry = pp->private;
2012 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
2013 (uintmax_t)entry->gpe_offset,
2014 G_PART_TYPE(table, entry, buf, sizeof(buf)));
2016 * libdisk compatibility quirk - the scheme dumps the
2017 * slicer name and partition type in a way that is
2018 * compatible with libdisk. When libdisk is not used
2019 * anymore, this should go away.
2021 G_PART_DUMPCONF(table, entry, sb, indent);
2022 } else if (cp != NULL) { /* Consumer configuration. */
2023 KASSERT(pp == NULL, ("%s", __func__));
2025 } else if (pp != NULL) { /* Provider configuration. */
2026 entry = pp->private;
2029 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
2030 (uintmax_t)entry->gpe_start);
2031 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
2032 (uintmax_t)entry->gpe_end);
2033 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
2035 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
2036 G_PART_TYPE(table, entry, buf, sizeof(buf)));
2037 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
2038 (uintmax_t)entry->gpe_offset);
2039 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
2040 (uintmax_t)pp->mediasize);
2041 G_PART_DUMPCONF(table, entry, sb, indent);
2042 } else { /* Geom configuration. */
2043 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
2044 table->gpt_scheme->name);
2045 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
2046 table->gpt_entries);
2047 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
2048 (uintmax_t)table->gpt_first);
2049 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
2050 (uintmax_t)table->gpt_last);
2051 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
2052 table->gpt_sectors);
2053 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
2055 sbuf_printf(sb, "%s<state>%s</state>\n", indent,
2056 table->gpt_corrupt ? "CORRUPT": "OK");
2057 sbuf_printf(sb, "%s<modified>%s</modified>\n", indent,
2058 table->gpt_opened ? "true": "false");
2059 G_PART_DUMPCONF(table, NULL, sb, indent);
2064 g_part_resize(struct g_consumer *cp)
2066 struct g_part_table *table;
2068 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2069 g_topology_assert();
2071 table = cp->geom->softc;
2072 if (table->gpt_opened == 0) {
2073 if (g_access(cp, 1, 1, 1) != 0)
2075 table->gpt_opened = 1;
2077 if (G_PART_RESIZE(table, NULL, NULL) == 0)
2078 printf("GEOM_PART: %s was automatically resized.\n"
2079 " Use `gpart commit %s` to save changes or "
2080 "`gpart undo %s` to revert them.\n", cp->geom->name,
2081 cp->geom->name, cp->geom->name);
2082 if (g_part_check_integrity(table, cp) != 0) {
2083 g_access(cp, -1, -1, -1);
2084 table->gpt_opened = 0;
2085 g_part_wither(table->gpt_gp, ENXIO);
2090 g_part_orphan(struct g_consumer *cp)
2092 struct g_provider *pp;
2093 struct g_part_table *table;
2096 KASSERT(pp != NULL, ("%s", __func__));
2097 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
2098 g_topology_assert();
2100 KASSERT(pp->error != 0, ("%s", __func__));
2101 table = cp->geom->softc;
2102 if (table != NULL && table->gpt_opened)
2103 g_access(cp, -1, -1, -1);
2104 g_part_wither(cp->geom, pp->error);
2108 g_part_spoiled(struct g_consumer *cp)
2111 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2112 g_topology_assert();
2114 cp->flags |= G_CF_ORPHAN;
2115 g_part_wither(cp->geom, ENXIO);
2119 g_part_start(struct bio *bp)
2122 struct g_consumer *cp;
2124 struct g_part_entry *entry;
2125 struct g_part_table *table;
2126 struct g_kerneldump *gkd;
2127 struct g_provider *pp;
2133 cp = LIST_FIRST(&gp->consumer);
2135 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
2138 entry = pp->private;
2139 if (entry == NULL) {
2140 g_io_deliver(bp, ENXIO);
2144 switch(bp->bio_cmd) {
2148 if (bp->bio_offset >= pp->mediasize) {
2149 g_io_deliver(bp, EIO);
2152 bp2 = g_clone_bio(bp);
2154 g_io_deliver(bp, ENOMEM);
2157 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
2158 bp2->bio_length = pp->mediasize - bp2->bio_offset;
2159 bp2->bio_done = g_std_done;
2160 bp2->bio_offset += entry->gpe_offset;
2161 g_io_request(bp2, cp);
2166 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
2168 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
2170 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
2172 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
2174 if (g_handleattr_str(bp, "PART::scheme",
2175 table->gpt_scheme->name))
2177 if (g_handleattr_str(bp, "PART::type",
2178 G_PART_TYPE(table, entry, buf, sizeof(buf))))
2180 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
2182 * Check that the partition is suitable for kernel
2183 * dumps. Typically only swap partitions should be
2184 * used. If the request comes from the nested scheme
2185 * we allow dumping there as well.
2187 if ((bp->bio_from == NULL ||
2188 bp->bio_from->geom->class != &g_part_class) &&
2189 G_PART_DUMPTO(table, entry) == 0) {
2190 g_io_deliver(bp, ENODEV);
2191 printf("GEOM_PART: Partition '%s' not suitable"
2192 " for kernel dumps (wrong type?)\n",
2196 gkd = (struct g_kerneldump *)bp->bio_data;
2197 if (gkd->offset >= pp->mediasize) {
2198 g_io_deliver(bp, EIO);
2201 if (gkd->offset + gkd->length > pp->mediasize)
2202 gkd->length = pp->mediasize - gkd->offset;
2203 gkd->offset += entry->gpe_offset;
2207 g_io_deliver(bp, EOPNOTSUPP);
2211 bp2 = g_clone_bio(bp);
2213 g_io_deliver(bp, ENOMEM);
2216 bp2->bio_done = g_std_done;
2217 g_io_request(bp2, cp);
2221 g_part_init(struct g_class *mp)
2224 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
2228 g_part_fini(struct g_class *mp)
2231 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
2235 g_part_unload_event(void *arg, int flag)
2237 struct g_consumer *cp;
2239 struct g_provider *pp;
2240 struct g_part_scheme *scheme;
2241 struct g_part_table *table;
2245 if (flag == EV_CANCEL)
2250 scheme = (void *)(*xchg);
2252 g_topology_assert();
2254 LIST_FOREACH(gp, &g_part_class.geom, geom) {
2256 if (table->gpt_scheme != scheme)
2260 LIST_FOREACH(pp, &gp->provider, provider)
2261 acc += pp->acr + pp->acw + pp->ace;
2262 LIST_FOREACH(cp, &gp->consumer, consumer)
2263 acc += cp->acr + cp->acw + cp->ace;
2266 g_part_wither(gp, ENOSYS);
2272 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
2278 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
2280 struct g_part_scheme *iter;
2287 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
2288 if (scheme == iter) {
2289 printf("GEOM_PART: scheme %s is already "
2290 "registered!\n", scheme->name);
2295 TAILQ_INSERT_TAIL(&g_part_schemes, scheme,
2297 g_retaste(&g_part_class);
2301 arg = (uintptr_t)scheme;
2302 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,