1 ext4: fix locking for O_APPEND writes
3 Al Viro pointed out that locking for O_APPEND writes was problematic,
4 since the location of the write isn't known until after we take the
5 i_mutex, which impacts the ext4_unaligned_aio() and s_bitmap_maxbytes
8 For O_APPEND always assume that the write is unaligned so call
9 ext4_unwritten_wait(). And to solve the second problem, take the
10 i_mutex earlier before we start the s_bitmap_maxbytes check.
12 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
14 fs/ext4/file.c | 42 ++++++++++++++++++++++++++----------------
15 1 file changed, 26 insertions(+), 16 deletions(-)
17 diff --git a/fs/ext4/file.c b/fs/ext4/file.c
18 index 3736d9d..7d55a59 100644
21 @@ -107,16 +107,36 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
22 BUG_ON(iocb->ki_pos != pos);
25 + * Unaligned direct AIO must be serialized; see comment above
26 + * In the case of O_APPEND, assume that we must always serialize
29 + ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
30 + !is_sync_kiocb(iocb) &&
31 + (file->f_flags & O_APPEND ||
32 + ext4_unaligned_aio(inode, iov, nr_segs, pos))) {
33 + aio_mutex = ext4_aio_mutex(inode);
34 + mutex_lock(aio_mutex);
35 + ext4_unwritten_wait(inode);
38 + mutex_lock(&inode->i_mutex);
39 + if (file->f_flags & O_APPEND)
40 + iocb->ki_pos = pos = i_size_read(inode);
43 * If we have encountered a bitmap-format file, the size limit
44 * is smaller than s_maxbytes, which is for extent-mapped files.
47 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
48 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
50 - if ((pos > sbi->s_bitmap_maxbytes ||
51 - (pos == sbi->s_bitmap_maxbytes && length > 0)))
53 + if ((pos > sbi->s_bitmap_maxbytes) ||
54 + (pos == sbi->s_bitmap_maxbytes && length > 0)) {
55 + mutex_unlock(&inode->i_mutex);
60 if (pos + length > sbi->s_bitmap_maxbytes) {
61 nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
62 @@ -125,16 +145,6 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
66 - /* Unaligned direct AIO must be serialized; see comment above */
67 - if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
68 - !is_sync_kiocb(iocb) &&
69 - ext4_unaligned_aio(inode, iov, nr_segs, pos)) {
70 - aio_mutex = ext4_aio_mutex(inode);
71 - mutex_lock(aio_mutex);
72 - ext4_unwritten_wait(inode);
75 - mutex_lock(&inode->i_mutex);
76 blk_start_plug(&plug);
78 iocb->private = &overwrite;
79 @@ -167,8 +177,7 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
80 if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
84 - mutex_lock(&inode->i_mutex);
87 ret = __generic_file_aio_write(iocb, iov, nr_segs);
88 mutex_unlock(&inode->i_mutex);
89 @@ -183,6 +192,7 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
91 blk_finish_plug(&plug);
95 mutex_unlock(aio_mutex);