Skip to contentSkip to conversation

Btrfs: fix our reservations for updating an inode when completing io

PATCH New Josef Bacik 1 participant 1 message Nov 8, 2011 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: fix our reservations for updating an inode when completing io

People have been reporting ENOSPC crashes in finish_ordered_io. This is because we try to steal from the delalloc block rsv to satisfy a reservation to update the inode. There are a few problems with this

1) We don't explicitly save space for updating the inode when doing delalloc. This is kind of a problem, and we've gotten away with this because usually an inode is updated by the mtime update and then when we write out the data it's already been touched so we don't use any space trying to update the inode. And then when the delayed inode stuff came along it just stole from the global reserve, which wasn't right either. So to fix this we reserve space if this is the first delalloc extent we're writing, and release it if we need to update due to an i_size change or we don't have anymore delalloc extents.

2) We don't usually use space when updating the inode in finish_ordered_io. This is because we either updated the same leaf when inserting our extent for the io we just completed, or we had updated the leaf when we updated the inode when we changed the mtime. So in the delayed inode stuff, just return 0 if we've updated the inode since the last transaction was committed.

With this patch I ran xfstests 13 in a loop for a couple of hours and didn't see any problems. Thanks,

---
 fs/btrfs/btrfs_inode.h   |    4 +--
 fs/btrfs/delayed-inode.c |   74 ++++++++++++++++++++++++++++++++++++++++++++--
 fs/btrfs/extent-tree.c   |   22 ++++++++++++--
 fs/btrfs/inode.c         |    1 +
 4 files changed, 92 insertions(+), 9 deletions(-)
fs/btrfs/btrfs_inode.h +1 -3
index 5a5d325..634608d 100644
@@ -147,14 +147,12 @@ struct btrfs_inode {
 	 * the btrfs file release call will add this inode to the
 	 * ordered operations list so that we make sure to flush out any
 	 * new data the application may have written before commit.
-	 *
-	 * yes, its silly to have a single bitflag, but we might grow more
-	 * of these.
 	 */
 	unsigned ordered_data_close:1;
 	unsigned orphan_meta_reserved:1;
 	unsigned dummy_inode:1;
 	unsigned in_defrag:1;
+	unsigned delalloc_meta_reserved:1;
 
 	/*
 	 * always compress this one file
fs/btrfs/delayed-inode.c +71 -3

101 lines not on this page · raw message

fs/btrfs/extent-tree.c +19 -3
index 18ea90c..0b044e5 100644
@@ -4063,23 +4063,30 @@ int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
  */
 static unsigned drop_outstanding_extent(struct inode *inode)
 {
+	unsigned drop_inode_space = 0;
 	unsigned dropped_extents = 0;
 
 	BUG_ON(!BTRFS_I(inode)->outstanding_extents);
 	BTRFS_I(inode)->outstanding_extents--;
 
+	if (BTRFS_I(inode)->outstanding_extents == 0 &&
+	    BTRFS_I(inode)->delalloc_meta_reserved) {
+		drop_inode_space = 1;
+		BTRFS_I(inode)->delalloc_meta_reserved = 0;
+	}
+
 	/*
 	 * If we have more or the same amount of outsanding extents than we have
 	 * reserved then we need to leave the reserved extents count alone.
 	 */
 	if (BTRFS_I(inode)->outstanding_extents >=
 	    BTRFS_I(inode)->reserved_extents)
-		return 0;
+		return drop_inode_space;
 
 	dropped_extents = BTRFS_I(inode)->reserved_extents -
 		BTRFS_I(inode)->outstanding_extents;
 	BTRFS_I(inode)->reserved_extents -= dropped_extents;
-	return dropped_extents;
+	return dropped_extents + drop_inode_space;
 }
 
 /**
@@ -4165,9 +4172,18 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
 		nr_extents = BTRFS_I(inode)->outstanding_extents -
 			BTRFS_I(inode)->reserved_extents;
 		BTRFS_I(inode)->reserved_extents += nr_extents;
+	}
 
-		to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
+	/*
+	 * Add an item to reserve for updating the inode when we complete the
+	 * delalloc io.
+	 */
+	if (!BTRFS_I(inode)->delalloc_meta_reserved) {
+		nr_extents++;
+		BTRFS_I(inode)->delalloc_meta_reserved = 1;
 	}
+
+	to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
 	to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
 	spin_unlock(&BTRFS_I(inode)->lock);
 
fs/btrfs/inode.c +1 -0
index 9d0eaa5..faf8a60 100644
@@ -6605,6 +6605,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	ei->orphan_meta_reserved = 0;
 	ei->dummy_inode = 0;
 	ei->in_defrag = 0;
+	ei->delalloc_meta_reserved = 0;
 	ei->force_compress = BTRFS_COMPRESS_NONE;
 
 	ei->delayed_node = NULL;
Signature · 1 line
1.7.5.2