Skip to content
Advertisement

Physical disk block size on POSIX using C/C++

I’m working on a high performance I/O program and I’m trying to find the best way to determine the _physical_ (and not the _logical_) byte size of a device’s disk blocks with C++. My research so far has led me to the following code snippet:

#include <iostream>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char ** argv)
{
    // file information including block size of the device
    struct stat info;
    // device to get block size from
    char * device = "/mnt/hdb1";

    if (stat(device, &info))
    {
        printf("stat() error");
        strerror(errno);
        exit(1);
    }
    printf("Prefered block size for '%s' is %i byten", device, info.st_blksize);
    return 0;
}

The man pages says the following about st_blksize:

The st_blksize field gives the “preferred” blocksize for efficient file system I/O. (Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite.)

, but it does not mention if st_blksize is the logical or the physical disk block size.

So, is st_blksize the physical disk block size, and if so, then is this the most POSIX OS portable way of detecting the physical disk block size.

Advertisement

Answer

I wrote an answer, that while hopeful did not work correctly for block devices.

There is no POSIX mechanism for obtaining the fundamental physical block size of a device, you will have to resort to ioctl, which is platform dependent.

For linux there’s ioctl(fd, BLKPBSZGET, &block_size)

For Solaris there’s the dkio interface, which allows you to get the physical block size.

dk_minfo_ext media_info;
if (-1 != ioctl(fd, DKIOMEDIAINFOEXT, &media_info))
    block_size = media_info.dki_pbsize;

For Mac OS X, it’s ioctl(fd, DKIOCGETPHYSICALBLOCKSIZE, &block_size).

For FreeBSD, it should be iotcl(fd, DIOCGSECTORSIZE, &block_size).

Advertisement