Author: ae Date: Mon May 12 11:14:07 2014 New Revision: 265911 URL: http://svnweb.freebsd.org/changeset/base/265911
Log: MFC r265318: For schemes that do an automatic partition aligning move this code to separate function. MFC r265331: Prevent an unexpected shrinking on resizing due to alignment for MBR, PC98 and VTOC8 schemes. MFC r265333: Add better error description for case when we are doing resize and scheme-specific method returns EBUSY. MFC r265539: It is safe to allow shrinking, when aligned size is bigger than current. Modified: stable/9/sys/geom/part/g_part.c stable/9/sys/geom/part/g_part_ebr.c stable/9/sys/geom/part/g_part_mbr.c stable/9/sys/geom/part/g_part_pc98.c stable/9/sys/geom/part/g_part_vtoc8.c Directory Properties: stable/9/sys/ (props changed) Modified: stable/9/sys/geom/part/g_part.c ============================================================================== --- stable/9/sys/geom/part/g_part.c Mon May 12 10:19:31 2014 (r265910) +++ stable/9/sys/geom/part/g_part.c Mon May 12 11:14:07 2014 (r265911) @@ -1310,7 +1310,9 @@ g_part_ctl_resize(struct gctl_req *req, error = G_PART_RESIZE(table, entry, gpp); if (error) { - gctl_error(req, "%d", error); + gctl_error(req, "%d%s", error, error != EBUSY ? "": + " resizing will lead to unexpected shrinking" + " due to alignment"); return (error); } Modified: stable/9/sys/geom/part/g_part_ebr.c ============================================================================== --- stable/9/sys/geom/part/g_part_ebr.c Mon May 12 10:19:31 2014 (r265910) +++ stable/9/sys/geom/part/g_part_ebr.c Mon May 12 11:14:07 2014 (r265911) @@ -217,47 +217,54 @@ ebr_set_chs(struct g_part_table *table, } static int +ebr_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size) +{ + uint32_t sectors; + + sectors = basetable->gpt_sectors; + if (*size < 2 * sectors) + return (EINVAL); + if (*start % sectors) { + *size += (*start % sectors) - sectors; + *start -= (*start % sectors) - sectors; + } + if (*size % sectors) + *size -= (*size % sectors); + if (*size < 2 * sectors) + return (EINVAL); + return (0); +} + + +static int g_part_ebr_add(struct g_part_table *basetable, struct g_part_entry *baseentry, struct g_part_parms *gpp) { - struct g_geom *gp; struct g_provider *pp; struct g_part_ebr_entry *entry; - uint32_t start, size, sectors; + uint32_t start, size; if (gpp->gpp_parms & G_PART_PARM_LABEL) return (EINVAL); - gp = basetable->gpt_gp; - pp = LIST_FIRST(&gp->consumer)->provider; - sectors = basetable->gpt_sectors; - + pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider; entry = (struct g_part_ebr_entry *)baseentry; - start = gpp->gpp_start; size = gpp->gpp_size; - if (size < 2 * 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 < 2 * sectors) + if (ebr_align(basetable, &start, &size) != 0) return (EINVAL); - if (baseentry->gpe_deleted) bzero(&entry->ent, sizeof(entry->ent)); KASSERT(baseentry->gpe_start <= start, ("%s", __func__)); KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__)); - baseentry->gpe_index = (start / sectors) + 1; - baseentry->gpe_offset = (off_t)(start + sectors) * pp->sectorsize; + baseentry->gpe_index = (start / basetable->gpt_sectors) + 1; + baseentry->gpe_offset = + (off_t)(start + basetable->gpt_sectors) * pp->sectorsize; baseentry->gpe_start = start; baseentry->gpe_end = start + size - 1; - entry->ent.dp_start = sectors; - entry->ent.dp_size = size - sectors; + entry->ent.dp_start = basetable->gpt_sectors; + entry->ent.dp_size = size - basetable->gpt_sectors; ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl, &entry->ent.dp_shd, &entry->ent.dp_ssect); ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl, Modified: stable/9/sys/geom/part/g_part_mbr.c ============================================================================== --- stable/9/sys/geom/part/g_part_mbr.c Mon May 12 10:19:31 2014 (r265910) +++ stable/9/sys/geom/part/g_part_mbr.c Mon May 12 11:14:07 2014 (r265911) @@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$"); #include <sys/systm.h> #include <sys/sysctl.h> #include <geom/geom.h> +#include <geom/geom_int.h> #include <geom/part/g_part.h> #include "g_part_if.h" @@ -194,34 +195,39 @@ mbr_set_chs(struct g_part_table *table, } static int +mbr_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size) +{ + uint32_t sectors; + + sectors = basetable->gpt_sectors; + if (*size < sectors) + return (EINVAL); + if (start != NULL && (*start % sectors)) { + *size += (*start % sectors) - sectors; + *start -= (*start % sectors) - sectors; + } + if (*size % sectors) + *size -= (*size % sectors); + if (*size < sectors) + return (EINVAL); + return (0); +} + +static int g_part_mbr_add(struct g_part_table *basetable, struct g_part_entry *baseentry, struct g_part_parms *gpp) { struct g_part_mbr_entry *entry; - struct g_part_mbr_table *table; - uint32_t start, size, sectors; + uint32_t start, size; if (gpp->gpp_parms & G_PART_PARM_LABEL) return (EINVAL); - sectors = basetable->gpt_sectors; - entry = (struct g_part_mbr_entry *)baseentry; - table = (struct g_part_mbr_table *)basetable; - start = gpp->gpp_start; size = gpp->gpp_size; - if (size < sectors) + if (mbr_align(basetable, &start, &size) != 0) 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); - if (baseentry->gpe_deleted) bzero(&entry->ent, sizeof(entry->ent)); @@ -335,18 +341,17 @@ g_part_mbr_resize(struct g_part_table *b struct g_part_entry *baseentry, struct g_part_parms *gpp) { struct g_part_mbr_entry *entry; - uint32_t size, sectors; + struct g_provider *pp; + uint32_t size; - sectors = basetable->gpt_sectors; size = gpp->gpp_size; - - if (size < sectors) + if (mbr_align(basetable, NULL, &size) != 0) return (EINVAL); - if (size % sectors) - size = size - (size % sectors); - if (size < sectors) - return (EINVAL); - + /* XXX: prevent unexpected shrinking. */ + pp = baseentry->gpe_pp; + if ((g_debugflags & 0x10) == 0 && size < gpp->gpp_size && + pp->mediasize / pp->sectorsize > size) + return (EBUSY); entry = (struct g_part_mbr_entry *)baseentry; baseentry->gpe_end = baseentry->gpe_start + size - 1; entry->ent.dp_size = size; Modified: stable/9/sys/geom/part/g_part_pc98.c ============================================================================== --- stable/9/sys/geom/part/g_part_pc98.c Mon May 12 10:19:31 2014 (r265910) +++ stable/9/sys/geom/part/g_part_pc98.c Mon May 12 11:14:07 2014 (r265911) @@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$"); #include <sys/systm.h> #include <sys/sysctl.h> #include <geom/geom.h> +#include <geom/geom_int.h> #include <geom/part/g_part.h> #include "g_part_if.h" @@ -175,32 +176,37 @@ pc98_set_chs(struct g_part_table *table, } static int +pc98_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size) +{ + uint32_t cyl; + + cyl = basetable->gpt_heads * basetable->gpt_sectors; + if (*size < cyl) + return (EINVAL); + if (start != NULL && (*start % cyl)) { + *size += (*start % cyl) - cyl; + *start -= (*start % cyl) - cyl; + } + if (*size % cyl) + *size -= (*size % cyl); + if (*size < cyl) + return (EINVAL); + return (0); +} + +static int g_part_pc98_add(struct g_part_table *basetable, struct g_part_entry *baseentry, struct g_part_parms *gpp) { struct g_part_pc98_entry *entry; - struct g_part_pc98_table *table; - uint32_t cyl, start, size; + uint32_t start, size; int error; - cyl = basetable->gpt_heads * basetable->gpt_sectors; - entry = (struct g_part_pc98_entry *)baseentry; - table = (struct g_part_pc98_table *)basetable; - start = gpp->gpp_start; size = gpp->gpp_size; - if (size < cyl) + if (pc98_align(basetable, &start, &size) != 0) return (EINVAL); - if (start % cyl) { - size = size - cyl + (start % cyl); - start = start - (start % cyl) + cyl; - } - if (size % cyl) - size = size - (size % cyl); - if (size < cyl) - return (EINVAL); - if (baseentry->gpe_deleted) bzero(&entry->ent, sizeof(entry->ent)); else @@ -343,18 +349,17 @@ g_part_pc98_resize(struct g_part_table * struct g_part_entry *baseentry, struct g_part_parms *gpp) { struct g_part_pc98_entry *entry; - uint32_t size, cyl; + struct g_provider *pp; + uint32_t size; - cyl = basetable->gpt_heads * basetable->gpt_sectors; size = gpp->gpp_size; - - if (size < cyl) + if (pc98_align(basetable, NULL, &size) != 0) return (EINVAL); - if (size % cyl) - size = size - (size % cyl); - if (size < cyl) - return (EINVAL); - + /* XXX: prevent unexpected shrinking. */ + pp = baseentry->gpe_pp; + if ((g_debugflags & 0x10) == 0 && size < gpp->gpp_size && + pp->mediasize / pp->sectorsize > size) + return (EBUSY); entry = (struct g_part_pc98_entry *)baseentry; baseentry->gpe_end = baseentry->gpe_start + size - 1; pc98_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl, Modified: stable/9/sys/geom/part/g_part_vtoc8.c ============================================================================== --- stable/9/sys/geom/part/g_part_vtoc8.c Mon May 12 10:19:31 2014 (r265910) +++ stable/9/sys/geom/part/g_part_vtoc8.c Mon May 12 11:14:07 2014 (r265911) @@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$"); #include <sys/sysctl.h> #include <sys/vtoc.h> #include <geom/geom.h> +#include <geom/geom_int.h> #include <geom/part/g_part.h> #include "g_part_if.h" @@ -143,6 +144,23 @@ vtoc8_parse_type(const char *type, uint1 } static int +vtoc8_align(struct g_part_vtoc8_table *table, uint64_t *start, uint64_t *size) +{ + + if (*size < table->secpercyl) + return (EINVAL); + if (start != NULL && (*start % table->secpercyl)) { + *size += (*start % table->secpercyl) - table->secpercyl; + *start -= (*start % table->secpercyl) - table->secpercyl; + } + if (*size % table->secpercyl) + *size -= (*size % table->secpercyl); + if (*size < table->secpercyl) + return (EINVAL); + return (0); +} + +static int g_part_vtoc8_add(struct g_part_table *basetable, struct g_part_entry *entry, struct g_part_parms *gpp) { @@ -160,16 +178,9 @@ g_part_vtoc8_add(struct g_part_table *ba table = (struct g_part_vtoc8_table *)basetable; index = entry->gpe_index - 1; - start = gpp->gpp_start; size = gpp->gpp_size; - if (start % table->secpercyl) { - size = size - table->secpercyl + (start % table->secpercyl); - start = start - (start % table->secpercyl) + table->secpercyl; - } - if (size % table->secpercyl) - size = size - (size % table->secpercyl); - if (size < table->secpercyl) + if (vtoc8_align(table, &start, &size) != 0) return (EINVAL); KASSERT(entry->gpe_start <= start, (__func__)); @@ -310,15 +321,18 @@ g_part_vtoc8_resize(struct g_part_table struct g_part_entry *entry, struct g_part_parms *gpp) { struct g_part_vtoc8_table *table; + struct g_provider *pp; uint64_t size; table = (struct g_part_vtoc8_table *)basetable; size = gpp->gpp_size; - if (size % table->secpercyl) - size = size - (size % table->secpercyl); - if (size < table->secpercyl) + if (vtoc8_align(table, NULL, &size) != 0) return (EINVAL); - + /* XXX: prevent unexpected shrinking. */ + pp = entry->gpe_pp; + if ((g_debugflags & 0x10) == 0 && size < gpp->gpp_size && + pp->mediasize / pp->sectorsize > size) + return (EBUSY); entry->gpe_end = entry->gpe_start + size - 1; be32enc(&table->vtoc.map[entry->gpe_index - 1].nblks, size); _______________________________________________ svn-src-all@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/svn-src-all To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"