-1

stat -c '%B' * reports the same number 512 for each file. What does %B mean? I don't understand the manual:

%B - The size in bytes of each block reported by ‘%b’

Is it related to my block size:

$ sudo blockdev --getbsz /dev/sda1
4096

Thanks.

ilkkachu
  • 138,973
Tim
  • 101,790

2 Answers2

2

There are two allocation sizes that matter when looking at a file system: the filesystem's block size and the disk's sector size. Most SATA drives have 512-byte sectors, although there are exceptions: drives with 4KB sectors are not uncommon. You can get this information with hdparm:

# hdparm -I /dev/sdb|grep Sector
        Logical/Physical Sector size:           512 bytes

When mapped to filesystem blocks, however, 512 bytes imposes a fair amount of overhead when naming blocks if the drive is large enough (the metadata required to store allocation information is space that can't be used to store data.) Generally, the block size is determined automatically by the format program, but you can usually override it. For example, the method used for ext3/4 is as follows:

-T usage-type[,...]

Specify how the filesystem is going to be used, so that mke2fs can choose optimal filesystem parameters for that use. The usage types that are supported are defined in the configuration file /etc/mke2fs.conf(5). The user may specify one or more usage types using a comma separated list.

If this option is is not specified, mke2fs will pick a single default usage type based on the size of the filesystem to be created. If the filesystem size is less than or equal to 3 megabytes, mke2fs will use the filesystem type floppy. If the filesystem size is greater than 3 but less than or equal to 512 megabytes, mke2fs(8) will use the filesystem small. Otherwise, mke2fs(8) will use the default filesystem type default.

The defaults indicate that 4KB blocks are the default, as my /etc/mke2fs.conf shows:

[defaults]
        base_features = sparse_super,large_file,filetype,resize_inode,dir_index,ext_attr
        default_mntopts = acl,user_xattr
        enable_periodic_fsck = 0
        blocksize = 4096
        inode_size = 256
        inode_ratio = 16384

So, %B is showing you the size of the physical sectors, which when multiplied by the number of sectors used %b gives you the total number of bytes allocated on disk. It's usually of no relevance at all, as no filesystem allocation will be smaller than the block size, but it does help you determine that all allocations of physical sectors will be multiples of 8 (4KB/512 bytes=8):

# stat test.img
  File: test.img
  Size: 536870912       Blocks: 1048584    IO Block: 4096   regular file

So, with this information, you can determine the number of bytes allocated on-disk and unused allocated space for this file:

Number of blocks * block size = 1048584 blocks * 512 bytes/block = 536875008 bytes.

Amount of unused allocated space: 536875008-536870912 bytes = 4096 bytes unused.

ErikF
  • 4,042
0

If the stat command behaves as documented in the man page, %B would return the value of DEV_BSIZE from the include file param.h.

This value is 512 on most systems, but 1024 on HP-UX.

BTW: This value is not related to the sector size of the drives.

schily
  • 19,173