Skip to content

Commit

Permalink
fs/hfsplus/extents.c: fix concurrent acess of alloc_blocks
Browse files Browse the repository at this point in the history
Concurrent access to alloc_blocks in hfsplus_inode_info() is protected
by extents_lock mutex.  This patch fixes two instances where
alloc_blocks modification was not protected with this lock.

This fixes possible allocation bitmap corruption in race conditions
while extending and truncating files.

[akpm@linux-foundation.org: take extents_lock before taking a copy of ->alloc_blocks]
[akpm@linux-foundation.org: remove now-unused label `out']
Signed-off-by: Sougata Santra <sougata@tuxera.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Cc: Vyacheslav Dubeyko <slava@dubeyko.com>
Cc: Alexey Khoroshilov <khoroshilov@ispras.ru>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Sougata Santra authored and Linus Torvalds committed Apr 3, 2014
1 parent abfeb72 commit d7bdb99
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions fs/hfsplus/extents.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,13 @@ int hfsplus_file_extend(struct inode *inode)
goto insert_extent;
}
out:
mutex_unlock(&hip->extents_lock);
if (!res) {
hip->alloc_blocks += len;
mutex_unlock(&hip->extents_lock);
hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
return 0;
}
mutex_unlock(&hip->extents_lock);
return res;

insert_extent:
Expand Down Expand Up @@ -554,11 +556,13 @@ void hfsplus_file_truncate(struct inode *inode)

blk_cnt = (inode->i_size + HFSPLUS_SB(sb)->alloc_blksz - 1) >>
HFSPLUS_SB(sb)->alloc_blksz_shift;

mutex_lock(&hip->extents_lock);

alloc_cnt = hip->alloc_blocks;
if (blk_cnt == alloc_cnt)
goto out;
goto out_unlock;

mutex_lock(&hip->extents_lock);
res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
if (res) {
mutex_unlock(&hip->extents_lock);
Expand Down Expand Up @@ -590,10 +594,10 @@ void hfsplus_file_truncate(struct inode *inode)
hfs_brec_remove(&fd);
}
hfs_find_exit(&fd);
mutex_unlock(&hip->extents_lock);

hip->alloc_blocks = blk_cnt;
out:
out_unlock:
mutex_unlock(&hip->extents_lock);
hip->phys_size = inode->i_size;
hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>
sb->s_blocksize_bits;
Expand Down

0 comments on commit d7bdb99

Please sign in to comment.