diff options
author | marcel <marcel@FreeBSD.org> | 2007-12-06 02:32:42 +0000 |
---|---|---|
committer | marcel <marcel@FreeBSD.org> | 2007-12-06 02:32:42 +0000 |
commit | 5f073f27891e074a1328a322fd6bcf3dc7aebd87 (patch) | |
tree | b427a34e149fa36d5ee8e94751835b890c388547 /sys/geom | |
parent | 444bd6eea2b5f822b330e6f735a5bd14d99d86c9 (diff) | |
download | FreeBSD-src-5f073f27891e074a1328a322fd6bcf3dc7aebd87.zip FreeBSD-src-5f073f27891e074a1328a322fd6bcf3dc7aebd87.tar.gz |
Add a BSD disklabel backend to g_part:
o Disklabels can have between 8 and 20 partitions (inclusive).
o No device special file is created for the raw partition.
o Switch ia64 to use this backend.
o No support for boot code yet.
Diffstat (limited to 'sys/geom')
-rw-r--r-- | sys/geom/part/g_part.c | 60 | ||||
-rw-r--r-- | sys/geom/part/g_part.h | 8 | ||||
-rw-r--r-- | sys/geom/part/g_part_bsd.c | 440 |
3 files changed, 494 insertions, 14 deletions
diff --git a/sys/geom/part/g_part.c b/sys/geom/part/g_part.c index 477584f..c3f59d7 100644 --- a/sys/geom/part/g_part.c +++ b/sys/geom/part/g_part.c @@ -453,6 +453,8 @@ g_part_ctl_add(struct gctl_req *req, struct g_part_parms *gpp) index = entry->gpe_index + 1; last = entry; } + if (entry->gpe_internal) + continue; if (gpp->gpp_start >= entry->gpe_start && gpp->gpp_start <= entry->gpe_end) { gctl_error(req, "%d start '%jd'", ENOSPC, @@ -666,6 +668,14 @@ g_part_ctl_create(struct gctl_req *req, struct g_part_parms *gpp) error = g_getattr("PART::depth", cp, &attr); table->gpt_depth = (!error) ? attr + 1 : 0; + /* If we're nested, get the absolute sector offset on disk. */ + if (table->gpt_depth) { + error = g_getattr("PART::offset", cp, &attr); + if (error) + goto fail; + table->gpt_offset = attr; + } + /* * Synthesize a disk geometry. Some partitioning schemes * depend on it and since some file systems need it even @@ -742,13 +752,16 @@ g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp) } pp = entry->gpe_pp; - if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) { - gctl_error(req, "%d", EBUSY); - return (EBUSY); + if (pp != NULL) { + if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) { + gctl_error(req, "%d", EBUSY); + return (EBUSY); + } + + pp->private = NULL; + entry->gpe_pp = NULL; } - pp->private = NULL; - entry->gpe_pp = NULL; if (entry->gpe_created) { LIST_REMOVE(entry, gpe_entry); g_free(entry); @@ -756,7 +769,9 @@ g_part_ctl_delete(struct gctl_req *req, struct g_part_parms *gpp) entry->gpe_modified = 0; entry->gpe_deleted = 1; } - g_wither_provider(pp, ENXIO); + + if (pp != NULL) + g_wither_provider(pp, ENXIO); /* Provide feedback if so requested. */ if (gpp->gpp_parms & G_PART_PARM_OUTPUT) { @@ -919,9 +934,11 @@ g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp) entry->gpe_modified = 0; if (entry->gpe_created) { pp = entry->gpe_pp; - pp->private = NULL; - entry->gpe_pp = NULL; - g_wither_provider(pp, ENXIO); + if (pp != NULL) { + pp->private = NULL; + entry->gpe_pp = NULL; + g_wither_provider(pp, ENXIO); + } entry->gpe_deleted = 1; } if (entry->gpe_deleted) { @@ -956,8 +973,10 @@ g_part_ctl_undo(struct gctl_req *req, struct g_part_parms *gpp) g_topology_lock(); - LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) - g_part_new_provider(gp, table, entry); + LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { + if (!entry->gpe_internal) + g_part_new_provider(gp, table, entry); + } table->gpt_opened = 0; g_access(cp, -1, -1, -1); @@ -1331,7 +1350,15 @@ g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) goto fail; table = gp->softc; - + + /* If we're nested, get the absolute sector offset on disk. */ + if (table->gpt_depth) { + error = g_getattr("PART::offset", cp, &attr); + if (error) + goto fail; + table->gpt_offset = attr; + } + /* * Synthesize a disk geometry. Some partitioning schemes * depend on it and since some file systems need it even @@ -1345,8 +1372,10 @@ g_part_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) goto fail; g_topology_lock(); - LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) - g_part_new_provider(gp, table, entry); + LIST_FOREACH(entry, &table->gpt_entry, gpe_entry) { + if (!entry->gpe_internal) + g_part_new_provider(gp, table, entry); + } g_access(cp, -1, 0, 0); return (gp); @@ -1507,6 +1536,9 @@ g_part_start(struct bio *bp) return; if (g_handleattr_int(bp, "PART::depth", table->gpt_depth)) return; + if (g_handleattr_int(bp, "PART::offset", + table->gpt_offset + entry->gpe_start)) + return; if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { /* * Check that the partition is suitable for kernel diff --git a/sys/geom/part/g_part.h b/sys/geom/part/g_part.h index 1410b7a..043cafd 100644 --- a/sys/geom/part/g_part.h +++ b/sys/geom/part/g_part.h @@ -69,6 +69,7 @@ struct g_part_entry { int gpe_created:1; /* Entry is newly created. */ int gpe_deleted:1; /* Entry has been deleted. */ int gpe_modified:1; /* Entry has been modified. */ + int gpe_internal:1; /* Entry is not a used entry. */ }; /* G_PART table (KOBJ instance). */ @@ -101,6 +102,13 @@ struct g_part_table { */ uint32_t gpt_sectors; uint32_t gpt_heads; + /* + * gpt_offset holds the absolute block address of the scheme + * on disk. Some partitioning schemes (historically) use + * absolute addressing. Relative addresses are obtained by + * subtracting gpt_offset from the absolute addresses. + */ + uint64_t gpt_offset; int gpt_depth; /* Sub-partitioning level. */ int gpt_isleaf:1; /* Cannot be sub-partitioned. */ diff --git a/sys/geom/part/g_part_bsd.c b/sys/geom/part/g_part_bsd.c new file mode 100644 index 0000000..51cee2a --- /dev/null +++ b/sys/geom/part/g_part_bsd.c @@ -0,0 +1,440 @@ +/*- + * Copyright (c) 2007 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/bio.h> +#include <sys/disklabel.h> +#include <sys/endian.h> +#include <sys/kernel.h> +#include <sys/kobj.h> +#include <sys/limits.h> +#include <sys/lock.h> +#include <sys/malloc.h> +#include <sys/mutex.h> +#include <sys/queue.h> +#include <sys/sbuf.h> +#include <sys/systm.h> +#include <geom/geom.h> +#include <geom/part/g_part.h> + +#include "g_part_if.h" + +struct g_part_bsd_table { + struct g_part_table base; + u_char *label; +}; + +struct g_part_bsd_entry { + struct g_part_entry base; + struct partition part; +}; + +static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *, + struct g_part_parms *); +static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *); +static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *); +static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *); +static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *, + struct g_part_parms *); +static char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *, + char *, size_t); +static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *); +static int g_part_bsd_read(struct g_part_table *, struct g_consumer *); +static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *, + char *, size_t); +static int g_part_bsd_write(struct g_part_table *, struct g_consumer *); + +static kobj_method_t g_part_bsd_methods[] = { + KOBJMETHOD(g_part_add, g_part_bsd_add), + KOBJMETHOD(g_part_create, g_part_bsd_create), + KOBJMETHOD(g_part_destroy, g_part_bsd_destroy), + KOBJMETHOD(g_part_dumpto, g_part_bsd_dumpto), + KOBJMETHOD(g_part_modify, g_part_bsd_modify), + KOBJMETHOD(g_part_name, g_part_bsd_name), + KOBJMETHOD(g_part_probe, g_part_bsd_probe), + KOBJMETHOD(g_part_read, g_part_bsd_read), + KOBJMETHOD(g_part_type, g_part_bsd_type), + KOBJMETHOD(g_part_write, g_part_bsd_write), + { 0, 0 } +}; + +static struct g_part_scheme g_part_bsd_scheme = { + "BSD", + g_part_bsd_methods, + sizeof(struct g_part_bsd_table), + .gps_entrysz = sizeof(struct g_part_bsd_entry), + .gps_minent = 8, + .gps_maxent = 20, +}; +G_PART_SCHEME_DECLARE(g_part_bsd_scheme); + +static int +bsd_parse_type(const char *type, uint8_t *fstype) +{ + const char *alias; + char *endp; + long lt; + + if (type[0] == '!') { + lt = strtol(type + 1, &endp, 0); + if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256) + return (EINVAL); + *fstype = (u_int)lt; + return (0); + } + alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP); + if (!strcasecmp(type, alias)) { + *fstype = FS_SWAP; + return (0); + } + alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS); + if (!strcasecmp(type, alias)) { + *fstype = FS_BSDFFS; + return (0); + } + alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM); + if (!strcasecmp(type, alias)) { + *fstype = FS_VINUM; + return (0); + } + return (EINVAL); +} + +static int +g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry, + struct g_part_parms *gpp) +{ + struct g_part_bsd_entry *entry; + struct g_part_bsd_table *table; + uint32_t start, size, sectors; + + if (gpp->gpp_parms & G_PART_PARM_LABEL) + return (EINVAL); + + sectors = basetable->gpt_sectors; + + entry = (struct g_part_bsd_entry *)baseentry; + table = (struct g_part_bsd_table *)basetable; + + start = gpp->gpp_start; + size = gpp->gpp_size; + if (size < sectors) + return (EINVAL); + if (start % sectors) { + size = size - sectors + (start % sectors); + start = start - (start % sectors) + sectors; + } + if (size % sectors) + size = size - (size % sectors); + if (size < sectors) + return (EINVAL); + + KASSERT(baseentry->gpe_start <= start, (__func__)); + KASSERT(baseentry->gpe_end >= start + size - 1, (__func__)); + baseentry->gpe_start = start; + baseentry->gpe_end = start + size - 1; + entry->part.p_size = size; + entry->part.p_offset = start + basetable->gpt_offset; + entry->part.p_fsize = 0; + entry->part.p_frag = 0; + entry->part.p_cpg = 0; + return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype)); +} + +static int +g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp) +{ + struct g_consumer *cp; + struct g_provider *pp; + struct g_part_entry *baseentry; + struct g_part_bsd_entry *entry; + struct g_part_bsd_table *table; + u_char *ptr; + uint64_t msize; + uint32_t ncyls, secpercyl; + + pp = gpp->gpp_provider; + cp = LIST_FIRST(&pp->consumers); + + if (pp->sectorsize < sizeof(struct disklabel)) + return (ENOSPC); + + msize = pp->mediasize / pp->sectorsize; + secpercyl = basetable->gpt_sectors * basetable->gpt_heads; + ncyls = msize / secpercyl; + + table = (struct g_part_bsd_table *)basetable; + ptr = table->label = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); + + le32enc(ptr + 0, DISKMAGIC); /* d_magic */ + le32enc(ptr + 40, pp->sectorsize); /* d_secsize */ + le32enc(ptr + 44, basetable->gpt_sectors); /* d_nsectors */ + le32enc(ptr + 48, basetable->gpt_heads); /* d_ntracks */ + le32enc(ptr + 52, ncyls); /* d_ncylinders */ + le32enc(ptr + 56, secpercyl); /* d_secpercyl */ + le32enc(ptr + 60, ncyls * secpercyl); /* d_secperunit */ + le16enc(ptr + 72, 3600); /* d_rpm */ + le32enc(ptr + 132, DISKMAGIC); /* d_magic2 */ + le16enc(ptr + 138, basetable->gpt_entries); /* d_npartitions */ + le32enc(ptr + 140, BBSIZE); /* d_bbsize */ + + basetable->gpt_first = 0; + basetable->gpt_last = ncyls * secpercyl - 1; + basetable->gpt_isleaf = 1; + + baseentry = g_part_new_entry(basetable, RAW_PART + 1, + basetable->gpt_first, basetable->gpt_last); + baseentry->gpe_internal = 1; + entry = (struct g_part_bsd_entry *)baseentry; + entry->part.p_size = basetable->gpt_last + 1; + entry->part.p_offset = basetable->gpt_offset; + + return (0); +} + +static int +g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) +{ + + /* Wipe the second sector to clear the partitioning. */ + basetable->gpt_smhead |= 2; + return (0); +} + +static int +g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) +{ + struct g_part_bsd_entry *entry; + + /* Allow dumping to a swap partition only. */ + entry = (struct g_part_bsd_entry *)baseentry; + return ((entry->part.p_fstype == FS_SWAP) ? 1 : 0); +} + +static int +g_part_bsd_modify(struct g_part_table *basetable, + struct g_part_entry *baseentry, struct g_part_parms *gpp) +{ + struct g_part_bsd_entry *entry; + + if (gpp->gpp_parms & G_PART_PARM_LABEL) + return (EINVAL); + + entry = (struct g_part_bsd_entry *)baseentry; + if (gpp->gpp_parms & G_PART_PARM_TYPE) + return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype)); + return (0); +} + +static char * +g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry, + char *buf, size_t bufsz) +{ + + snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1); + return (buf); +} + +static int +g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp) +{ + struct g_provider *pp; + u_char *buf; + uint32_t magic1, magic2; + int error; + + pp = cp->provider; + + /* Sanity-check the provider. */ + if (pp->sectorsize < sizeof(struct disklabel) || + pp->mediasize < BBSIZE) + return (ENOSPC); + + /* Check that there's a disklabel. */ + buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); + if (buf == NULL) + return (error); + magic1 = le32dec(buf + 0); + magic2 = le32dec(buf + 132); + g_free(buf); + return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC) + ? G_PART_PROBE_PRI_NORM : ENXIO); +} + +static int +g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp) +{ + struct g_provider *pp; + struct g_part_bsd_table *table; + struct g_part_entry *baseentry; + struct g_part_bsd_entry *entry; + struct partition part; + u_char *buf, *p; + off_t chs, msize; + u_int sectors, heads; + int error, index; + + pp = cp->provider; + table = (struct g_part_bsd_table *)basetable; + msize = pp->mediasize / pp->sectorsize; + + buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error); + if (buf == NULL) + return (error); + + table->label = buf; + + if (le32dec(buf + 40) != pp->sectorsize) + goto invalid_label; + sectors = le32dec(buf + 44); + if (sectors < 1 || sectors > 63) + goto invalid_label; + if (sectors != basetable->gpt_sectors) { + if (basetable->gpt_fixgeom) + goto invalid_label; + g_part_geometry_heads(msize, sectors, &chs, &heads); + if (chs == 0) + goto invalid_label; + basetable->gpt_sectors = sectors; + basetable->gpt_heads = heads; + } + heads = le32dec(buf + 48); + if (heads < 1 || heads > 255) + goto invalid_label; + if (heads != basetable->gpt_heads) { + if (basetable->gpt_fixgeom) + goto invalid_label; + basetable->gpt_heads = heads; + } + chs = le32dec(buf + 52) * heads * sectors; + if (chs < 1 || chs > msize) + goto invalid_label; + + basetable->gpt_first = 0; + basetable->gpt_last = chs - 1; + basetable->gpt_isleaf = 1; + + basetable->gpt_entries = le16dec(buf + 138); + if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent || + basetable->gpt_entries > g_part_bsd_scheme.gps_maxent) + goto invalid_label; + + for (index = basetable->gpt_entries - 1; index >= 0; index--) { + p = buf + 148 + index * 16; + part.p_size = le32dec(p + 0); + part.p_offset = le32dec(p + 4); + part.p_fsize = le32dec(p + 8); + part.p_fstype = p[12]; + part.p_frag = p[13]; + part.p_cpg = le16dec(p + 14); + if (part.p_size == 0) + continue; + if (part.p_fstype == FS_UNUSED && index != RAW_PART) + continue; + if (part.p_offset < basetable->gpt_offset) + continue; + baseentry = g_part_new_entry(basetable, index + 1, + part.p_offset - basetable->gpt_offset, + part.p_offset - basetable->gpt_offset + part.p_size - 1); + entry = (struct g_part_bsd_entry *)baseentry; + entry->part = part; + if (part.p_fstype == FS_UNUSED) + baseentry->gpe_internal = 1; + } + + return (0); + + invalid_label: + printf("GEOM: %s: invalid disklabel.\n", pp->name); + g_free(table->label); + return (EINVAL); +} + +static const char * +g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry, + char *buf, size_t bufsz) +{ + struct g_part_bsd_entry *entry; + int type; + + entry = (struct g_part_bsd_entry *)baseentry; + type = entry->part.p_fstype; + if (type == FS_SWAP) + return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP)); + if (type == FS_BSDFFS) + return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS)); + if (type == FS_VINUM) + return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM)); + snprintf(buf, bufsz, "!%d", type); + return (buf); +} + +static int +g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp) +{ + struct g_provider *pp; + struct g_part_entry *baseentry; + struct g_part_bsd_entry *entry; + struct g_part_bsd_table *table; + uint16_t sum; + u_char *p, *pe; + int error, index; + + pp = cp->provider; + table = (struct g_part_bsd_table *)basetable; + baseentry = LIST_FIRST(&basetable->gpt_entry); + for (index = 1; index <= basetable->gpt_entries; index++) { + p = table->label + 148 + (index - 1) * 16; + entry = (baseentry != NULL && index == baseentry->gpe_index) + ? (struct g_part_bsd_entry *)baseentry : NULL; + if (entry != NULL && !baseentry->gpe_deleted) { + le32enc(p + 0, entry->part.p_size); + le32enc(p + 4, entry->part.p_offset); + le32enc(p + 8, entry->part.p_fsize); + p[12] = entry->part.p_fstype; + p[13] = entry->part.p_frag; + le16enc(p + 14, entry->part.p_cpg); + } else + bzero(p, 16); + + if (entry != NULL) + baseentry = LIST_NEXT(baseentry, gpe_entry); + } + + /* Calculate checksum. */ + le16enc(table->label + 136, 0); + pe = table->label + 148 + basetable->gpt_entries * 16; + sum = 0; + for (p = table->label; p < pe; p += 2) + sum ^= le16dec(p); + le16enc(table->label + 136, sum); + + error = g_write_data(cp, pp->sectorsize, table->label, pp->sectorsize); + return (error); +} |