linux/fs/ext4
Lukas Czerner 372a03e018 ext4: fix data corruption caused by unaligned direct AIO
Ext4 needs to serialize unaligned direct AIO because the zeroing of
partial blocks of two competing unaligned AIOs can result in data
corruption.

However it decides not to serialize if the potentially unaligned aio is
past i_size with the rationale that no pending writes are possible past
i_size. Unfortunately if the i_size is not block aligned and the second
unaligned write lands past i_size, but still into the same block, it has
the potential of corrupting the previous unaligned write to the same
block.

This is (very simplified) reproducer from Frank

    // 41472 = (10 * 4096) + 512
    // 37376 = 41472 - 4096

    ftruncate(fd, 41472);
    io_prep_pwrite(iocbs[0], fd, buf[0], 4096, 37376);
    io_prep_pwrite(iocbs[1], fd, buf[1], 4096, 41472);

    io_submit(io_ctx, 1, &iocbs[1]);
    io_submit(io_ctx, 1, &iocbs[2]);

    io_getevents(io_ctx, 2, 2, events, NULL);

Without this patch the 512B range from 40960 up to the start of the
second unaligned write (41472) is going to be zeroed overwriting the data
written by the first write. This is a data corruption.

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
*
00009200  30 30 30 30 30 30 30 30  30 30 30 30 30 30 30 30
*
0000a000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
*
0000a200  31 31 31 31 31 31 31 31  31 31 31 31 31 31 31 31

With this patch the data corruption is avoided because we will recognize
the unaligned_aio and wait for the unwritten extent conversion.

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
*
00009200  30 30 30 30 30 30 30 30  30 30 30 30 30 30 30 30
*
0000a200  31 31 31 31 31 31 31 31  31 31 31 31 31 31 31 31
*
0000b200

Reported-by: Frank Sorenson <fsorenso@redhat.com>
Signed-off-by: Lukas Czerner <lczerner@redhat.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Fixes: e9e3bcecf4 ("ext4: serialize unaligned asynchronous DIO")
Cc: stable@vger.kernel.org
2019-03-14 23:20:25 -04:00
..
acl.c ext4: compare old and new mode before setting update_mode flag 2018-12-10 00:22:38 -05:00
acl.h ext4: fix up remaining files with SPDX cleanups 2017-12-17 22:00:59 -05:00
balloc.c ext4: use ext4_warning() for sb_getblk failure 2018-08-01 12:02:31 -04:00
bitmap.c License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
block_validity.c ext4: use 'sbi' instead of 'EXT4_SB(sb)' 2018-01-11 13:17:49 -05:00
dir.c ext4: avoid divide by zero fault when deleting corrupted inline directories 2018-08-27 09:22:45 -04:00
ext4_extents.h ext4: adjust reserved cluster count when removing extents 2018-10-01 14:25:08 -04:00
ext4_jbd2.c ext4: shutdown should not prevent get_write_access 2018-02-18 22:07:36 -05:00
ext4_jbd2.h ext4: fix NULL pointer dereference while journal is aborted 2019-03-14 23:19:22 -04:00
ext4.h ext4: don't update s_rev_level if not required 2019-02-14 17:52:18 -05:00
extents_status.c ext4: fix reserved cluster accounting at page invalidation time 2018-10-01 14:33:24 -04:00
extents_status.h ext4: reduce reserved cluster count by number of allocated clusters 2018-10-01 14:24:08 -04:00
extents.c ext4: fix bigalloc cluster freeing when hole punching under load 2019-02-28 23:34:11 -05:00
file.c ext4: fix data corruption caused by unaligned direct AIO 2019-03-14 23:20:25 -04:00
fsmap.c ext4: make function ‘ext4_getfsmap_find_fixed_metadata’ static 2018-05-10 11:50:04 -04:00
fsmap.h ext4: fix up remaining files with SPDX cleanups 2017-12-17 22:00:59 -05:00
fsync.c Revert "ext4: use ext4_write_inode() when fsyncing w/o a journal" 2019-01-31 23:41:11 -05:00
hash.c ext4: annotate implicit fall throughs 2019-02-21 10:49:53 -05:00
ialloc.c ext4: avoid declaring fs inconsistent due to invalid file handles 2018-12-19 12:29:13 -05:00
indirect.c ext4: annotate more implicit fall throughs 2019-02-21 10:51:27 -05:00
inline.c ext4: fix a potential fiemap/page fault deadlock w/ inline_data 2018-12-25 00:56:33 -05:00
inode.c ext4: fix compile error when using BUFFER_TRACE 2019-02-21 11:29:10 -05:00
ioctl.c ext4: disallow files with EXT4_JOURNAL_DATA_FL from EXT4_IOC_SWAP_BOOT 2019-02-11 01:07:10 -05:00
Kconfig ext4: Change debugging support help prefix from EXT4 to Ext4 2019-02-21 11:37:28 -05:00
Makefile License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
mballoc.c ext4: replace opencoded i_writecount usage with inode_is_open_for_write() 2019-02-10 23:04:16 -05:00
mballoc.h ext4: fix up remaining files with SPDX cleanups 2017-12-17 22:00:59 -05:00
migrate.c ext4: clean up indentation issues, remove extraneous tabs 2018-12-04 00:16:44 -05:00
mmp.c ext4: don't mark mmp buffer head dirty 2018-09-15 17:11:25 -04:00
move_extent.c ext4: fix argument checking in EXT4_IOC_MOVE_EXT 2018-10-02 01:34:44 -04:00
namei.c ext4: avoid declaring fs inconsistent due to invalid file handles 2018-12-19 12:29:13 -05:00
page-io.c ext4: cleanup clean_bdev_aliases() calls 2019-02-10 23:32:07 -05:00
readpage.c fs: don't open code lru_to_page() 2019-01-04 13:13:48 -08:00
resize.c ext4: fix crash during online resizing 2019-02-11 13:30:32 -05:00
super.c ext4: don't update s_rev_level if not required 2019-02-14 17:52:18 -05:00
symlink.c ext4: switch to fscrypt_get_symlink() 2018-01-11 22:10:40 -05:00
sysfs.c ext4: add sysfs attr /sys/fs/ext4/<disk>/journal_task 2019-02-21 11:49:27 -05:00
truncate.h ext4: handle layout changes to pinned DAX mappings 2018-07-29 17:00:22 -04:00
xattr_security.c ext4: use XATTR_CREATE in ext4_initxattrs() 2018-05-10 11:52:14 -04:00
xattr_trusted.c License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
xattr_user.c License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
xattr.c ext4: fix some error pointer dereferences 2019-02-21 11:17:34 -05:00
xattr.h ext4: add extra checks to ext4_xattr_block_get() 2018-03-30 20:04:11 -04:00