libfdisk: support alignment to non power of 2
Let's create a disk with
33553920 bytes optimal I/O size:
# modprobe scsi_debug dev_size_mb=1000 opt_blks=65535
and try to create partition:
echo -e 'n\n\n\n\n+512M\np\nq\n' | fdisk /dev/sdc
old version:
Device Boot Start End Sectors Size Id Type
/dev/sdc1 65535
1114110 1048576 512M 83 Linux
The next partition will be expected on sector
1114110 + 1, but it's
not aligned to optimal I/O:
((
1114110 + 1) * 512) %
33553920 = 8192
fixed version:
Device Boot Start End Sectors Size Id Type
/dev/sdc1 65535
1114094 1048560 512M 83 Linux
((
1114094 + 1) * 512) %
33553920 = 0
Note that the same problem with alignment calculation has been fixed
in Linux kernel by commit
b8839b8c55f3fdd60dc36abcda7e0266aff7985c
(Oct 2014).
The patch also improves fdisk_align_lba_in_range() to not align sizes
smaller than grain (default 1MiB) to make it possible to create really
small partitions.
Reported-by: Tom Yan <tom.ty89@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>