0

With reference to the Advanced Programming in the Unix Environment the file holes are created when we try to seek past the end of the file and write something there.For example,

    int x = lseek(fd,1639,SEEK_END);
    int y = write(fd,buff,100);

If you consider the above example and suppose that the file previously had some contents, so that the current file offset is somewhere ahead of the beginning of the file. Now, we seek to the end of the file using the lseek function (as in the example above), and write a buffer buff of size 100 to the file. As you know, a hole will be created in the file.

So, can Unix allocate this hole to some other file? Or, in other words, is this hole available for allocation?

  • 1
    You can't allocate a hole. But because the hole hasn't taken any disk space, that potential disk space is still available for other files. – roaima Aug 25 '17 at 18:26
3

First of all, what is a hole in a file ? It's just data set to zero without being explicitly written as such. When you seek 1639 bytes past the end of your file and writes 100 bytes in it, your file has actually grown by 1639+100 bytes. So, you have created a hole but that hole is actually filled with zeros.

If that hole is big enough, certain file systems (like ext) allow you to spare that space by not allocating the corresponding blocks on the disk. I.e. if your seek is going to write one or more blocks of zeros, the blocks are not allocated on disk and this will create true holes in the list of blocks that contain the file data. That's what we call a sparse file.

The size of a block depends on the way your FS is formatted. Nowadays with ext it's usually 4096 bytes. That means that with your 1639 byte seek, your are not creating a sparse file and 1639 zeros will just be written on disk. OTOH, with 4096 byte blocks, if you seek at least 8191 (2 * 4096 - 1) bytes, you can be certain to create at least one.

Since the blocks are not actually allocated and filled with zeros, that makes your file use less disk space and your write operation be much faster. Of course, to answer your question, that spared disk space is available for other files.

Actually, the best demonstration does not necessitate a C program:

$ df -h /tmp
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda4        38G   28G  7.5G  80% /

$ time dd of=/tmp/foo bs=1M seek=$((1024*1024*10)) count=0
0+0 records in
0+0 records out
0 bytes (0 B) copied, 4.741e-05 s, 0.0 kB/s

real    0m0.002s
user    0m0.000s
sys 0m0.000s

$ ls -lh /tmp/foo
-rw-r--r-- 1 xhienne xhienne 10T Aug 25 20:08 /tmp/foo

$ df -h /tmp
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda4        38G   28G  7.5G  80% /

Here I have created a 10 TB file (full of zeros) in a couple of milliseconds on a partition that has only 7+ GB of free space (which is still available).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.