• Filipe Manana's avatar
    Btrfs: fix file corruption after snapshotting due to mix of buffered/DIO writes · 099a2655
    Filipe Manana authored
    [ Upstream commit 609e804d ]
    
    When we are mixing buffered writes with direct IO writes against the same
    file and snapshotting is happening concurrently, we can end up with a
    corrupt file content in the snapshot. Example:
    
    1) Inode/file is empty.
    
    2) Snapshotting starts.
    
    2) Buffered write at offset 0 length 256Kb. This updates the i_size of the
       inode to 256Kb, disk_i_size remains zero. This happens after the task
       doing the snapshot flushes all existing delalloc.
    
    3) DIO write at offset 256Kb length 768Kb. Once the ordered extent
       completes it sets the inode's disk_i_size to 1Mb (256Kb + 768Kb) and
       updates the inode item in the fs tree with a size of 1Mb (which is
       the value of disk_i_size).
    
    4) The dealloc for the range [0, 256Kb[ did not start yet.
    
    5) The transaction used in the DIO ordered extent completion, which updated
       the inode item, is committed by the snapshotting task.
    
    6) Snapshot creation completes.
    
    7) Dealloc for the range [0, 256Kb[ is flushed.
    
    After that when reading the file from the snapshot we always get zeroes for
    the range [0, 256Kb[, the file has a size of 1Mb and the data written by
    the direct IO write is found. From an application's point of view this is
    a corruption, since in the source subvolume it could never read a version
    of the file that included the data from the direct IO write without the
    data from the buffered write included as well. In the snapshot's tree,
    file extent items are missing for the range [0, 256Kb[.
    
    The issue, obviously, does not happen when using the -o flushoncommit
    mount option.
    
    Fix this by flushing delalloc for all the roots that are about to be
    snapshotted when committing a transaction. This guarantees total ordering
    when updating the disk_i_size of an inode since the flush for dealloc is
    done when a transaction is in the TRANS_STATE_COMMIT_START state and wait
    is done once no more external writers exist. This is similar to what we
    do when using the flushoncommit mount option, but we do it only if the
    transaction has snapshots to create and only for the roots of the
    subvolumes to be snapshotted. The bulk of the dealloc is flushed in the
    snapshot creation ioctl, so the flush work we do inside the transaction
    is minimized.
    
    This issue, involving buffered and direct IO writes with snapshotting, is
    often triggered by fstest btrfs/078, and got reported by fsck when not
    using the NO_HOLES features, for example:
    
      $ cat results/btrfs/078.full
      (...)
      _check_btrfs_filesystem: filesystem on /dev/sdc is inconsistent
      *** fsck.btrfs output ***
      [1/7] checking root items
      [2/7] checking extents
      [3/7] checking free space cache
      [4/7] checking fs roots
      root 258 inode 264 errors 100, file extent discount
      Found file extent holes:
            start: 524288, len: 65536
      ERROR: errors found in fs roots
    Signed-off-by: 's avatarFilipe Manana <fdmanana@suse.com>
    Signed-off-by: 's avatarDavid Sterba <dsterba@suse.com>
    Signed-off-by: 's avatarSasha Levin (Microsoft) <sashal@kernel.org>
    099a2655
Name
Last commit
Last update
..
tests Loading commit data...
Kconfig Loading commit data...
Makefile Loading commit data...
acl.c Loading commit data...
async-thread.c Loading commit data...
async-thread.h Loading commit data...
backref.c Loading commit data...
backref.h Loading commit data...
btrfs_inode.h Loading commit data...
check-integrity.c Loading commit data...
check-integrity.h Loading commit data...
compression.c Loading commit data...
compression.h Loading commit data...
ctree.c Loading commit data...
ctree.h Loading commit data...
dedupe.h Loading commit data...
delayed-inode.c Loading commit data...
delayed-inode.h Loading commit data...
delayed-ref.c Loading commit data...
delayed-ref.h Loading commit data...
dev-replace.c Loading commit data...
dev-replace.h Loading commit data...
dir-item.c Loading commit data...
disk-io.c Loading commit data...
disk-io.h Loading commit data...
export.c Loading commit data...
export.h Loading commit data...
extent-tree.c Loading commit data...
extent_io.c Loading commit data...
extent_io.h Loading commit data...
extent_map.c Loading commit data...
extent_map.h Loading commit data...
file-item.c Loading commit data...
file.c Loading commit data...
free-space-cache.c Loading commit data...
free-space-cache.h Loading commit data...
free-space-tree.c Loading commit data...
free-space-tree.h Loading commit data...
inode-item.c Loading commit data...
inode-map.c Loading commit data...
inode-map.h Loading commit data...
inode.c Loading commit data...
ioctl.c Loading commit data...
locking.c Loading commit data...
locking.h Loading commit data...
lzo.c Loading commit data...
math.h Loading commit data...
ordered-data.c Loading commit data...
ordered-data.h Loading commit data...
orphan.c Loading commit data...
print-tree.c Loading commit data...
print-tree.h Loading commit data...
props.c Loading commit data...
props.h Loading commit data...
qgroup.c Loading commit data...
qgroup.h Loading commit data...
raid56.c Loading commit data...
raid56.h Loading commit data...
rcu-string.h Loading commit data...
reada.c Loading commit data...
ref-verify.c Loading commit data...
ref-verify.h Loading commit data...
relocation.c Loading commit data...
root-tree.c Loading commit data...
scrub.c Loading commit data...
send.c Loading commit data...
send.h Loading commit data...
struct-funcs.c Loading commit data...
super.c Loading commit data...
sysfs.c Loading commit data...
sysfs.h Loading commit data...
transaction.c Loading commit data...
transaction.h Loading commit data...
tree-checker.c Loading commit data...
tree-checker.h Loading commit data...
tree-defrag.c Loading commit data...
tree-log.c Loading commit data...
tree-log.h Loading commit data...
ulist.c Loading commit data...
ulist.h Loading commit data...
uuid-tree.c Loading commit data...
volumes.c Loading commit data...
volumes.h Loading commit data...
xattr.c Loading commit data...
xattr.h Loading commit data...
zlib.c Loading commit data...
zstd.c Loading commit data...