Skip to contentSkip to conversation

hfsplus: fix concurrent acess of alloc_blocks

PATCH In review Sougata Santra 2 participants 4 messages Feb 17, 2014 – Feb 19, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hfsplus: fix concurrent acess of alloc_blocks

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.

---
 fs/hfsplus/extents.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/hfsplus/extents.c +4 -2
index fbb212f..314c858 100644
@@ -498,11 +498,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:
@@ -592,9 +594,9 @@ 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;
+	mutex_unlock(&hip->extents_lock);
 out:
 	hip->phys_size = inode->i_size;
 	hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 16 lines · On Mon, 17 Feb 2014 14:20:47 +0200 Sougata Santra <[email protected]> wrote:

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.

...

fs/hfsplus/extents.c +0 -0
@@ -498,11 +498,13 @@ int hfsplus_file_extend(struct inode *inode)
 			goto insert_extent;
 	}
 out:

fs/hfsplus/extents.c int hfsplus_file_extend(struct inode *inode) · line 509

Quoted 10 lines
fs/hfsplus/extents.c +3 -1
-	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;
 

This looks OK.

fs/hfsplus/extents.c void hfsplus_file_truncate(struct inode *inode) · line 602

Quoted 11 lines
fs/hfsplus/extents.c +1 -1
@@ -592,9 +594,9 @@ 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;
+	mutex_unlock(&hip->extents_lock);
 out:
 	hip->phys_size = inode->i_size;
 	hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>

But this does not. To provide locking for hfsplus_inode_info.alloc_blocks, we must take the lock *before* taking a local copy of ->alloc_blocks.

Please review:

fs/hfsplus/extents.c renamed from fs/hfsplus/extents.c~hfsplus-fix-concurrent-acess-of-alloc_blocks-fix +5 -2
@@ -556,11 +556,13 @@ void hfsplus_file_truncate(struct 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);
@@ -594,6 +596,7 @@ void hfsplus_file_truncate(struct inode
 	hfs_find_exit(&fd);
 
 	hip->alloc_blocks = blk_cnt;
+out_unlock:
 	mutex_unlock(&hip->extents_lock);
 out:
 	hip->phys_size = inode->i_size;

_

#2 lore raw
Quoted 75 lines · Andrew Morton
Quoted 16 lines · On Mon, 17 Feb 2014 14:20:47 +0200 Sougata Santra <[email protected]> wrote:

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.

...

fs/hfsplus/extents.c +0 -0
@@ -498,11 +498,13 @@ int hfsplus_file_extend(struct inode *inode)
 			goto insert_extent;
 	}
 out:

fs/hfsplus/extents.c int hfsplus_file_extend(struct inode *inode) · line 509

Quoted 10 lines
fs/hfsplus/extents.c +3 -1
-	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;
 

This looks OK.

fs/hfsplus/extents.c void hfsplus_file_truncate(struct inode *inode) · line 602

Quoted 11 lines
fs/hfsplus/extents.c +1 -1
@@ -592,9 +594,9 @@ 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;
+	mutex_unlock(&hip->extents_lock);
 out:
 	hip->phys_size = inode->i_size;
 	hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>

But this does not. To provide locking for hfsplus_inode_info.alloc_blocks, we must take the lock *before* taking a local copy of ->alloc_blocks.

Please review:

fs/hfsplus/extents.c renamed from fs/hfsplus/extents.c~hfsplus-fix-concurrent-acess-of-alloc_blocks-fix +5 -2
@@ -556,11 +556,13 @@ void hfsplus_file_truncate(struct 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);
@@ -594,6 +596,7 @@ void hfsplus_file_truncate(struct inode
 	hfs_find_exit(&fd);
 
 	hip->alloc_blocks = blk_cnt;
+out_unlock:
 	mutex_unlock(&hip->extents_lock);
 out:
 	hip->phys_size = inode->i_size;
Quoted 2 lines

_

This is good, Missed it. Thank you.

#3 lore raw
In reply to Andrew Morton
Quoted 68 lines · Andrew Morton
Quoted 16 lines · On Mon, 17 Feb 2014 14:20:47 +0200 Sougata Santra <[email protected]> wrote:

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.

...

fs/hfsplus/extents.c +0 -0
@@ -498,11 +498,13 @@ int hfsplus_file_extend(struct inode *inode)
  			goto insert_extent;
  	}
  out:

fs/hfsplus/extents.c int hfsplus_file_extend(struct inode *inode) · line 509

Quoted 10 lines
fs/hfsplus/extents.c +3 -1
-	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;
 

This looks OK.

fs/hfsplus/extents.c void hfsplus_file_truncate(struct inode *inode) · line 602

Quoted 11 lines
fs/hfsplus/extents.c +1 -1
@@ -592,9 +594,9 @@ 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;
+	mutex_unlock(&hip->extents_lock);
  out:
  	hip->phys_size = inode->i_size;
  	hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>

But this does not. To provide locking for hfsplus_inode_info.alloc_blocks, we must take the lock *before* taking a local copy of ->alloc_blocks.

Please review:

fs/hfsplus/extents.c renamed from fs/hfsplus/extents.c~hfsplus-fix-concurrent-acess-of-alloc_blocks-fix +4 -2
@@ -556,11 +556,13 @@ void hfsplus_file_truncate(struct 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);

fs/hfsplus/extents.c void hfsplus_file_truncate(struct inode · line 601

Quoted 6 lines
fs/hfsplus/extents.c renamed from fs/hfsplus/extents.c~hfsplus-fix-concurrent-acess-of-alloc_blocks-fix +1 -0
@@ -594,6 +596,7 @@ void hfsplus_file_truncate(struct inode
  	hfs_find_exit(&fd);
 
  	hip->alloc_blocks = blk_cnt;
+out_unlock:
  	mutex_unlock(&hip->extents_lock);
  out:

We can also remove this label ?

Quoted 3 lines
fs/hfsplus/extents.c renamed from fs/hfsplus/extents.c~hfsplus-fix-concurrent-acess-of-alloc_blocks-fix +0 -0
  	hip->phys_size = inode->i_size;

_