]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/4.16.4/ext4-protect-i_disksize-update-by-i_data_sem-in-direct-write-path.patch
Fixes for 4.19
[thirdparty/kernel/stable-queue.git] / releases / 4.16.4 / ext4-protect-i_disksize-update-by-i_data_sem-in-direct-write-path.patch
1 From 73fdad00b208b139cf43f3163fbc0f67e4c6047c Mon Sep 17 00:00:00 2001
2 From: Eryu Guan <guaneryu@gmail.com>
3 Date: Thu, 22 Mar 2018 11:41:25 -0400
4 Subject: ext4: protect i_disksize update by i_data_sem in direct write path
5
6 From: Eryu Guan <guaneryu@gmail.com>
7
8 commit 73fdad00b208b139cf43f3163fbc0f67e4c6047c upstream.
9
10 i_disksize update should be protected by i_data_sem, by either taking
11 the lock explicitly or by using ext4_update_i_disksize() helper. But the
12 i_disksize updates in ext4_direct_IO_write() are not protected at all,
13 which may be racing with i_disksize updates in writeback path in
14 delalloc buffer write path.
15
16 This is found by code inspection, and I didn't hit any i_disksize
17 corruption due to this bug. Thanks to Jan Kara for catching this bug and
18 suggesting the fix!
19
20 Reported-by: Jan Kara <jack@suse.cz>
21 Suggested-by: Jan Kara <jack@suse.cz>
22 Signed-off-by: Eryu Guan <guaneryu@gmail.com>
23 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
24 Cc: stable@vger.kernel.org
25 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
26
27 ---
28 fs/ext4/inode.c | 5 ++---
29 1 file changed, 2 insertions(+), 3 deletions(-)
30
31 --- a/fs/ext4/inode.c
32 +++ b/fs/ext4/inode.c
33 @@ -3658,7 +3658,6 @@ static ssize_t ext4_direct_IO_write(stru
34 {
35 struct file *file = iocb->ki_filp;
36 struct inode *inode = file->f_mapping->host;
37 - struct ext4_inode_info *ei = EXT4_I(inode);
38 ssize_t ret;
39 loff_t offset = iocb->ki_pos;
40 size_t count = iov_iter_count(iter);
41 @@ -3682,7 +3681,7 @@ static ssize_t ext4_direct_IO_write(stru
42 goto out;
43 }
44 orphan = 1;
45 - ei->i_disksize = inode->i_size;
46 + ext4_update_i_disksize(inode, inode->i_size);
47 ext4_journal_stop(handle);
48 }
49
50 @@ -3790,7 +3789,7 @@ static ssize_t ext4_direct_IO_write(stru
51 if (ret > 0) {
52 loff_t end = offset + ret;
53 if (end > inode->i_size) {
54 - ei->i_disksize = end;
55 + ext4_update_i_disksize(inode, end);
56 i_size_write(inode, end);
57 /*
58 * We're going to return a positive `ret'