Skip to content

Commit f5ccfe1

Browse files
committed
ext4: fix locking for O_APPEND writes
Al Viro pointed out that locking for O_APPEND writes was problematic, since the location of the write isn't known until after we take the i_mutex, which impacts the ext4_unaligned_aio() and s_bitmap_maxbytes check. For O_APPEND always assume that the write is unaligned so call ext4_unwritten_wait(). And to solve the second problem, take the i_mutex earlier before we start the s_bitmap_maxbytes check. Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent 7ed07ba commit f5ccfe1

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

‎fs/ext4/file.c‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,37 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
106106

107107
BUG_ON(iocb->ki_pos != pos);
108108

109+
/*
110+
* Unaligned direct AIO must be serialized; see comment above
111+
* In the case of O_APPEND, assume that we must always serialize
112+
*/
113+
if (o_direct &&
114+
ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
115+
!is_sync_kiocb(iocb) &&
116+
(file->f_flags & O_APPEND ||
117+
ext4_unaligned_aio(inode, iov, nr_segs, pos))) {
118+
aio_mutex = ext4_aio_mutex(inode);
119+
mutex_lock(aio_mutex);
120+
ext4_unwritten_wait(inode);
121+
}
122+
123+
mutex_lock(&inode->i_mutex);
124+
if (file->f_flags & O_APPEND)
125+
iocb->ki_pos = pos = i_size_read(inode);
126+
109127
/*
110128
* If we have encountered a bitmap-format file, the size limit
111129
* is smaller than s_maxbytes, which is for extent-mapped files.
112130
*/
113-
114131
if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
115132
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
116133

117-
if ((pos > sbi->s_bitmap_maxbytes ||
118-
(pos == sbi->s_bitmap_maxbytes && length > 0)))
119-
return -EFBIG;
134+
if ((pos > sbi->s_bitmap_maxbytes) ||
135+
(pos == sbi->s_bitmap_maxbytes && length > 0)) {
136+
mutex_unlock(&inode->i_mutex);
137+
ret = -EFBIG;
138+
goto errout;
139+
}
120140

121141
if (pos + length > sbi->s_bitmap_maxbytes) {
122142
nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
@@ -125,16 +145,6 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
125145
}
126146

127147
if (o_direct) {
128-
/* Unaligned direct AIO must be serialized; see comment above */
129-
if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
130-
!is_sync_kiocb(iocb) &&
131-
ext4_unaligned_aio(inode, iov, nr_segs, pos)) {
132-
aio_mutex = ext4_aio_mutex(inode);
133-
mutex_lock(aio_mutex);
134-
ext4_unwritten_wait(inode);
135-
}
136-
137-
mutex_lock(&inode->i_mutex);
138148
blk_start_plug(&plug);
139149

140150
iocb->private = &overwrite;
@@ -167,8 +177,7 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
167177
if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
168178
overwrite = 1;
169179
}
170-
} else
171-
mutex_lock(&inode->i_mutex);
180+
}
172181

173182
ret = __generic_file_aio_write(iocb, iov, nr_segs);
174183
mutex_unlock(&inode->i_mutex);
@@ -183,6 +192,7 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
183192
if (o_direct)
184193
blk_finish_plug(&plug);
185194

195+
errout:
186196
if (aio_mutex)
187197
mutex_unlock(aio_mutex);
188198
return ret;

0 commit comments

Comments
 (0)