1 ext4: add locking for O_APPEND writes
3 Al Viro pointed out that we need to make sure we only allow one
4 O_APPEND write to proceed at a time so that the the s_bitmap_maxbytes
5 check can be properly checked.
7 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
9 fs/ext4/file.c | 46 +++++++++++++++++++++++++++++-----------------
10 1 file changed, 29 insertions(+), 17 deletions(-)
12 diff --git a/fs/ext4/file.c b/fs/ext4/file.c
13 index 6ac33da..16eef81 100644
16 @@ -98,24 +98,32 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
17 struct file *file = iocb->ki_filp;
18 struct inode *inode = file_inode(iocb->ki_filp);
20 + struct mutex *aio_mutex = NULL;
21 int o_direct = file->f_flags & O_DIRECT;
23 + int overwrite = 0, i_mutex_grabbed = 0;
24 size_t length = iov_length(iov, nr_segs);
27 BUG_ON(iocb->ki_pos != pos);
29 + if (file->f_flags & O_APPEND) {
30 + mutex_lock(&inode->i_mutex);
31 + i_mutex_grabbed = 1;
32 + iocb->ki_pos = pos = i_size_read(inode);
36 * If we have encountered a bitmap-format file, the size limit
37 * is smaller than s_maxbytes, which is for extent-mapped files.
40 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
41 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
43 if ((pos > sbi->s_bitmap_maxbytes ||
44 - (pos == sbi->s_bitmap_maxbytes && length > 0)))
46 + (pos == sbi->s_bitmap_maxbytes && length > 0))) {
51 if (pos + length > sbi->s_bitmap_maxbytes) {
52 nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
53 @@ -123,19 +131,20 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
58 - struct mutex *aio_mutex = NULL;
60 - /* Unaligned direct AIO must be serialized; see comment above */
61 - if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
62 - !is_sync_kiocb(iocb) &&
63 - ext4_unaligned_aio(inode, iov, nr_segs, pos)) {
64 - aio_mutex = ext4_aio_mutex(inode);
65 - mutex_lock(aio_mutex);
66 - ext4_unwritten_wait(inode);
68 + /* Unaligned direct AIO must be serialized; see comment above */
69 + if (o_direct && ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
70 + !is_sync_kiocb(iocb) &&
71 + ext4_unaligned_aio(inode, iov, nr_segs, pos)) {
72 + aio_mutex = ext4_aio_mutex(inode);
73 + mutex_lock(aio_mutex);
74 + ext4_unwritten_wait(inode);
77 + if (!i_mutex_grabbed)
78 mutex_lock(&inode->i_mutex);
79 + i_mutex_grabbed = 1;
83 mutex_unlock(aio_mutex);
84 blk_start_plug(&plug);
85 @@ -170,11 +179,11 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
86 if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
90 - mutex_lock(&inode->i_mutex);
93 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
94 mutex_unlock(&inode->i_mutex);
95 + i_mutex_grabbed = 0;
99 @@ -186,6 +195,9 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
101 blk_finish_plug(&plug);
104 + if (i_mutex_grabbed)
105 + mutex_unlock(&inode->i_mutex);