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
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
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
35 @@ -3658,7 +3658,6 @@ static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
37 struct file *file = iocb->ki_filp;
38 struct inode *inode = file->f_mapping->host;
39 - struct ext4_inode_info *ei = EXT4_I(inode);
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)
47 - ei->i_disksize = inode->i_size;
48 + ext4_update_i_disksize(inode, inode->i_size);
49 ext4_journal_stop(handle);
52 @@ -3790,7 +3789,7 @@ static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
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);
60 * We're going to return a positive `ret'