Skip to contentSkip to conversation

Btrfs: fix lost i_size update after cloning inline extent

PATCH In review [email protected] 2 participants 3 messages Apr 4, 2020 – Apr 6, 2020 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: fix lost i_size update after cloning inline extent

From: Filipe Manana <[email protected]>

When not using the NO_HOLES feature we were not marking the destination's file range as written after cloning an inline extent into it. This can lead to a data loss if the current destination file size is smaller than the source file's size.

Example:

  $ mkfs.btrfs -f -O ^no-holes /dev/sdc
  $ mount /mnt/sdc /mnt
  $ echo "hello world" > /mnt/foo
  $ cp --reflink=always /mnt/foo /mnt/bar
  $ rm -f /mnt/foo
  $ umount /mnt
  $ mount /mnt/sdc /mnt
  $ cat /mnt/bar
  $
  $ stat -c %s /mnt/bar
  0

# -> the file is empty, since we deleted foo, the data lost is forever

Fix that by calling btrfs_inode_set_file_extent_range() after cloning an inline extent.

A test case for fstests will follow soon.

Fixes 9ddc959e802bf ("btrfs: use the file extent tree infrastructure")
---
 fs/btrfs/reflink.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/reflink.c +1 -0
index d1973141d3bb..040009d1cc31 100644
@@ -264,6 +264,7 @@ static int clone_copy_inline_extent(struct inode *dst,
 			    size);
 	inode_add_bytes(dst, datal);
 	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(dst)->runtime_flags);
+	ret = btrfs_inode_set_file_extent_range(BTRFS_I(dst), 0, aligned_end);
 out:
 	if (!ret && !trans) {
 		/*
Signature · 1 line
2.11.0
author Tested-by Johannes Hirte on v1 1/1
#1 lore raw
Quoted 26 lines · On Sat, Apr 4, 2020 at 9:21 PM <[email protected]> wrote:

From: Filipe Manana <[email protected]>

When not using the NO_HOLES feature we were not marking the destination's file range as written after cloning an inline extent into it. This can lead to a data loss if the current destination file size is smaller than the source file's size.

Example:

  $ mkfs.btrfs -f -O ^no-holes /dev/sdc
  $ mount /mnt/sdc /mnt
  $ echo "hello world" > /mnt/foo
  $ cp --reflink=always /mnt/foo /mnt/bar
  $ rm -f /mnt/foo
  $ umount /mnt
  $ mount /mnt/sdc /mnt
  $ cat /mnt/bar
  $
  $ stat -c %s /mnt/bar
  0

# -> the file is empty, since we deleted foo, the data lost is forever

Quoted 7 lines

Fix that by calling btrfs_inode_set_file_extent_range() after cloning an inline extent.

A test case for fstests will follow soon.

Fixes 9ddc959e802bf ("btrfs: use the file extent tree infrastructure")
Quoted 19 lines
---
 fs/btrfs/reflink.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/reflink.c +1 -1
index d1973141d3bb..040009d1cc31 100644
@@ -264,6 +264,7 @@ static int clone_copy_inline_extent(struct inode *dst,
                            size);
        inode_add_bytes(dst, datal);
        set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(dst)->runtime_flags);
+       ret = btrfs_inode_set_file_extent_range(BTRFS_I(dst), 0, aligned_end);
 out:
        if (!ret && !trans) {
                /*
--

2.11.0

#2 lore raw
Quoted 30 lines · Filipe Manana
Quoted 29 lines · On Sat, Apr 4, 2020 at 9:21 PM <[email protected]> wrote:

From: Filipe Manana <[email protected]>

When not using the NO_HOLES feature we were not marking the destination's file range as written after cloning an inline extent into it. This can lead to a data loss if the current destination file size is smaller than the source file's size.

Example:

  $ mkfs.btrfs -f -O ^no-holes /dev/sdc
  $ mount /mnt/sdc /mnt
  $ echo "hello world" > /mnt/foo
  $ cp --reflink=always /mnt/foo /mnt/bar
  $ rm -f /mnt/foo
  $ umount /mnt
  $ mount /mnt/sdc /mnt
  $ cat /mnt/bar
  $
  $ stat -c %s /mnt/bar
  0

# -> the file is empty, since we deleted foo, the data lost is forever

Fix that by calling btrfs_inode_set_file_extent_range() after cloning an inline extent.

Quoted 8 lines
Quoted 4 lines

A test case for fstests will follow soon.

Fixes 9ddc959e802bf ("btrfs: use the file extent tree infrastructure")

Thanks, added to misc-next.