Re: st_blocks - was: Re: crazy swapping

Linus Torvalds (torvalds@transmeta.com)
Thu, 17 Dec 1998 00:25:19 -0800 (PST)


On Fri, 4 Dec 1998 Andries.Brouwer@cwi.nl wrote:
>
> if (st.st_blocks * 512 < st.st_size) {
> fprintf(stderr, "File %s appears to have holes in it");
>
> This is interesting.
> So far, the unit used in st_blocks has been undocumented.

It's not undocumented, it's just really really badly documented.

As far as I can tell, st_blocks is _always_ given in 512-byte entities,
for completely historical reasons, and has nothing to do with Linux other
than the fact that Linux tries to look like UNIX.

I assume (ie just rambling here) that it is just because historically UNIX
BLOCK_SIZE used to be designed to 512 at some time, and that st_blocks was
added first - so it just meant how many of these (hardcoded) 512-byte
blocks the file contained.

When st_blocksize was added, st_blocks had to remain in multiples of 512
bytes for backwards compatibility (otherwise "du" etc would be confused).

So st_blocksize means the "optimal minimum size for reading and writing"
(especially the latter - writing chunks less than st_blocksize is likely
to be a read-modify-write cycle when re-writing a file) and st_blocks is
"how many 512-byte blocks it takes on disk".

I suspect that you can get a good idea of how true this is of all unixes
by looking at the sources of GNU "du". I wouldn't be in the least
surprised if you find a few #ifdef's there for different UNIXes that do
this differently. But basically 512 is just a magic number.

Linus

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/