containers / libkrun

A dynamic library providing Virtualization-based process isolation capabilities
Apache License 2.0
807 stars 67 forks source link

virtiofs: fallocate: should enlarge but not shrink the file #203

Closed djs55 closed 2 months ago

djs55 commented 2 months ago

Before this patch on virtiofs we had:

$ fallocate -o 0 -l 1024 f
$ ls -l f
-rw-r--r--    1 root     root          1024 Jun 24 13:00 f
$ fallocate -o 0 -l 512 f
$ ls -l f
-rw-r--r--    1 root     root           512 Jun 24 13:00 f

Unfortunately the second fallocate call caused the file to shrink, due to the call to ftruncate.

On a tmpfs for comparison the file doesn't shrink:

$ fallocate -o 0 -l 1024 f
$ ls -l f
-rw-r--r--    1 root     root          1024 Jun 24 12:55 f
$ fallocate -o 0 -l 512 f
$ ls -l f
-rw-r--r--    1 root     root          1024 Jun 24 12:56 f

This patch only calls ftuncate() if the proposed_length is larger than the current length.