Skip to content

Commit 6ab855a

Browse files
WeiWei Wangtorvalds
authored andcommitted
ocfs2: add ip_alloc_sem in direct IO to protect allocation changes
In ocfs2, ip_alloc_sem is used to protect allocation changes on the node. In direct IO, we add ip_alloc_sem to protect date consistent between direct-io and ocfs2_truncate_file race (buffer io use ip_alloc_sem already). Although inode->i_mutex lock is used to avoid concurrency of above situation, i think ip_alloc_sem is still needed because protect allocation changes is significant. Other filesystem like ext4 also uses rw_semaphore to protect data consistent between get_block-vs-truncate race by other means, So ip_alloc_sem in ocfs2 direct io is needed. Signed-off-by: Weiwei Wang <[email protected]> Signed-off-by: Mark Fasheh <[email protected]> Cc: Joel Becker <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 3423768 commit 6ab855a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

fs/ocfs2/aops.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,14 @@ static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
533533

534534
inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
535535

536+
down_read(&OCFS2_I(inode)->ip_alloc_sem);
537+
536538
/* This figures out the size of the next contiguous block, and
537539
* our logical offset */
538540
ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
539541
&contig_blocks, &ext_flags);
542+
up_read(&OCFS2_I(inode)->ip_alloc_sem);
543+
540544
if (ret) {
541545
mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
542546
(unsigned long long)iblock);
@@ -557,6 +561,8 @@ static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
557561

558562
alloc_locked = 1;
559563

564+
down_write(&OCFS2_I(inode)->ip_alloc_sem);
565+
560566
/* fill hole, allocate blocks can't be larger than the size
561567
* of the hole */
562568
clusters_to_alloc = ocfs2_clusters_for_bytes(inode->i_sb, len);
@@ -569,18 +575,21 @@ static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
569575
ret = ocfs2_extend_allocation(inode, cpos,
570576
clusters_to_alloc, 0);
571577
if (ret < 0) {
578+
up_write(&OCFS2_I(inode)->ip_alloc_sem);
572579
mlog_errno(ret);
573580
goto bail;
574581
}
575582

576583
ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
577584
&contig_blocks, &ext_flags);
578585
if (ret < 0) {
586+
up_write(&OCFS2_I(inode)->ip_alloc_sem);
579587
mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
580588
(unsigned long long)iblock);
581589
ret = -EIO;
582590
goto bail;
583591
}
592+
up_write(&OCFS2_I(inode)->ip_alloc_sem);
584593
}
585594

586595
/*
@@ -835,12 +844,17 @@ static ssize_t ocfs2_direct_IO_write(struct kiocb *iocb,
835844

836845
/* zeroing out the previously allocated cluster tail
837846
* that but not zeroed */
838-
if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
847+
if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
848+
down_read(&OCFS2_I(inode)->ip_alloc_sem);
839849
ret = ocfs2_direct_IO_zero_extend(osb, inode, offset,
840850
zero_len_tail, cluster_align_tail);
841-
else
851+
up_read(&OCFS2_I(inode)->ip_alloc_sem);
852+
} else {
853+
down_write(&OCFS2_I(inode)->ip_alloc_sem);
842854
ret = ocfs2_direct_IO_extend_no_holes(osb, inode,
843855
offset);
856+
up_write(&OCFS2_I(inode)->ip_alloc_sem);
857+
}
844858
if (ret < 0) {
845859
mlog_errno(ret);
846860
ocfs2_inode_unlock(inode, 1);

0 commit comments

Comments
 (0)