浏览代码

Btrfs: take i_mutex before generic_write_checks

btrfs_file_write was incorrectly calling generic_write_checks without
taking i_mutex.  This lead to problems with racing around i_size when
doing O_APPEND writes.

The fix here is to move i_mutex higher.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
Chris Mason 15 年之前
父节点
当前提交
ab93dbecfb
共有 1 个文件被更改,包括 17 次插入8 次删除
  1. 17 8
      fs/btrfs/file.c

+ 17 - 8
fs/btrfs/file.c

@@ -920,26 +920,35 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
 	start_pos = pos;
 	start_pos = pos;
 
 
 	vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
 	vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
+
+	/* do the reserve before the mutex lock in case we have to do some
+	 * flushing.  We wouldn't deadlock, but this is more polite.
+	 */
+	err = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
+	if (err)
+		goto out_nolock;
+
+	mutex_lock(&inode->i_mutex);
+
 	current->backing_dev_info = inode->i_mapping->backing_dev_info;
 	current->backing_dev_info = inode->i_mapping->backing_dev_info;
 	err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
 	err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
 	if (err)
 	if (err)
-		goto out_nolock;
+		goto out;
+
 	if (count == 0)
 	if (count == 0)
-		goto out_nolock;
+		goto out;
 
 
 	err = file_remove_suid(file);
 	err = file_remove_suid(file);
 	if (err)
 	if (err)
-		goto out_nolock;
-
-	err = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
-	if (err)
-		goto out_nolock;
+		goto out;
 
 
 	file_update_time(file);
 	file_update_time(file);
 
 
 	pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
 	pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
 
 
-	mutex_lock(&inode->i_mutex);
+	/* generic_write_checks can change our pos */
+	start_pos = pos;
+
 	BTRFS_I(inode)->sequence++;
 	BTRFS_I(inode)->sequence++;
 	first_index = pos >> PAGE_CACHE_SHIFT;
 	first_index = pos >> PAGE_CACHE_SHIFT;
 	last_index = (pos + count) >> PAGE_CACHE_SHIFT;
 	last_index = (pos + count) >> PAGE_CACHE_SHIFT;