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"
54 #define _PATH_DEV "/dev/"
57 static kobj_method_t g_part_null_methods[] = {
61 static struct g_part_scheme g_part_null_scheme = {
64 sizeof(struct g_part_table),
67 TAILQ_HEAD(, g_part_scheme) g_part_schemes =
68 TAILQ_HEAD_INITIALIZER(g_part_schemes);
70 struct g_part_alias_list {
72 enum g_part_alias alias;
73 } g_part_alias_list[G_PART_ALIAS_COUNT] = {
74 { "apple-apfs", G_PART_ALIAS_APPLE_APFS },
75 { "apple-boot", G_PART_ALIAS_APPLE_BOOT },
76 { "apple-core-storage", G_PART_ALIAS_APPLE_CORE_STORAGE },
77 { "apple-hfs", G_PART_ALIAS_APPLE_HFS },
78 { "apple-label", G_PART_ALIAS_APPLE_LABEL },
79 { "apple-raid", G_PART_ALIAS_APPLE_RAID },
80 { "apple-raid-offline", G_PART_ALIAS_APPLE_RAID_OFFLINE },
81 { "apple-tv-recovery", G_PART_ALIAS_APPLE_TV_RECOVERY },
82 { "apple-ufs", G_PART_ALIAS_APPLE_UFS },
83 { "bios-boot", G_PART_ALIAS_BIOS_BOOT },
84 { "chromeos-firmware", G_PART_ALIAS_CHROMEOS_FIRMWARE },
85 { "chromeos-kernel", G_PART_ALIAS_CHROMEOS_KERNEL },
86 { "chromeos-reserved", G_PART_ALIAS_CHROMEOS_RESERVED },
87 { "chromeos-root", G_PART_ALIAS_CHROMEOS_ROOT },
88 { "dragonfly-ccd", G_PART_ALIAS_DFBSD_CCD },
89 { "dragonfly-hammer", G_PART_ALIAS_DFBSD_HAMMER },
90 { "dragonfly-hammer2", G_PART_ALIAS_DFBSD_HAMMER2 },
91 { "dragonfly-label32", G_PART_ALIAS_DFBSD },
92 { "dragonfly-label64", G_PART_ALIAS_DFBSD64 },
93 { "dragonfly-legacy", G_PART_ALIAS_DFBSD_LEGACY },
94 { "dragonfly-swap", G_PART_ALIAS_DFBSD_SWAP },
95 { "dragonfly-ufs", G_PART_ALIAS_DFBSD_UFS },
96 { "dragonfly-vinum", G_PART_ALIAS_DFBSD_VINUM },
97 { "ebr", G_PART_ALIAS_EBR },
98 { "efi", G_PART_ALIAS_EFI },
99 { "fat16", G_PART_ALIAS_MS_FAT16 },
100 { "fat32", G_PART_ALIAS_MS_FAT32 },
101 { "fat32lba", G_PART_ALIAS_MS_FAT32LBA },
102 { "freebsd", G_PART_ALIAS_FREEBSD },
103 { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
104 { "freebsd-nandfs", G_PART_ALIAS_FREEBSD_NANDFS },
105 { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
106 { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
107 { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
108 { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
109 { "linux-data", G_PART_ALIAS_LINUX_DATA },
110 { "linux-lvm", G_PART_ALIAS_LINUX_LVM },
111 { "linux-raid", G_PART_ALIAS_LINUX_RAID },
112 { "linux-swap", G_PART_ALIAS_LINUX_SWAP },
113 { "mbr", G_PART_ALIAS_MBR },
114 { "ms-basic-data", G_PART_ALIAS_MS_BASIC_DATA },
115 { "ms-ldm-data", G_PART_ALIAS_MS_LDM_DATA },
116 { "ms-ldm-metadata", G_PART_ALIAS_MS_LDM_METADATA },
117 { "ms-recovery", G_PART_ALIAS_MS_RECOVERY },
118 { "ms-reserved", G_PART_ALIAS_MS_RESERVED },
119 { "ms-spaces", G_PART_ALIAS_MS_SPACES },
120 { "netbsd-ccd", G_PART_ALIAS_NETBSD_CCD },
121 { "netbsd-cgd", G_PART_ALIAS_NETBSD_CGD },
122 { "netbsd-ffs", G_PART_ALIAS_NETBSD_FFS },
123 { "netbsd-lfs", G_PART_ALIAS_NETBSD_LFS },
124 { "netbsd-raid", G_PART_ALIAS_NETBSD_RAID },
125 { "netbsd-swap", G_PART_ALIAS_NETBSD_SWAP },
126 { "ntfs", G_PART_ALIAS_MS_NTFS },
127 { "openbsd-data", G_PART_ALIAS_OPENBSD_DATA },
128 { "prep-boot", G_PART_ALIAS_PREP_BOOT },
129 { "vmware-reserved", G_PART_ALIAS_VMRESERVED },
130 { "vmware-vmfs", G_PART_ALIAS_VMFS },
131 { "vmware-vmkdiag", G_PART_ALIAS_VMKDIAG },
132 { "vmware-vsanhdr", G_PART_ALIAS_VMVSANHDR },
135 SYSCTL_DECL(_kern_geom);
136 SYSCTL_NODE(_kern_geom, OID_AUTO, part, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
138 static u_int check_integrity = 1;
139 SYSCTL_UINT(_kern_geom_part, OID_AUTO, check_integrity,
140 CTLFLAG_RWTUN, &check_integrity, 1,
141 "Enable integrity checking");
142 static u_int auto_resize = 1;
143 SYSCTL_UINT(_kern_geom_part, OID_AUTO, auto_resize,
144 CTLFLAG_RWTUN, &auto_resize, 1,
145 "Enable auto resize");
146 static u_int allow_nesting = 0;
147 SYSCTL_UINT(_kern_geom_part, OID_AUTO, allow_nesting,
148 CTLFLAG_RWTUN, &allow_nesting, 0,
149 "Allow additional levels of nesting");
150 char g_part_separator[MAXPATHLEN] = "";
151 SYSCTL_STRING(_kern_geom_part, OID_AUTO, separator,
152 CTLFLAG_RDTUN, &g_part_separator, sizeof(g_part_separator),
153 "Partition name separator");
156 * The GEOM partitioning class.
158 static g_ctl_req_t g_part_ctlreq;
159 static g_ctl_destroy_geom_t g_part_destroy_geom;
160 static g_fini_t g_part_fini;
161 static g_init_t g_part_init;
162 static g_taste_t g_part_taste;
164 static g_access_t g_part_access;
165 static g_dumpconf_t g_part_dumpconf;
166 static g_orphan_t g_part_orphan;
167 static g_spoiled_t g_part_spoiled;
168 static g_start_t g_part_start;
169 static g_resize_t g_part_resize;
170 static g_ioctl_t g_part_ioctl;
172 static struct g_class g_part_class = {
174 .version = G_VERSION,
176 .ctlreq = g_part_ctlreq,
177 .destroy_geom = g_part_destroy_geom,
180 .taste = g_part_taste,
182 .access = g_part_access,
183 .dumpconf = g_part_dumpconf,
184 .orphan = g_part_orphan,
185 .spoiled = g_part_spoiled,
186 .start = g_part_start,
187 .resize = g_part_resize,
188 .ioctl = g_part_ioctl,
191 DECLARE_GEOM_CLASS(g_part_class, g_part);
192 MODULE_VERSION(g_part, 0);
198 static void g_part_wither(struct g_geom *, int);
201 g_part_alias_name(enum g_part_alias alias)
205 for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
206 if (g_part_alias_list[i].alias != alias)
208 return (g_part_alias_list[i].lexeme);
215 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
218 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
219 off_t chs, cylinders;
225 for (idx = 0; candidate_heads[idx] != 0; idx++) {
226 heads = candidate_heads[idx];
227 cylinders = blocks / heads / sectors;
228 if (cylinders < heads || cylinders < sectors)
230 if (cylinders > 1023)
232 chs = cylinders * heads * sectors;
233 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
241 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
244 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
246 u_int heads, sectors;
249 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 || sectors == 0 ||
250 g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
251 table->gpt_fixgeom = 0;
252 table->gpt_heads = 0;
253 table->gpt_sectors = 0;
255 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
256 sectors = candidate_sectors[idx];
257 g_part_geometry_heads(blocks, sectors, &chs, &heads);
261 * Prefer a geometry with sectors > 1, but only if
262 * it doesn't bump down the number of heads to 1.
264 if (chs > bestchs || (chs == bestchs && heads > 1 &&
265 table->gpt_sectors == 1)) {
267 table->gpt_heads = heads;
268 table->gpt_sectors = sectors;
272 * If we didn't find a geometry at all, then the disk is
273 * too big. This means we can use the maximum number of
277 table->gpt_heads = 255;
278 table->gpt_sectors = 63;
281 table->gpt_fixgeom = 1;
282 table->gpt_heads = heads;
283 table->gpt_sectors = sectors;
288 g_part_get_physpath_done(struct bio *bp)
291 struct g_part_entry *entry;
292 struct g_part_table *table;
293 struct g_provider *pp;
296 pbp = bp->bio_parent;
302 if (bp->bio_error == 0) {
304 size_t len, remainder;
305 len = strlcat(bp->bio_data, "/", bp->bio_length);
306 if (len < bp->bio_length) {
307 end = bp->bio_data + len;
308 remainder = bp->bio_length - len;
309 G_PART_NAME(table, entry, end, remainder);
316 #define DPRINTF(...) if (bootverbose) { \
317 printf("GEOM_PART: " __VA_ARGS__); \
321 g_part_check_integrity(struct g_part_table *table, struct g_consumer *cp)
323 struct g_part_entry *e1, *e2;
324 struct g_provider *pp;
330 if (table->gpt_last < table->gpt_first) {
331 DPRINTF("last LBA is below first LBA: %jd < %jd\n",
332 (intmax_t)table->gpt_last, (intmax_t)table->gpt_first);
335 if (table->gpt_last > pp->mediasize / pp->sectorsize - 1) {
336 DPRINTF("last LBA extends beyond mediasize: "
337 "%jd > %jd\n", (intmax_t)table->gpt_last,
338 (intmax_t)pp->mediasize / pp->sectorsize - 1);
341 LIST_FOREACH(e1, &table->gpt_entry, gpe_entry) {
342 if (e1->gpe_deleted || e1->gpe_internal)
344 if (e1->gpe_start < table->gpt_first) {
345 DPRINTF("partition %d has start offset below first "
346 "LBA: %jd < %jd\n", e1->gpe_index,
347 (intmax_t)e1->gpe_start,
348 (intmax_t)table->gpt_first);
351 if (e1->gpe_start > table->gpt_last) {
352 DPRINTF("partition %d has start offset beyond last "
353 "LBA: %jd > %jd\n", e1->gpe_index,
354 (intmax_t)e1->gpe_start,
355 (intmax_t)table->gpt_last);
358 if (e1->gpe_end < e1->gpe_start) {
359 DPRINTF("partition %d has end offset below start "
360 "offset: %jd < %jd\n", e1->gpe_index,
361 (intmax_t)e1->gpe_end,
362 (intmax_t)e1->gpe_start);
365 if (e1->gpe_end > table->gpt_last) {
366 DPRINTF("partition %d has end offset beyond last "
367 "LBA: %jd > %jd\n", e1->gpe_index,
368 (intmax_t)e1->gpe_end,
369 (intmax_t)table->gpt_last);
372 if (pp->stripesize > 0) {
373 offset = e1->gpe_start * pp->sectorsize;
374 if (e1->gpe_offset > offset)
375 offset = e1->gpe_offset;
376 if ((offset + pp->stripeoffset) % pp->stripesize) {
377 DPRINTF("partition %d on (%s, %s) is not "
378 "aligned on %ju bytes\n", e1->gpe_index,
379 pp->name, table->gpt_scheme->name,
380 (uintmax_t)pp->stripesize);
381 /* Don't treat this as a critical failure */
385 while ((e2 = LIST_NEXT(e2, gpe_entry)) != NULL) {
386 if (e2->gpe_deleted || e2->gpe_internal)
388 if (e1->gpe_start >= e2->gpe_start &&
389 e1->gpe_start <= e2->gpe_end) {
390 DPRINTF("partition %d has start offset inside "
391 "partition %d: start[%d] %jd >= start[%d] "
392 "%jd <= end[%d] %jd\n",
393 e1->gpe_index, e2->gpe_index,
394 e2->gpe_index, (intmax_t)e2->gpe_start,
395 e1->gpe_index, (intmax_t)e1->gpe_start,
396 e2->gpe_index, (intmax_t)e2->gpe_end);
399 if (e1->gpe_end >= e2->gpe_start &&
400 e1->gpe_end <= e2->gpe_end) {
401 DPRINTF("partition %d has end offset inside "
402 "partition %d: start[%d] %jd >= end[%d] "
403 "%jd <= end[%d] %jd\n",
404 e1->gpe_index, e2->gpe_index,
405 e2->gpe_index, (intmax_t)e2->gpe_start,
406 e1->gpe_index, (intmax_t)e1->gpe_end,
407 e2->gpe_index, (intmax_t)e2->gpe_end);
410 if (e1->gpe_start < e2->gpe_start &&
411 e1->gpe_end > e2->gpe_end) {
412 DPRINTF("partition %d contains partition %d: "
413 "start[%d] %jd > start[%d] %jd, end[%d] "
414 "%jd < end[%d] %jd\n",
415 e1->gpe_index, e2->gpe_index,
416 e1->gpe_index, (intmax_t)e1->gpe_start,
417 e2->gpe_index, (intmax_t)e2->gpe_start,
418 e2->gpe_index, (intmax_t)e2->gpe_end,
419 e1->gpe_index, (intmax_t)e1->gpe_end);
425 printf("GEOM_PART: integrity check failed (%s, %s)\n",
426 pp->name, table->gpt_scheme->name);
427 if (check_integrity != 0)
429 table->gpt_corrupt = 1;
435 struct g_part_entry *
436 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
439 struct g_part_entry *entry, *last;
442 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
443 if (entry->gpe_index == index)
445 if (entry->gpe_index > index) {
452 entry = g_malloc(table->gpt_scheme->gps_entrysz,
454 entry->gpe_index = index;
456 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
458 LIST_INSERT_AFTER(last, entry, gpe_entry);
460 entry->gpe_offset = 0;
461 entry->gpe_start = start;
462 entry->gpe_end = end;
467 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
468 struct g_part_entry *entry)
470 struct g_consumer *cp;
471 struct g_provider *pp;
472 struct g_geom_alias *gap;
475 cp = LIST_FIRST(&gp->consumer);
478 offset = entry->gpe_start * pp->sectorsize;
479 if (entry->gpe_offset < offset)
480 entry->gpe_offset = offset;
482 if (entry->gpe_pp == NULL) {
483 entry->gpe_pp = G_PART_NEW_PROVIDER(table, gp, entry, gp->name);
485 * If our parent provider had any aliases, then copy them to our
486 * provider so when geom DEV tastes things later, they will be
487 * there for it to create the aliases with those name used in
488 * place of the geom's name we use to create the provider. The
489 * kobj interface that generates names makes this awkward.
491 LIST_FOREACH(gap, &pp->aliases, ga_next)
492 G_PART_ADD_ALIAS(table, entry->gpe_pp, entry, gap->ga_alias);
493 entry->gpe_pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
494 entry->gpe_pp->private = entry; /* Close the circle. */
496 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */
497 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
499 entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
500 entry->gpe_pp->sectorsize = pp->sectorsize;
501 entry->gpe_pp->stripesize = pp->stripesize;
502 entry->gpe_pp->stripeoffset = pp->stripeoffset + entry->gpe_offset;
503 if (pp->stripesize > 0)
504 entry->gpe_pp->stripeoffset %= pp->stripesize;
505 entry->gpe_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
506 g_error_provider(entry->gpe_pp, 0);
509 static struct g_geom*
510 g_part_find_geom(const char *name)
513 LIST_FOREACH(gp, &g_part_class.geom, geom) {
514 if ((gp->flags & G_GEOM_WITHER) == 0 &&
515 strcmp(name, gp->name) == 0)
522 g_part_parm_geom(struct gctl_req *req, const char *name, struct g_geom **v)
527 gname = gctl_get_asciiparam(req, name);
530 if (strncmp(gname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
531 gname += sizeof(_PATH_DEV) - 1;
532 gp = g_part_find_geom(gname);
534 gctl_error(req, "%d %s '%s'", EINVAL, name, gname);
542 g_part_parm_provider(struct gctl_req *req, const char *name,
543 struct g_provider **v)
545 struct g_provider *pp;
548 pname = gctl_get_asciiparam(req, name);
551 if (strncmp(pname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
552 pname += sizeof(_PATH_DEV) - 1;
553 pp = g_provider_by_name(pname);
555 gctl_error(req, "%d %s '%s'", EINVAL, name, pname);
563 g_part_parm_quad(struct gctl_req *req, const char *name, quad_t *v)
569 p = gctl_get_asciiparam(req, name);
572 q = strtoq(p, &x, 0);
573 if (*x != '\0' || q < 0) {
574 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
582 g_part_parm_scheme(struct gctl_req *req, const char *name,
583 struct g_part_scheme **v)
585 struct g_part_scheme *s;
588 p = gctl_get_asciiparam(req, name);
591 TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
592 if (s == &g_part_null_scheme)
594 if (!strcasecmp(s->name, p))
598 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
606 g_part_parm_str(struct gctl_req *req, const char *name, const char **v)
610 p = gctl_get_asciiparam(req, name);
613 /* An empty label is always valid. */
614 if (strcmp(name, "label") != 0 && p[0] == '\0') {
615 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
623 g_part_parm_intmax(struct gctl_req *req, const char *name, u_int *v)
628 p = gctl_get_param(req, name, &size);
631 if (size != sizeof(*p) || *p < 0 || *p > INT_MAX) {
632 gctl_error(req, "%d %s '%jd'", EINVAL, name, *p);
640 g_part_parm_uint32(struct gctl_req *req, const char *name, u_int *v)
645 p = gctl_get_param(req, name, &size);
648 if (size != sizeof(*p) || *p > INT_MAX) {
649 gctl_error(req, "%d %s '%u'", EINVAL, name, (unsigned int)*p);
657 g_part_parm_bootcode(struct gctl_req *req, const char *name, const void **v,
663 p = gctl_get_param(req, name, &size);
672 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
674 struct g_part_scheme *iter, *scheme;
675 struct g_part_table *table;
679 scheme = (table != NULL) ? table->gpt_scheme : NULL;
680 pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
683 if (pri > 0) { /* error */
688 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
689 if (iter == &g_part_null_scheme)
691 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
694 table->gpt_scheme = iter;
695 table->gpt_depth = depth;
696 probe = G_PART_PROBE(table, cp);
697 if (probe <= 0 && probe > pri) {
700 if (gp->softc != NULL)
701 kobj_delete((kobj_t)gp->softc, M_GEOM);
706 kobj_delete((kobj_t)table, M_GEOM);
710 return ((scheme == NULL) ? ENXIO : 0);
714 * Control request functions.
718 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
721 struct g_provider *pp;
722 struct g_part_entry *delent, *last, *entry;
723 struct g_part_table *table;
730 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
733 pp = LIST_FIRST(&gp->consumer)->provider;
735 end = gpp->gpp_start + gpp->gpp_size - 1;
737 if (gpp->gpp_start < table->gpt_first ||
738 gpp->gpp_start > table->gpt_last) {
739 gctl_error(req, "%d start '%jd'", EINVAL,
740 (intmax_t)gpp->gpp_start);
743 if (end < gpp->gpp_start || end > table->gpt_last) {
744 gctl_error(req, "%d size '%jd'", EINVAL,
745 (intmax_t)gpp->gpp_size);
748 if (gpp->gpp_index > table->gpt_entries) {
749 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
753 delent = last = NULL;
754 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
755 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
756 if (entry->gpe_deleted) {
757 if (entry->gpe_index == index)
761 if (entry->gpe_index == index)
762 index = entry->gpe_index + 1;
763 if (entry->gpe_index < index)
765 if (entry->gpe_internal)
767 if (gpp->gpp_start >= entry->gpe_start &&
768 gpp->gpp_start <= entry->gpe_end) {
769 gctl_error(req, "%d start '%jd'", ENOSPC,
770 (intmax_t)gpp->gpp_start);
773 if (end >= entry->gpe_start && end <= entry->gpe_end) {
774 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
777 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
778 gctl_error(req, "%d size '%jd'", ENOSPC,
779 (intmax_t)gpp->gpp_size);
783 if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
784 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
787 if (index > table->gpt_entries) {
788 gctl_error(req, "%d index '%d'", ENOSPC, index);
792 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
793 M_WAITOK | M_ZERO) : delent;
794 entry->gpe_index = index;
795 entry->gpe_start = gpp->gpp_start;
796 entry->gpe_end = end;
797 error = G_PART_ADD(table, entry, gpp);
799 gctl_error(req, "%d", error);
804 if (delent == NULL) {
806 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
808 LIST_INSERT_AFTER(last, entry, gpe_entry);
809 entry->gpe_created = 1;
811 entry->gpe_deleted = 0;
812 entry->gpe_modified = 1;
814 g_part_new_provider(gp, table, entry);
816 /* Provide feedback if so requested. */
817 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
818 sb = sbuf_new_auto();
819 G_PART_FULLNAME(table, entry, sb, gp->name);
820 if (pp->stripesize > 0 && entry->gpe_pp->stripeoffset != 0)
821 sbuf_printf(sb, " added, but partition is not "
822 "aligned on %ju bytes\n", (uintmax_t)pp->stripesize);
824 sbuf_cat(sb, " added\n");
826 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
833 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
836 struct g_part_table *table;
841 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
845 sz = table->gpt_scheme->gps_bootcodesz;
850 if (gpp->gpp_codesize > sz) {
855 error = G_PART_BOOTCODE(table, gpp);
859 /* Provide feedback if so requested. */
860 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
861 sb = sbuf_new_auto();
862 sbuf_printf(sb, "bootcode written to %s\n", gp->name);
864 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
870 gctl_error(req, "%d", error);
875 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
877 struct g_consumer *cp;
879 struct g_provider *pp;
880 struct g_part_entry *entry, *tmp;
881 struct g_part_table *table;
886 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
890 if (!table->gpt_opened) {
891 gctl_error(req, "%d", EPERM);
897 cp = LIST_FIRST(&gp->consumer);
898 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
900 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
901 while (table->gpt_smhead != 0) {
902 i = ffs(table->gpt_smhead) - 1;
903 error = g_write_data(cp, i * pp->sectorsize, buf,
909 table->gpt_smhead &= ~(1 << i);
911 while (table->gpt_smtail != 0) {
912 i = ffs(table->gpt_smtail) - 1;
913 error = g_write_data(cp, pp->mediasize - (i + 1) *
914 pp->sectorsize, buf, pp->sectorsize);
919 table->gpt_smtail &= ~(1 << i);
924 if (table->gpt_scheme == &g_part_null_scheme) {
926 g_access(cp, -1, -1, -1);
927 g_part_wither(gp, ENXIO);
931 error = G_PART_WRITE(table, cp);
935 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
936 if (!entry->gpe_deleted) {
937 /* Notify consumers that provider might be changed. */
938 if (entry->gpe_modified && (
939 entry->gpe_pp->acw + entry->gpe_pp->ace +
940 entry->gpe_pp->acr) == 0)
941 g_media_changed(entry->gpe_pp, M_NOWAIT);
942 entry->gpe_created = 0;
943 entry->gpe_modified = 0;
946 LIST_REMOVE(entry, gpe_entry);
949 table->gpt_created = 0;
950 table->gpt_opened = 0;
953 g_access(cp, -1, -1, -1);
958 gctl_error(req, "%d", error);
963 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
965 struct g_consumer *cp;
967 struct g_provider *pp;
968 struct g_part_scheme *scheme;
969 struct g_part_table *null, *table;
973 pp = gpp->gpp_provider;
974 scheme = gpp->gpp_scheme;
975 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
978 /* Check that there isn't already a g_part geom on the provider. */
979 gp = g_part_find_geom(pp->name);
982 if (null->gpt_scheme != &g_part_null_scheme) {
983 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
989 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
990 (gpp->gpp_entries < scheme->gps_minent ||
991 gpp->gpp_entries > scheme->gps_maxent)) {
992 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
997 gp = g_new_geomf(&g_part_class, "%s", pp->name);
998 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
1002 table->gpt_scheme = gpp->gpp_scheme;
1003 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
1004 gpp->gpp_entries : scheme->gps_minent;
1005 LIST_INIT(&table->gpt_entry);
1007 cp = g_new_consumer(gp);
1008 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1009 error = g_attach(cp, pp);
1011 error = g_access(cp, 1, 1, 1);
1013 g_part_wither(gp, error);
1014 gctl_error(req, "%d geom '%s'", error, pp->name);
1017 table->gpt_opened = 1;
1019 cp = LIST_FIRST(&gp->consumer);
1020 table->gpt_opened = null->gpt_opened;
1021 table->gpt_smhead = null->gpt_smhead;
1022 table->gpt_smtail = null->gpt_smtail;
1025 g_topology_unlock();
1027 /* Make sure the provider has media. */
1028 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1033 /* Make sure we can nest and if so, determine our depth. */
1034 error = g_getattr("PART::isleaf", cp, &attr);
1035 if (!error && attr) {
1039 error = g_getattr("PART::depth", cp, &attr);
1040 table->gpt_depth = (!error) ? attr + 1 : 0;
1043 * Synthesize a disk geometry. Some partitioning schemes
1044 * depend on it and since some file systems need it even
1045 * when the partitition scheme doesn't, we do it here in
1046 * scheme-independent code.
1048 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1050 error = G_PART_CREATE(table, gpp);
1056 table->gpt_created = 1;
1058 kobj_delete((kobj_t)null, M_GEOM);
1061 * Support automatic commit by filling in the gpp_geom
1064 gpp->gpp_parms |= G_PART_PARM_GEOM;
1067 /* Provide feedback if so requested. */
1068 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1069 sb = sbuf_new_auto();
1070 sbuf_printf(sb, "%s created\n", gp->name);
1072 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1080 g_access(cp, -1, -1, -1);
1081 g_part_wither(gp, error);
1083 kobj_delete((kobj_t)gp->softc, M_GEOM);
1086 gctl_error(req, "%d provider", error);
1091 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
1094 struct g_provider *pp;
1095 struct g_part_entry *entry;
1096 struct g_part_table *table;
1100 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1101 g_topology_assert();
1105 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1106 if (entry->gpe_deleted || entry->gpe_internal)
1108 if (entry->gpe_index == gpp->gpp_index)
1111 if (entry == NULL) {
1112 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1118 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
1119 gctl_error(req, "%d", EBUSY);
1124 entry->gpe_pp = NULL;
1128 g_wither_provider(pp, ENXIO);
1130 /* Provide feedback if so requested. */
1131 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1132 sb = sbuf_new_auto();
1133 G_PART_FULLNAME(table, entry, sb, gp->name);
1134 sbuf_cat(sb, " deleted\n");
1136 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1140 if (entry->gpe_created) {
1141 LIST_REMOVE(entry, gpe_entry);
1144 entry->gpe_modified = 0;
1145 entry->gpe_deleted = 1;
1151 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
1153 struct g_consumer *cp;
1155 struct g_provider *pp;
1156 struct g_part_entry *entry, *tmp;
1157 struct g_part_table *null, *table;
1162 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1163 g_topology_assert();
1166 /* Check for busy providers. */
1167 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1168 if (entry->gpe_deleted || entry->gpe_internal)
1170 if (gpp->gpp_force) {
1174 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1177 gctl_error(req, "%d", EBUSY);
1181 if (gpp->gpp_force) {
1182 /* Destroy all providers. */
1183 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1187 g_wither_provider(pp, ENXIO);
1189 LIST_REMOVE(entry, gpe_entry);
1194 error = G_PART_DESTROY(table, gpp);
1196 gctl_error(req, "%d", error);
1200 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
1204 null->gpt_scheme = &g_part_null_scheme;
1205 LIST_INIT(&null->gpt_entry);
1207 cp = LIST_FIRST(&gp->consumer);
1209 null->gpt_last = pp->mediasize / pp->sectorsize - 1;
1211 null->gpt_depth = table->gpt_depth;
1212 null->gpt_opened = table->gpt_opened;
1213 null->gpt_smhead = table->gpt_smhead;
1214 null->gpt_smtail = table->gpt_smtail;
1216 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1217 LIST_REMOVE(entry, gpe_entry);
1220 kobj_delete((kobj_t)table, M_GEOM);
1222 /* Provide feedback if so requested. */
1223 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1224 sb = sbuf_new_auto();
1225 sbuf_printf(sb, "%s destroyed\n", gp->name);
1227 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1234 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
1237 struct g_part_entry *entry;
1238 struct g_part_table *table;
1243 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1244 g_topology_assert();
1248 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1249 if (entry->gpe_deleted || entry->gpe_internal)
1251 if (entry->gpe_index == gpp->gpp_index)
1254 if (entry == NULL) {
1255 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1259 error = G_PART_MODIFY(table, entry, gpp);
1261 gctl_error(req, "%d", error);
1265 if (!entry->gpe_created)
1266 entry->gpe_modified = 1;
1268 /* Provide feedback if so requested. */
1269 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1270 sb = sbuf_new_auto();
1271 G_PART_FULLNAME(table, entry, sb, gp->name);
1272 sbuf_cat(sb, " modified\n");
1274 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1281 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
1283 gctl_error(req, "%d verb 'move'", ENOSYS);
1288 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
1290 struct g_part_table *table;
1293 int error, recovered;
1296 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1297 g_topology_assert();
1299 error = recovered = 0;
1301 if (table->gpt_corrupt) {
1302 error = G_PART_RECOVER(table);
1304 error = g_part_check_integrity(table,
1305 LIST_FIRST(&gp->consumer));
1307 gctl_error(req, "%d recovering '%s' failed",
1313 /* Provide feedback if so requested. */
1314 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1315 sb = sbuf_new_auto();
1317 sbuf_printf(sb, "%s recovered\n", gp->name);
1319 sbuf_printf(sb, "%s recovering is not needed\n",
1322 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1329 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
1332 struct g_provider *pp;
1333 struct g_part_entry *pe, *entry;
1334 struct g_part_table *table;
1341 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1342 g_topology_assert();
1345 /* check gpp_index */
1346 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1347 if (entry->gpe_deleted || entry->gpe_internal)
1349 if (entry->gpe_index == gpp->gpp_index)
1352 if (entry == NULL) {
1353 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1357 /* check gpp_size */
1358 end = entry->gpe_start + gpp->gpp_size - 1;
1359 if (gpp->gpp_size < 1 || end > table->gpt_last) {
1360 gctl_error(req, "%d size '%jd'", EINVAL,
1361 (intmax_t)gpp->gpp_size);
1365 LIST_FOREACH(pe, &table->gpt_entry, gpe_entry) {
1366 if (pe->gpe_deleted || pe->gpe_internal || pe == entry)
1368 if (end >= pe->gpe_start && end <= pe->gpe_end) {
1369 gctl_error(req, "%d end '%jd'", ENOSPC,
1373 if (entry->gpe_start < pe->gpe_start && end > pe->gpe_end) {
1374 gctl_error(req, "%d size '%jd'", ENOSPC,
1375 (intmax_t)gpp->gpp_size);
1381 if ((g_debugflags & G_F_FOOTSHOOTING) == 0 &&
1382 (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)) {
1383 if (entry->gpe_end - entry->gpe_start + 1 > gpp->gpp_size) {
1384 /* Deny shrinking of an opened partition. */
1385 gctl_error(req, "%d", EBUSY);
1390 error = G_PART_RESIZE(table, entry, gpp);
1392 gctl_error(req, "%d%s", error, error != EBUSY ? "":
1393 " resizing will lead to unexpected shrinking"
1394 " due to alignment");
1398 if (!entry->gpe_created)
1399 entry->gpe_modified = 1;
1401 /* update mediasize of changed provider */
1402 mediasize = (entry->gpe_end - entry->gpe_start + 1) *
1404 g_resize_provider(pp, mediasize);
1406 /* Provide feedback if so requested. */
1407 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1408 sb = sbuf_new_auto();
1409 G_PART_FULLNAME(table, entry, sb, gp->name);
1410 sbuf_cat(sb, " resized\n");
1412 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1419 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
1423 struct g_part_entry *entry;
1424 struct g_part_table *table;
1429 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1430 g_topology_assert();
1434 if (gpp->gpp_parms & G_PART_PARM_INDEX) {
1435 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1436 if (entry->gpe_deleted || entry->gpe_internal)
1438 if (entry->gpe_index == gpp->gpp_index)
1441 if (entry == NULL) {
1442 gctl_error(req, "%d index '%d'", ENOENT,
1449 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
1451 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
1455 /* Provide feedback if so requested. */
1456 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1457 sb = sbuf_new_auto();
1458 sbuf_printf(sb, "%s %sset on ", gpp->gpp_attrib,
1461 G_PART_FULLNAME(table, entry, sb, gp->name);
1463 sbuf_cat(sb, gp->name);
1466 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1473 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1475 struct g_consumer *cp;
1476 struct g_provider *pp;
1478 struct g_part_entry *entry, *tmp;
1479 struct g_part_table *table;
1483 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1484 g_topology_assert();
1487 if (!table->gpt_opened) {
1488 gctl_error(req, "%d", EPERM);
1492 cp = LIST_FIRST(&gp->consumer);
1493 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1494 entry->gpe_modified = 0;
1495 if (entry->gpe_created) {
1499 entry->gpe_pp = NULL;
1500 g_wither_provider(pp, ENXIO);
1502 entry->gpe_deleted = 1;
1504 if (entry->gpe_deleted) {
1505 LIST_REMOVE(entry, gpe_entry);
1510 g_topology_unlock();
1512 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1513 table->gpt_created) ? 1 : 0;
1516 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1517 if (entry->gpe_internal)
1522 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1523 LIST_REMOVE(entry, gpe_entry);
1526 error = g_part_probe(gp, cp, table->gpt_depth);
1529 g_access(cp, -1, -1, -1);
1530 g_part_wither(gp, error);
1536 * Synthesize a disk geometry. Some partitioning schemes
1537 * depend on it and since some file systems need it even
1538 * when the partitition scheme doesn't, we do it here in
1539 * scheme-independent code.
1542 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1545 error = G_PART_READ(table, cp);
1548 error = g_part_check_integrity(table, cp);
1553 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1554 if (!entry->gpe_internal)
1555 g_part_new_provider(gp, table, entry);
1558 table->gpt_opened = 0;
1559 g_access(cp, -1, -1, -1);
1564 gctl_error(req, "%d", error);
1569 g_part_wither(struct g_geom *gp, int error)
1571 struct g_part_entry *entry;
1572 struct g_part_table *table;
1573 struct g_provider *pp;
1576 if (table != NULL) {
1578 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1579 LIST_REMOVE(entry, gpe_entry);
1581 entry->gpe_pp = NULL;
1584 g_wither_provider(pp, error);
1588 G_PART_DESTROY(table, NULL);
1589 kobj_delete((kobj_t)table, M_GEOM);
1591 g_wither_geom(gp, error);
1599 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1601 struct g_part_parms gpp;
1602 struct g_part_table *table;
1603 struct gctl_req_arg *ap;
1604 enum g_part_ctl ctlreq;
1605 unsigned int i, mparms, oparms, parm;
1606 int auto_commit, close_on_error;
1607 int error, modifies;
1609 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1610 g_topology_assert();
1612 ctlreq = G_PART_CTL_NONE;
1615 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1618 if (!strcmp(verb, "add")) {
1619 ctlreq = G_PART_CTL_ADD;
1620 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1621 G_PART_PARM_START | G_PART_PARM_TYPE;
1622 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1626 if (!strcmp(verb, "bootcode")) {
1627 ctlreq = G_PART_CTL_BOOTCODE;
1628 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1629 oparms |= G_PART_PARM_SKIP_DSN;
1633 if (!strcmp(verb, "commit")) {
1634 ctlreq = G_PART_CTL_COMMIT;
1635 mparms |= G_PART_PARM_GEOM;
1637 } else if (!strcmp(verb, "create")) {
1638 ctlreq = G_PART_CTL_CREATE;
1639 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1640 oparms |= G_PART_PARM_ENTRIES;
1644 if (!strcmp(verb, "delete")) {
1645 ctlreq = G_PART_CTL_DELETE;
1646 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1647 } else if (!strcmp(verb, "destroy")) {
1648 ctlreq = G_PART_CTL_DESTROY;
1649 mparms |= G_PART_PARM_GEOM;
1650 oparms |= G_PART_PARM_FORCE;
1654 if (!strcmp(verb, "modify")) {
1655 ctlreq = G_PART_CTL_MODIFY;
1656 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1657 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1658 } else if (!strcmp(verb, "move")) {
1659 ctlreq = G_PART_CTL_MOVE;
1660 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1664 if (!strcmp(verb, "recover")) {
1665 ctlreq = G_PART_CTL_RECOVER;
1666 mparms |= G_PART_PARM_GEOM;
1667 } else if (!strcmp(verb, "resize")) {
1668 ctlreq = G_PART_CTL_RESIZE;
1669 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX |
1674 if (!strcmp(verb, "set")) {
1675 ctlreq = G_PART_CTL_SET;
1676 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1677 oparms |= G_PART_PARM_INDEX;
1681 if (!strcmp(verb, "undo")) {
1682 ctlreq = G_PART_CTL_UNDO;
1683 mparms |= G_PART_PARM_GEOM;
1685 } else if (!strcmp(verb, "unset")) {
1686 ctlreq = G_PART_CTL_UNSET;
1687 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1688 oparms |= G_PART_PARM_INDEX;
1692 if (ctlreq == G_PART_CTL_NONE) {
1693 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1697 bzero(&gpp, sizeof(gpp));
1698 for (i = 0; i < req->narg; i++) {
1701 switch (ap->name[0]) {
1703 if (!strcmp(ap->name, "arg0")) {
1705 (G_PART_PARM_GEOM | G_PART_PARM_PROVIDER);
1707 if (!strcmp(ap->name, "attrib"))
1708 parm = G_PART_PARM_ATTRIB;
1711 if (!strcmp(ap->name, "bootcode"))
1712 parm = G_PART_PARM_BOOTCODE;
1715 if (!strcmp(ap->name, "class"))
1719 if (!strcmp(ap->name, "entries"))
1720 parm = G_PART_PARM_ENTRIES;
1723 if (!strcmp(ap->name, "flags"))
1724 parm = G_PART_PARM_FLAGS;
1725 else if (!strcmp(ap->name, "force"))
1726 parm = G_PART_PARM_FORCE;
1729 if (!strcmp(ap->name, "index"))
1730 parm = G_PART_PARM_INDEX;
1733 if (!strcmp(ap->name, "label"))
1734 parm = G_PART_PARM_LABEL;
1737 if (!strcmp(ap->name, "output"))
1738 parm = G_PART_PARM_OUTPUT;
1741 if (!strcmp(ap->name, "scheme"))
1742 parm = G_PART_PARM_SCHEME;
1743 else if (!strcmp(ap->name, "size"))
1744 parm = G_PART_PARM_SIZE;
1745 else if (!strcmp(ap->name, "start"))
1746 parm = G_PART_PARM_START;
1747 else if (!strcmp(ap->name, "skip_dsn"))
1748 parm = G_PART_PARM_SKIP_DSN;
1751 if (!strcmp(ap->name, "type"))
1752 parm = G_PART_PARM_TYPE;
1755 if (!strcmp(ap->name, "verb"))
1757 else if (!strcmp(ap->name, "version"))
1758 parm = G_PART_PARM_VERSION;
1761 if ((parm & (mparms | oparms)) == 0) {
1762 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1766 case G_PART_PARM_ATTRIB:
1767 error = g_part_parm_str(req, ap->name,
1770 case G_PART_PARM_BOOTCODE:
1771 error = g_part_parm_bootcode(req, ap->name,
1772 &gpp.gpp_codeptr, &gpp.gpp_codesize);
1774 case G_PART_PARM_ENTRIES:
1775 error = g_part_parm_intmax(req, ap->name,
1778 case G_PART_PARM_FLAGS:
1779 error = g_part_parm_str(req, ap->name, &gpp.gpp_flags);
1781 case G_PART_PARM_FORCE:
1782 error = g_part_parm_uint32(req, ap->name,
1785 case G_PART_PARM_GEOM:
1786 error = g_part_parm_geom(req, ap->name, &gpp.gpp_geom);
1788 case G_PART_PARM_INDEX:
1789 error = g_part_parm_intmax(req, ap->name,
1792 case G_PART_PARM_LABEL:
1793 error = g_part_parm_str(req, ap->name, &gpp.gpp_label);
1795 case G_PART_PARM_OUTPUT:
1796 error = 0; /* Write-only parameter */
1798 case G_PART_PARM_PROVIDER:
1799 error = g_part_parm_provider(req, ap->name,
1802 case G_PART_PARM_SCHEME:
1803 error = g_part_parm_scheme(req, ap->name,
1806 case G_PART_PARM_SIZE:
1807 error = g_part_parm_quad(req, ap->name, &gpp.gpp_size);
1809 case G_PART_PARM_SKIP_DSN:
1810 error = g_part_parm_uint32(req, ap->name,
1813 case G_PART_PARM_START:
1814 error = g_part_parm_quad(req, ap->name,
1817 case G_PART_PARM_TYPE:
1818 error = g_part_parm_str(req, ap->name, &gpp.gpp_type);
1820 case G_PART_PARM_VERSION:
1821 error = g_part_parm_uint32(req, ap->name,
1826 gctl_error(req, "%d %s", error, ap->name);
1830 if (error == ENOATTR) {
1831 gctl_error(req, "%d param '%s'", error,
1836 gpp.gpp_parms |= parm;
1838 if ((gpp.gpp_parms & mparms) != mparms) {
1839 parm = mparms - (gpp.gpp_parms & mparms);
1840 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1844 /* Obtain permissions if possible/necessary. */
1847 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1848 table = gpp.gpp_geom->softc;
1849 if (table != NULL && table->gpt_corrupt &&
1850 ctlreq != G_PART_CTL_DESTROY &&
1851 ctlreq != G_PART_CTL_RECOVER) {
1852 gctl_error(req, "%d table '%s' is corrupt",
1853 EPERM, gpp.gpp_geom->name);
1856 if (table != NULL && !table->gpt_opened) {
1857 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1860 gctl_error(req, "%d geom '%s'", error,
1861 gpp.gpp_geom->name);
1864 table->gpt_opened = 1;
1869 /* Allow the scheme to check or modify the parameters. */
1870 if (table != NULL) {
1871 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1873 gctl_error(req, "%d pre-check failed", error);
1877 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1880 case G_PART_CTL_NONE:
1881 panic("%s", __func__);
1882 case G_PART_CTL_ADD:
1883 error = g_part_ctl_add(req, &gpp);
1885 case G_PART_CTL_BOOTCODE:
1886 error = g_part_ctl_bootcode(req, &gpp);
1888 case G_PART_CTL_COMMIT:
1889 error = g_part_ctl_commit(req, &gpp);
1891 case G_PART_CTL_CREATE:
1892 error = g_part_ctl_create(req, &gpp);
1894 case G_PART_CTL_DELETE:
1895 error = g_part_ctl_delete(req, &gpp);
1897 case G_PART_CTL_DESTROY:
1898 error = g_part_ctl_destroy(req, &gpp);
1900 case G_PART_CTL_MODIFY:
1901 error = g_part_ctl_modify(req, &gpp);
1903 case G_PART_CTL_MOVE:
1904 error = g_part_ctl_move(req, &gpp);
1906 case G_PART_CTL_RECOVER:
1907 error = g_part_ctl_recover(req, &gpp);
1909 case G_PART_CTL_RESIZE:
1910 error = g_part_ctl_resize(req, &gpp);
1912 case G_PART_CTL_SET:
1913 error = g_part_ctl_setunset(req, &gpp, 1);
1915 case G_PART_CTL_UNDO:
1916 error = g_part_ctl_undo(req, &gpp);
1918 case G_PART_CTL_UNSET:
1919 error = g_part_ctl_setunset(req, &gpp, 0);
1923 /* Implement automatic commit. */
1925 auto_commit = (modifies &&
1926 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1927 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1929 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, ("%s",
1931 error = g_part_ctl_commit(req, &gpp);
1936 if (error && close_on_error) {
1937 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1938 table->gpt_opened = 0;
1943 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1947 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1948 g_topology_assert();
1950 g_part_wither(gp, EINVAL);
1954 static struct g_geom *
1955 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1957 struct g_consumer *cp;
1959 struct g_part_entry *entry;
1960 struct g_part_table *table;
1961 struct root_hold_token *rht;
1965 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1966 g_topology_assert();
1968 /* Skip providers that are already open for writing. */
1973 * Create a GEOM with consumer and hook it up to the provider.
1974 * With that we become part of the topology. Obtain read access
1977 gp = g_new_geomf(mp, "%s", pp->name);
1978 cp = g_new_consumer(gp);
1979 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1980 error = g_attach(cp, pp);
1982 error = g_access(cp, 1, 0, 0);
1986 g_destroy_consumer(cp);
1991 rht = root_mount_hold(mp->name);
1992 g_topology_unlock();
1995 * Short-circuit the whole probing galore when there's no
1998 if (pp->mediasize == 0 || pp->sectorsize == 0) {
2003 /* Make sure we can nest and if so, determine our depth. */
2004 error = g_getattr("PART::isleaf", cp, &attr);
2005 if (!error && attr) {
2009 error = g_getattr("PART::depth", cp, &attr);
2010 depth = (!error) ? attr + 1 : 0;
2012 error = g_part_probe(gp, cp, depth);
2019 * Synthesize a disk geometry. Some partitioning schemes
2020 * depend on it and since some file systems need it even
2021 * when the partitition scheme doesn't, we do it here in
2022 * scheme-independent code.
2024 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
2026 error = G_PART_READ(table, cp);
2029 error = g_part_check_integrity(table, cp);
2034 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
2035 if (!entry->gpe_internal)
2036 g_part_new_provider(gp, table, entry);
2039 root_mount_rel(rht);
2040 g_access(cp, -1, 0, 0);
2045 root_mount_rel(rht);
2046 g_access(cp, -1, 0, 0);
2048 g_destroy_consumer(cp);
2058 g_part_access(struct g_provider *pp, int dr, int dw, int de)
2060 struct g_consumer *cp;
2062 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
2065 cp = LIST_FIRST(&pp->geom->consumer);
2067 /* We always gain write-exclusive access. */
2068 return (g_access(cp, dr, dw, dw + de));
2072 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2073 struct g_consumer *cp, struct g_provider *pp)
2076 struct g_part_entry *entry;
2077 struct g_part_table *table;
2079 KASSERT(sb != NULL && gp != NULL, ("%s", __func__));
2082 if (indent == NULL) {
2083 KASSERT(cp == NULL && pp != NULL, ("%s", __func__));
2084 entry = pp->private;
2087 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
2088 (uintmax_t)entry->gpe_offset,
2089 G_PART_TYPE(table, entry, buf, sizeof(buf)));
2091 * libdisk compatibility quirk - the scheme dumps the
2092 * slicer name and partition type in a way that is
2093 * compatible with libdisk. When libdisk is not used
2094 * anymore, this should go away.
2096 G_PART_DUMPCONF(table, entry, sb, indent);
2097 } else if (cp != NULL) { /* Consumer configuration. */
2098 KASSERT(pp == NULL, ("%s", __func__));
2100 } else if (pp != NULL) { /* Provider configuration. */
2101 entry = pp->private;
2104 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
2105 (uintmax_t)entry->gpe_start);
2106 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
2107 (uintmax_t)entry->gpe_end);
2108 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
2110 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
2111 G_PART_TYPE(table, entry, buf, sizeof(buf)));
2112 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
2113 (uintmax_t)entry->gpe_offset);
2114 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
2115 (uintmax_t)pp->mediasize);
2116 G_PART_DUMPCONF(table, entry, sb, indent);
2117 } else { /* Geom configuration. */
2118 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
2119 table->gpt_scheme->name);
2120 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
2121 table->gpt_entries);
2122 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
2123 (uintmax_t)table->gpt_first);
2124 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
2125 (uintmax_t)table->gpt_last);
2126 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
2127 table->gpt_sectors);
2128 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
2130 sbuf_printf(sb, "%s<state>%s</state>\n", indent,
2131 table->gpt_corrupt ? "CORRUPT": "OK");
2132 sbuf_printf(sb, "%s<modified>%s</modified>\n", indent,
2133 table->gpt_opened ? "true": "false");
2134 G_PART_DUMPCONF(table, NULL, sb, indent);
2139 * This start routine is only called for non-trivial requests, all the
2140 * trivial ones are handled autonomously by the slice code.
2141 * For requests we handle here, we must call the g_io_deliver() on the
2142 * bio, and return non-zero to indicate to the slice code that we did so.
2143 * This code executes in the "DOWN" I/O path, this means:
2145 * * Don't grab the topology lock.
2146 * * Don't call biowait, g_getattr(), g_setattr() or g_read_data()
2149 g_part_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
2151 struct g_part_table *table;
2153 table = pp->geom->softc;
2154 return G_PART_IOCTL(table, pp, cmd, data, fflag, td);
2158 g_part_resize(struct g_consumer *cp)
2160 struct g_part_table *table;
2162 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2163 g_topology_assert();
2165 if (auto_resize == 0)
2168 table = cp->geom->softc;
2169 if (table->gpt_opened == 0) {
2170 if (g_access(cp, 1, 1, 1) != 0)
2172 table->gpt_opened = 1;
2174 if (G_PART_RESIZE(table, NULL, NULL) == 0)
2175 printf("GEOM_PART: %s was automatically resized.\n"
2176 " Use `gpart commit %s` to save changes or "
2177 "`gpart undo %s` to revert them.\n", cp->geom->name,
2178 cp->geom->name, cp->geom->name);
2179 if (g_part_check_integrity(table, cp) != 0) {
2180 g_access(cp, -1, -1, -1);
2181 table->gpt_opened = 0;
2182 g_part_wither(table->gpt_gp, ENXIO);
2187 g_part_orphan(struct g_consumer *cp)
2189 struct g_provider *pp;
2190 struct g_part_table *table;
2193 KASSERT(pp != NULL, ("%s", __func__));
2194 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
2195 g_topology_assert();
2197 KASSERT(pp->error != 0, ("%s", __func__));
2198 table = cp->geom->softc;
2199 if (table != NULL && table->gpt_opened)
2200 g_access(cp, -1, -1, -1);
2201 g_part_wither(cp->geom, pp->error);
2205 g_part_spoiled(struct g_consumer *cp)
2208 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2209 g_topology_assert();
2211 cp->flags |= G_CF_ORPHAN;
2212 g_part_wither(cp->geom, ENXIO);
2216 g_part_start(struct bio *bp)
2219 struct g_consumer *cp;
2221 struct g_part_entry *entry;
2222 struct g_part_table *table;
2223 struct g_kerneldump *gkd;
2224 struct g_provider *pp;
2225 void (*done_func)(struct bio *) = g_std_done;
2228 biotrack(bp, __func__);
2233 cp = LIST_FIRST(&gp->consumer);
2235 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
2238 entry = pp->private;
2239 if (entry == NULL) {
2240 g_io_deliver(bp, ENXIO);
2244 switch(bp->bio_cmd) {
2248 if (bp->bio_offset >= pp->mediasize) {
2249 g_io_deliver(bp, EIO);
2252 bp2 = g_clone_bio(bp);
2254 g_io_deliver(bp, ENOMEM);
2257 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
2258 bp2->bio_length = pp->mediasize - bp2->bio_offset;
2259 bp2->bio_done = g_std_done;
2260 bp2->bio_offset += entry->gpe_offset;
2261 g_io_request(bp2, cp);
2267 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
2269 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
2272 * allow_nesting overrides "isleaf" to false _unless_ the
2273 * provider offset is zero, since otherwise we would recurse.
2275 if (g_handleattr_int(bp, "PART::isleaf",
2276 table->gpt_isleaf &&
2277 (allow_nesting == 0 || entry->gpe_offset == 0)))
2279 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
2281 if (g_handleattr_str(bp, "PART::scheme",
2282 table->gpt_scheme->name))
2284 if (g_handleattr_str(bp, "PART::type",
2285 G_PART_TYPE(table, entry, buf, sizeof(buf))))
2287 if (!strcmp("GEOM::physpath", bp->bio_attribute)) {
2288 done_func = g_part_get_physpath_done;
2291 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
2293 * Check that the partition is suitable for kernel
2294 * dumps. Typically only swap partitions should be
2295 * used. If the request comes from the nested scheme
2296 * we allow dumping there as well.
2298 if ((bp->bio_from == NULL ||
2299 bp->bio_from->geom->class != &g_part_class) &&
2300 G_PART_DUMPTO(table, entry) == 0) {
2301 g_io_deliver(bp, ENODEV);
2302 printf("GEOM_PART: Partition '%s' not suitable"
2303 " for kernel dumps (wrong type?)\n",
2307 gkd = (struct g_kerneldump *)bp->bio_data;
2308 if (gkd->offset >= pp->mediasize) {
2309 g_io_deliver(bp, EIO);
2312 if (gkd->offset + gkd->length > pp->mediasize)
2313 gkd->length = pp->mediasize - gkd->offset;
2314 gkd->offset += entry->gpe_offset;
2318 g_io_deliver(bp, EOPNOTSUPP);
2322 bp2 = g_clone_bio(bp);
2324 g_io_deliver(bp, ENOMEM);
2327 bp2->bio_done = done_func;
2328 g_io_request(bp2, cp);
2332 g_part_init(struct g_class *mp)
2335 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
2339 g_part_fini(struct g_class *mp)
2342 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
2346 g_part_unload_event(void *arg, int flag)
2348 struct g_consumer *cp;
2350 struct g_provider *pp;
2351 struct g_part_scheme *scheme;
2352 struct g_part_table *table;
2356 if (flag == EV_CANCEL)
2361 scheme = (void *)(*xchg);
2363 g_topology_assert();
2365 LIST_FOREACH(gp, &g_part_class.geom, geom) {
2367 if (table->gpt_scheme != scheme)
2371 LIST_FOREACH(pp, &gp->provider, provider)
2372 acc += pp->acr + pp->acw + pp->ace;
2373 LIST_FOREACH(cp, &gp->consumer, consumer)
2374 acc += cp->acr + cp->acw + cp->ace;
2377 g_part_wither(gp, ENOSYS);
2383 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
2389 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
2391 struct g_part_scheme *iter;
2398 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
2399 if (scheme == iter) {
2400 printf("GEOM_PART: scheme %s is already "
2401 "registered!\n", scheme->name);
2406 TAILQ_INSERT_TAIL(&g_part_schemes, scheme,
2408 g_retaste(&g_part_class);
2412 arg = (uintptr_t)scheme;
2413 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,