Optimizing logical sector size for physical sector size 4096 HDD

hard-diskioperformance

With many new hard drive disks the physical sector size is 4096. Would it be possible to make the system use a logical sector size of the same size, rather than the default logical sector size of 512?

Will it speed up bulk reads and writes?
Where can it be configured?

Best Answer

512 byte is not really the default sector size. It depends on your hardware.

You can display what physical/logical sector sizes your disk reports via the /sys pseudo filesystem, for instance:

# cat /sys/block/sda/queue/physical_block_size
4096
# cat /sys/block/sda/queue/logical_block_size
512

What is the difference between those two values?

  • The physical_block_size is the minimal size of a block the drive is able to write in an atomic operation.
  • The logical_block_size is the smallest size the drive is able to write (cf. the linux kernel documentation).

Thus, if you have a 4k drive it makes sense that your storage stack (filesystem etc.) uses something equal or greater than the physical sector size.

Those values are also displayed in recent versions of fdisk, for instance:

# fdisk -l /dev/sda
[..]
Sector size (logical/physical): 512 bytes / 4096 bytes

On current linux distributions, programs (that should care about the optimal sector size) like mkfs.xfs will pick the optimal sector size by default (e.g. 4096 bytes).

But you can also explicitly specify it via an option, for instance:

# mkfs.xfs -f -s size=4096 /dev/sda

Or:

# mkfs.ext4 -F -b 4096 /dev/sda

In any case, most mkfs variants will also display the used block size during execution.

For an existing filesystem the block size can be determined with a command like:

# xfs_info /mnt
[..]
meta-data=                       sectsz=4096
data     =                       bsize=4096
naming   =version 2              bsize=4096
log      =internal               bsize=4096
         =                       sectsz=4096
realtime =none                   extsz=4096

Or:

# tune2fs -l /dev/sda
Block size:               4096
Fragment size:            4096

Or:

# btrfs inspect-internal dump-super /dev/sda | grep size
csum_size             4
sys_array_size        97
sectorsize            4096
nodesize              16384
leafsize              16384
stripesize            4096
dev_item.sector_size  4096

When creating the filesystem on a partition, another thing to check then is if the partition start address is actually aligned to the physical block size. For example, look at the fdisk -l output, convert the start addresses into bytes, divide them by the physical block size - the reminder must be zero if the partitions are aligned.

Related Question