Hello,
As per the stackoverflow answer (
https://stackoverflow.com/a/11779492), `truncate` doesn't write anything to file but it pretends(block allocation doesn't happen) to write nulls (/dev/zero) in case if file is extended. As oppose to this, `fallocate` actually allocates the space for file.
To understand the depth of the fallocate() ffs project, I tried following on Linux:
Initial disk:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 9.8G 1004M 8.3G 11% /
fallocate:
$ touch bar
$ fallocate -l 5G bar
$ ls -lh
-rw-r--r-- 1 hrishikesh.c Tech 5.0G Nov 17 22:45 bar
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 9.8G 6.0G 3.3G 65% /
I see that 5G of space is allocated to file bar(as Avail space is reduced from 8.3G to 3.3G).
truncate:
$ touch baz
$ truncate -s 3G baz
$ ls -lh
total 5.1G
-rw-r--r-- 1 hrishikesh.c Tech 5.0G Nov 17 22:45 bar
-rw-r--r-- 1 hrishikesh.c Tech 3.0G Nov 17 22:49 baz
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 9.8G 6.0G 3.3G 65% /
But in case of `truncate` disk available space is not reduced.
Questions:
1. As what I follow from the above stackoverflow answer and truncate man page, even though `truncate` doesn't allocate space for file baz but filesystem should still update the free space by reducing it to 0.3G(otherwise filesystem metadata are not consistent with file metadata). Could anyone please correct me?
2. Does it mean that `truncate` only updates file vnode (i.e. size) attribute and doesn't update super block (free_space) attribute?
3. I checked first 100 bytes in both above files using c lang fread() function, all are filled with NULL character ( '\0' ), how file bar (previously fallocate'ed file) got initialised with NULLs(as per my understanding since they are uninitialised, they should be some random bytes.. and not all nulls right?).
Could anyone please help me by answering above questions.
Thanks in advance.
Regards,
Hrishikesh