What is a sparse file and why do we need it?

Say you have a file with many empty bytes \x00. These many empty bytes \x00 are called holes. Storing empty bytes is just not efficient, we know there are many of them in the file, so why store them on the storage device? We could instead store metadata describing those zeros. When a process reads the file those zero byte blocks get generated dynamically as opposed to being stored on physical storage (look at this schematic from Wikipedia):

Sparse File - Wikipedia

This is why a sparse file is efficient, because it does not store the zeros on disk, instead it holds enough data describing the zeros that will be generated.

Note: the logical file size is greater than the physical file size for sparse files. This is because we have not stored the zeros physically on a storage device.


Edit:

When you run:

$ dd if=/dev/zero of=output bs=1G count=4

The command here copies 4G blocks of null bytes to output. To see that:

$ stat output
File: ouput
  Size: 4294967296      Blocks: 8388616    IO Block: 4096   regular file
--omitted--

You can see that this file has 8388616 blocks allocated to it, these blocks store nothing but empty bytes copied from /dev/zero and they do occupy physical disk space, they’re holes stored on disk (sparse zeros). dd did what you asked for, copying blocks of data from one file to another.

Now, run this command to detect the holes and make the file sparse in-place:

$ fallocate -d output
$ stat output
File: swapfile
  Size: 4294967296      Blocks: 0          IO Block: 4096   regular file
--omitted--

Do you notice something? The the number of blocks now is 0 because the blocks that were storing only empty bytes were de-allocated. Remember, output‘s blocks store nothing, only a bunch of empty zeros, fallocate -d detected the blocks that contain only empty zeros and deallocated them, since all the blocks for this file contain zeros, they were all de-allocated.

Also notice how the size remained the same. This is the logical (virtual) size of the file, not its size on disk. It’s crucial to know that output doesn’t occupy physical storage space now, it has 0 blocks allocated to it and thus I doesn’t really use disk space. The size preserved after running fallocate -d so when you later read from the file, you get the empty bytes generated to you by the filesystem at runtime. The physical size of output however, is zero, it uses no data blocks.

Remember, when you read output file the empty bytes are generated by the filesystem at runtime dynamically, they’re not really physically stored on disk, and the file’s size as reported by stat is the logical size, and the physical size is zero for output. In this case the filesystem has to generate 4G of empty bytes when a process reads the file.

To generate a sparse file using dd:

$ dd if=/dev/zero of=output2 bs=1G seek=0 count=0
$ stat 
stat output2
  File: output2
  Size: 4294967296      Blocks: 0          IO Block: 4096   regular file

GNU dd internally uses lseek and ftruncate, so check truncate(2) and lseek(2).

Leave a Comment