aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ext4/file.c
diff options
context:
space:
mode:
authorTheodore Ts'o <tytso@mit.edu>2014-04-21 14:37:52 -0400
committerTheodore Ts'o <tytso@mit.edu>2014-04-21 14:37:52 -0400
commitf5ccfe1ddbaf9d923a3ebdadcb1e5e32d83e9c28 (patch)
treef79ae8f5ac9b34cbb3cb73f14f9706905219aa6b /fs/ext4/file.c
parent7ed07ba8c3e6160e0af3adc0f59561de154c4c2e (diff)
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>
Diffstat (limited to 'fs/ext4/file.c')
-rw-r--r--fs/ext4/file.c42
1 files changed, 26 insertions, 16 deletions
diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index 3736d9dfe325..7d55a591deba 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -107,16 +107,36 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
107 BUG_ON(iocb->ki_pos != pos); 107 BUG_ON(iocb->ki_pos != pos);
108 108
109 /* 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
127 /*
110 * If we have encountered a bitmap-format file, the size limit 128 * If we have encountered a bitmap-format file, the size limit
111 * is smaller than s_maxbytes, which is for extent-mapped files. 129 * is smaller than s_maxbytes, which is for extent-mapped files.
112 */ 130 */
113
114 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 131 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
115 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 132 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
116 133
117 if ((pos > sbi->s_bitmap_maxbytes || 134 if ((pos > sbi->s_bitmap_maxbytes) ||
118 (pos == sbi->s_bitmap_maxbytes && length > 0))) 135 (pos == sbi->s_bitmap_maxbytes && length > 0)) {
119 return -EFBIG; 136 mutex_unlock(&inode->i_mutex);
137 ret = -EFBIG;
138 goto errout;
139 }
120 140
121 if (pos + length > sbi->s_bitmap_maxbytes) { 141 if (pos + length > sbi->s_bitmap_maxbytes) {
122 nr_segs = iov_shorten((struct iovec *)iov, nr_segs, 142 nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
@@ -125,16 +145,6 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
125 } 145 }
126 146
127 if (o_direct) { 147 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);
138 blk_start_plug(&plug); 148 blk_start_plug(&plug);
139 149
140 iocb->private = &overwrite; 150 iocb->private = &overwrite;
@@ -167,8 +177,7 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
167 if (err == len && (map.m_flags & EXT4_MAP_MAPPED)) 177 if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
168 overwrite = 1; 178 overwrite = 1;
169 } 179 }
170 } else 180 }
171 mutex_lock(&inode->i_mutex);
172 181
173 ret = __generic_file_aio_write(iocb, iov, nr_segs); 182 ret = __generic_file_aio_write(iocb, iov, nr_segs);
174 mutex_unlock(&inode->i_mutex); 183 mutex_unlock(&inode->i_mutex);
@@ -183,6 +192,7 @@ ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
183 if (o_direct) 192 if (o_direct)
184 blk_finish_plug(&plug); 193 blk_finish_plug(&plug);
185 194
195errout:
186 if (aio_mutex) 196 if (aio_mutex)
187 mutex_unlock(aio_mutex); 197 mutex_unlock(aio_mutex);
188 return ret; 198 return ret;