Btrfs: fix race between fsync and direct IO writes for prealloc extents
authorFilipe Manana <fdmanana@suse.com>
Mon, 9 May 2016 12:15:27 +0000 (13:15 +0100)
committerFilipe Manana <fdmanana@suse.com>
Fri, 13 May 2016 00:59:32 +0000 (01:59 +0100)
commit0b901916a00bc7b14ee83cc8e41c3b0d561a8f22
tree8e0b34af9beea79a4fdcbd6de58074bfaa557c5c
parent5062af35c3c6e49110ab1ec99295339259298a3d
Btrfs: fix race between fsync and direct IO writes for prealloc extents

When we do a direct IO write against a preallocated extent (fallocate)
that does not go beyond the i_size of the inode, we do the write operation
without holding the inode's i_mutex (an optimization that landed in
commit 38851cc19adb ("Btrfs: implement unlocked dio write")). This allows
for a very tiny time window where a race can happen with a concurrent
fsync using the fast code path, as the direct IO write path creates first
a new extent map (no longer flagged as a prealloc extent) and then it
creates the ordered extent, while the fast fsync path first collects
ordered extents and then it collects extent maps. This allows for the
possibility of the fast fsync path to collect the new extent map without
collecting the new ordered extent, and therefore logging an extent item
based on the extent map without waiting for the ordered extent to be
created and complete. This can result in a situation where after a log
replay we end up with an extent not marked anymore as prealloc but it was
only partially written (or not written at all), exposing random, stale or
garbage data corresponding to the unwritten pages and without any
checksums in the csum tree covering the extent's range.

This is an extension of what was done in commit de0ee0edb21f ("Btrfs: fix
race between fsync and lockless direct IO writes").

So fix this by creating first the ordered extent and then the extent
map, so that this way if the fast fsync patch collects the new extent
map it also collects the corresponding ordered extent.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: Josef Bacik <jbacik@fb.com>
fs/btrfs/inode.c