Skip to content

Commit 3e4c56d

Browse files
alex chentorvalds
alex chen
authored andcommitted
ocfs2: ip_alloc_sem should be taken in ocfs2_get_block()
ip_alloc_sem should be taken in ocfs2_get_block() when reading file in DIRECT mode to prevent concurrent access to extent tree with ocfs2_dio_end_io_write(), which may cause BUGON in the following situation: read file 'A' end_io of writing file 'A' vfs_read __vfs_read ocfs2_file_read_iter generic_file_read_iter ocfs2_direct_IO __blockdev_direct_IO do_blockdev_direct_IO do_direct_IO get_more_blocks ocfs2_get_block ocfs2_extent_map_get_blocks ocfs2_get_clusters ocfs2_get_clusters_nocache() ocfs2_search_extent_list return the index of record which contains the v_cluster, that is v_cluster > rec[i]->e_cpos. ocfs2_dio_end_io ocfs2_dio_end_io_write down_write(&oi->ip_alloc_sem); ocfs2_mark_extent_written ocfs2_change_extent_flag ocfs2_split_extent ... --> modify the rec[i]->e_cpos, resulting in v_cluster < rec[i]->e_cpos. BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos)) [alex.chen@huawei.com: v3] Link: http://lkml.kernel.org/r/59EF3614.6050008@huawei.com Link: http://lkml.kernel.org/r/59EF3614.6050008@huawei.com Fixes: c15471f ("ocfs2: fix sparse file & data ordering issue in direct io") Signed-off-by: Alex Chen <alex.chen@huawei.com> Reviewed-by: Jun Piao <piaojun@huawei.com> Reviewed-by: Joseph Qi <jiangqi903@gmail.com> Reviewed-by: Gang He <ghe@suse.com> Acked-by: Changwei Ge <ge.changwei@h3c.com> Cc: Mark Fasheh <mfasheh@versity.com> Cc: Joel Becker <jlbec@evilplan.org> Cc: Junxiao Bi <junxiao.bi@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 28f5a8a commit 3e4c56d

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

Diff for: fs/ocfs2/aops.c

+18-8
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
134134
return err;
135135
}
136136

137+
static int ocfs2_lock_get_block(struct inode *inode, sector_t iblock,
138+
struct buffer_head *bh_result, int create)
139+
{
140+
int ret = 0;
141+
struct ocfs2_inode_info *oi = OCFS2_I(inode);
142+
143+
down_read(&oi->ip_alloc_sem);
144+
ret = ocfs2_get_block(inode, iblock, bh_result, create);
145+
up_read(&oi->ip_alloc_sem);
146+
147+
return ret;
148+
}
149+
137150
int ocfs2_get_block(struct inode *inode, sector_t iblock,
138151
struct buffer_head *bh_result, int create)
139152
{
@@ -2128,7 +2141,7 @@ static void ocfs2_dio_free_write_ctx(struct inode *inode,
21282141
* called like this: dio->get_blocks(dio->inode, fs_startblk,
21292142
* fs_count, map_bh, dio->rw == WRITE);
21302143
*/
2131-
static int ocfs2_dio_get_block(struct inode *inode, sector_t iblock,
2144+
static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock,
21322145
struct buffer_head *bh_result, int create)
21332146
{
21342147
struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
@@ -2154,12 +2167,9 @@ static int ocfs2_dio_get_block(struct inode *inode, sector_t iblock,
21542167
* while file size will be changed.
21552168
*/
21562169
if (pos + total_len <= i_size_read(inode)) {
2157-
down_read(&oi->ip_alloc_sem);
2158-
/* This is the fast path for re-write. */
2159-
ret = ocfs2_get_block(inode, iblock, bh_result, create);
2160-
2161-
up_read(&oi->ip_alloc_sem);
21622170

2171+
/* This is the fast path for re-write. */
2172+
ret = ocfs2_lock_get_block(inode, iblock, bh_result, create);
21632173
if (buffer_mapped(bh_result) &&
21642174
!buffer_new(bh_result) &&
21652175
ret == 0)
@@ -2424,9 +2434,9 @@ static ssize_t ocfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
24242434
return 0;
24252435

24262436
if (iov_iter_rw(iter) == READ)
2427-
get_block = ocfs2_get_block;
2437+
get_block = ocfs2_lock_get_block;
24282438
else
2429-
get_block = ocfs2_dio_get_block;
2439+
get_block = ocfs2_dio_wr_get_block;
24302440

24312441
return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
24322442
iter, get_block,

0 commit comments

Comments
 (0)