add patch move-call-to-ext4_error-into-ext4_xattr_check_block
[ext4-patch-queue.git] / protect_disksize_update_in_direct_write_path
blob6814bfcd506f691209122b68cc74000a269c77f0
1 ext4: protect i_disksize update by i_data_sem in direct write path
3 From: Eryu Guan <guaneryu@gmail.com>
5 i_disksize update should be protected by i_data_sem, by either taking
6 the lock explicitly or by using ext4_update_i_disksize() helper. But the
7 i_disksize updates in ext4_direct_IO_write() are not protected at all,
8 which may be racing with i_disksize updates in writeback path in
9 delalloc buffer write path.
11 This is found by code inspection, and I didn't hit any i_disksize
12 corruption due to this bug. Thanks to Jan Kara for catching this bug and
13 suggesting the fix!
15 Reported-by: Jan Kara <jack@suse.cz>
16 Suggested-by: Jan Kara <jack@suse.cz>
17 Signed-off-by: Eryu Guan <guaneryu@gmail.com>
18 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
19 Cc: stable@vger.kernel.org
20 ---
22 There's no v1 version of this patch.
24 It's a bit unfortunate that I have to remove the "ei" definition in this
25 patch and reintroduce it in patch 2/2, otherwise I got transient
26 "defined but not used" build warning with only this patch applied.
28  fs/ext4/inode.c | 5 ++---
29  1 file changed, 2 insertions(+), 3 deletions(-)
31 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
32 index c94780075b04..bff44b4a0783 100644
33 --- a/fs/ext4/inode.c
34 +++ b/fs/ext4/inode.c
35 @@ -3658,7 +3658,6 @@ static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
36  {
37         struct file *file = iocb->ki_filp;
38         struct inode *inode = file->f_mapping->host;
39 -       struct ext4_inode_info *ei = EXT4_I(inode);
40         ssize_t ret;
41         loff_t offset = iocb->ki_pos;
42         size_t count = iov_iter_count(iter);
43 @@ -3682,7 +3681,7 @@ static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
44                         goto out;
45                 }
46                 orphan = 1;
47 -               ei->i_disksize = inode->i_size;
48 +               ext4_update_i_disksize(inode, inode->i_size);
49                 ext4_journal_stop(handle);
50         }
52 @@ -3790,7 +3789,7 @@ static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
53                 if (ret > 0) {
54                         loff_t end = offset + ret;
55                         if (end > inode->i_size) {
56 -                               ei->i_disksize = end;
57 +                               ext4_update_i_disksize(inode, end);
58                                 i_size_write(inode, end);
59                                 /*
60                                  * We're going to return a positive `ret'
61 -- 
62 2.14.3