I always assumed the st_blocks field returned by stat()/lstat()... system calls and which du uses to get the disk usage of files was expressed in 512 bytes units.
Checking the POSIX specification, I now see POSIX makes no guarantee for that. The perl documentation for its own stat() function also warns against making that assumption.
In any case, as indicated by POSIX, that block size is not related to the st_blksize field returned by stat(), so has to be found elsewhere.
Checking the GNU du or GNU find source code, I see evidence HP/UX uses 1024 bytes units instead. GNU find adjusts its -printf %b output to always give a number of 512-byte units which is probably the source of my confusion.
Is there any other Unix-like system, still currently in use where st_blocks is not expressed in 512 byte units? Can that be filesystem dependant (as POSIX suggests)? I guess mounting an HP/UX NFS share could do it.
S_BLKSIZEmacro to determine the st_blocks size. Do you know more about the history of thestat()API? It seems to mest_blksizeandst_blocksshould have come in pair, and possibly they got misaligned at some point for historical reason? – Stéphane Chazelas Sep 18 '21 at 08:26st_blksizecame first around BSD 4.1 in 1983, and st_blocks in BSD 4.2. They may not have been meant to be related. – Stéphane Chazelas Sep 18 '21 at 11:37S_BLKSIZEis Linux only and you are right,st_blksizeis a result from developingFFSwhilest_blockswas added later to get betterduresults for files with indirect blocks and for holey files. – schily Sep 18 '21 at 12:21$ grep -Irl S_BLKSIZE /usr/include/at least on Ubuntu 20.04, I get this file:/usr/include/x86_64-linux-gnu/sys/stat.h. EDIT: OK, alright – it's the GNU libc, not Linux kernel stictly speaking. – saulius2 Aug 27 '23 at 17:01