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.
This commit is contained in:
ae 2014-05-12 10:19:31 +00:00
parent 767aadd2f0
commit 8bd4b60954
5 changed files with 117 additions and 84 deletions

View File

@ -1314,7 +1314,9 @@ g_part_ctl_resize(struct gctl_req *req, struct g_part_parms *gpp)
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);
}

View File

@ -216,48 +216,55 @@ ebr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
*secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
}
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)
if (ebr_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 < 2 * sectors)
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,

View File

@ -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,35 +195,40 @@ mbr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
*secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
}
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));
@ -336,18 +342,17 @@ g_part_mbr_resize(struct g_part_table *basetable,
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;

View File

@ -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"
@ -174,33 +175,38 @@ pc98_set_chs(struct g_part_table *table, uint32_t lba, u_short *cylp,
*secp = sec;
}
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 *basetable,
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,

View File

@ -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"
@ -142,6 +143,23 @@ vtoc8_parse_type(const char *type, uint16_t *tag)
return (EINVAL);
}
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 *basetable, struct g_part_entry *entry,
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 *basetable,
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);