diff options
author | Karel Zak | 2012-12-05 14:32:20 +0100 |
---|---|---|
committer | Karel Zak | 2013-03-11 11:20:40 +0100 |
commit | 9475cc78ff5318fb659a7b90bb181f9c9522de02 (patch) | |
tree | 5a084b4eb71dfd2fad97d51b879fde061d9e0209 /fdisks/fdisk.c | |
parent | libfdisk: add parttype code (diff) | |
download | kernel-qcow2-util-linux-9475cc78ff5318fb659a7b90bb181f9c9522de02.tar.gz kernel-qcow2-util-linux-9475cc78ff5318fb659a7b90bb181f9c9522de02.tar.xz kernel-qcow2-util-linux-9475cc78ff5318fb659a7b90bb181f9c9522de02.zip |
libfdisk: add alignment code
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'fdisks/fdisk.c')
-rw-r--r-- | fdisks/fdisk.c | 91 |
1 files changed, 2 insertions, 89 deletions
diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index dd06b9b22..f34617f08 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -288,86 +288,6 @@ test_c(char **m, char *mesg) { return val; } -static int -lba_is_aligned(struct fdisk_context *cxt, sector_t lba) -{ - unsigned int granularity = max(cxt->phy_sector_size, cxt->min_io_size); - unsigned long long offset; - - if (cxt->grain > granularity) - granularity = cxt->grain; - offset = (lba * cxt->sector_size) & (granularity - 1); - - return !((granularity + cxt->alignment_offset - offset) & (granularity - 1)); -} - -static int -lba_is_phy_aligned(struct fdisk_context *cxt, unsigned long long lba) -{ - unsigned int granularity = max(cxt->phy_sector_size, cxt->min_io_size); - unsigned long long offset = (lba * cxt->sector_size) & (granularity - 1); - - return !((granularity + cxt->alignment_offset - offset) & (granularity - 1)); -} - -sector_t align_lba(struct fdisk_context *cxt, sector_t lba, int direction) -{ - sector_t res; - - if (lba_is_aligned(cxt, lba)) - res = lba; - else { - sector_t sects_in_phy = cxt->grain / cxt->sector_size; - - if (lba < cxt->first_lba) - res = cxt->first_lba; - - else if (direction == ALIGN_UP) - res = ((lba + sects_in_phy) / sects_in_phy) * sects_in_phy; - - else if (direction == ALIGN_DOWN) - res = (lba / sects_in_phy) * sects_in_phy; - - else /* ALIGN_NEAREST */ - res = ((lba + sects_in_phy / 2) / sects_in_phy) * sects_in_phy; - - if (cxt->alignment_offset && !lba_is_aligned(cxt, res) && - res > cxt->alignment_offset / cxt->sector_size) { - /* - * apply alignment_offset - * - * On disk with alignment compensation physical blocks starts - * at LBA < 0 (usually LBA -1). It means we have to move LBA - * according the offset to be on the physical boundary. - */ - /* fprintf(stderr, "LBA: %llu apply alignment_offset\n", res); */ - res -= (max(cxt->phy_sector_size, cxt->min_io_size) - - cxt->alignment_offset) / cxt->sector_size; - - if (direction == ALIGN_UP && res < lba) - res += sects_in_phy; - } - } - - return res; -} - - -sector_t align_lba_in_range(struct fdisk_context *cxt, - sector_t lba, sector_t start, sector_t stop) -{ - start = align_lba(cxt, start, ALIGN_UP); - stop = align_lba(cxt, stop, ALIGN_DOWN); - - lba = align_lba(cxt, lba, ALIGN_NEAREST); - - if (lba < start) - return start; - else if (lba > stop) - return stop; - return lba; -} - int warn_geometry(struct fdisk_context *cxt) { char *m = NULL; @@ -862,13 +782,6 @@ void check_consistency(struct fdisk_context *cxt, struct partition *p, int parti } } -void check_alignment(struct fdisk_context *cxt, sector_t lba, int partition) -{ - if (!lba_is_phy_aligned(cxt, lba)) - printf(_("Partition %i does not start on physical sector boundary.\n"), - partition + 1); -} - static void list_disk_geometry(struct fdisk_context *cxt) { unsigned long long bytes = cxt->total_sectors * cxt->sector_size; @@ -1111,7 +1024,7 @@ static void list_table(struct fdisk_context *cxt, int xtra) /* type id */ p->sys_ind, /* type name */ type ? type->name : _("Unknown")); check_consistency(cxt, p, i); - check_alignment(cxt, get_partition_start(pe), i); + fdisk_warn_alignment(cxt, get_partition_start(pe), i); } } @@ -1146,7 +1059,7 @@ x_list_table(struct fdisk_context *cxt, int extend) { (unsigned long) get_nr_sects(p), p->sys_ind); if (p->sys_ind) { check_consistency(cxt, p, i); - check_alignment(cxt, get_partition_start(pe), i); + fdisk_warn_alignment(cxt, get_partition_start(pe), i); } } } |