2 * Copyright (c) 2002, 2005-2009 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
35 #include <sys/limits.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/queue.h>
41 #include <sys/sysctl.h>
42 #include <sys/systm.h>
44 #include <geom/geom.h>
45 #include <geom/geom_ctl.h>
46 #include <geom/geom_int.h>
47 #include <geom/part/g_part.h>
49 #include "g_part_if.h"
52 #define _PATH_DEV "/dev/"
55 static kobj_method_t g_part_null_methods[] = {
59 static struct g_part_scheme g_part_null_scheme = {
62 sizeof(struct g_part_table),
65 TAILQ_HEAD(, g_part_scheme) g_part_schemes =
66 TAILQ_HEAD_INITIALIZER(g_part_schemes);
68 struct g_part_alias_list {
70 enum g_part_alias alias;
71 } g_part_alias_list[G_PART_ALIAS_COUNT] = {
72 { "apple-boot", G_PART_ALIAS_APPLE_BOOT },
73 { "apple-core-storage", G_PART_ALIAS_APPLE_CORE_STORAGE },
74 { "apple-hfs", G_PART_ALIAS_APPLE_HFS },
75 { "apple-label", G_PART_ALIAS_APPLE_LABEL },
76 { "apple-raid", G_PART_ALIAS_APPLE_RAID },
77 { "apple-raid-offline", G_PART_ALIAS_APPLE_RAID_OFFLINE },
78 { "apple-tv-recovery", G_PART_ALIAS_APPLE_TV_RECOVERY },
79 { "apple-ufs", G_PART_ALIAS_APPLE_UFS },
80 { "bios-boot", G_PART_ALIAS_BIOS_BOOT },
81 { "ebr", G_PART_ALIAS_EBR },
82 { "efi", G_PART_ALIAS_EFI },
83 { "fat16", G_PART_ALIAS_MS_FAT16 },
84 { "fat32", G_PART_ALIAS_MS_FAT32 },
85 { "freebsd", G_PART_ALIAS_FREEBSD },
86 { "freebsd-boot", G_PART_ALIAS_FREEBSD_BOOT },
87 { "freebsd-nandfs", G_PART_ALIAS_FREEBSD_NANDFS },
88 { "freebsd-swap", G_PART_ALIAS_FREEBSD_SWAP },
89 { "freebsd-ufs", G_PART_ALIAS_FREEBSD_UFS },
90 { "freebsd-vinum", G_PART_ALIAS_FREEBSD_VINUM },
91 { "freebsd-zfs", G_PART_ALIAS_FREEBSD_ZFS },
92 { "linux-data", G_PART_ALIAS_LINUX_DATA },
93 { "linux-lvm", G_PART_ALIAS_LINUX_LVM },
94 { "linux-raid", G_PART_ALIAS_LINUX_RAID },
95 { "linux-swap", G_PART_ALIAS_LINUX_SWAP },
96 { "mbr", G_PART_ALIAS_MBR },
97 { "ms-basic-data", G_PART_ALIAS_MS_BASIC_DATA },
98 { "ms-ldm-data", G_PART_ALIAS_MS_LDM_DATA },
99 { "ms-ldm-metadata", G_PART_ALIAS_MS_LDM_METADATA },
100 { "ms-reserved", G_PART_ALIAS_MS_RESERVED },
101 { "ntfs", G_PART_ALIAS_MS_NTFS },
102 { "netbsd-ccd", G_PART_ALIAS_NETBSD_CCD },
103 { "netbsd-cgd", G_PART_ALIAS_NETBSD_CGD },
104 { "netbsd-ffs", G_PART_ALIAS_NETBSD_FFS },
105 { "netbsd-lfs", G_PART_ALIAS_NETBSD_LFS },
106 { "netbsd-raid", G_PART_ALIAS_NETBSD_RAID },
107 { "netbsd-swap", G_PART_ALIAS_NETBSD_SWAP },
108 { "vmware-vmfs", G_PART_ALIAS_VMFS },
109 { "vmware-vmkdiag", G_PART_ALIAS_VMKDIAG },
110 { "vmware-reserved", G_PART_ALIAS_VMRESERVED },
111 { "vmware-vsanhdr", G_PART_ALIAS_VMVSANHDR },
112 { "dragonfly-label32", G_PART_ALIAS_DFBSD },
113 { "dragonfly-label64", G_PART_ALIAS_DFBSD64 },
114 { "dragonfly-swap", G_PART_ALIAS_DFBSD_SWAP },
115 { "dragonfly-ufs", G_PART_ALIAS_DFBSD_UFS },
116 { "dragonfly-vinum", G_PART_ALIAS_DFBSD_VINUM },
117 { "dragonfly-ccd", G_PART_ALIAS_DFBSD_CCD },
118 { "dragonfly-legacy", G_PART_ALIAS_DFBSD_LEGACY },
119 { "dragonfly-hammer", G_PART_ALIAS_DFBSD_HAMMER },
120 { "dragonfly-hammer2", G_PART_ALIAS_DFBSD_HAMMER2 },
121 { "prep-boot", G_PART_ALIAS_PREP_BOOT },
124 SYSCTL_DECL(_kern_geom);
125 SYSCTL_NODE(_kern_geom, OID_AUTO, part, CTLFLAG_RW, 0,
127 static u_int check_integrity = 1;
128 TUNABLE_INT("kern.geom.part.check_integrity", &check_integrity);
129 SYSCTL_UINT(_kern_geom_part, OID_AUTO, check_integrity,
130 CTLFLAG_RW | CTLFLAG_TUN, &check_integrity, 1,
131 "Enable integrity checking");
134 * The GEOM partitioning class.
136 static g_ctl_req_t g_part_ctlreq;
137 static g_ctl_destroy_geom_t g_part_destroy_geom;
138 static g_fini_t g_part_fini;
139 static g_init_t g_part_init;
140 static g_taste_t g_part_taste;
142 static g_access_t g_part_access;
143 static g_dumpconf_t g_part_dumpconf;
144 static g_orphan_t g_part_orphan;
145 static g_spoiled_t g_part_spoiled;
146 static g_start_t g_part_start;
147 static g_resize_t g_part_resize;
149 static struct g_class g_part_class = {
151 .version = G_VERSION,
153 .ctlreq = g_part_ctlreq,
154 .destroy_geom = g_part_destroy_geom,
157 .taste = g_part_taste,
159 .access = g_part_access,
160 .dumpconf = g_part_dumpconf,
161 .orphan = g_part_orphan,
162 .spoiled = g_part_spoiled,
163 .start = g_part_start,
164 .resize = g_part_resize
167 DECLARE_GEOM_CLASS(g_part_class, g_part);
168 MODULE_VERSION(g_part, 0);
174 static void g_part_wither(struct g_geom *, int);
177 g_part_alias_name(enum g_part_alias alias)
181 for (i = 0; i < G_PART_ALIAS_COUNT; i++) {
182 if (g_part_alias_list[i].alias != alias)
184 return (g_part_alias_list[i].lexeme);
191 g_part_geometry_heads(off_t blocks, u_int sectors, off_t *bestchs,
194 static u_int candidate_heads[] = { 1, 2, 16, 32, 64, 128, 255, 0 };
195 off_t chs, cylinders;
201 for (idx = 0; candidate_heads[idx] != 0; idx++) {
202 heads = candidate_heads[idx];
203 cylinders = blocks / heads / sectors;
204 if (cylinders < heads || cylinders < sectors)
206 if (cylinders > 1023)
208 chs = cylinders * heads * sectors;
209 if (chs > *bestchs || (chs == *bestchs && *bestheads == 1)) {
217 g_part_geometry(struct g_part_table *table, struct g_consumer *cp,
220 static u_int candidate_sectors[] = { 1, 9, 17, 33, 63, 0 };
222 u_int heads, sectors;
225 if (g_getattr("GEOM::fwsectors", cp, §ors) != 0 || sectors == 0 ||
226 g_getattr("GEOM::fwheads", cp, &heads) != 0 || heads == 0) {
227 table->gpt_fixgeom = 0;
228 table->gpt_heads = 0;
229 table->gpt_sectors = 0;
231 for (idx = 0; candidate_sectors[idx] != 0; idx++) {
232 sectors = candidate_sectors[idx];
233 g_part_geometry_heads(blocks, sectors, &chs, &heads);
237 * Prefer a geometry with sectors > 1, but only if
238 * it doesn't bump down the number of heads to 1.
240 if (chs > bestchs || (chs == bestchs && heads > 1 &&
241 table->gpt_sectors == 1)) {
243 table->gpt_heads = heads;
244 table->gpt_sectors = sectors;
248 * If we didn't find a geometry at all, then the disk is
249 * too big. This means we can use the maximum number of
253 table->gpt_heads = 255;
254 table->gpt_sectors = 63;
257 table->gpt_fixgeom = 1;
258 table->gpt_heads = heads;
259 table->gpt_sectors = sectors;
263 #define DPRINTF(...) if (bootverbose) { \
264 printf("GEOM_PART: " __VA_ARGS__); \
268 g_part_check_integrity(struct g_part_table *table, struct g_consumer *cp)
270 struct g_part_entry *e1, *e2;
271 struct g_provider *pp;
277 if (table->gpt_last < table->gpt_first) {
278 DPRINTF("last LBA is below first LBA: %jd < %jd\n",
279 (intmax_t)table->gpt_last, (intmax_t)table->gpt_first);
282 if (table->gpt_last > pp->mediasize / pp->sectorsize - 1) {
283 DPRINTF("last LBA extends beyond mediasize: "
284 "%jd > %jd\n", (intmax_t)table->gpt_last,
285 (intmax_t)pp->mediasize / pp->sectorsize - 1);
288 LIST_FOREACH(e1, &table->gpt_entry, gpe_entry) {
289 if (e1->gpe_deleted || e1->gpe_internal)
291 if (e1->gpe_start < table->gpt_first) {
292 DPRINTF("partition %d has start offset below first "
293 "LBA: %jd < %jd\n", e1->gpe_index,
294 (intmax_t)e1->gpe_start,
295 (intmax_t)table->gpt_first);
298 if (e1->gpe_start > table->gpt_last) {
299 DPRINTF("partition %d has start offset beyond last "
300 "LBA: %jd > %jd\n", e1->gpe_index,
301 (intmax_t)e1->gpe_start,
302 (intmax_t)table->gpt_last);
305 if (e1->gpe_end < e1->gpe_start) {
306 DPRINTF("partition %d has end offset below start "
307 "offset: %jd < %jd\n", e1->gpe_index,
308 (intmax_t)e1->gpe_end,
309 (intmax_t)e1->gpe_start);
312 if (e1->gpe_end > table->gpt_last) {
313 DPRINTF("partition %d has end offset beyond last "
314 "LBA: %jd > %jd\n", e1->gpe_index,
315 (intmax_t)e1->gpe_end,
316 (intmax_t)table->gpt_last);
319 if (pp->stripesize > 0) {
320 offset = e1->gpe_start * pp->sectorsize;
321 if (e1->gpe_offset > offset)
322 offset = e1->gpe_offset;
323 if ((offset + pp->stripeoffset) % pp->stripesize) {
324 DPRINTF("partition %d is not aligned on %u "
325 "bytes\n", e1->gpe_index, pp->stripesize);
326 /* Don't treat this as a critical failure */
330 while ((e2 = LIST_NEXT(e2, gpe_entry)) != NULL) {
331 if (e2->gpe_deleted || e2->gpe_internal)
333 if (e1->gpe_start >= e2->gpe_start &&
334 e1->gpe_start <= e2->gpe_end) {
335 DPRINTF("partition %d has start offset inside "
336 "partition %d: start[%d] %jd >= start[%d] "
337 "%jd <= end[%d] %jd\n",
338 e1->gpe_index, e2->gpe_index,
339 e2->gpe_index, (intmax_t)e2->gpe_start,
340 e1->gpe_index, (intmax_t)e1->gpe_start,
341 e2->gpe_index, (intmax_t)e2->gpe_end);
344 if (e1->gpe_end >= e2->gpe_start &&
345 e1->gpe_end <= e2->gpe_end) {
346 DPRINTF("partition %d has end offset inside "
347 "partition %d: start[%d] %jd >= end[%d] "
348 "%jd <= end[%d] %jd\n",
349 e1->gpe_index, e2->gpe_index,
350 e2->gpe_index, (intmax_t)e2->gpe_start,
351 e1->gpe_index, (intmax_t)e1->gpe_end,
352 e2->gpe_index, (intmax_t)e2->gpe_end);
355 if (e1->gpe_start < e2->gpe_start &&
356 e1->gpe_end > e2->gpe_end) {
357 DPRINTF("partition %d contains partition %d: "
358 "start[%d] %jd > start[%d] %jd, end[%d] "
359 "%jd < end[%d] %jd\n",
360 e1->gpe_index, e2->gpe_index,
361 e1->gpe_index, (intmax_t)e1->gpe_start,
362 e2->gpe_index, (intmax_t)e2->gpe_start,
363 e2->gpe_index, (intmax_t)e2->gpe_end,
364 e1->gpe_index, (intmax_t)e1->gpe_end);
370 printf("GEOM_PART: integrity check failed (%s, %s)\n",
371 pp->name, table->gpt_scheme->name);
372 if (check_integrity != 0)
374 table->gpt_corrupt = 1;
380 struct g_part_entry *
381 g_part_new_entry(struct g_part_table *table, int index, quad_t start,
384 struct g_part_entry *entry, *last;
387 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
388 if (entry->gpe_index == index)
390 if (entry->gpe_index > index) {
397 entry = g_malloc(table->gpt_scheme->gps_entrysz,
399 entry->gpe_index = index;
401 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
403 LIST_INSERT_AFTER(last, entry, gpe_entry);
405 entry->gpe_offset = 0;
406 entry->gpe_start = start;
407 entry->gpe_end = end;
412 g_part_new_provider(struct g_geom *gp, struct g_part_table *table,
413 struct g_part_entry *entry)
415 struct g_consumer *cp;
416 struct g_provider *pp;
420 cp = LIST_FIRST(&gp->consumer);
423 offset = entry->gpe_start * pp->sectorsize;
424 if (entry->gpe_offset < offset)
425 entry->gpe_offset = offset;
427 if (entry->gpe_pp == NULL) {
428 sb = sbuf_new_auto();
429 G_PART_FULLNAME(table, entry, sb, gp->name);
431 entry->gpe_pp = g_new_providerf(gp, "%s", sbuf_data(sb));
433 entry->gpe_pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
434 entry->gpe_pp->private = entry; /* Close the circle. */
436 entry->gpe_pp->index = entry->gpe_index - 1; /* index is 1-based. */
437 entry->gpe_pp->mediasize = (entry->gpe_end - entry->gpe_start + 1) *
439 entry->gpe_pp->mediasize -= entry->gpe_offset - offset;
440 entry->gpe_pp->sectorsize = pp->sectorsize;
441 entry->gpe_pp->stripesize = pp->stripesize;
442 entry->gpe_pp->stripeoffset = pp->stripeoffset + entry->gpe_offset;
443 if (pp->stripesize > 0)
444 entry->gpe_pp->stripeoffset %= pp->stripesize;
445 entry->gpe_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
446 g_error_provider(entry->gpe_pp, 0);
449 static struct g_geom*
450 g_part_find_geom(const char *name)
453 LIST_FOREACH(gp, &g_part_class.geom, geom) {
454 if ((gp->flags & G_GEOM_WITHER) == 0 &&
455 strcmp(name, gp->name) == 0)
462 g_part_parm_geom(struct gctl_req *req, const char *name, struct g_geom **v)
467 gname = gctl_get_asciiparam(req, name);
470 if (strncmp(gname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
471 gname += sizeof(_PATH_DEV) - 1;
472 gp = g_part_find_geom(gname);
474 gctl_error(req, "%d %s '%s'", EINVAL, name, gname);
482 g_part_parm_provider(struct gctl_req *req, const char *name,
483 struct g_provider **v)
485 struct g_provider *pp;
488 pname = gctl_get_asciiparam(req, name);
491 if (strncmp(pname, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
492 pname += sizeof(_PATH_DEV) - 1;
493 pp = g_provider_by_name(pname);
495 gctl_error(req, "%d %s '%s'", EINVAL, name, pname);
503 g_part_parm_quad(struct gctl_req *req, const char *name, quad_t *v)
509 p = gctl_get_asciiparam(req, name);
512 q = strtoq(p, &x, 0);
513 if (*x != '\0' || q < 0) {
514 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
522 g_part_parm_scheme(struct gctl_req *req, const char *name,
523 struct g_part_scheme **v)
525 struct g_part_scheme *s;
528 p = gctl_get_asciiparam(req, name);
531 TAILQ_FOREACH(s, &g_part_schemes, scheme_list) {
532 if (s == &g_part_null_scheme)
534 if (!strcasecmp(s->name, p))
538 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
546 g_part_parm_str(struct gctl_req *req, const char *name, const char **v)
550 p = gctl_get_asciiparam(req, name);
553 /* An empty label is always valid. */
554 if (strcmp(name, "label") != 0 && p[0] == '\0') {
555 gctl_error(req, "%d %s '%s'", EINVAL, name, p);
563 g_part_parm_intmax(struct gctl_req *req, const char *name, u_int *v)
568 p = gctl_get_param(req, name, &size);
571 if (size != sizeof(*p) || *p < 0 || *p > INT_MAX) {
572 gctl_error(req, "%d %s '%jd'", EINVAL, name, *p);
580 g_part_parm_uint32(struct gctl_req *req, const char *name, u_int *v)
585 p = gctl_get_param(req, name, &size);
588 if (size != sizeof(*p) || *p > INT_MAX) {
589 gctl_error(req, "%d %s '%u'", EINVAL, name, (unsigned int)*p);
597 g_part_parm_bootcode(struct gctl_req *req, const char *name, const void **v,
603 p = gctl_get_param(req, name, &size);
612 g_part_probe(struct g_geom *gp, struct g_consumer *cp, int depth)
614 struct g_part_scheme *iter, *scheme;
615 struct g_part_table *table;
619 scheme = (table != NULL) ? table->gpt_scheme : NULL;
620 pri = (scheme != NULL) ? G_PART_PROBE(table, cp) : INT_MIN;
623 if (pri > 0) { /* error */
628 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
629 if (iter == &g_part_null_scheme)
631 table = (void *)kobj_create((kobj_class_t)iter, M_GEOM,
634 table->gpt_scheme = iter;
635 table->gpt_depth = depth;
636 probe = G_PART_PROBE(table, cp);
637 if (probe <= 0 && probe > pri) {
640 if (gp->softc != NULL)
641 kobj_delete((kobj_t)gp->softc, M_GEOM);
646 kobj_delete((kobj_t)table, M_GEOM);
650 return ((scheme == NULL) ? ENXIO : 0);
654 * Control request functions.
658 g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp)
661 struct g_provider *pp;
662 struct g_part_entry *delent, *last, *entry;
663 struct g_part_table *table;
670 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
673 pp = LIST_FIRST(&gp->consumer)->provider;
675 end = gpp->gpp_start + gpp->gpp_size - 1;
677 if (gpp->gpp_start < table->gpt_first ||
678 gpp->gpp_start > table->gpt_last) {
679 gctl_error(req, "%d start '%jd'", EINVAL,
680 (intmax_t)gpp->gpp_start);
683 if (end < gpp->gpp_start || end > table->gpt_last) {
684 gctl_error(req, "%d size '%jd'", EINVAL,
685 (intmax_t)gpp->gpp_size);
688 if (gpp->gpp_index > table->gpt_entries) {
689 gctl_error(req, "%d index '%d'", EINVAL, gpp->gpp_index);
693 delent = last = NULL;
694 index = (gpp->gpp_index > 0) ? gpp->gpp_index : 1;
695 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
696 if (entry->gpe_deleted) {
697 if (entry->gpe_index == index)
701 if (entry->gpe_index == index)
702 index = entry->gpe_index + 1;
703 if (entry->gpe_index < index)
705 if (entry->gpe_internal)
707 if (gpp->gpp_start >= entry->gpe_start &&
708 gpp->gpp_start <= entry->gpe_end) {
709 gctl_error(req, "%d start '%jd'", ENOSPC,
710 (intmax_t)gpp->gpp_start);
713 if (end >= entry->gpe_start && end <= entry->gpe_end) {
714 gctl_error(req, "%d end '%jd'", ENOSPC, (intmax_t)end);
717 if (gpp->gpp_start < entry->gpe_start && end > entry->gpe_end) {
718 gctl_error(req, "%d size '%jd'", ENOSPC,
719 (intmax_t)gpp->gpp_size);
723 if (gpp->gpp_index > 0 && index != gpp->gpp_index) {
724 gctl_error(req, "%d index '%d'", EEXIST, gpp->gpp_index);
727 if (index > table->gpt_entries) {
728 gctl_error(req, "%d index '%d'", ENOSPC, index);
732 entry = (delent == NULL) ? g_malloc(table->gpt_scheme->gps_entrysz,
733 M_WAITOK | M_ZERO) : delent;
734 entry->gpe_index = index;
735 entry->gpe_start = gpp->gpp_start;
736 entry->gpe_end = end;
737 error = G_PART_ADD(table, entry, gpp);
739 gctl_error(req, "%d", error);
744 if (delent == NULL) {
746 LIST_INSERT_HEAD(&table->gpt_entry, entry, gpe_entry);
748 LIST_INSERT_AFTER(last, entry, gpe_entry);
749 entry->gpe_created = 1;
751 entry->gpe_deleted = 0;
752 entry->gpe_modified = 1;
754 g_part_new_provider(gp, table, entry);
756 /* Provide feedback if so requested. */
757 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
758 sb = sbuf_new_auto();
759 G_PART_FULLNAME(table, entry, sb, gp->name);
760 if (pp->stripesize > 0 && entry->gpe_pp->stripeoffset != 0)
761 sbuf_printf(sb, " added, but partition is not "
762 "aligned on %u bytes\n", pp->stripesize);
764 sbuf_cat(sb, " added\n");
766 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
773 g_part_ctl_bootcode(struct gctl_req *req, struct g_part_parms *gpp)
776 struct g_part_table *table;
781 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
785 sz = table->gpt_scheme->gps_bootcodesz;
790 if (gpp->gpp_codesize > sz) {
795 error = G_PART_BOOTCODE(table, gpp);
799 /* Provide feedback if so requested. */
800 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
801 sb = sbuf_new_auto();
802 sbuf_printf(sb, "bootcode written to %s\n", gp->name);
804 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
810 gctl_error(req, "%d", error);
815 g_part_ctl_commit(struct gctl_req *req, struct g_part_parms *gpp)
817 struct g_consumer *cp;
819 struct g_provider *pp;
820 struct g_part_entry *entry, *tmp;
821 struct g_part_table *table;
826 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
830 if (!table->gpt_opened) {
831 gctl_error(req, "%d", EPERM);
837 cp = LIST_FIRST(&gp->consumer);
838 if ((table->gpt_smhead | table->gpt_smtail) != 0) {
840 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
841 while (table->gpt_smhead != 0) {
842 i = ffs(table->gpt_smhead) - 1;
843 error = g_write_data(cp, i * pp->sectorsize, buf,
849 table->gpt_smhead &= ~(1 << i);
851 while (table->gpt_smtail != 0) {
852 i = ffs(table->gpt_smtail) - 1;
853 error = g_write_data(cp, pp->mediasize - (i + 1) *
854 pp->sectorsize, buf, pp->sectorsize);
859 table->gpt_smtail &= ~(1 << i);
864 if (table->gpt_scheme == &g_part_null_scheme) {
866 g_access(cp, -1, -1, -1);
867 g_part_wither(gp, ENXIO);
871 error = G_PART_WRITE(table, cp);
875 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
876 if (!entry->gpe_deleted) {
877 entry->gpe_created = 0;
878 entry->gpe_modified = 0;
881 LIST_REMOVE(entry, gpe_entry);
884 table->gpt_created = 0;
885 table->gpt_opened = 0;
888 g_access(cp, -1, -1, -1);
893 gctl_error(req, "%d", error);
898 g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp)
900 struct g_consumer *cp;
902 struct g_provider *pp;
903 struct g_part_scheme *scheme;
904 struct g_part_table *null, *table;
908 pp = gpp->gpp_provider;
909 scheme = gpp->gpp_scheme;
910 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
913 /* Check that there isn't already a g_part geom on the provider. */
914 gp = g_part_find_geom(pp->name);
917 if (null->gpt_scheme != &g_part_null_scheme) {
918 gctl_error(req, "%d geom '%s'", EEXIST, pp->name);
924 if ((gpp->gpp_parms & G_PART_PARM_ENTRIES) &&
925 (gpp->gpp_entries < scheme->gps_minent ||
926 gpp->gpp_entries > scheme->gps_maxent)) {
927 gctl_error(req, "%d entries '%d'", EINVAL, gpp->gpp_entries);
932 gp = g_new_geomf(&g_part_class, "%s", pp->name);
933 gp->softc = kobj_create((kobj_class_t)gpp->gpp_scheme, M_GEOM,
937 table->gpt_scheme = gpp->gpp_scheme;
938 table->gpt_entries = (gpp->gpp_parms & G_PART_PARM_ENTRIES) ?
939 gpp->gpp_entries : scheme->gps_minent;
940 LIST_INIT(&table->gpt_entry);
942 cp = g_new_consumer(gp);
943 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
944 error = g_attach(cp, pp);
946 error = g_access(cp, 1, 1, 1);
948 g_part_wither(gp, error);
949 gctl_error(req, "%d geom '%s'", error, pp->name);
952 table->gpt_opened = 1;
954 cp = LIST_FIRST(&gp->consumer);
955 table->gpt_opened = null->gpt_opened;
956 table->gpt_smhead = null->gpt_smhead;
957 table->gpt_smtail = null->gpt_smtail;
962 /* Make sure the provider has media. */
963 if (pp->mediasize == 0 || pp->sectorsize == 0) {
968 /* Make sure we can nest and if so, determine our depth. */
969 error = g_getattr("PART::isleaf", cp, &attr);
970 if (!error && attr) {
974 error = g_getattr("PART::depth", cp, &attr);
975 table->gpt_depth = (!error) ? attr + 1 : 0;
978 * Synthesize a disk geometry. Some partitioning schemes
979 * depend on it and since some file systems need it even
980 * when the partitition scheme doesn't, we do it here in
981 * scheme-independent code.
983 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
985 error = G_PART_CREATE(table, gpp);
991 table->gpt_created = 1;
993 kobj_delete((kobj_t)null, M_GEOM);
996 * Support automatic commit by filling in the gpp_geom
999 gpp->gpp_parms |= G_PART_PARM_GEOM;
1002 /* Provide feedback if so requested. */
1003 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1004 sb = sbuf_new_auto();
1005 sbuf_printf(sb, "%s created\n", gp->name);
1007 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1015 g_access(cp, -1, -1, -1);
1016 g_part_wither(gp, error);
1018 kobj_delete((kobj_t)gp->softc, M_GEOM);
1021 gctl_error(req, "%d provider", error);
1026 g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp)
1029 struct g_provider *pp;
1030 struct g_part_entry *entry;
1031 struct g_part_table *table;
1035 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1036 g_topology_assert();
1040 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1041 if (entry->gpe_deleted || entry->gpe_internal)
1043 if (entry->gpe_index == gpp->gpp_index)
1046 if (entry == NULL) {
1047 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1053 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) {
1054 gctl_error(req, "%d", EBUSY);
1059 entry->gpe_pp = NULL;
1063 g_wither_provider(pp, ENXIO);
1065 /* Provide feedback if so requested. */
1066 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1067 sb = sbuf_new_auto();
1068 G_PART_FULLNAME(table, entry, sb, gp->name);
1069 sbuf_cat(sb, " deleted\n");
1071 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1075 if (entry->gpe_created) {
1076 LIST_REMOVE(entry, gpe_entry);
1079 entry->gpe_modified = 0;
1080 entry->gpe_deleted = 1;
1086 g_part_ctl_destroy(struct gctl_req *req, struct g_part_parms *gpp)
1088 struct g_consumer *cp;
1090 struct g_provider *pp;
1091 struct g_part_entry *entry, *tmp;
1092 struct g_part_table *null, *table;
1097 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1098 g_topology_assert();
1101 /* Check for busy providers. */
1102 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1103 if (entry->gpe_deleted || entry->gpe_internal)
1105 if (gpp->gpp_force) {
1109 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1112 gctl_error(req, "%d", EBUSY);
1116 if (gpp->gpp_force) {
1117 /* Destroy all providers. */
1118 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1122 g_wither_provider(pp, ENXIO);
1124 LIST_REMOVE(entry, gpe_entry);
1129 error = G_PART_DESTROY(table, gpp);
1131 gctl_error(req, "%d", error);
1135 gp->softc = kobj_create((kobj_class_t)&g_part_null_scheme, M_GEOM,
1139 null->gpt_scheme = &g_part_null_scheme;
1140 LIST_INIT(&null->gpt_entry);
1142 cp = LIST_FIRST(&gp->consumer);
1144 null->gpt_last = pp->mediasize / pp->sectorsize - 1;
1146 null->gpt_depth = table->gpt_depth;
1147 null->gpt_opened = table->gpt_opened;
1148 null->gpt_smhead = table->gpt_smhead;
1149 null->gpt_smtail = table->gpt_smtail;
1151 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1152 LIST_REMOVE(entry, gpe_entry);
1155 kobj_delete((kobj_t)table, M_GEOM);
1157 /* Provide feedback if so requested. */
1158 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1159 sb = sbuf_new_auto();
1160 sbuf_printf(sb, "%s destroyed\n", gp->name);
1162 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1169 g_part_ctl_modify(struct gctl_req *req, struct g_part_parms *gpp)
1172 struct g_part_entry *entry;
1173 struct g_part_table *table;
1178 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1179 g_topology_assert();
1183 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1184 if (entry->gpe_deleted || entry->gpe_internal)
1186 if (entry->gpe_index == gpp->gpp_index)
1189 if (entry == NULL) {
1190 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1194 error = G_PART_MODIFY(table, entry, gpp);
1196 gctl_error(req, "%d", error);
1200 if (!entry->gpe_created)
1201 entry->gpe_modified = 1;
1203 /* Provide feedback if so requested. */
1204 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1205 sb = sbuf_new_auto();
1206 G_PART_FULLNAME(table, entry, sb, gp->name);
1207 sbuf_cat(sb, " modified\n");
1209 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1216 g_part_ctl_move(struct gctl_req *req, struct g_part_parms *gpp)
1218 gctl_error(req, "%d verb 'move'", ENOSYS);
1223 g_part_ctl_recover(struct gctl_req *req, struct g_part_parms *gpp)
1225 struct g_part_table *table;
1228 int error, recovered;
1231 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1232 g_topology_assert();
1234 error = recovered = 0;
1236 if (table->gpt_corrupt) {
1237 error = G_PART_RECOVER(table);
1239 error = g_part_check_integrity(table,
1240 LIST_FIRST(&gp->consumer));
1242 gctl_error(req, "%d recovering '%s' failed",
1248 /* Provide feedback if so requested. */
1249 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1250 sb = sbuf_new_auto();
1252 sbuf_printf(sb, "%s recovered\n", gp->name);
1254 sbuf_printf(sb, "%s recovering is not needed\n",
1257 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1264 g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
1267 struct g_provider *pp;
1268 struct g_part_entry *pe, *entry;
1269 struct g_part_table *table;
1276 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1277 g_topology_assert();
1280 /* check gpp_index */
1281 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1282 if (entry->gpe_deleted || entry->gpe_internal)
1284 if (entry->gpe_index == gpp->gpp_index)
1287 if (entry == NULL) {
1288 gctl_error(req, "%d index '%d'", ENOENT, gpp->gpp_index);
1292 /* check gpp_size */
1293 end = entry->gpe_start + gpp->gpp_size - 1;
1294 if (gpp->gpp_size < 1 || end > table->gpt_last) {
1295 gctl_error(req, "%d size '%jd'", EINVAL,
1296 (intmax_t)gpp->gpp_size);
1300 LIST_FOREACH(pe, &table->gpt_entry, gpe_entry) {
1301 if (pe->gpe_deleted || pe->gpe_internal || pe == entry)
1303 if (end >= pe->gpe_start && end <= pe->gpe_end) {
1304 gctl_error(req, "%d end '%jd'", ENOSPC,
1308 if (entry->gpe_start < pe->gpe_start && end > pe->gpe_end) {
1309 gctl_error(req, "%d size '%jd'", ENOSPC,
1310 (intmax_t)gpp->gpp_size);
1316 if ((g_debugflags & 16) == 0 &&
1317 (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)) {
1318 if (entry->gpe_end - entry->gpe_start + 1 > gpp->gpp_size) {
1319 /* Deny shrinking of an opened partition. */
1320 gctl_error(req, "%d", EBUSY);
1325 error = G_PART_RESIZE(table, entry, gpp);
1327 gctl_error(req, "%d%s", error, error != EBUSY ? "":
1328 " resizing will lead to unexpected shrinking"
1329 " due to alignment");
1333 if (!entry->gpe_created)
1334 entry->gpe_modified = 1;
1336 /* update mediasize of changed provider */
1337 mediasize = (entry->gpe_end - entry->gpe_start + 1) *
1339 g_resize_provider(pp, mediasize);
1341 /* Provide feedback if so requested. */
1342 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1343 sb = sbuf_new_auto();
1344 G_PART_FULLNAME(table, entry, sb, gp->name);
1345 sbuf_cat(sb, " resized\n");
1347 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1354 g_part_ctl_setunset(struct gctl_req *req, struct g_part_parms *gpp,
1358 struct g_part_entry *entry;
1359 struct g_part_table *table;
1364 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1365 g_topology_assert();
1369 if (gpp->gpp_parms & G_PART_PARM_INDEX) {
1370 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1371 if (entry->gpe_deleted || entry->gpe_internal)
1373 if (entry->gpe_index == gpp->gpp_index)
1376 if (entry == NULL) {
1377 gctl_error(req, "%d index '%d'", ENOENT,
1384 error = G_PART_SETUNSET(table, entry, gpp->gpp_attrib, set);
1386 gctl_error(req, "%d attrib '%s'", error, gpp->gpp_attrib);
1390 /* Provide feedback if so requested. */
1391 if (gpp->gpp_parms & G_PART_PARM_OUTPUT) {
1392 sb = sbuf_new_auto();
1393 sbuf_printf(sb, "%s %sset on ", gpp->gpp_attrib,
1396 G_PART_FULLNAME(table, entry, sb, gp->name);
1398 sbuf_cat(sb, gp->name);
1401 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1408 g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp)
1410 struct g_consumer *cp;
1411 struct g_provider *pp;
1413 struct g_part_entry *entry, *tmp;
1414 struct g_part_table *table;
1418 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, gp->name));
1419 g_topology_assert();
1422 if (!table->gpt_opened) {
1423 gctl_error(req, "%d", EPERM);
1427 cp = LIST_FIRST(&gp->consumer);
1428 LIST_FOREACH_SAFE(entry, &table->gpt_entry, gpe_entry, tmp) {
1429 entry->gpe_modified = 0;
1430 if (entry->gpe_created) {
1434 entry->gpe_pp = NULL;
1435 g_wither_provider(pp, ENXIO);
1437 entry->gpe_deleted = 1;
1439 if (entry->gpe_deleted) {
1440 LIST_REMOVE(entry, gpe_entry);
1445 g_topology_unlock();
1447 reprobe = (table->gpt_scheme == &g_part_null_scheme ||
1448 table->gpt_created) ? 1 : 0;
1451 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1452 if (entry->gpe_internal)
1457 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1458 LIST_REMOVE(entry, gpe_entry);
1461 error = g_part_probe(gp, cp, table->gpt_depth);
1464 g_access(cp, -1, -1, -1);
1465 g_part_wither(gp, error);
1471 * Synthesize a disk geometry. Some partitioning schemes
1472 * depend on it and since some file systems need it even
1473 * when the partitition scheme doesn't, we do it here in
1474 * scheme-independent code.
1477 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1480 error = G_PART_READ(table, cp);
1483 error = g_part_check_integrity(table, cp);
1488 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1489 if (!entry->gpe_internal)
1490 g_part_new_provider(gp, table, entry);
1493 table->gpt_opened = 0;
1494 g_access(cp, -1, -1, -1);
1499 gctl_error(req, "%d", error);
1504 g_part_wither(struct g_geom *gp, int error)
1506 struct g_part_entry *entry;
1507 struct g_part_table *table;
1510 if (table != NULL) {
1511 G_PART_DESTROY(table, NULL);
1512 while ((entry = LIST_FIRST(&table->gpt_entry)) != NULL) {
1513 LIST_REMOVE(entry, gpe_entry);
1516 if (gp->softc != NULL) {
1517 kobj_delete((kobj_t)gp->softc, M_GEOM);
1521 g_wither_geom(gp, error);
1529 g_part_ctlreq(struct gctl_req *req, struct g_class *mp, const char *verb)
1531 struct g_part_parms gpp;
1532 struct g_part_table *table;
1533 struct gctl_req_arg *ap;
1534 enum g_part_ctl ctlreq;
1535 unsigned int i, mparms, oparms, parm;
1536 int auto_commit, close_on_error;
1537 int error, modifies;
1539 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, verb));
1540 g_topology_assert();
1542 ctlreq = G_PART_CTL_NONE;
1545 oparms = G_PART_PARM_FLAGS | G_PART_PARM_OUTPUT | G_PART_PARM_VERSION;
1548 if (!strcmp(verb, "add")) {
1549 ctlreq = G_PART_CTL_ADD;
1550 mparms |= G_PART_PARM_GEOM | G_PART_PARM_SIZE |
1551 G_PART_PARM_START | G_PART_PARM_TYPE;
1552 oparms |= G_PART_PARM_INDEX | G_PART_PARM_LABEL;
1556 if (!strcmp(verb, "bootcode")) {
1557 ctlreq = G_PART_CTL_BOOTCODE;
1558 mparms |= G_PART_PARM_GEOM | G_PART_PARM_BOOTCODE;
1562 if (!strcmp(verb, "commit")) {
1563 ctlreq = G_PART_CTL_COMMIT;
1564 mparms |= G_PART_PARM_GEOM;
1566 } else if (!strcmp(verb, "create")) {
1567 ctlreq = G_PART_CTL_CREATE;
1568 mparms |= G_PART_PARM_PROVIDER | G_PART_PARM_SCHEME;
1569 oparms |= G_PART_PARM_ENTRIES;
1573 if (!strcmp(verb, "delete")) {
1574 ctlreq = G_PART_CTL_DELETE;
1575 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1576 } else if (!strcmp(verb, "destroy")) {
1577 ctlreq = G_PART_CTL_DESTROY;
1578 mparms |= G_PART_PARM_GEOM;
1579 oparms |= G_PART_PARM_FORCE;
1583 if (!strcmp(verb, "modify")) {
1584 ctlreq = G_PART_CTL_MODIFY;
1585 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1586 oparms |= G_PART_PARM_LABEL | G_PART_PARM_TYPE;
1587 } else if (!strcmp(verb, "move")) {
1588 ctlreq = G_PART_CTL_MOVE;
1589 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX;
1593 if (!strcmp(verb, "recover")) {
1594 ctlreq = G_PART_CTL_RECOVER;
1595 mparms |= G_PART_PARM_GEOM;
1596 } else if (!strcmp(verb, "resize")) {
1597 ctlreq = G_PART_CTL_RESIZE;
1598 mparms |= G_PART_PARM_GEOM | G_PART_PARM_INDEX |
1603 if (!strcmp(verb, "set")) {
1604 ctlreq = G_PART_CTL_SET;
1605 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1606 oparms |= G_PART_PARM_INDEX;
1610 if (!strcmp(verb, "undo")) {
1611 ctlreq = G_PART_CTL_UNDO;
1612 mparms |= G_PART_PARM_GEOM;
1614 } else if (!strcmp(verb, "unset")) {
1615 ctlreq = G_PART_CTL_UNSET;
1616 mparms |= G_PART_PARM_ATTRIB | G_PART_PARM_GEOM;
1617 oparms |= G_PART_PARM_INDEX;
1621 if (ctlreq == G_PART_CTL_NONE) {
1622 gctl_error(req, "%d verb '%s'", EINVAL, verb);
1626 bzero(&gpp, sizeof(gpp));
1627 for (i = 0; i < req->narg; i++) {
1630 switch (ap->name[0]) {
1632 if (!strcmp(ap->name, "arg0")) {
1634 (G_PART_PARM_GEOM | G_PART_PARM_PROVIDER);
1636 if (!strcmp(ap->name, "attrib"))
1637 parm = G_PART_PARM_ATTRIB;
1640 if (!strcmp(ap->name, "bootcode"))
1641 parm = G_PART_PARM_BOOTCODE;
1644 if (!strcmp(ap->name, "class"))
1648 if (!strcmp(ap->name, "entries"))
1649 parm = G_PART_PARM_ENTRIES;
1652 if (!strcmp(ap->name, "flags"))
1653 parm = G_PART_PARM_FLAGS;
1654 else if (!strcmp(ap->name, "force"))
1655 parm = G_PART_PARM_FORCE;
1658 if (!strcmp(ap->name, "index"))
1659 parm = G_PART_PARM_INDEX;
1662 if (!strcmp(ap->name, "label"))
1663 parm = G_PART_PARM_LABEL;
1666 if (!strcmp(ap->name, "output"))
1667 parm = G_PART_PARM_OUTPUT;
1670 if (!strcmp(ap->name, "scheme"))
1671 parm = G_PART_PARM_SCHEME;
1672 else if (!strcmp(ap->name, "size"))
1673 parm = G_PART_PARM_SIZE;
1674 else if (!strcmp(ap->name, "start"))
1675 parm = G_PART_PARM_START;
1678 if (!strcmp(ap->name, "type"))
1679 parm = G_PART_PARM_TYPE;
1682 if (!strcmp(ap->name, "verb"))
1684 else if (!strcmp(ap->name, "version"))
1685 parm = G_PART_PARM_VERSION;
1688 if ((parm & (mparms | oparms)) == 0) {
1689 gctl_error(req, "%d param '%s'", EINVAL, ap->name);
1693 case G_PART_PARM_ATTRIB:
1694 error = g_part_parm_str(req, ap->name,
1697 case G_PART_PARM_BOOTCODE:
1698 error = g_part_parm_bootcode(req, ap->name,
1699 &gpp.gpp_codeptr, &gpp.gpp_codesize);
1701 case G_PART_PARM_ENTRIES:
1702 error = g_part_parm_intmax(req, ap->name,
1705 case G_PART_PARM_FLAGS:
1706 error = g_part_parm_str(req, ap->name, &gpp.gpp_flags);
1708 case G_PART_PARM_FORCE:
1709 error = g_part_parm_uint32(req, ap->name,
1712 case G_PART_PARM_GEOM:
1713 error = g_part_parm_geom(req, ap->name, &gpp.gpp_geom);
1715 case G_PART_PARM_INDEX:
1716 error = g_part_parm_intmax(req, ap->name,
1719 case G_PART_PARM_LABEL:
1720 error = g_part_parm_str(req, ap->name, &gpp.gpp_label);
1722 case G_PART_PARM_OUTPUT:
1723 error = 0; /* Write-only parameter */
1725 case G_PART_PARM_PROVIDER:
1726 error = g_part_parm_provider(req, ap->name,
1729 case G_PART_PARM_SCHEME:
1730 error = g_part_parm_scheme(req, ap->name,
1733 case G_PART_PARM_SIZE:
1734 error = g_part_parm_quad(req, ap->name, &gpp.gpp_size);
1736 case G_PART_PARM_START:
1737 error = g_part_parm_quad(req, ap->name,
1740 case G_PART_PARM_TYPE:
1741 error = g_part_parm_str(req, ap->name, &gpp.gpp_type);
1743 case G_PART_PARM_VERSION:
1744 error = g_part_parm_uint32(req, ap->name,
1749 gctl_error(req, "%d %s", error, ap->name);
1753 if (error == ENOATTR) {
1754 gctl_error(req, "%d param '%s'", error,
1759 gpp.gpp_parms |= parm;
1761 if ((gpp.gpp_parms & mparms) != mparms) {
1762 parm = mparms - (gpp.gpp_parms & mparms);
1763 gctl_error(req, "%d param '%x'", ENOATTR, parm);
1767 /* Obtain permissions if possible/necessary. */
1770 if (modifies && (gpp.gpp_parms & G_PART_PARM_GEOM)) {
1771 table = gpp.gpp_geom->softc;
1772 if (table != NULL && table->gpt_corrupt &&
1773 ctlreq != G_PART_CTL_DESTROY &&
1774 ctlreq != G_PART_CTL_RECOVER) {
1775 gctl_error(req, "%d table '%s' is corrupt",
1776 EPERM, gpp.gpp_geom->name);
1779 if (table != NULL && !table->gpt_opened) {
1780 error = g_access(LIST_FIRST(&gpp.gpp_geom->consumer),
1783 gctl_error(req, "%d geom '%s'", error,
1784 gpp.gpp_geom->name);
1787 table->gpt_opened = 1;
1792 /* Allow the scheme to check or modify the parameters. */
1793 if (table != NULL) {
1794 error = G_PART_PRECHECK(table, ctlreq, &gpp);
1796 gctl_error(req, "%d pre-check failed", error);
1800 error = EDOOFUS; /* Prevent bogus uninit. warning. */
1803 case G_PART_CTL_NONE:
1804 panic("%s", __func__);
1805 case G_PART_CTL_ADD:
1806 error = g_part_ctl_add(req, &gpp);
1808 case G_PART_CTL_BOOTCODE:
1809 error = g_part_ctl_bootcode(req, &gpp);
1811 case G_PART_CTL_COMMIT:
1812 error = g_part_ctl_commit(req, &gpp);
1814 case G_PART_CTL_CREATE:
1815 error = g_part_ctl_create(req, &gpp);
1817 case G_PART_CTL_DELETE:
1818 error = g_part_ctl_delete(req, &gpp);
1820 case G_PART_CTL_DESTROY:
1821 error = g_part_ctl_destroy(req, &gpp);
1823 case G_PART_CTL_MODIFY:
1824 error = g_part_ctl_modify(req, &gpp);
1826 case G_PART_CTL_MOVE:
1827 error = g_part_ctl_move(req, &gpp);
1829 case G_PART_CTL_RECOVER:
1830 error = g_part_ctl_recover(req, &gpp);
1832 case G_PART_CTL_RESIZE:
1833 error = g_part_ctl_resize(req, &gpp);
1835 case G_PART_CTL_SET:
1836 error = g_part_ctl_setunset(req, &gpp, 1);
1838 case G_PART_CTL_UNDO:
1839 error = g_part_ctl_undo(req, &gpp);
1841 case G_PART_CTL_UNSET:
1842 error = g_part_ctl_setunset(req, &gpp, 0);
1846 /* Implement automatic commit. */
1848 auto_commit = (modifies &&
1849 (gpp.gpp_parms & G_PART_PARM_FLAGS) &&
1850 strchr(gpp.gpp_flags, 'C') != NULL) ? 1 : 0;
1852 KASSERT(gpp.gpp_parms & G_PART_PARM_GEOM, ("%s",
1854 error = g_part_ctl_commit(req, &gpp);
1859 if (error && close_on_error) {
1860 g_access(LIST_FIRST(&gpp.gpp_geom->consumer), -1, -1, -1);
1861 table->gpt_opened = 0;
1866 g_part_destroy_geom(struct gctl_req *req, struct g_class *mp,
1870 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, gp->name));
1871 g_topology_assert();
1873 g_part_wither(gp, EINVAL);
1877 static struct g_geom *
1878 g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
1880 struct g_consumer *cp;
1882 struct g_part_entry *entry;
1883 struct g_part_table *table;
1884 struct root_hold_token *rht;
1888 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s,%s)", __func__, mp->name, pp->name));
1889 g_topology_assert();
1891 /* Skip providers that are already open for writing. */
1896 * Create a GEOM with consumer and hook it up to the provider.
1897 * With that we become part of the topology. Optain read access
1900 gp = g_new_geomf(mp, "%s", pp->name);
1901 cp = g_new_consumer(gp);
1902 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1903 error = g_attach(cp, pp);
1905 error = g_access(cp, 1, 0, 0);
1909 g_destroy_consumer(cp);
1914 rht = root_mount_hold(mp->name);
1915 g_topology_unlock();
1918 * Short-circuit the whole probing galore when there's no
1921 if (pp->mediasize == 0 || pp->sectorsize == 0) {
1926 /* Make sure we can nest and if so, determine our depth. */
1927 error = g_getattr("PART::isleaf", cp, &attr);
1928 if (!error && attr) {
1932 error = g_getattr("PART::depth", cp, &attr);
1933 depth = (!error) ? attr + 1 : 0;
1935 error = g_part_probe(gp, cp, depth);
1942 * Synthesize a disk geometry. Some partitioning schemes
1943 * depend on it and since some file systems need it even
1944 * when the partitition scheme doesn't, we do it here in
1945 * scheme-independent code.
1947 g_part_geometry(table, cp, pp->mediasize / pp->sectorsize);
1949 error = G_PART_READ(table, cp);
1952 error = g_part_check_integrity(table, cp);
1957 LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) {
1958 if (!entry->gpe_internal)
1959 g_part_new_provider(gp, table, entry);
1962 root_mount_rel(rht);
1963 g_access(cp, -1, 0, 0);
1968 root_mount_rel(rht);
1969 g_access(cp, -1, 0, 0);
1971 g_destroy_consumer(cp);
1981 g_part_access(struct g_provider *pp, int dr, int dw, int de)
1983 struct g_consumer *cp;
1985 G_PART_TRACE((G_T_ACCESS, "%s(%s,%d,%d,%d)", __func__, pp->name, dr,
1988 cp = LIST_FIRST(&pp->geom->consumer);
1990 /* We always gain write-exclusive access. */
1991 return (g_access(cp, dr, dw, dw + de));
1995 g_part_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1996 struct g_consumer *cp, struct g_provider *pp)
1999 struct g_part_entry *entry;
2000 struct g_part_table *table;
2002 KASSERT(sb != NULL && gp != NULL, ("%s", __func__));
2005 if (indent == NULL) {
2006 KASSERT(cp == NULL && pp != NULL, ("%s", __func__));
2007 entry = pp->private;
2010 sbuf_printf(sb, " i %u o %ju ty %s", entry->gpe_index,
2011 (uintmax_t)entry->gpe_offset,
2012 G_PART_TYPE(table, entry, buf, sizeof(buf)));
2014 * libdisk compatibility quirk - the scheme dumps the
2015 * slicer name and partition type in a way that is
2016 * compatible with libdisk. When libdisk is not used
2017 * anymore, this should go away.
2019 G_PART_DUMPCONF(table, entry, sb, indent);
2020 } else if (cp != NULL) { /* Consumer configuration. */
2021 KASSERT(pp == NULL, ("%s", __func__));
2023 } else if (pp != NULL) { /* Provider configuration. */
2024 entry = pp->private;
2027 sbuf_printf(sb, "%s<start>%ju</start>\n", indent,
2028 (uintmax_t)entry->gpe_start);
2029 sbuf_printf(sb, "%s<end>%ju</end>\n", indent,
2030 (uintmax_t)entry->gpe_end);
2031 sbuf_printf(sb, "%s<index>%u</index>\n", indent,
2033 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
2034 G_PART_TYPE(table, entry, buf, sizeof(buf)));
2035 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
2036 (uintmax_t)entry->gpe_offset);
2037 sbuf_printf(sb, "%s<length>%ju</length>\n", indent,
2038 (uintmax_t)pp->mediasize);
2039 G_PART_DUMPCONF(table, entry, sb, indent);
2040 } else { /* Geom configuration. */
2041 sbuf_printf(sb, "%s<scheme>%s</scheme>\n", indent,
2042 table->gpt_scheme->name);
2043 sbuf_printf(sb, "%s<entries>%u</entries>\n", indent,
2044 table->gpt_entries);
2045 sbuf_printf(sb, "%s<first>%ju</first>\n", indent,
2046 (uintmax_t)table->gpt_first);
2047 sbuf_printf(sb, "%s<last>%ju</last>\n", indent,
2048 (uintmax_t)table->gpt_last);
2049 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", indent,
2050 table->gpt_sectors);
2051 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", indent,
2053 sbuf_printf(sb, "%s<state>%s</state>\n", indent,
2054 table->gpt_corrupt ? "CORRUPT": "OK");
2055 sbuf_printf(sb, "%s<modified>%s</modified>\n", indent,
2056 table->gpt_opened ? "true": "false");
2057 G_PART_DUMPCONF(table, NULL, sb, indent);
2062 g_part_resize(struct g_consumer *cp)
2064 struct g_part_table *table;
2066 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2067 g_topology_assert();
2069 table = cp->geom->softc;
2070 if (table->gpt_opened == 0) {
2071 if (g_access(cp, 1, 1, 1) != 0)
2073 table->gpt_opened = 1;
2075 if (G_PART_RESIZE(table, NULL, NULL) == 0)
2076 printf("GEOM_PART: %s was automatically resized.\n"
2077 " Use `gpart commit %s` to save changes or "
2078 "`gpart undo %s` to revert them.\n", cp->geom->name,
2079 cp->geom->name, cp->geom->name);
2080 if (g_part_check_integrity(table, cp) != 0) {
2081 g_access(cp, -1, -1, -1);
2082 table->gpt_opened = 0;
2083 g_part_wither(table->gpt_gp, ENXIO);
2088 g_part_orphan(struct g_consumer *cp)
2090 struct g_provider *pp;
2091 struct g_part_table *table;
2094 KASSERT(pp != NULL, ("%s", __func__));
2095 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, pp->name));
2096 g_topology_assert();
2098 KASSERT(pp->error != 0, ("%s", __func__));
2099 table = cp->geom->softc;
2100 if (table != NULL && table->gpt_opened)
2101 g_access(cp, -1, -1, -1);
2102 g_part_wither(cp->geom, pp->error);
2106 g_part_spoiled(struct g_consumer *cp)
2109 G_PART_TRACE((G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name));
2110 g_topology_assert();
2112 cp->flags |= G_CF_ORPHAN;
2113 g_part_wither(cp->geom, ENXIO);
2117 g_part_start(struct bio *bp)
2120 struct g_consumer *cp;
2122 struct g_part_entry *entry;
2123 struct g_part_table *table;
2124 struct g_kerneldump *gkd;
2125 struct g_provider *pp;
2131 cp = LIST_FIRST(&gp->consumer);
2133 G_PART_TRACE((G_T_BIO, "%s: cmd=%d, provider=%s", __func__, bp->bio_cmd,
2136 entry = pp->private;
2137 if (entry == NULL) {
2138 g_io_deliver(bp, ENXIO);
2142 switch(bp->bio_cmd) {
2146 if (bp->bio_offset >= pp->mediasize) {
2147 g_io_deliver(bp, EIO);
2150 bp2 = g_clone_bio(bp);
2152 g_io_deliver(bp, ENOMEM);
2155 if (bp2->bio_offset + bp2->bio_length > pp->mediasize)
2156 bp2->bio_length = pp->mediasize - bp2->bio_offset;
2157 bp2->bio_done = g_std_done;
2158 bp2->bio_offset += entry->gpe_offset;
2159 g_io_request(bp2, cp);
2164 if (g_handleattr_int(bp, "GEOM::fwheads", table->gpt_heads))
2166 if (g_handleattr_int(bp, "GEOM::fwsectors", table->gpt_sectors))
2168 if (g_handleattr_int(bp, "PART::isleaf", table->gpt_isleaf))
2170 if (g_handleattr_int(bp, "PART::depth", table->gpt_depth))
2172 if (g_handleattr_str(bp, "PART::scheme",
2173 table->gpt_scheme->name))
2175 if (g_handleattr_str(bp, "PART::type",
2176 G_PART_TYPE(table, entry, buf, sizeof(buf))))
2178 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
2180 * Check that the partition is suitable for kernel
2181 * dumps. Typically only swap partitions should be
2182 * used. If the request comes from the nested scheme
2183 * we allow dumping there as well.
2185 if ((bp->bio_from == NULL ||
2186 bp->bio_from->geom->class != &g_part_class) &&
2187 G_PART_DUMPTO(table, entry) == 0) {
2188 g_io_deliver(bp, ENODEV);
2189 printf("GEOM_PART: Partition '%s' not suitable"
2190 " for kernel dumps (wrong type?)\n",
2194 gkd = (struct g_kerneldump *)bp->bio_data;
2195 if (gkd->offset >= pp->mediasize) {
2196 g_io_deliver(bp, EIO);
2199 if (gkd->offset + gkd->length > pp->mediasize)
2200 gkd->length = pp->mediasize - gkd->offset;
2201 gkd->offset += entry->gpe_offset;
2205 g_io_deliver(bp, EOPNOTSUPP);
2209 bp2 = g_clone_bio(bp);
2211 g_io_deliver(bp, ENOMEM);
2214 bp2->bio_done = g_std_done;
2215 g_io_request(bp2, cp);
2219 g_part_init(struct g_class *mp)
2222 TAILQ_INSERT_HEAD(&g_part_schemes, &g_part_null_scheme, scheme_list);
2226 g_part_fini(struct g_class *mp)
2229 TAILQ_REMOVE(&g_part_schemes, &g_part_null_scheme, scheme_list);
2233 g_part_unload_event(void *arg, int flag)
2235 struct g_consumer *cp;
2237 struct g_provider *pp;
2238 struct g_part_scheme *scheme;
2239 struct g_part_table *table;
2243 if (flag == EV_CANCEL)
2248 scheme = (void *)(*xchg);
2250 g_topology_assert();
2252 LIST_FOREACH(gp, &g_part_class.geom, geom) {
2254 if (table->gpt_scheme != scheme)
2258 LIST_FOREACH(pp, &gp->provider, provider)
2259 acc += pp->acr + pp->acw + pp->ace;
2260 LIST_FOREACH(cp, &gp->consumer, consumer)
2261 acc += cp->acr + cp->acw + cp->ace;
2264 g_part_wither(gp, ENOSYS);
2270 TAILQ_REMOVE(&g_part_schemes, scheme, scheme_list);
2276 g_part_modevent(module_t mod, int type, struct g_part_scheme *scheme)
2278 struct g_part_scheme *iter;
2285 TAILQ_FOREACH(iter, &g_part_schemes, scheme_list) {
2286 if (scheme == iter) {
2287 printf("GEOM_PART: scheme %s is already "
2288 "registered!\n", scheme->name);
2293 TAILQ_INSERT_TAIL(&g_part_schemes, scheme,
2295 g_retaste(&g_part_class);
2299 arg = (uintptr_t)scheme;
2300 error = g_waitfor_event(g_part_unload_event, &arg, M_WAITOK,