add patch add-ext4_should_use_dax
[ext4-patch-queue.git] / fix-fallocate-and-delalloc-i_size-interaction
blobe132e28b2c6729e4a78b06893a3ac8fe58cbef99
1 ext4: fix interaction between i_size, fallocate, and delalloc after a crash
3 If there are pending writes subject to delayed allocation, then i_size
4 will show size after the writes have completed, while i_disksize
5 contains the value of i_size on the disk (since the writes have not
6 been persisted to disk).
8 If fallocate(2) is called with the FALLOC_FL_KEEP_SIZE flag, either
9 with or without the FALLOC_FL_ZERO_RANGE flag set, and the new size
10 after the fallocate(2) is between i_size and i_disksize, then after a
11 crash, if a journal commit has resulted in the changes made by the
12 fallocate() call to be persisted after a crash, but the delayed
13 allocation write has not resolved itself, i_size would not be updated,
14 and this would cause the following e2fsck complaint:
16 Inode 12, end of extent exceeds allowed value
17         (logical block 33, physical block 33441, len 7)
19 This can only take place on a sparse file, where the fallocate(2) call
20 is allocating blocks in a range which is before a pending delayed
21 allocation write which is extending i_size.  Since this situation is
22 quite rare, and the window in which the crash must take place is
23 typically < 30 seconds, in practice this condition will rarely happen.
25 Nevertheless, it can be triggered in testing, and in particular by
26 xfstests generic/456.
28 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
29 Reported-by: Amir Goldstein <amir73il@gmail.com>
30 Cc: stable@vger.kernel.org
31 ---
32  fs/ext4/extents.c | 6 ++++--
33  1 file changed, 4 insertions(+), 2 deletions(-)
35 diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
36 index 97f0fd06728d..07bca11749d4 100644
37 --- a/fs/ext4/extents.c
38 +++ b/fs/ext4/extents.c
39 @@ -4794,7 +4794,8 @@ static long ext4_zero_range(struct file *file, loff_t offset,
40         }
42         if (!(mode & FALLOC_FL_KEEP_SIZE) &&
43 -            offset + len > i_size_read(inode)) {
44 +           (offset + len > i_size_read(inode) ||
45 +            offset + len > EXT4_I(inode)->i_disksize)) {
46                 new_size = offset + len;
47                 ret = inode_newsize_ok(inode, new_size);
48                 if (ret)
49 @@ -4965,7 +4966,8 @@ long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
50         }
52         if (!(mode & FALLOC_FL_KEEP_SIZE) &&
53 -            offset + len > i_size_read(inode)) {
54 +           (offset + len > i_size_read(inode) ||
55 +            offset + len > EXT4_I(inode)->i_disksize)) {
56                 new_size = offset + len;
57                 ret = inode_newsize_ok(inode, new_size);
58                 if (ret)