How is it possible 8 blocks get allocated but file size 0?
This happens if the file system is encrypted; the FS needs to store extra metadata for the file, even if it is empty.
As I happen to have a machine handy with a vanilla ecryptfs mount (Ubuntu 12.04-LTS), I can confirm that an empty file will get 8 blocks:
$ touch test
$ ls -ls test
8 -rw-rw-r-- 1 admin admin 0 feb 27 16:45 test
You can get a zero-size file with blocks if you have extended attributes on the file, more than what can fit inside the inode itself:
$ touch abc
$ setfattr -n user.test -v xyz abc # this doesn't do it
$ ls -s abc # since the data fits in the inode
0 abc
$ setfattr -n user.test -v "$(printf %100s " ")" abc
$ ls -s abc
4 abc
But, I can't see how you'd get 8 kB that way, as according to the xattr
man page, the size is limited to the block size on ext2/3/4, and the block size is limited by the system page size, so 4 kB on x86. Also, a newly created file shouldn't have any extended attributes, unless you're running SELinux, but in that case, ls -l
should show the dot at the end of the permission bits to indicate the presence of an SELinux tag.