Skip to content

Commit d501473

Browse files
osandovkdave
authored andcommitted
Btrfs: fix error handling in btrfs_truncate()
Jun Wu at Facebook reported that an internal service was seeing a return value of 1 from ftruncate() on Btrfs in some cases. This is coming from the NEED_TRUNCATE_BLOCK return value from btrfs_truncate_inode_items(). btrfs_truncate() uses two variables for error handling, ret and err. When btrfs_truncate_inode_items() returns non-zero, we set err to the return value. However, NEED_TRUNCATE_BLOCK is not an error. Make sure we only set err if ret is an error (i.e., negative). To reproduce the issue: mount a filesystem with -o compress-force=zstd and the following program will encounter return value of 1 from ftruncate: int main(void) { char buf[256] = { 0 }; int ret; int fd; fd = open("test", O_CREAT | O_WRONLY | O_TRUNC, 0666); if (fd == -1) { perror("open"); return EXIT_FAILURE; } if (write(fd, buf, sizeof(buf)) != sizeof(buf)) { perror("write"); close(fd); return EXIT_FAILURE; } if (fsync(fd) == -1) { perror("fsync"); close(fd); return EXIT_FAILURE; } ret = ftruncate(fd, 128); if (ret) { printf("ftruncate() returned %d\n", ret); close(fd); return EXIT_FAILURE; } close(fd); return EXIT_SUCCESS; } Fixes: ddfae63 ("btrfs: move btrfs_truncate_block out of trans handle") CC: [email protected] # 4.15+ Reported-by: Jun Wu <[email protected]> Signed-off-by: Omar Sandoval <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 02ee654 commit d501473

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

fs/btrfs/inode.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9124,7 +9124,8 @@ static int btrfs_truncate(struct inode *inode, bool skip_writeback)
91249124
BTRFS_EXTENT_DATA_KEY);
91259125
trans->block_rsv = &fs_info->trans_block_rsv;
91269126
if (ret != -ENOSPC && ret != -EAGAIN) {
9127-
err = ret;
9127+
if (ret < 0)
9128+
err = ret;
91289129
break;
91299130
}
91309131

0 commit comments

Comments
 (0)