Skip to content

Commit ddbd2fd

Browse files
djs55slp
authored andcommitted
virtiofs: fallocate: should enlarge but not shrink the file
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. Signed-off-by: David Scott <[email protected]>
1 parent c4c4274 commit ddbd2fd

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/devices/src/virtio/fs/macos/passthrough.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,11 +1731,12 @@ impl FileSystem for PassthroughFs {
17311731

17321732
let fd = data.file.write().unwrap().as_raw_fd();
17331733

1734+
let proposed_length = (offset + length) as i64;
17341735
let mut fs = libc::fstore_t {
17351736
fst_flags: libc::F_ALLOCATECONTIG,
17361737
fst_posmode: libc::F_PEOFPOSMODE,
17371738
fst_offset: 0,
1738-
fst_length: (offset + length) as i64,
1739+
fst_length: proposed_length,
17391740
fst_bytesalloc: 0,
17401741
};
17411742

@@ -1748,7 +1749,12 @@ impl FileSystem for PassthroughFs {
17481749
}
17491750
}
17501751

1751-
let res = unsafe { libc::ftruncate(fd, (offset + length) as i64) };
1752+
let st = fstat(fd, true)?;
1753+
if st.st_size >= proposed_length {
1754+
// fallocate should not shrink the file. The file is already larger than needed.
1755+
return Ok(());
1756+
}
1757+
let res = unsafe { libc::ftruncate(fd, proposed_length) };
17521758

17531759
if res == 0 {
17541760
Ok(())

0 commit comments

Comments
 (0)