Btrfs: Fix race against disk_i_size updates
authorChris Mason <chris.mason@oracle.com>
Wed, 24 Sep 2008 00:19:49 +0000 (20:19 -0400)
committerChris Mason <chris.mason@oracle.com>
Thu, 25 Sep 2008 15:04:07 +0000 (11:04 -0400)
The code to update the on disk i_size happens before the
ordered_extent record is removed.  So, it is possible for multiple
ordered_extent completion routines to run at the same time, and to
find each other in the ordered tree.

The end result is they both decide not to update disk_i_size, leaving
it too small.  This temporary fix just puts the updates inside
the extent_mutex.  A real solution would be stronger ordering of
disk_i_size updates against removing the ordered extent from the tree.

Signed-off-by: Chris Mason <chris.mason@oracle.com>
fs/btrfs/inode.c

index 2e7d82e..adb169d 100644 (file)
@@ -608,9 +608,11 @@ nocow:
        add_pending_csums(trans, inode, ordered_extent->file_offset,
                          &ordered_extent->list);
 
        add_pending_csums(trans, inode, ordered_extent->file_offset,
                          &ordered_extent->list);
 
+       mutex_lock(&BTRFS_I(inode)->extent_mutex);
        btrfs_ordered_update_i_size(inode, ordered_extent);
        btrfs_update_inode(trans, root, inode);
        btrfs_remove_ordered_extent(inode, ordered_extent);
        btrfs_ordered_update_i_size(inode, ordered_extent);
        btrfs_update_inode(trans, root, inode);
        btrfs_remove_ordered_extent(inode, ordered_extent);
+       mutex_unlock(&BTRFS_I(inode)->extent_mutex);
 
        /* once for us */
        btrfs_put_ordered_extent(ordered_extent);
 
        /* once for us */
        btrfs_put_ordered_extent(ordered_extent);