Subject: Re: ls -lsh
To: None <>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-userlevel
Date: 08/05/2004 15:37:52
> That looks confusing to me.  Why '-s' should even be working with
> '-l' is beyond me.

Because the information -s provides is not otherwise available, even
with -l?

> Also, doesnt -s report the number of blocks which are actually
> allocated to the file, or something?

Right, the amount of disk actually used.

> Something isn't right with the -s option.  Assuming an 8k/1k
> filesystem, which my root fs seems to be: [...]

> Taking the size of /netbsd (4741076 bytes (whatever happened to a 2M
> kernel, BTW...)), and rounding up to the next blocksize... [...]

> 4632

> As you can see, ls -s reports 4640 1k blocks, even though the size -
> -- rounded up and fully populated -- is 8 shy of that.

On an 8k/1k filesystem, that is exactly the amount of space used by one
indirect block, and one indrect block is exactly what a file of that
size would need.

Remember: amount of disk actually occupied.  This bears only a
comparatively loose relationship to the file's st_size.  (It doesn't
include the space taken up by the inode itself, though it arguably

As an example in the other direction, a file with holes will show a
much smaller number with -s than would be implied by the same file's -l

[Sparkle] 4> ls -l z
z not found
[Sparkle] 5> touch z
[Sparkle] 6> trunc z 104857600
[Sparkle] 7> ls -l z
-rw-r--r--  1 mouse    104857600 Aug  5 15:41 z
[Sparkle] 8> ls -s z
  24 z
[Sparkle] 9> ls -ls z
  24 -rw-r--r--  1 mouse    104857600 Aug  5 15:41 z
[Sparkle] 10> 

Those 24 sectors (three blocks - it's an 8k/1k filesystem) are for one
double-indirect block, one single-indirect block, and one data block.
(I'm not sure why it allocated a data block and the indirect blocks to
point to it; trunc just calls truncate(2).  It's clearly not necessary;
if I grow it to 1048576000000 and then shrink it back to 104857600, I
end up with a file whose -s size is 0.  But that's irrelevant for our
purposes here.)

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B