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>
21 There's no v1 version of this patch.
23 It's a bit unfortunate that I have to remove the "ei" definition in this
24 patch and reintroduce it in patch 2/2, otherwise I got transient
25 "defined but not used" build warning with only this patch applied.
27 fs/ext4/inode.c | 5 ++---
28 1 file changed, 2 insertions(+), 3 deletions(-)
30 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
31 index c94780075b04..bff44b4a0783 100644
34 @@ -3658,7 +3658,6 @@ static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
36 struct file *file = iocb->ki_filp;
37 struct inode *inode = file->f_mapping->host;
38 - struct ext4_inode_info *ei = EXT4_I(inode);
40 loff_t offset = iocb->ki_pos;
41 size_t count = iov_iter_count(iter);
42 @@ -3682,7 +3681,7 @@ static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
46 - ei->i_disksize = inode->i_size;
47 + ext4_update_i_disksize(inode, inode->i_size);
48 ext4_journal_stop(handle);
51 @@ -3790,7 +3789,7 @@ static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
53 loff_t end = offset + ret;
54 if (end > inode->i_size) {
55 - ei->i_disksize = end;
56 + ext4_update_i_disksize(inode, end);
57 i_size_write(inode, end);
59 * We're going to return a positive `ret'