2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002, 2005-2009 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
37 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
46 #include <geom/geom.h>
47 #include <geom/geom_ctl.h>
48 #include <geom/geom_int.h>
49 #include <geom/part/g_part.h>
51 #include "g_part_if.h"
53 static kobj_method_t g_part_null_methods[] = {
57 static struct g_part_scheme g_part_null_scheme = {
60 sizeof(struct g_part_table),
63 TAILQ_HEAD(, g_part_scheme) g_part_schemes =
64 TAILQ_HEAD_INITIALIZER(g_part_schemes);
66 struct g_part_alias_list {
68 enum g_part_alias alias;
69 } g_part_alias_list[G_PART_ALIAS_COUNT] = {
70 { "apple-apfs", G_PART_ALIAS_APPLE_APFS },
71 { "apple-boot", G_PART_ALIAS_APPLE_BOOT },
72 { "apple-core-storage", G_PART_ALIAS_APPLE_CORE_STORAGE },
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 { "apple-zfs", G_PART_ALIAS_APPLE_ZFS },
80 { "bios-boot", G_PART_ALIAS_BIOS_BOOT },
81 { "chromeos-firmware", G_PART_ALIAS_CHROMEOS_FIRMWARE },
82 { "chromeos-kernel", G_PART_ALIAS_CHROMEOS_KERNEL },
83 { "chromeos-reserved", G_PART_ALIAS_CHROMEOS_RESERVED },
84 { "chromeos-root", G_PART_ALIAS_CHROMEOS_ROOT },
85 { "dragonfly-ccd", G_PART_ALIAS_DFBSD_CCD },
86 { "dragonfly-hammer", G_PART_ALIAS_DFBSD_HAMMER },
87 { "dragonfly-hammer2", G_PART_ALIAS_DFBSD_HAMMER2 },
88 { "dragonfly-label32", G_PART_ALIAS_DFBSD },
89 { "dragonfly-label64", G_PART_ALIAS_DFBSD64 },
90 { "dragonfly-legacy", G_PART_ALIAS_DFBSD_LEGACY },
91 { "dragonfly-swap", G_PART_ALIAS_DFBSD_SWAP },
92 { "dragonfly-ufs", G_PART_ALIAS_DFBSD_UFS },
93 { "dragonfly-vinum", G_PART_ALIAS_DFBSD_VINUM },
94 { "ebr", G_PART_ALIAS_EBR },
95 { "efi", G_PART_ALIAS_EFI },
96 { "fat16", G_PART_ALIAS_MS_FAT16 },
97 { "fat32", G_PART_ALIAS_MS_FAT32 },
98 { "fat32lba", G_PART_ALIAS_MS_FAT32LBA },
99 { "freebsd", G_PART_ALIAS_FREEBSD },
100 { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
101 { "freebsd-nandfs", G_PART_ALIAS_FREEBSD_NANDFS },
102 { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
103 { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
104 { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
105 { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
106 { "linux-data", G_PART_ALIAS_LINUX_DATA },
107 { "linux-lvm", G_PART_ALIAS_LINUX_LVM },
108 { "linux-raid", G_PART_ALIAS_LINUX_RAID },
109 { "linux-swap", G_PART_ALIAS_LINUX_SWAP },
110 { "mbr", G_PART_ALIAS_MBR },
111 { "ms-basic-data", G_PART_ALIAS_MS_BASIC_DATA },
112 { "ms-ldm-data", G_PART_ALIAS_MS_LDM_DATA },
113 { "ms-ldm-metadata", G_PART_ALIAS_MS_LDM_METADATA },
114 { "ms-recovery", G_PART_ALIAS_MS_RECOVERY },
115 { "ms-reserved", G_PART_ALIAS_MS_RESERVED },
116 { "ms-spaces", G_PART_ALIAS_MS_SPACES },
117 { "netbsd-ccd", G_PART_ALIAS_NETBSD_CCD },
118 { "netbsd-cgd", G_PART_ALIAS_NETBSD_CGD },
119 { "netbsd-ffs", G_PART_ALIAS_NETBSD_FFS },
120 { "netbsd-lfs", G_PART_ALIAS_NETBSD_LFS },
121 { "netbsd-raid", G_PART_ALIAS_NETBSD_RAID },
122 { "netbsd-swap", G_PART_ALIAS_NETBSD_SWAP },
123 { "ntfs", G_PART_ALIAS_MS_NTFS },
124 { "openbsd-data", G_PART_ALIAS_OPENBSD_DATA },
125 { "prep-boot", G_PART_ALIAS_PREP_BOOT },
126 { "solaris-boot", G_PART_ALIAS_SOLARIS_BOOT },
127 { "solaris-root", G_PART_ALIAS_SOLARIS_ROOT },
128 { "solaris-swap", G_PART_ALIAS_SOLARIS_SWAP },
129 { "solaris-backup", G_PART_ALIAS_SOLARIS_BACKUP },
130 { "solaris-var", G_PART_ALIAS_SOLARIS_VAR },
131 { "solaris-home", G_PART_ALIAS_SOLARIS_HOME },
132 { "solaris-altsec", G_PART_ALIAS_SOLARIS_ALTSEC },
133 { "solaris-reserved", G_PART_ALIAS_SOLARIS_RESERVED },
134 { "vmware-reserved", G_PART_ALIAS_VMRESERVED },
135 { "vmware-vmfs", G_PART_ALIAS_VMFS },
136 { "vmware-vmkdiag", G_PART_ALIAS_VMKDIAG },
137 { "vmware-vsanhdr", G_PART_ALIAS_VMVSANHDR },
140 SYSCTL_DECL(_kern_geom);
141 SYSCTL_NODE(_kern_geom, OID_AUTO, part, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
143 u_int geom_part_check_integrity = 1;
144 SYSCTL_UINT(_kern_geom_part, OID_AUTO, check_integrity,
145 CTLFLAG_RWTUN, &geom_part_check_integrity, 1,
146 "Enable integrity checking");
147 static u_int auto_resize = 1;
148 SYSCTL_UINT(_kern_geom_part, OID_AUTO, auto_resize,
149 CTLFLAG_RWTUN, &auto_resize, 1,
150 "Enable auto resize");
151 static u_int allow_nesting = 0;
152 SYSCTL_UINT(_kern_geom_part, OID_AUTO, allow_nesting,
153 CTLFLAG_RWTUN, &allow_nesting, 0,
154 "Allow additional levels of nesting");
155 char g_part_separator[MAXPATHLEN] = "";
156 SYSCTL_STRING(_kern_geom_part, OID_AUTO, separator,
157 CTLFLAG_RDTUN, &g_part_separator, sizeof(g_part_separator),
158 "Partition name separator");
161 * The GEOM partitioning class.
163 static g_ctl_req_t g_part_ctlreq;
164 static g_ctl_destroy_geom_t g_part_destroy_geom;
165 static g_fini_t g_part_fini;
166 static g_init_t g_part_init;
167 static g_taste_t g_part_taste;
169 static g_access_t g_part_access;
170 static g_dumpconf_t g_part_dumpconf;
171 static g_orphan_t g_part_orphan;
172 static g_spoiled_t g_part_spoiled;
173 static g_start_t g_part_start;
174 static g_resize_t g_part_resize;
175 static g_ioctl_t g_part_ioctl;
177 static struct g_class g_part_class = {
179 .version = G_VERSION,
181 .ctlreq = g_part_ctlreq,
182 .destroy_geom = g_part_destroy_geom,
185 .taste = g_part_taste,
187 .access = g_part_access,
188 .dumpconf = g_part_dumpconf,
189 .orphan = g_part_orphan,
190 .spoiled = g_part_spoiled,
191 .start = g_part_start,
192 .resize = g_part_resize,
193 .ioctl = g_part_ioctl,
196 DECLARE_GEOM_CLASS(g_part_class, g_part);
197 MODULE_VERSION(g_part, 0);
203 static void g_part_wither(struct g_geom *, int);
206 g_part_alias_name(enum g_part_alias alias)
210 for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
211 if (g_part_alias_list[i].alias != alias)
213 return (g_part_alias_list[i].lexeme);
220 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
223 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
224 off_t chs, cylinders;
230 for (idx = 0; candidate_heads[idx] != 0; idx++) {
231 heads = candidate_heads[idx];
232 cylinders = blocks / heads / sectors;
233 if (cylinders < heads || cylinders < sectors)
235 if (cylinders > 1023)
237 chs = cylinders * heads * sectors;
238 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
246 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
249 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
251 u_int heads, sectors;
254 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 || sectors == 0 ||
255 g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
256 table->gpt_fixgeom = 0;
257 table->gpt_heads = 0;
258 table->gpt_sectors = 0;
260 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
261 sectors = candidate_sectors[idx];
262 g_part_geometry_heads(blocks, sectors, &chs, &heads);
266 * Prefer a geometry with sectors > 1, but only if
267 * it doesn't bump down the number of heads to 1.
269 if (chs > bestchs || (chs == bestchs && heads > 1 &&
270 table->gpt_sectors == 1)) {
272 table->gpt_heads = heads;
273 table->gpt_sectors = sectors;
277 * If we didn't find a geometry at all, then the disk is
278 * too big. This means we can use the maximum number of
282 table->gpt_heads = 255;
283 table->gpt_sectors = 63;
286 table->gpt_fixgeom = 1;
287 table->gpt_heads = heads;
288 table->gpt_sectors = sectors;
293 g_part_get_physpath_done(struct bio *bp)
296 struct g_part_entry *entry;
297 struct g_part_table *table;
298 struct g_provider *pp;
301 pbp = bp->bio_parent;
307 if (bp->bio_error == 0) {
309 size_t len, remainder;
310 len = strlcat(bp->bio_data, "/", bp->bio_length);
311 if (len < bp->bio_length) {
312 end = bp->bio_data + len;
313 remainder = bp->bio_length - len;
314 G_PART_NAME(table, entry, end, remainder);
320 #define DPRINTF(...) if (bootverbose) { \
321 printf("GEOM_PART: " __VA_ARGS__); \
325 g_part_check_integrity(struct g_part_table *table, struct g_consumer *cp)
327 struct g_part_entry *e1, *e2;
328 struct g_provider *pp;
334 if (table->gpt_last < table->gpt_first) {
335 DPRINTF("last LBA is below first LBA: %jd < %jd\n",
336 (intmax_t)table->gpt_last, (intmax_t)table->gpt_first);
339 if (table->gpt_last > pp->mediasize / pp->sectorsize - 1) {
340 DPRINTF("last LBA extends beyond mediasize: "
341 "%jd > %jd\n", (intmax_t)table->gpt_last,
342 (intmax_t)pp->mediasize / pp->sectorsize - 1);
345 LIST_FOREACH(e1, &table->gpt_entry, gpe_entry) {
346 if (e1->gpe_deleted || e1->gpe_internal)
348 if (e1->gpe_start < table->gpt_first) {
349 DPRINTF("partition %d has start offset below first "
350 "LBA: %jd < %jd\n", e1->gpe_index,
351 (intmax_t)e1->gpe_start,
352 (intmax_t)table->gpt_first);
355 if (e1->gpe_start > table->gpt_last) {
356 DPRINTF("partition %d has start offset beyond last "
357 "LBA: %jd > %jd\n", e1->gpe_index,
358 (intmax_t)e1->gpe_start,
359 (intmax_t)table->gpt_last);
362 if (e1->gpe_end < e1->gpe_start) {
363 DPRINTF("partition %d has end offset below start "
364 "offset: %jd < %jd\n", e1->gpe_index,
365 (intmax_t)e1->gpe_end,
366 (intmax_t)e1->gpe_start);
369 if (e1->gpe_end > table->gpt_last) {
370 DPRINTF("partition %d has end offset beyond last "
371 "LBA: %jd > %jd\n", e1->gpe_index,
372 (intmax_t)e1->gpe_end,
373 (intmax_t)table->gpt_last);
376 if (pp->stripesize > 0) {
377 offset = e1->gpe_start * pp->sectorsize;
378 if (e1->gpe_offset > offset)
379 offset = e1->gpe_offset;
380 if ((offset + pp->stripeoffset) % pp->stripesize) {
381 DPRINTF("partition %d on (%s, %s) is not "
382 "aligned on %ju bytes\n", e1->gpe_index,
383 pp->name, table->gpt_scheme->name,
384 (uintmax_t)pp->stripesize);
385 /* Don't treat this as a critical failure */
389 while ((e2 = LIST_NEXT(e2, gpe_entry)) != NULL) {
390 if (e2->gpe_deleted || e2->gpe_internal)
392 if (e1->gpe_start >= e2->gpe_start &&
393 e1->gpe_start <= e2->gpe_end) {
394 DPRINTF("partition %d has start offset inside "
395 "partition %d: start[%d] %jd >= start[%d] "
396 "%jd <= end[%d] %jd\n",
397 e1->gpe_index, e2->gpe_index,
398 e2->gpe_index, (intmax_t)e2->gpe_start,
399 e1->gpe_index, (intmax_t)e1->gpe_start,
400 e2->gpe_index, (intmax_t)e2->gpe_end);
403 if (e1->gpe_end >= e2->gpe_start &&
404 e1->gpe_end <= e2->gpe_end) {
405 DPRINTF("partition %d has end offset inside "
406 "partition %d: start[%d] %jd >= end[%d] "
407 "%jd <= end[%d] %jd\n",
408 e1->gpe_index, e2->gpe_index,
409 e2->gpe_index, (intmax_t)e2->gpe_start,
410 e1->gpe_index, (intmax_t)e1->gpe_end,
411 e2->gpe_index, (intmax_t)e2->gpe_end);
414 if (e1->gpe_start < e2->gpe_start &&
415 e1->gpe_end > e2->gpe_end) {
416 DPRINTF("partition %d contains partition %d: "
417 "start[%d] %jd > start[%d] %jd, end[%d] "
418 "%jd < end[%d] %jd\n",
419 e1->gpe_index, e2->gpe_index,
420 e1->gpe_index, (intmax_t)e1->gpe_start,
421 e2->gpe_index, (intmax_t)e2->gpe_start,
422 e2->gpe_index, (intmax_t)e2->gpe_end,
423 e1->gpe_index, (intmax_t)e1->gpe_end);
429 printf("GEOM_PART: integrity check failed (%s, %s)\n",
430 pp->name, table->gpt_scheme->name);
431 if (geom_part_check_integrity != 0)
433 table->gpt_corrupt = 1;
439 struct g_part_entry *
440 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
443 struct g_part_entry *entry, *last;
446 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
447 if (entry->gpe_index == index)
449 if (entry->gpe_index > index) {
456 entry = g_malloc(table->gpt_scheme->gps_entrysz,
458 entry->gpe_index = index;
460 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
462 LIST_INSERT_AFTER(last, entry, gpe_entry);
464 entry->gpe_offset = 0;
465 entry->gpe_start = start;
466 entry->gpe_end = end;
471 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
472 struct g_part_entry *entry)
474 struct g_consumer *cp;
475 struct g_provider *pp;
476 struct g_geom_alias *gap;
479 cp = LIST_FIRST(&gp->consumer);
482 offset = entry->gpe_start * pp->sectorsize;
483 if (entry->gpe_offset < offset)
484 entry->gpe_offset = offset;
486 if (entry->gpe_pp == NULL) {
487 entry->gpe_pp = G_PART_NEW_PROVIDER(table, gp, entry, gp->name);
489 * If our parent provider had any aliases, then copy them to our
490 * provider so when geom DEV tastes things later, they will be
491 * there for it to create the aliases with those name used in
492 * place of the geom's name we use to create the provider. The
493 * kobj interface that generates names makes this awkward.
495 LIST_FOREACH(gap, &pp->aliases, ga_next)
496 G_PART_ADD_ALIAS(table, entry->gpe_pp, entry, gap->ga_alias);
497 entry->gpe_pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
498 entry->gpe_pp->private = entry; /* Close the circle. */
500 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */
501 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
503 entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
504 entry->gpe_pp->sectorsize = pp->sectorsize;
505 entry->gpe_pp->stripesize = pp->stripesize;
506 entry->gpe_pp->stripeoffset = pp->stripeoffset + entry->gpe_offset;
507 if (pp->stripesize > 0)
508 entry->gpe_pp->stripeoffset %= pp->stripesize;
509 entry->gpe_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
510 g_error_provider(entry->gpe_pp, 0);
513 static struct g_geom*
514 g_part_find_geom(const char *name)
517 LIST_FOREACH(gp, &g_part_class.geom, geom) {
518 if ((gp->flags & G_GEOM_WITHER) == 0 &&
519 strcmp(name, gp->name) == 0)
526 g_part_parm_geom(struct gctl_req *req, const char *name, struct g_geom **v)
531 gname = gctl_get_asciiparam(req, name);
534 if (strncmp(gname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
535 gname += sizeof(_PATH_DEV) - 1;
536 gp = g_part_find_geom(gname);
538 gctl_error(req, "%d %s '%s'", EINVAL, name, gname);
546 g_part_parm_provider(struct gctl_req *req, const char *name,
547 struct g_provider **v)
549 struct g_provider *pp;
552 pname = gctl_get_asciiparam(req, name);
555 if (strncmp(pname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
556 pname += sizeof(_PATH_DEV) - 1;
557 pp = g_provider_by_name(pname);
559 gctl_error(req, "%d %s '%s'", EINVAL, name, pname);
567 g_part_parm_quad(struct gctl_req *req, const char *name, quad_t *v)
573 p = gctl_get_asciiparam(req, name);
576 q = strtoq(p, &x, 0);
577 if (*x != '\0' || q < 0) {
578 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
586 g_part_parm_scheme(struct gctl_req *req, const char *name,
587 struct g_part_scheme **v)
589 struct g_part_scheme *s;
592 p = gctl_get_asciiparam(req, name);
595 TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
596 if (s == &g_part_null_scheme)
598 if (!strcasecmp(s->name, p))
602 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
610 g_part_parm_str(struct gctl_req *req, const char *name, const char **v)
614 p = gctl_get_asciiparam(req, name);
617 /* An empty label is always valid. */
618 if (strcmp(name, "label") != 0 && p[0] == '\0') {
619 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
627 g_part_parm_intmax(struct gctl_req *req, const char *name, u_int *v)
632 p = gctl_get_param(req, name, &size);
635 if (size != sizeof(*p) || *p < 0 || *p > INT_MAX) {
636 gctl_error(req, "%d %s '%jd'", EINVAL, name, *p);
644 g_part_parm_uint32(struct gctl_req *req, const char *name, u_int *v)
649 p = gctl_get_param(req, name, &size);
652 if (size != sizeof(*p) || *p > INT_MAX) {
653 gctl_error(req, "%d %s '%u'", EINVAL, name, (unsigned int)*p);
661 g_part_parm_bootcode(struct gctl_req *req, const char *name, const void **v,
667 p = gctl_get_param(req, name, &size);
676 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
678 struct g_part_scheme *iter, *scheme;
679 struct g_part_table *table;
683 scheme = (table != NULL) ? table->gpt_scheme : NULL;
684 pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
687 if (pri > 0) { /* error */
692 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
693 if (iter == &g_part_null_scheme)
695 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
698 table->gpt_scheme = iter;
699 table->gpt_depth = depth;
700 probe = G_PART_PROBE(table, cp);
701 if (probe <= 0 && probe > pri) {
704 if (gp->softc != NULL)
705 kobj_delete((kobj_t)gp->softc, M_GEOM);
710 kobj_delete((kobj_t)table, M_GEOM);
714 return ((scheme == NULL) ? ENXIO : 0);
718 * Control request functions.
722 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
725 struct g_provider *pp;
726 struct g_part_entry *delent, *last, *entry;
727 struct g_part_table *table;
734 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
737 pp = LIST_FIRST(&gp->consumer)->provider;
739 end = gpp->gpp_start + gpp->gpp_size - 1;
741 if (gpp->gpp_start < table->gpt_first ||
742 gpp->gpp_start > table->gpt_last) {
743 gctl_error(req, "%d start '%jd'", EINVAL,
744 (intmax_t)gpp->gpp_start);
747 if (end < gpp->gpp_start || end > table->gpt_last) {
748 gctl_error(req, "%d size '%jd'", EINVAL,
749 (intmax_t)gpp->gpp_size);
752 if (gpp->gpp_index > table->gpt_entries) {
753 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
757 delent = last = NULL;
758 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
759 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
760 if (entry->gpe_deleted) {
761 if (entry->gpe_index == index)
765 if (entry->gpe_index == index)
766 index = entry->gpe_index + 1;
767 if (entry->gpe_index < index)
769 if (entry->gpe_internal)
771 if (gpp->gpp_start >= entry->gpe_start &&
772 gpp->gpp_start <= entry->gpe_end) {
773 gctl_error(req, "%d start '%jd'", ENOSPC,
774 (intmax_t)gpp->gpp_start);
777 if (end >= entry->gpe_start && end <= entry->gpe_end) {
778 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
781 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
782 gctl_error(req, "%d size '%jd'", ENOSPC,
783 (intmax_t)gpp->gpp_size);
787 if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
788 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
791 if (index > table->gpt_entries) {
792 gctl_error(req, "%d index '%d'", ENOSPC, index);
796 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
797 M_WAITOK | M_ZERO) : delent;
798 entry->gpe_index = index;
799 entry->gpe_start = gpp->gpp_start;
800 entry->gpe_end = end;
801 error = G_PART_ADD(table, entry, gpp);
803 gctl_error(req, "%d", error);
808 if (delent == NULL) {
810 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
812 LIST_INSERT_AFTER(last, entry, gpe_entry);
813 entry->gpe_created = 1;
815 entry->gpe_deleted = 0;
816 entry->gpe_modified = 1;
818 g_part_new_provider(gp, table, entry);
820 /* Provide feedback if so requested. */
821 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
822 sb = sbuf_new_auto();
823 G_PART_FULLNAME(table, entry, sb, gp->name);
824 if (pp->stripesize > 0 && entry->gpe_pp->stripeoffset != 0)
825 sbuf_printf(sb, " added, but partition is not "
826 "aligned on %ju bytes\n", (uintmax_t)pp->stripesize);
828 sbuf_cat(sb, " added\n");
830 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
837 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
840 struct g_part_table *table;
845 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
849 sz = table->gpt_scheme->gps_bootcodesz;
854 if (gpp->gpp_codesize > sz) {
859 error = G_PART_BOOTCODE(table, gpp);
863 /* Provide feedback if so requested. */
864 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
865 sb = sbuf_new_auto();
866 sbuf_printf(sb, "bootcode written to %s\n", gp->name);
868 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
874 gctl_error(req, "%d", error);
879 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
881 struct g_consumer *cp;
883 struct g_provider *pp;
884 struct g_part_entry *entry, *tmp;
885 struct g_part_table *table;
890 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
894 if (!table->gpt_opened) {
895 gctl_error(req, "%d", EPERM);
901 cp = LIST_FIRST(&gp->consumer);
902 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
904 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
905 while (table->gpt_smhead != 0) {
906 i = ffs(table->gpt_smhead) - 1;
907 error = g_write_data(cp, i * pp->sectorsize, buf,
913 table->gpt_smhead &= ~(1 << i);
915 while (table->gpt_smtail != 0) {
916 i = ffs(table->gpt_smtail) - 1;
917 error = g_write_data(cp, pp->mediasize - (i + 1) *
918 pp->sectorsize, buf, pp->sectorsize);
923 table->gpt_smtail &= ~(1 << i);
928 if (table->gpt_scheme == &g_part_null_scheme) {
930 g_access(cp, -1, -1, -1);
931 g_part_wither(gp, ENXIO);
935 error = G_PART_WRITE(table, cp);
939 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
940 if (!entry->gpe_deleted) {
941 /* Notify consumers that provider might be changed. */
942 if (entry->gpe_modified && (
943 entry->gpe_pp->acw + entry->gpe_pp->ace +
944 entry->gpe_pp->acr) == 0)
945 g_media_changed(entry->gpe_pp, M_NOWAIT);
946 entry->gpe_created = 0;
947 entry->gpe_modified = 0;
950 LIST_REMOVE(entry, gpe_entry);
953 table->gpt_created = 0;
954 table->gpt_opened = 0;
957 g_access(cp, -1, -1, -1);
962 gctl_error(req, "%d", error);
967 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
969 struct g_consumer *cp;
971 struct g_provider *pp;
972 struct g_part_scheme *scheme;
973 struct g_part_table *null, *table;
977 pp = gpp->gpp_provider;
978 scheme = gpp->gpp_scheme;
979 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
982 /* Check that there isn't already a g_part geom on the provider. */
983 gp = g_part_find_geom(pp->name);
986 if (null->gpt_scheme != &g_part_null_scheme) {
987 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
993 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
994 (gpp->gpp_entries < scheme->gps_minent ||
995 gpp->gpp_entries > scheme->gps_maxent)) {
996 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
1001 gp = g_new_geomf(&g_part_class, "%s", pp->name);
1002 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
1006 table->gpt_scheme = gpp->gpp_scheme;
1007 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
1008 gpp->gpp_entries : scheme->gps_minent;
1009 LIST_INIT(&table->gpt_entry);
1011 cp = g_new_consumer(gp);
1012 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1013 error = g_attach(cp, pp);
1015 error = g_access(cp, 1, 1, 1);
1017 g_part_wither(gp, error);
1018 gctl_error(req, "%d geom '%s'", error, pp->name);
1021 table->gpt_opened = 1;
1023 cp = LIST_FIRST(&gp->consumer);
1024 table->gpt_opened = null->gpt_opened;
1025 table->gpt_smhead = null->gpt_smhead;
1026 table->gpt_smtail = null->gpt_smtail;
1029 g_topology_unlock();
1031 /* Make sure the provider has media. */
1032 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1037 /* Make sure we can nest and if so, determine our depth. */
1038 error = g_getattr("PART::isleaf", cp, &attr);
1039 if (!error && attr) {
1043 error = g_getattr("PART::depth", cp, &attr);
1044 table->gpt_depth = (!error) ? attr + 1 : 0;
1047 * Synthesize a disk geometry. Some partitioning schemes
1048 * depend on it and since some file systems need it even
1049 * when the partitition scheme doesn't, we do it here in
1050 * scheme-independent code.
1052 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1054 error = G_PART_CREATE(table, gpp);
1060 table->gpt_created = 1;
1062 kobj_delete((kobj_t)null, M_GEOM);
1065 * Support automatic commit by filling in the gpp_geom
1068 gpp->gpp_parms |= G_PART_PARM_GEOM;
1071 /* Provide feedback if so requested. */
1072 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1073 sb = sbuf_new_auto();
1074 sbuf_printf(sb, "%s created\n", gp->name);
1076 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1084 g_access(cp, -1, -1, -1);
1085 g_part_wither(gp, error);
1087 kobj_delete((kobj_t)gp->softc, M_GEOM);
1090 gctl_error(req, "%d provider", error);
1095 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
1098 struct g_provider *pp;
1099 struct g_part_entry *entry;
1100 struct g_part_table *table;
1104 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1105 g_topology_assert();
1109 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1110 if (entry->gpe_deleted || entry->gpe_internal)
1112 if (entry->gpe_index == gpp->gpp_index)
1115 if (entry == NULL) {
1116 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1122 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
1123 gctl_error(req, "%d", EBUSY);
1128 entry->gpe_pp = NULL;
1132 g_wither_provider(pp, ENXIO);
1134 /* Provide feedback if so requested. */
1135 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1136 sb = sbuf_new_auto();
1137 G_PART_FULLNAME(table, entry, sb, gp->name);
1138 sbuf_cat(sb, " deleted\n");
1140 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1144 if (entry->gpe_created) {
1145 LIST_REMOVE(entry, gpe_entry);
1148 entry->gpe_modified = 0;
1149 entry->gpe_deleted = 1;
1155 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
1157 struct g_consumer *cp;
1159 struct g_provider *pp;
1160 struct g_part_entry *entry, *tmp;
1161 struct g_part_table *null, *table;
1166 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1167 g_topology_assert();
1170 /* Check for busy providers. */
1171 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1172 if (entry->gpe_deleted || entry->gpe_internal)
1174 if (gpp->gpp_force) {
1178 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1181 gctl_error(req, "%d", EBUSY);
1185 if (gpp->gpp_force) {
1186 /* Destroy all providers. */
1187 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1191 g_wither_provider(pp, ENXIO);
1193 LIST_REMOVE(entry, gpe_entry);
1198 error = G_PART_DESTROY(table, gpp);
1200 gctl_error(req, "%d", error);
1204 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
1208 null->gpt_scheme = &g_part_null_scheme;
1209 LIST_INIT(&null->gpt_entry);
1211 cp = LIST_FIRST(&gp->consumer);
1213 null->gpt_last = pp->mediasize / pp->sectorsize - 1;
1215 null->gpt_depth = table->gpt_depth;
1216 null->gpt_opened = table->gpt_opened;
1217 null->gpt_smhead = table->gpt_smhead;
1218 null->gpt_smtail = table->gpt_smtail;
1220 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1221 LIST_REMOVE(entry, gpe_entry);
1224 kobj_delete((kobj_t)table, M_GEOM);
1226 /* Provide feedback if so requested. */
1227 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1228 sb = sbuf_new_auto();
1229 sbuf_printf(sb, "%s destroyed\n", gp->name);
1231 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1238 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
1241 struct g_part_entry *entry;
1242 struct g_part_table *table;
1247 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1248 g_topology_assert();
1252 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1253 if (entry->gpe_deleted || entry->gpe_internal)
1255 if (entry->gpe_index == gpp->gpp_index)
1258 if (entry == NULL) {
1259 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1263 error = G_PART_MODIFY(table, entry, gpp);
1265 gctl_error(req, "%d", error);
1269 if (!entry->gpe_created)
1270 entry->gpe_modified = 1;
1272 /* Provide feedback if so requested. */
1273 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1274 sb = sbuf_new_auto();
1275 G_PART_FULLNAME(table, entry, sb, gp->name);
1276 sbuf_cat(sb, " modified\n");
1278 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1285 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
1287 gctl_error(req, "%d verb 'move'", ENOSYS);
1292 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
1294 struct g_part_table *table;
1297 int error, recovered;
1300 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1301 g_topology_assert();
1303 error = recovered = 0;
1305 if (table->gpt_corrupt) {
1306 error = G_PART_RECOVER(table);
1308 error = g_part_check_integrity(table,
1309 LIST_FIRST(&gp->consumer));
1311 gctl_error(req, "%d recovering '%s' failed",
1317 /* Provide feedback if so requested. */
1318 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1319 sb = sbuf_new_auto();
1321 sbuf_printf(sb, "%s recovered\n", gp->name);
1323 sbuf_printf(sb, "%s recovering is not needed\n",
1326 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1333 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
1336 struct g_provider *pp;
1337 struct g_part_entry *pe, *entry;
1338 struct g_part_table *table;
1345 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1346 g_topology_assert();
1349 /* check gpp_index */
1350 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1351 if (entry->gpe_deleted || entry->gpe_internal)
1353 if (entry->gpe_index == gpp->gpp_index)
1356 if (entry == NULL) {
1357 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1361 /* check gpp_size */
1362 end = entry->gpe_start + gpp->gpp_size - 1;
1363 if (gpp->gpp_size < 1 || end > table->gpt_last) {
1364 gctl_error(req, "%d size '%jd'", EINVAL,
1365 (intmax_t)gpp->gpp_size);
1369 LIST_FOREACH(pe, &table->gpt_entry, gpe_entry) {
1370 if (pe->gpe_deleted || pe->gpe_internal || pe == entry)
1372 if (end >= pe->gpe_start && end <= pe->gpe_end) {
1373 gctl_error(req, "%d end '%jd'", ENOSPC,
1377 if (entry->gpe_start < pe->gpe_start && end > pe->gpe_end) {
1378 gctl_error(req, "%d size '%jd'", ENOSPC,
1379 (intmax_t)gpp->gpp_size);
1385 if ((g_debugflags & G_F_FOOTSHOOTING) == 0 &&
1386 (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)) {
1387 if (entry->gpe_end - entry->gpe_start + 1 > gpp->gpp_size) {
1388 /* Deny shrinking of an opened partition. */
1389 gctl_error(req, "%d", EBUSY);
1394 error = G_PART_RESIZE(table, entry, gpp);
1396 gctl_error(req, "%d%s", error, error != EBUSY ? "":
1397 " resizing will lead to unexpected shrinking"
1398 " due to alignment");
1402 if (!entry->gpe_created)
1403 entry->gpe_modified = 1;
1405 /* update mediasize of changed provider */
1406 mediasize = (entry->gpe_end - entry->gpe_start + 1) *
1408 g_resize_provider(pp, mediasize);
1410 /* Provide feedback if so requested. */
1411 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1412 sb = sbuf_new_auto();
1413 G_PART_FULLNAME(table, entry, sb, gp->name);
1414 sbuf_cat(sb, " resized\n");
1416 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1423 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
1427 struct g_part_entry *entry;
1428 struct g_part_table *table;
1433 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1434 g_topology_assert();
1438 if (gpp->gpp_parms & G_PART_PARM_INDEX) {
1439 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1440 if (entry->gpe_deleted || entry->gpe_internal)
1442 if (entry->gpe_index == gpp->gpp_index)
1445 if (entry == NULL) {
1446 gctl_error(req, "%d index '%d'", ENOENT,
1453 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
1455 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
1459 /* Provide feedback if so requested. */
1460 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1461 sb = sbuf_new_auto();
1462 sbuf_printf(sb, "%s %sset on ", gpp->gpp_attrib,
1465 G_PART_FULLNAME(table, entry, sb, gp->name);
1467 sbuf_cat(sb, gp->name);
1470 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1477 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1479 struct g_consumer *cp;
1480 struct g_provider *pp;
1482 struct g_part_entry *entry, *tmp;
1483 struct g_part_table *table;
1487 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1488 g_topology_assert();
1491 if (!table->gpt_opened) {
1492 gctl_error(req, "%d", EPERM);
1496 cp = LIST_FIRST(&gp->consumer);
1497 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1498 entry->gpe_modified = 0;
1499 if (entry->gpe_created) {
1503 entry->gpe_pp = NULL;
1504 g_wither_provider(pp, ENXIO);
1506 entry->gpe_deleted = 1;
1508 if (entry->gpe_deleted) {
1509 LIST_REMOVE(entry, gpe_entry);
1514 g_topology_unlock();
1516 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1517 table->gpt_created) ? 1 : 0;
1520 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1521 if (entry->gpe_internal)
1526 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1527 LIST_REMOVE(entry, gpe_entry);
1530 error = g_part_probe(gp, cp, table->gpt_depth);
1533 g_access(cp, -1, -1, -1);
1534 g_part_wither(gp, error);
1540 * Synthesize a disk geometry. Some partitioning schemes
1541 * depend on it and since some file systems need it even
1542 * when the partitition scheme doesn't, we do it here in
1543 * scheme-independent code.
1546 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1549 error = G_PART_READ(table, cp);
1552 error = g_part_check_integrity(table, cp);
1557 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1558 if (!entry->gpe_internal)
1559 g_part_new_provider(gp, table, entry);
1562 table->gpt_opened = 0;
1563 g_access(cp, -1, -1, -1);
1568 gctl_error(req, "%d", error);
1573 g_part_wither(struct g_geom *gp, int error)
1575 struct g_part_entry *entry;
1576 struct g_part_table *table;
1577 struct g_provider *pp;
1580 if (table != NULL) {
1582 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1583 LIST_REMOVE(entry, gpe_entry);
1585 entry->gpe_pp = NULL;
1588 g_wither_provider(pp, error);
1592 G_PART_DESTROY(table, NULL);
1593 kobj_delete((kobj_t)table, M_GEOM);
1595 g_wither_geom(gp, error);
1603 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1605 struct g_part_parms gpp;
1606 struct g_part_table *table;
1607 struct gctl_req_arg *ap;
1608 enum g_part_ctl ctlreq;
1609 unsigned int i, mparms, oparms, parm;
1610 int auto_commit, close_on_error;
1611 int error, modifies;
1613 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1614 g_topology_assert();
1616 ctlreq = G_PART_CTL_NONE;
1619 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1622 if (!strcmp(verb, "add")) {
1623 ctlreq = G_PART_CTL_ADD;
1624 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1625 G_PART_PARM_START | G_PART_PARM_TYPE;
1626 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1630 if (!strcmp(verb, "bootcode")) {
1631 ctlreq = G_PART_CTL_BOOTCODE;
1632 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1633 oparms |= G_PART_PARM_SKIP_DSN;
1637 if (!strcmp(verb, "commit")) {
1638 ctlreq = G_PART_CTL_COMMIT;
1639 mparms |= G_PART_PARM_GEOM;
1641 } else if (!strcmp(verb, "create")) {
1642 ctlreq = G_PART_CTL_CREATE;
1643 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1644 oparms |= G_PART_PARM_ENTRIES;
1648 if (!strcmp(verb, "delete")) {
1649 ctlreq = G_PART_CTL_DELETE;
1650 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1651 } else if (!strcmp(verb, "destroy")) {
1652 ctlreq = G_PART_CTL_DESTROY;
1653 mparms |= G_PART_PARM_GEOM;
1654 oparms |= G_PART_PARM_FORCE;
1658 if (!strcmp(verb, "modify")) {
1659 ctlreq = G_PART_CTL_MODIFY;
1660 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1661 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1662 } else if (!strcmp(verb, "move")) {
1663 ctlreq = G_PART_CTL_MOVE;
1664 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1668 if (!strcmp(verb, "recover")) {
1669 ctlreq = G_PART_CTL_RECOVER;
1670 mparms |= G_PART_PARM_GEOM;
1671 } else if (!strcmp(verb, "resize")) {
1672 ctlreq = G_PART_CTL_RESIZE;
1673 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX |
1678 if (!strcmp(verb, "set")) {
1679 ctlreq = G_PART_CTL_SET;
1680 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1681 oparms |= G_PART_PARM_INDEX;
1685 if (!strcmp(verb, "undo")) {
1686 ctlreq = G_PART_CTL_UNDO;
1687 mparms |= G_PART_PARM_GEOM;
1689 } else if (!strcmp(verb, "unset")) {
1690 ctlreq = G_PART_CTL_UNSET;
1691 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1692 oparms |= G_PART_PARM_INDEX;
1696 if (ctlreq == G_PART_CTL_NONE) {
1697 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1701 bzero(&gpp, sizeof(gpp));
1702 for (i = 0; i < req->narg; i++) {
1705 switch (ap->name[0]) {
1707 if (!strcmp(ap->name, "arg0")) {
1709 (G_PART_PARM_GEOM | G_PART_PARM_PROVIDER);
1711 if (!strcmp(ap->name, "attrib"))
1712 parm = G_PART_PARM_ATTRIB;
1715 if (!strcmp(ap->name, "bootcode"))
1716 parm = G_PART_PARM_BOOTCODE;
1719 if (!strcmp(ap->name, "class"))
1723 if (!strcmp(ap->name, "entries"))
1724 parm = G_PART_PARM_ENTRIES;
1727 if (!strcmp(ap->name, "flags"))
1728 parm = G_PART_PARM_FLAGS;
1729 else if (!strcmp(ap->name, "force"))
1730 parm = G_PART_PARM_FORCE;
1733 if (!strcmp(ap->name, "index"))
1734 parm = G_PART_PARM_INDEX;
1737 if (!strcmp(ap->name, "label"))
1738 parm = G_PART_PARM_LABEL;
1741 if (!strcmp(ap->name, "output"))
1742 parm = G_PART_PARM_OUTPUT;
1745 if (!strcmp(ap->name, "scheme"))
1746 parm = G_PART_PARM_SCHEME;
1747 else if (!strcmp(ap->name, "size"))
1748 parm = G_PART_PARM_SIZE;
1749 else if (!strcmp(ap->name, "start"))
1750 parm = G_PART_PARM_START;
1751 else if (!strcmp(ap->name, "skip_dsn"))
1752 parm = G_PART_PARM_SKIP_DSN;
1755 if (!strcmp(ap->name, "type"))
1756 parm = G_PART_PARM_TYPE;
1759 if (!strcmp(ap->name, "verb"))
1761 else if (!strcmp(ap->name, "version"))
1762 parm = G_PART_PARM_VERSION;
1765 if ((parm & (mparms | oparms)) == 0) {
1766 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1770 case G_PART_PARM_ATTRIB:
1771 error = g_part_parm_str(req, ap->name,
1774 case G_PART_PARM_BOOTCODE:
1775 error = g_part_parm_bootcode(req, ap->name,
1776 &gpp.gpp_codeptr, &gpp.gpp_codesize);
1778 case G_PART_PARM_ENTRIES:
1779 error = g_part_parm_intmax(req, ap->name,
1782 case G_PART_PARM_FLAGS:
1783 error = g_part_parm_str(req, ap->name, &gpp.gpp_flags);
1785 case G_PART_PARM_FORCE:
1786 error = g_part_parm_uint32(req, ap->name,
1789 case G_PART_PARM_GEOM:
1790 error = g_part_parm_geom(req, ap->name, &gpp.gpp_geom);
1792 case G_PART_PARM_INDEX:
1793 error = g_part_parm_intmax(req, ap->name,
1796 case G_PART_PARM_LABEL:
1797 error = g_part_parm_str(req, ap->name, &gpp.gpp_label);
1799 case G_PART_PARM_OUTPUT:
1800 error = 0; /* Write-only parameter */
1802 case G_PART_PARM_PROVIDER:
1803 error = g_part_parm_provider(req, ap->name,
1806 case G_PART_PARM_SCHEME:
1807 error = g_part_parm_scheme(req, ap->name,
1810 case G_PART_PARM_SIZE:
1811 error = g_part_parm_quad(req, ap->name, &gpp.gpp_size);
1813 case G_PART_PARM_SKIP_DSN:
1814 error = g_part_parm_uint32(req, ap->name,
1817 case G_PART_PARM_START:
1818 error = g_part_parm_quad(req, ap->name,
1821 case G_PART_PARM_TYPE:
1822 error = g_part_parm_str(req, ap->name, &gpp.gpp_type);
1824 case G_PART_PARM_VERSION:
1825 error = g_part_parm_uint32(req, ap->name,
1830 gctl_error(req, "%d %s", error, ap->name);
1834 if (error == ENOATTR) {
1835 gctl_error(req, "%d param '%s'", error,
1840 gpp.gpp_parms |= parm;
1842 if ((gpp.gpp_parms & mparms) != mparms) {
1843 parm = mparms - (gpp.gpp_parms & mparms);
1844 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1848 /* Obtain permissions if possible/necessary. */
1851 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1852 table = gpp.gpp_geom->softc;
1853 if (table != NULL && table->gpt_corrupt &&
1854 ctlreq != G_PART_CTL_DESTROY &&
1855 ctlreq != G_PART_CTL_RECOVER &&
1856 geom_part_check_integrity) {
1857 gctl_error(req, "%d table '%s' is corrupt",
1858 EPERM, gpp.gpp_geom->name);
1861 if (table != NULL && !table->gpt_opened) {
1862 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1865 gctl_error(req, "%d geom '%s'", error,
1866 gpp.gpp_geom->name);
1869 table->gpt_opened = 1;
1874 /* Allow the scheme to check or modify the parameters. */
1875 if (table != NULL) {
1876 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1878 gctl_error(req, "%d pre-check failed", error);
1882 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1885 case G_PART_CTL_NONE:
1886 panic("%s", __func__);
1887 case G_PART_CTL_ADD:
1888 error = g_part_ctl_add(req, &gpp);
1890 case G_PART_CTL_BOOTCODE:
1891 error = g_part_ctl_bootcode(req, &gpp);
1893 case G_PART_CTL_COMMIT:
1894 error = g_part_ctl_commit(req, &gpp);
1896 case G_PART_CTL_CREATE:
1897 error = g_part_ctl_create(req, &gpp);
1899 case G_PART_CTL_DELETE:
1900 error = g_part_ctl_delete(req, &gpp);
1902 case G_PART_CTL_DESTROY:
1903 error = g_part_ctl_destroy(req, &gpp);
1905 case G_PART_CTL_MODIFY:
1906 error = g_part_ctl_modify(req, &gpp);
1908 case G_PART_CTL_MOVE:
1909 error = g_part_ctl_move(req, &gpp);
1911 case G_PART_CTL_RECOVER:
1912 error = g_part_ctl_recover(req, &gpp);
1914 case G_PART_CTL_RESIZE:
1915 error = g_part_ctl_resize(req, &gpp);
1917 case G_PART_CTL_SET:
1918 error = g_part_ctl_setunset(req, &gpp, 1);
1920 case G_PART_CTL_UNDO:
1921 error = g_part_ctl_undo(req, &gpp);
1923 case G_PART_CTL_UNSET:
1924 error = g_part_ctl_setunset(req, &gpp, 0);
1928 /* Implement automatic commit. */
1930 auto_commit = (modifies &&
1931 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1932 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1934 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, ("%s",
1936 error = g_part_ctl_commit(req, &gpp);
1941 if (error && close_on_error) {
1942 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1943 table->gpt_opened = 0;
1948 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1952 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1953 g_topology_assert();
1955 g_part_wither(gp, EINVAL);
1959 static struct g_geom *
1960 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1962 struct g_consumer *cp;
1964 struct g_part_entry *entry;
1965 struct g_part_table *table;
1966 struct root_hold_token *rht;
1970 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1971 g_topology_assert();
1973 /* Skip providers that are already open for writing. */
1978 * Create a GEOM with consumer and hook it up to the provider.
1979 * With that we become part of the topology. Obtain read access
1982 gp = g_new_geomf(mp, "%s", pp->name);
1983 cp = g_new_consumer(gp);
1984 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1985 error = g_attach(cp, pp);
1987 error = g_access(cp, 1, 0, 0);
1991 g_destroy_consumer(cp);
1996 rht = root_mount_hold(mp->name);
1997 g_topology_unlock();
2000 * Short-circuit the whole probing galore when there's no
2003 if (pp->mediasize == 0 || pp->sectorsize == 0) {
2008 /* Make sure we can nest and if so, determine our depth. */
2009 error = g_getattr("PART::isleaf", cp, &attr);
2010 if (!error && attr) {
2014 error = g_getattr("PART::depth", cp, &attr);
2015 depth = (!error) ? attr + 1 : 0;
2017 error = g_part_probe(gp, cp, depth);
2024 * Synthesize a disk geometry. Some partitioning schemes
2025 * depend on it and since some file systems need it even
2026 * when the partitition scheme doesn't, we do it here in
2027 * scheme-independent code.
2029 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
2031 error = G_PART_READ(table, cp);
2034 error = g_part_check_integrity(table, cp);
2039 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
2040 if (!entry->gpe_internal)
2041 g_part_new_provider(gp, table, entry);
2044 root_mount_rel(rht);
2045 g_access(cp, -1, 0, 0);
2050 root_mount_rel(rht);
2051 g_access(cp, -1, 0, 0);
2053 g_destroy_consumer(cp);
2063 g_part_access(struct g_provider *pp, int dr, int dw, int de)
2065 struct g_consumer *cp;
2067 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
2070 cp = LIST_FIRST(&pp->geom->consumer);
2072 /* We always gain write-exclusive access. */
2073 return (g_access(cp, dr, dw, dw + de));
2077 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2078 struct g_consumer *cp, struct g_provider *pp)
2081 struct g_part_entry *entry;
2082 struct g_part_table *table;
2084 KASSERT(sb != NULL && gp != NULL, ("%s", __func__));
2087 if (indent == NULL) {
2088 KASSERT(cp == NULL && pp != NULL, ("%s", __func__));
2089 entry = pp->private;
2092 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
2093 (uintmax_t)entry->gpe_offset,
2094 G_PART_TYPE(table, entry, buf, sizeof(buf)));
2096 * libdisk compatibility quirk - the scheme dumps the
2097 * slicer name and partition type in a way that is
2098 * compatible with libdisk. When libdisk is not used
2099 * anymore, this should go away.
2101 G_PART_DUMPCONF(table, entry, sb, indent);
2102 } else if (cp != NULL) { /* Consumer configuration. */
2103 KASSERT(pp == NULL, ("%s", __func__));
2105 } else if (pp != NULL) { /* Provider configuration. */
2106 entry = pp->private;
2109 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
2110 (uintmax_t)entry->gpe_start);
2111 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
2112 (uintmax_t)entry->gpe_end);
2113 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
2115 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
2116 G_PART_TYPE(table, entry, buf, sizeof(buf)));
2117 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
2118 (uintmax_t)entry->gpe_offset);
2119 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
2120 (uintmax_t)pp->mediasize);
2121 G_PART_DUMPCONF(table, entry, sb, indent);
2122 } else { /* Geom configuration. */
2123 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
2124 table->gpt_scheme->name);
2125 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
2126 table->gpt_entries);
2127 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
2128 (uintmax_t)table->gpt_first);
2129 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
2130 (uintmax_t)table->gpt_last);
2131 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
2132 table->gpt_sectors);
2133 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
2135 sbuf_printf(sb, "%s<state>%s</state>\n", indent,
2136 table->gpt_corrupt ? "CORRUPT": "OK");
2137 sbuf_printf(sb, "%s<modified>%s</modified>\n", indent,
2138 table->gpt_opened ? "true": "false");
2139 G_PART_DUMPCONF(table, NULL, sb, indent);
2144 * This start routine is only called for non-trivial requests, all the
2145 * trivial ones are handled autonomously by the slice code.
2146 * For requests we handle here, we must call the g_io_deliver() on the
2147 * bio, and return non-zero to indicate to the slice code that we did so.
2148 * This code executes in the "DOWN" I/O path, this means:
2150 * * Don't grab the topology lock.
2151 * * Don't call biowait, g_getattr(), g_setattr() or g_read_data()
2154 g_part_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
2156 struct g_part_table *table;
2158 table = pp->geom->softc;
2159 return G_PART_IOCTL(table, pp, cmd, data, fflag, td);
2163 g_part_resize(struct g_consumer *cp)
2165 struct g_part_table *table;
2167 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2168 g_topology_assert();
2170 if (auto_resize == 0)
2173 table = cp->geom->softc;
2174 if (table->gpt_opened == 0) {
2175 if (g_access(cp, 1, 1, 1) != 0)
2177 table->gpt_opened = 1;
2179 if (G_PART_RESIZE(table, NULL, NULL) == 0)
2180 printf("GEOM_PART: %s was automatically resized.\n"
2181 " Use `gpart commit %s` to save changes or "
2182 "`gpart undo %s` to revert them.\n", cp->geom->name,
2183 cp->geom->name, cp->geom->name);
2184 if (g_part_check_integrity(table, cp) != 0) {
2185 g_access(cp, -1, -1, -1);
2186 table->gpt_opened = 0;
2187 g_part_wither(table->gpt_gp, ENXIO);
2192 g_part_orphan(struct g_consumer *cp)
2194 struct g_provider *pp;
2195 struct g_part_table *table;
2198 KASSERT(pp != NULL, ("%s", __func__));
2199 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
2200 g_topology_assert();
2202 KASSERT(pp->error != 0, ("%s", __func__));
2203 table = cp->geom->softc;
2204 if (table != NULL && table->gpt_opened)
2205 g_access(cp, -1, -1, -1);
2206 g_part_wither(cp->geom, pp->error);
2210 g_part_spoiled(struct g_consumer *cp)
2213 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2214 g_topology_assert();
2216 cp->flags |= G_CF_ORPHAN;
2217 g_part_wither(cp->geom, ENXIO);
2221 g_part_start(struct bio *bp)
2224 struct g_consumer *cp;
2226 struct g_part_entry *entry;
2227 struct g_part_table *table;
2228 struct g_kerneldump *gkd;
2229 struct g_provider *pp;
2230 void (*done_func)(struct bio *) = g_std_done;
2233 biotrack(bp, __func__);
2238 cp = LIST_FIRST(&gp->consumer);
2240 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
2243 entry = pp->private;
2244 if (entry == NULL) {
2245 g_io_deliver(bp, ENXIO);
2249 switch(bp->bio_cmd) {
2253 if (bp->bio_offset >= pp->mediasize) {
2254 g_io_deliver(bp, EIO);
2257 bp2 = g_clone_bio(bp);
2259 g_io_deliver(bp, ENOMEM);
2262 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
2263 bp2->bio_length = pp->mediasize - bp2->bio_offset;
2264 bp2->bio_done = g_std_done;
2265 bp2->bio_offset += entry->gpe_offset;
2266 g_io_request(bp2, cp);
2272 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
2274 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
2277 * allow_nesting overrides "isleaf" to false _unless_ the
2278 * provider offset is zero, since otherwise we would recurse.
2280 if (g_handleattr_int(bp, "PART::isleaf",
2281 table->gpt_isleaf &&
2282 (allow_nesting == 0 || entry->gpe_offset == 0)))
2284 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
2286 if (g_handleattr_str(bp, "PART::scheme",
2287 table->gpt_scheme->name))
2289 if (g_handleattr_str(bp, "PART::type",
2290 G_PART_TYPE(table, entry, buf, sizeof(buf))))
2292 if (!strcmp("GEOM::physpath", bp->bio_attribute)) {
2293 done_func = g_part_get_physpath_done;
2296 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
2298 * Check that the partition is suitable for kernel
2299 * dumps. Typically only swap partitions should be
2300 * used. If the request comes from the nested scheme
2301 * we allow dumping there as well.
2303 if ((bp->bio_from == NULL ||
2304 bp->bio_from->geom->class != &g_part_class) &&
2305 G_PART_DUMPTO(table, entry) == 0) {
2306 g_io_deliver(bp, ENODEV);
2307 printf("GEOM_PART: Partition '%s' not suitable"
2308 " for kernel dumps (wrong type?)\n",
2312 gkd = (struct g_kerneldump *)bp->bio_data;
2313 if (gkd->offset >= pp->mediasize) {
2314 g_io_deliver(bp, EIO);
2317 if (gkd->offset + gkd->length > pp->mediasize)
2318 gkd->length = pp->mediasize - gkd->offset;
2319 gkd->offset += entry->gpe_offset;
2323 g_io_deliver(bp, EOPNOTSUPP);
2327 bp2 = g_clone_bio(bp);
2329 g_io_deliver(bp, ENOMEM);
2332 bp2->bio_done = done_func;
2333 g_io_request(bp2, cp);
2337 g_part_init(struct g_class *mp)
2340 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
2344 g_part_fini(struct g_class *mp)
2347 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
2351 g_part_unload_event(void *arg, int flag)
2353 struct g_consumer *cp;
2355 struct g_provider *pp;
2356 struct g_part_scheme *scheme;
2357 struct g_part_table *table;
2361 if (flag == EV_CANCEL)
2366 scheme = (void *)(*xchg);
2368 g_topology_assert();
2370 LIST_FOREACH(gp, &g_part_class.geom, geom) {
2372 if (table->gpt_scheme != scheme)
2376 LIST_FOREACH(pp, &gp->provider, provider)
2377 acc += pp->acr + pp->acw + pp->ace;
2378 LIST_FOREACH(cp, &gp->consumer, consumer)
2379 acc += cp->acr + cp->acw + cp->ace;
2382 g_part_wither(gp, ENOSYS);
2388 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
2394 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
2396 struct g_part_scheme *iter;
2403 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
2404 if (scheme == iter) {
2405 printf("GEOM_PART: scheme %s is already "
2406 "registered!\n", scheme->name);
2411 TAILQ_INSERT_TAIL(&g_part_schemes, scheme,
2413 g_retaste(&g_part_class);
2417 arg = (uintptr_t)scheme;
2418 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,