Skip to contentSkip to conversation

btrfs: add btrfs_delete_ref_head helper

PATCH In review Josef Bacik 4 participants 44 messages Jul 19, 2018 – Aug 21, 2018

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

PATCH 1/22 btrfs: add btrfs_delete_ref_head helper

From: Josef Bacik <[email protected]>

We do this dance in cleanup_ref_head and check_ref_cleanup, unify it into a helper and cleanup the calling functions.

---
 fs/btrfs/delayed-ref.c | 14 ++++++++++++++
 fs/btrfs/delayed-ref.h |  3 ++-
 fs/btrfs/extent-tree.c | 24 ++++--------------------
 3 files changed, 20 insertions(+), 21 deletions(-)
fs/btrfs/delayed-ref.c +14 -0
index 03dec673d12a..e1b322d651dd 100644
@@ -393,6 +393,20 @@ btrfs_select_ref_head(struct btrfs_trans_handle *trans)
 	return head;
 }
 
+void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
+			   struct btrfs_delayed_ref_head *head)
+{
+	lockdep_assert_held(&delayed_refs->lock);
+	lockdep_assert_held(&head->lock);
+
+	rb_erase(&head->href_node, &delayed_refs->href_root);
+	RB_CLEAR_NODE(&head->href_node);
+	atomic_dec(&delayed_refs->num_entries);
+	delayed_refs->num_heads--;
+	if (head->processing == 0)
+		delayed_refs->num_heads_ready--;
+}
+
 /*
  * Helper to insert the ref_node to the tail or merge with tail.
  *
fs/btrfs/delayed-ref.h +2 -1
index ea1aecb6a50d..36318182e4ec 100644
@@ -263,7 +263,8 @@ static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
 {
 	mutex_unlock(&head->mutex);
 }
-
+void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
+			   struct btrfs_delayed_ref_head *head);
 
 struct btrfs_delayed_ref_head *
 btrfs_select_ref_head(struct btrfs_trans_handle *trans);
fs/btrfs/extent-tree.c +4 -20
index 3d9fe58c0080..ccaccd78534e 100644
@@ -2577,12 +2577,9 @@ static int cleanup_ref_head(struct btrfs_trans_handle *trans,
 		spin_unlock(&delayed_refs->lock);
 		return 1;
 	}
-	delayed_refs->num_heads--;
-	rb_erase(&head->href_node, &delayed_refs->href_root);
-	RB_CLEAR_NODE(&head->href_node);
-	spin_unlock(&head->lock);
+	btrfs_delete_ref_head(delayed_refs, head);
 	spin_unlock(&delayed_refs->lock);
-	atomic_dec(&delayed_refs->num_entries);
+	spin_unlock(&head->lock);
 
 	trace_run_delayed_ref_head(fs_info, head, 0);
 
@@ -7122,22 +7119,9 @@ static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
 	if (!mutex_trylock(&head->mutex))
 		goto out;
 
-	/*
-	 * at this point we have a head with no other entries.  Go
-	 * ahead and process it.
-	 */
-	rb_erase(&head->href_node, &delayed_refs->href_root);
-	RB_CLEAR_NODE(&head->href_node);
-	atomic_dec(&delayed_refs->num_entries);
-
-	/*
-	 * we don't take a ref on the node because we're removing it from the
-	 * tree, so we just steal the ref the tree was holding.
-	 */
-	delayed_refs->num_heads--;
-	if (head->processing == 0)
-		delayed_refs->num_heads_ready--;
+	btrfs_delete_ref_head(delayed_refs, head);
 	head->processing = 0;
+
 	spin_unlock(&head->lock);
 	spin_unlock(&delayed_refs->lock);
 
Signature · 1 line
2.14.3
#1 lore raw

PATCH 2/22 btrfs: add cleanup_ref_head_accounting helper

From: Josef Bacik <[email protected]>

We were missing some quota cleanups in check_ref_cleanup, so break the ref head accounting cleanup into a helper and call that from both check_ref_cleanup and cleanup_ref_head. This will hopefully ensure that we don't screw up accounting in the future for other things that we add.

---
 fs/btrfs/extent-tree.c | 65 +++++++++++++++++++++++++++++---------------------
 1 file changed, 38 insertions(+), 27 deletions(-)
fs/btrfs/extent-tree.c +38 -27
index ccaccd78534e..cf1152d01309 100644
@@ -2547,6 +2547,40 @@ static int cleanup_extent_op(struct btrfs_trans_handle *trans,
 	return ret ? ret : 1;
 }
 
+static void cleanup_ref_head_accounting(struct btrfs_trans_handle *trans,
+					struct btrfs_delayed_ref_head *head)
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_delayed_ref_root *delayed_refs =
+		&trans->transaction->delayed_refs;
+
+	if (head->total_ref_mod < 0) {
+		struct btrfs_space_info *space_info;
+		u64 flags;
+
+		if (head->is_data)
+			flags = BTRFS_BLOCK_GROUP_DATA;
+		else if (head->is_system)
+			flags = BTRFS_BLOCK_GROUP_SYSTEM;
+		else
+			flags = BTRFS_BLOCK_GROUP_METADATA;
+		space_info = __find_space_info(fs_info, flags);
+		ASSERT(space_info);
+		percpu_counter_add(&space_info->total_bytes_pinned,
+				   -head->num_bytes);
+
+		if (head->is_data) {
+			spin_lock(&delayed_refs->lock);
+			delayed_refs->pending_csums -= head->num_bytes;
+			spin_unlock(&delayed_refs->lock);
+		}
+	}
+
+	/* Also free its reserved qgroup space */
+	btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
+				      head->qgroup_reserved);
+}
+
 static int cleanup_ref_head(struct btrfs_trans_handle *trans,
 			    struct btrfs_fs_info *fs_info,
 			    struct btrfs_delayed_ref_head *head)
@@ -2581,30 +2615,6 @@ static int cleanup_ref_head(struct btrfs_trans_handle *trans,
 	spin_unlock(&delayed_refs->lock);
 	spin_unlock(&head->lock);
 
-	trace_run_delayed_ref_head(fs_info, head, 0);
-
-	if (head->total_ref_mod < 0) {
-		struct btrfs_space_info *space_info;
-		u64 flags;
-
-		if (head->is_data)
-			flags = BTRFS_BLOCK_GROUP_DATA;
-		else if (head->is_system)
-			flags = BTRFS_BLOCK_GROUP_SYSTEM;
-		else
-			flags = BTRFS_BLOCK_GROUP_METADATA;
-		space_info = __find_space_info(fs_info, flags);
-		ASSERT(space_info);
-		percpu_counter_add(&space_info->total_bytes_pinned,
-				   -head->num_bytes);
-
-		if (head->is_data) {
-			spin_lock(&delayed_refs->lock);
-			delayed_refs->pending_csums -= head->num_bytes;
-			spin_unlock(&delayed_refs->lock);
-		}
-	}
-
 	if (head->must_insert_reserved) {
 		btrfs_pin_extent(fs_info, head->bytenr,
 				 head->num_bytes, 1);
@@ -2614,9 +2624,9 @@ static int cleanup_ref_head(struct btrfs_trans_handle *trans,
 		}
 	}
 
-	/* Also free its reserved qgroup space */
-	btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
-				      head->qgroup_reserved);
+	cleanup_ref_head_accounting(trans, head);
+
+	trace_run_delayed_ref_head(fs_info, head, 0);
 	btrfs_delayed_ref_unlock(head);
 	btrfs_put_delayed_ref_head(head);
 	return 0;
@@ -7129,6 +7139,7 @@ static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
 	if (head->must_insert_reserved)
 		ret = 1;
 
+	cleanup_ref_head_accounting(trans, head);
 	mutex_unlock(&head->mutex);
 	btrfs_put_delayed_ref_head(head);
 	return ret;
Signature · 1 line
2.14.3
#2 lore raw

PATCH 3/22 btrfs: use cleanup_extent_op in check_ref_cleanup

In reply to Josef Bacik

From: Josef Bacik <[email protected]>

Unify the extent_op handling as well, just add a flag so we don't actually run the extent op from check_ref_cleanup and instead return a value so that we can skip cleaning up the ref head.

---
 fs/btrfs/extent-tree.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
fs/btrfs/extent-tree.c +10 -9
index cf1152d01309..33cd43dd73ee 100644
@@ -2528,19 +2528,24 @@ static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_ref
 }
 
 static int cleanup_extent_op(struct btrfs_trans_handle *trans,
-			     struct btrfs_fs_info *fs_info,
-			     struct btrfs_delayed_ref_head *head)
+			     struct btrfs_delayed_ref_head *head,
+			     bool run_extent_op)
 {
+	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct btrfs_delayed_extent_op *extent_op = head->extent_op;
 	int ret;
 
 	if (!extent_op)
 		return 0;
+
 	head->extent_op = NULL;
 	if (head->must_insert_reserved) {
 		btrfs_free_delayed_extent_op(extent_op);
 		return 0;
+	} else if (!run_extent_op) {
+		return 1;
 	}
+
 	spin_unlock(&head->lock);
 	ret = run_delayed_extent_op(trans, fs_info, head, extent_op);
 	btrfs_free_delayed_extent_op(extent_op);
@@ -2590,7 +2595,7 @@ static int cleanup_ref_head(struct btrfs_trans_handle *trans,
 
 	delayed_refs = &trans->transaction->delayed_refs;
 
-	ret = cleanup_extent_op(trans, fs_info, head);
+	ret = cleanup_extent_op(trans, head, true);
 	if (ret < 0) {
 		unselect_delayed_ref_head(delayed_refs, head);
 		btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
@@ -7115,12 +7120,8 @@ static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
 	if (!RB_EMPTY_ROOT(&head->ref_tree))
 		goto out;
 
-	if (head->extent_op) {
-		if (!head->must_insert_reserved)
-			goto out;
-		btrfs_free_delayed_extent_op(head->extent_op);
-		head->extent_op = NULL;
-	}
+	if (cleanup_extent_op(trans, head, false))
+		goto out;
 
 	/*
 	 * waiting for the lock here would deadlock.  If someone else has it
Signature · 1 line
2.14.3
#3 lore raw

PATCH 4/22 btrfs: only track ref_heads in delayed_ref_updates

In reply to Josef Bacik

From: Josef Bacik <[email protected]>

We use this number to figure out how many delayed refs to run, but __btrfs_run_delayed_refs really only checks every time we need a new delayed ref head, so we always run at least one ref head completely no matter what the number of items on it. So instead track only the ref heads added by this trans handle and adjust the counting appropriately in __btrfs_run_delayed_refs.

---
 fs/btrfs/delayed-ref.c | 3 ---
 fs/btrfs/extent-tree.c | 5 +----
 2 files changed, 1 insertion(+), 7 deletions(-)
fs/btrfs/delayed-ref.c +0 -3
index e1b322d651dd..e5a8fb7ba9f6 100644
@@ -234,8 +234,6 @@ static inline void drop_delayed_ref(struct btrfs_trans_handle *trans,
 	ref->in_tree = 0;
 	btrfs_put_delayed_ref(ref);
 	atomic_dec(&delayed_refs->num_entries);
-	if (trans->delayed_ref_updates)
-		trans->delayed_ref_updates--;
 }
 
 static bool merge_ref(struct btrfs_trans_handle *trans,
@@ -460,7 +458,6 @@ static int insert_delayed_ref(struct btrfs_trans_handle *trans,
 	if (ref->action == BTRFS_ADD_DELAYED_REF)
 		list_add_tail(&ref->add_list, &href->ref_add_list);
 	atomic_inc(&root->num_entries);
-	trans->delayed_ref_updates++;
 	spin_unlock(&href->lock);
 	return ret;
 }
fs/btrfs/extent-tree.c +1 -4
index 33cd43dd73ee..168312ead4ae 100644
@@ -2667,6 +2667,7 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
 				spin_unlock(&delayed_refs->lock);
 				break;
 			}
+			count++;
 
 			/* grab the lock that says we are going to process
 			 * all the refs for this head */
@@ -2680,7 +2681,6 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
 			 */
 			if (ret == -EAGAIN) {
 				locked_ref = NULL;
-				count++;
 				continue;
 			}
 		}
@@ -2708,7 +2708,6 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
 			unselect_delayed_ref_head(delayed_refs, locked_ref);
 			locked_ref = NULL;
 			cond_resched();
-			count++;
 			continue;
 		}
 
@@ -2726,7 +2725,6 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
 				return ret;
 			}
 			locked_ref = NULL;
-			count++;
 			continue;
 		}
 
@@ -2777,7 +2775,6 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
 		}
 
 		btrfs_put_delayed_ref(ref);
-		count++;
 		cond_resched();
 	}
 
Signature · 1 line
2.14.3
#4 lore raw

Re: [PATCH 04/22] btrfs: only track ref_heads in delayed_ref_updates

Quoted 29 lines · Josef Bacik

From: Josef Bacik <[email protected]>

We use this number to figure out how many delayed refs to run, but __btrfs_run_delayed_refs really only checks every time we need a new delayed ref head, so we always run at least one ref head completely no matter what the number of items on it. So instead track only the ref heads added by this trans handle and adjust the counting appropriately in __btrfs_run_delayed_refs.

---
 fs/btrfs/delayed-ref.c | 3 ---
 fs/btrfs/extent-tree.c | 5 +----
 2 files changed, 1 insertion(+), 7 deletions(-)
fs/btrfs/delayed-ref.c +0 -2
index e1b322d651dd..e5a8fb7ba9f6 100644
@@ -234,8 +234,6 @@ static inline void drop_delayed_ref(struct btrfs_trans_handle *trans,
 	ref->in_tree = 0;
 	btrfs_put_delayed_ref(ref);
 	atomic_dec(&delayed_refs->num_entries);
-	if (trans->delayed_ref_updates)
-		trans->delayed_ref_updates--;
 }
 
 static bool merge_ref(struct btrfs_trans_handle *trans,

fs/btrfs/delayed-ref.c static int insert_delayed_ref(struct btrfs_trans_handle *trans, · line 463

Quoted 7 lines
fs/btrfs/delayed-ref.c +0 -1
@@ -460,7 +458,6 @@ static int insert_delayed_ref(struct btrfs_trans_handle *trans,
 	if (ref->action == BTRFS_ADD_DELAYED_REF)
 		list_add_tail(&ref->add_list, &href->ref_add_list);
 	atomic_inc(&root->num_entries);
-	trans->delayed_ref_updates++;
 	spin_unlock(&href->lock);
 	return ret;
 }

This seems unrelated to what the changelog is talking about.

Quoted 45 lines
fs/btrfs/extent-tree.c +1 -4
index 33cd43dd73ee..168312ead4ae 100644
@@ -2667,6 +2667,7 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
 				spin_unlock(&delayed_refs->lock);
 				break;
 			}
+			count++;
 
 			/* grab the lock that says we are going to process
 			 * all the refs for this head */
@@ -2680,7 +2681,6 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
 			 */
 			if (ret == -EAGAIN) {
 				locked_ref = NULL;
-				count++;
 				continue;
 			}
 		}
@@ -2708,7 +2708,6 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
 			unselect_delayed_ref_head(delayed_refs, locked_ref);
 			locked_ref = NULL;
 			cond_resched();
-			count++;
 			continue;
 		}
 
@@ -2726,7 +2725,6 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
 				return ret;
 			}
 			locked_ref = NULL;
-			count++;
 			continue;
 		}
 
@@ -2777,7 +2775,6 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
 		}
 
 		btrfs_put_delayed_ref(ref);
-		count++;
 		cond_resched();
 	}
 
#5 lore raw

PATCH 5/22 btrfs: introduce delayed_refs_rsv

In reply to Josef Bacik

From: Josef Bacik <[email protected]>

Traditionally we've had voodoo in btrfs to account for the space that delayed refs may take up by having a global_block_rsv. This works most of the time, except when it doesn't. We've had issues reported and seen in production where sometimes the global reserve is exhausted during transaction commit before we can run all of our delayed refs, resulting in an aborted transaction. Because of this voodoo we have equally dubious flushing semantics around throttling delayed refs which we often get wrong.

So instead give them their own block_rsv. This way we can always know exactly how much outstanding space we need for delayed refs. This allows us to make sure we are constantly filling that reservation up with space, and allows us to put more precise pressure on the enospc system. Instead of doing math to see if its a good time to throttle, the normal enospc code will be invoked if we have a lot of delayed refs pending, and they will be run via the normal flushing mechanism.

For now the delayed_refs_rsv will hold the reservations for the delayed refs, the block group updates, and deleting csums. We could have a separate rsv for the block group updates, but the csum deletion stuff is still handled via the delayed_refs so that will stay there.

---
 fs/btrfs/ctree.h             |  24 +++-
 fs/btrfs/delayed-ref.c       |  28 ++++-
 fs/btrfs/disk-io.c           |   5 +
 fs/btrfs/extent-tree.c       | 270 +++++++++++++++++++++++++++++++++++--------
 fs/btrfs/transaction.c       |  68 +++++------
 include/trace/events/btrfs.h |   2 +
 6 files changed, 297 insertions(+), 100 deletions(-)
fs/btrfs/ctree.h +18 -6
index 118346aceea9..b33f851fb97d 100644
@@ -448,8 +448,9 @@ struct btrfs_space_info {
 #define	BTRFS_BLOCK_RSV_TRANS		3
 #define	BTRFS_BLOCK_RSV_CHUNK		4
 #define	BTRFS_BLOCK_RSV_DELOPS		5
-#define	BTRFS_BLOCK_RSV_EMPTY		6
-#define	BTRFS_BLOCK_RSV_TEMP		7
+#define BTRFS_BLOCK_RSV_DELREFS		6
+#define	BTRFS_BLOCK_RSV_EMPTY		7
+#define	BTRFS_BLOCK_RSV_TEMP		8
 
 struct btrfs_block_rsv {
 	u64 size;
@@ -790,6 +791,8 @@ struct btrfs_fs_info {
 	struct btrfs_block_rsv chunk_block_rsv;
 	/* block reservation for delayed operations */
 	struct btrfs_block_rsv delayed_block_rsv;
+	/* block reservation for delayed refs */
+	struct btrfs_block_rsv delayed_refs_rsv;
 
 	struct btrfs_block_rsv empty_block_rsv;
 
@@ -2748,10 +2751,12 @@ enum btrfs_reserve_flush_enum {
 enum btrfs_flush_state {
 	FLUSH_DELAYED_ITEMS_NR	=	1,
 	FLUSH_DELAYED_ITEMS	=	2,
-	FLUSH_DELALLOC		=	3,
-	FLUSH_DELALLOC_WAIT	=	4,
-	ALLOC_CHUNK		=	5,
-	COMMIT_TRANS		=	6,
+	FLUSH_DELAYED_REFS_NR	=	3,
+	FLUSH_DELAYED_REFS	=	4,
+	FLUSH_DELALLOC		=	5,
+	FLUSH_DELALLOC_WAIT	=	6,
+	ALLOC_CHUNK		=	7,
+	COMMIT_TRANS		=	8,
 };
 
 int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes);
@@ -2803,6 +2808,13 @@ int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
 void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
 			     struct btrfs_block_rsv *block_rsv,
 			     u64 num_bytes);
+void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr);
+void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
+int btrfs_refill_delayed_block_rsv(struct btrfs_fs_info *fs_info,
+				   enum btrfs_reserve_flush_enum flush);
+void btrfs_migrate_to_delayed_block_rsv(struct btrfs_fs_info *fs_info,
+					struct btrfs_block_rsv *src,
+					u64 num_bytes);
 int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info,
 			     struct btrfs_block_group_cache *cache);
 void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache);
fs/btrfs/delayed-ref.c +23 -5
index e5a8fb7ba9f6..1ab12e8876ed 100644
@@ -467,11 +467,14 @@ static int insert_delayed_ref(struct btrfs_trans_handle *trans,
  * existing and update must have the same bytenr
  */
 static noinline void
-update_existing_head_ref(struct btrfs_delayed_ref_root *delayed_refs,
+update_existing_head_ref(struct btrfs_trans_handle *trans,
 			 struct btrfs_delayed_ref_head *existing,
 			 struct btrfs_delayed_ref_head *update,
 			 int *old_ref_mod_ret)
 {
+	struct btrfs_delayed_ref_root *delayed_refs =
+		&trans->transaction->delayed_refs;
+	struct btrfs_fs_info *fs_info = trans->fs_info;
 	int old_ref_mod;
 
 	BUG_ON(existing->is_data != update->is_data);
@@ -529,10 +532,18 @@ update_existing_head_ref(struct btrfs_delayed_ref_root *delayed_refs,
 	 * versa we need to make sure to adjust pending_csums accordingly.
 	 */
 	if (existing->is_data) {
-		if (existing->total_ref_mod >= 0 && old_ref_mod < 0)
+		u64 csum_items =
+			btrfs_csum_bytes_to_leaves(fs_info,
+						   existing->num_bytes);
+
+		if (existing->total_ref_mod >= 0 && old_ref_mod < 0) {
 			delayed_refs->pending_csums -= existing->num_bytes;
-		if (existing->total_ref_mod < 0 && old_ref_mod >= 0)
+			btrfs_delayed_refs_rsv_release(fs_info, csum_items);
+		}
+		if (existing->total_ref_mod < 0 && old_ref_mod >= 0) {
 			delayed_refs->pending_csums += existing->num_bytes;
+			trans->delayed_ref_updates += csum_items;
+		}
 	}
 	spin_unlock(&existing->lock);
 }
@@ -638,7 +649,7 @@ add_delayed_ref_head(struct btrfs_trans_handle *trans,
 			&& head_ref->qgroup_reserved
 			&& existing->qgroup_ref_root
 			&& existing->qgroup_reserved);
-		update_existing_head_ref(delayed_refs, existing, head_ref,
+		update_existing_head_ref(trans, existing, head_ref,
 					 old_ref_mod);
 		/*
 		 * we've updated the existing ref, free the newly
@@ -649,8 +660,12 @@ add_delayed_ref_head(struct btrfs_trans_handle *trans,
 	} else {
 		if (old_ref_mod)
 			*old_ref_mod = 0;
-		if (head_ref->is_data && head_ref->ref_mod < 0)
+		if (head_ref->is_data && head_ref->ref_mod < 0) {
 			delayed_refs->pending_csums += head_ref->num_bytes;
+			trans->delayed_ref_updates +=
+				btrfs_csum_bytes_to_leaves(trans->fs_info,
+							   head_ref->num_bytes);
+		}
 		delayed_refs->num_heads++;
 		delayed_refs->num_heads_ready++;
 		atomic_inc(&delayed_refs->num_entries);
@@ -779,6 +794,7 @@ int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
 
 	ret = insert_delayed_ref(trans, delayed_refs, head_ref, &ref->node);
 	spin_unlock(&delayed_refs->lock);
+	btrfs_update_delayed_refs_rsv(trans);
 
 	trace_add_delayed_tree_ref(fs_info, &ref->node, ref,
 				   action == BTRFS_ADD_DELAYED_EXTENT ?
@@ -867,6 +883,7 @@ int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
 
 	ret = insert_delayed_ref(trans, delayed_refs, head_ref, &ref->node);
 	spin_unlock(&delayed_refs->lock);
+	btrfs_update_delayed_refs_rsv(trans);
 
 	trace_add_delayed_data_ref(trans->fs_info, &ref->node, ref,
 				   action == BTRFS_ADD_DELAYED_EXTENT ?
@@ -904,6 +921,7 @@ int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
 			     NULL, NULL, NULL);
 
 	spin_unlock(&delayed_refs->lock);
+	btrfs_update_delayed_refs_rsv(trans);
 	return 0;
 }
 
fs/btrfs/disk-io.c +5 -0
index 205092dc9390..6e5347fc3604 100644
@@ -2737,6 +2737,9 @@ int open_ctree(struct super_block *sb,
 	btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
 	btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
 			     BTRFS_BLOCK_RSV_DELOPS);
+	btrfs_init_block_rsv(&fs_info->delayed_refs_rsv,
+			     BTRFS_BLOCK_RSV_DELREFS);
+
 	atomic_set(&fs_info->async_delalloc_pages, 0);
 	atomic_set(&fs_info->defrag_running, 0);
 	atomic_set(&fs_info->qgroup_op_seq, 0);
@@ -2956,6 +2959,8 @@ int open_ctree(struct super_block *sb,
 		goto fail_alloc;
 	}
 
+	if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
+		printk(KERN_ERR "WE HAVE MIXED BGS\n");
 	/*
 	 * Needn't use the lock because there is no other task which will
 	 * update the flag.
fs/btrfs/extent-tree.c +222 -48

440 lines not on this page · raw message

fs/btrfs/transaction.c +27 -41

124 lines not on this page · raw message

include/trace/events/btrfs.h +2 -0
index 39b94ec965be..c0c536d8848a 100644
@@ -1049,6 +1049,8 @@ TRACE_EVENT(btrfs_trigger_flush,
 		{ FLUSH_DELAYED_ITEMS,		"FLUSH_DELAYED_ITEMS"},		\
 		{ FLUSH_DELALLOC,		"FLUSH_DELALLOC"},		\
 		{ FLUSH_DELALLOC_WAIT,		"FLUSH_DELALLOC_WAIT"},		\
+		{ FLUSH_DELAYED_REFS_NR,	"FLUSH_DELAYED_REFS_NR"},	\
+		{ FLUSH_DELAYED_REFS,		"FLUSH_ELAYED_REFS"},		\
 		{ ALLOC_CHUNK,			"ALLOC_CHUNK"},			\
 		{ COMMIT_TRANS,			"COMMIT_TRANS"})
 
Signature · 1 line
2.14.3
#6 lore raw

PATCH 6/22 btrfs: check if free bgs for commit

In reply to Josef Bacik

may_commit_transaction will skip committing the transaction if we don't have enough pinned space or if we're trying to find space for a SYSTEM chunk. However if we have pending free block groups in this transaction we still want to commit as we may be able to allocate a chunk to make our reservation. So instead of just returning ENOSPC, check if we have free block groups pending, and if so commit the transaction to allow us to use that free space.

---
 fs/btrfs/extent-tree.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
fs/btrfs/extent-tree.c +12 -6
index 77f6f6bff36e..523bc197c40b 100644
@@ -4897,6 +4897,7 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,
 	struct btrfs_trans_handle *trans;
 	u64 bytes;
 	u64 reclaim_bytes = 0;
+	bool do_commit = true;
 
 	trans = (struct btrfs_trans_handle *)current->journal_info;
 	if (trans)
@@ -4924,8 +4925,10 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,
 	 * See if there is some space in the delayed insertion reservation for
 	 * this reservation.
 	 */
-	if (space_info != delayed_rsv->space_info)
-		return -ENOSPC;
+	if (space_info != delayed_rsv->space_info) {
+		do_commit = false;
+		goto commit;
+	}
 
 	spin_lock(&delayed_rsv->lock);
 	reclaim_bytes += delayed_rsv->reserved;
@@ -4939,15 +4942,18 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,
 	bytes -= reclaim_bytes;
 
 	if (percpu_counter_compare(&space_info->total_bytes_pinned,
-				   bytes) < 0) {
-		return -ENOSPC;
-	}
-
+				   bytes) < 0)
+		do_commit = false;
 commit:
 	trans = btrfs_join_transaction(fs_info->extent_root);
 	if (IS_ERR(trans))
 		return -ENOSPC;
 
+	if (!do_commit &&
+	    !test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags)) {
+		btrfs_end_transaction(trans);
+		return -ENOSPC;
+	}
 	return btrfs_commit_transaction(trans);
 }
 
Signature · 1 line
2.14.3
#7 lore raw

PATCH 7/22 btrfs: don't leak ret from do_chunk_alloc

In reply to Josef Bacik

If we're trying to make a data reservation and we have to allocate a data chunk we could leak ret == 1, as do_chunk_alloc() will return 1 if it allocated a chunk. Since the end of the function is the success path just return 0.

---
 fs/btrfs/extent-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent-tree.c +1 -1
index 523bc197c40b..6de9a180abdd 100644
@@ -4360,7 +4360,7 @@ int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
 				      data_sinfo->flags, bytes, 1);
 	spin_unlock(&data_sinfo->lock);
 
-	return ret;
+	return 0;
 }
 
 int btrfs_check_data_free_space(struct inode *inode,
Signature · 1 line
2.14.3
Reviewed-by on v1 7/22
#8 lore raw

Re: [PATCH 07/22] btrfs: don't leak ret from do_chunk_alloc

Quoted 6 lines · Josef Bacik

If we're trying to make a data reservation and we have to allocate a data chunk we could leak ret == 1, as do_chunk_alloc() will return 1 if it allocated a chunk. Since the end of the function is the success path just return 0.

The logic flow in this function is a steaming pile of turd and is in dire need of refactoring...

Quoted 18 lines
---
 fs/btrfs/extent-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent-tree.c +1 -1
index 523bc197c40b..6de9a180abdd 100644
@@ -4360,7 +4360,7 @@ int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
 				      data_sinfo->flags, bytes, 1);
 	spin_unlock(&data_sinfo->lock);
 
-	return ret;
+	return 0;
 }
 
 int btrfs_check_data_free_space(struct inode *inode,
#9 lore raw

Re: [PATCH 07/22] btrfs: don't leak ret from do_chunk_alloc

Quoted 8 lines · Nikolay Borisov
Quoted 5 lines · Josef Bacik

If we're trying to make a data reservation and we have to allocate a data chunk we could leak ret == 1, as do_chunk_alloc() will return 1 if it allocated a chunk. Since the end of the function is the success path just return 0.

Quoted 6 lines
Quoted 1 line

The logic flow in this function is a steaming pile of turd and is in dire need of refactoring...

Agreed, fixing the default return code seems like the right fix now, compared to untangling the gotos. Patch added to misc-next.

#10 lore raw

PATCH 8/22 btrfs: dump block_rsv whe dumping space info

In reply to Josef Bacik

For enospc_debug having the block rsvs is super helpful to see if we've done something wrong.

---
 fs/btrfs/extent-tree.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
fs/btrfs/extent-tree.c +15 -0
index 6de9a180abdd..35c1c1b7ba1d 100644
@@ -8050,6 +8050,15 @@ static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
 	return ret;
 }
 
+static void dump_block_rsv(struct btrfs_block_rsv *rsv)
+{
+	spin_lock(&rsv->lock);
+	printk(KERN_ERR "%d: size %llu reserved %llu\n",
+	       rsv->type, (unsigned long long)rsv->size,
+	       (unsigned long long)rsv->reserved);
+	spin_unlock(&rsv->lock);
+}
+
 static void dump_space_info(struct btrfs_fs_info *fs_info,
 			    struct btrfs_space_info *info, u64 bytes,
 			    int dump_block_groups)
@@ -8069,6 +8078,12 @@ static void dump_space_info(struct btrfs_fs_info *fs_info,
 		info->bytes_readonly);
 	spin_unlock(&info->lock);
 
+	dump_block_rsv(&fs_info->global_block_rsv);
+	dump_block_rsv(&fs_info->trans_block_rsv);
+	dump_block_rsv(&fs_info->chunk_block_rsv);
+	dump_block_rsv(&fs_info->delayed_block_rsv);
+	dump_block_rsv(&fs_info->delayed_refs_rsv);
+
 	if (!dump_block_groups)
 		return;
 
Signature · 1 line
2.14.3
#11 lore raw

Re: [PATCH 08/22] btrfs: dump block_rsv whe dumping space info

Quoted 13 lines · Josef Bacik

For enospc_debug having the block rsvs is super helpful to see if we've done something wrong.

---
 fs/btrfs/extent-tree.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
fs/btrfs/extent-tree.c +0 -0
index 6de9a180abdd..35c1c1b7ba1d 100644

fs/btrfs/extent-tree.c static noinline int find_free_extent(struct btrfs_fs_info *fs_info, · line 8058

Quoted 9 lines
fs/btrfs/extent-tree.c +6 -0
@@ -8050,6 +8050,15 @@ static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
 	return ret;
 }
 
+static void dump_block_rsv(struct btrfs_block_rsv *rsv)
+{
+	spin_lock(&rsv->lock);
+	printk(KERN_ERR "%d: size %llu reserved %llu\n",
+	       rsv->type, (unsigned long long)rsv->size,
+	       (unsigned long long)rsv->reserved);

Why do you cast to unsigned long long given that size/reserved are already defined as u64? Also use pr_err rather than printk.

Quoted 20 lines
fs/btrfs/extent-tree.c +9 -0
+	spin_unlock(&rsv->lock);
+}
+
 static void dump_space_info(struct btrfs_fs_info *fs_info,
 			    struct btrfs_space_info *info, u64 bytes,
 			    int dump_block_groups)
@@ -8069,6 +8078,12 @@ static void dump_space_info(struct btrfs_fs_info *fs_info,
 		info->bytes_readonly);
 	spin_unlock(&info->lock);
 
+	dump_block_rsv(&fs_info->global_block_rsv);
+	dump_block_rsv(&fs_info->trans_block_rsv);
+	dump_block_rsv(&fs_info->chunk_block_rsv);
+	dump_block_rsv(&fs_info->delayed_block_rsv);
+	dump_block_rsv(&fs_info->delayed_refs_rsv);
+
 	if (!dump_block_groups)
 		return;
 
#12 lore raw

Re: [PATCH 08/22] btrfs: dump block_rsv whe dumping space info

In reply to Josef Bacik
Quoted 13 lines · Josef Bacik

For enospc_debug having the block rsvs is super helpful to see if we've done something wrong.

---
 fs/btrfs/extent-tree.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
fs/btrfs/extent-tree.c +0 -0
index 6de9a180abdd..35c1c1b7ba1d 100644

fs/btrfs/extent-tree.c static noinline int find_free_extent(struct btrfs_fs_info *fs_info, · line 8058

Quoted 9 lines
fs/btrfs/extent-tree.c +6 -0
@@ -8050,6 +8050,15 @@ static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
 	return ret;
 }
 
+static void dump_block_rsv(struct btrfs_block_rsv *rsv)
+{
+	spin_lock(&rsv->lock);
+	printk(KERN_ERR "%d: size %llu reserved %llu\n",
+	       rsv->type, (unsigned long long)rsv->size,
+	       (unsigned long long)rsv->reserved);

As you have fs_info available in the caller, you can pass it and use the btrfs_err helper that print the filesystem identification.

Quoted 26 lines
fs/btrfs/extent-tree.c +9 -0
+	spin_unlock(&rsv->lock);
+}
+
 static void dump_space_info(struct btrfs_fs_info *fs_info,
 			    struct btrfs_space_info *info, u64 bytes,
 			    int dump_block_groups)
@@ -8069,6 +8078,12 @@ static void dump_space_info(struct btrfs_fs_info *fs_info,
 		info->bytes_readonly);
 	spin_unlock(&info->lock);
 
+	dump_block_rsv(&fs_info->global_block_rsv);
+	dump_block_rsv(&fs_info->trans_block_rsv);
+	dump_block_rsv(&fs_info->chunk_block_rsv);
+	dump_block_rsv(&fs_info->delayed_block_rsv);
+	dump_block_rsv(&fs_info->delayed_refs_rsv);
+
 	if (!dump_block_groups)
 		return;
 
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#13 lore raw

PATCH 9/22 btrfs: release metadata before running delayed refs

In reply to Josef Bacik

We want to release the unused reservation we have since it refills the delayed refs reserve, which will make everything go smoother when running the delayed refs if we're short on our reservation.

---
 fs/btrfs/transaction.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/transaction.c +3 -3
index 337397f99c95..4b171d8a7554 100644
@@ -1916,6 +1916,9 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 		return ret;
 	}
 
+	btrfs_trans_release_metadata(trans);
+	trans->block_rsv = NULL;
+
 	/* make a pass through all the delayed refs we have so far
 	 * any runnings procs may add more while we are here
 	 */
@@ -1925,9 +1928,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 		return ret;
 	}
 
-	btrfs_trans_release_metadata(trans);
-	trans->block_rsv = NULL;
-
 	cur_trans = trans->transaction;
 
 	/*
Signature · 1 line
2.14.3
#14 lore raw

PATCH 10/22 btrfs: alloc space cache inode with GFP_NOFS

In reply to Josef Bacik

If we're allocating a new space cache inode it's likely going to be under a transaction handle, so we need to use GFP_NOFS to keep from deadlocking. Otherwise GFP_KERNEL is fine.

---
 fs/btrfs/free-space-cache.c | 5 +++++
 fs/btrfs/inode.c            | 3 ++-
 fs/btrfs/transaction.h      | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
fs/btrfs/free-space-cache.c +5 -0
index d5f80cb300be..13bc514e4e16 100644
@@ -68,7 +68,12 @@ static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
 	btrfs_disk_key_to_cpu(&location, &disk_key);
 	btrfs_release_path(path);
 
+	/* We need this set so that we use GFP_NOFS when allocating our inode. */
+	if (current->journal_info == NULL)
+		current->journal_info = BTRFS_TRANS_STUB;
 	inode = btrfs_iget(fs_info->sb, &location, root, NULL);
+	if (current->journal_info == BTRFS_TRANS_STUB)
+		current->journal_info = NULL;
 	if (IS_ERR(inode))
 		return inode;
 	if (is_bad_inode(inode)) {
fs/btrfs/inode.c +2 -1
index eba61bcb9bb3..14ecfe5d6110 100644
@@ -9211,8 +9211,9 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
 	struct btrfs_inode *ei;
 	struct inode *inode;
+	gfp_t flags = (current->journal_info) ? GFP_NOFS : GFP_KERNEL;
 
-	ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
+	ei = kmem_cache_alloc(btrfs_inode_cachep, flags);
 	if (!ei)
 		return NULL;
 
fs/btrfs/transaction.h +1 -0
index 94439482a0ec..172ff923bf15 100644
@@ -106,6 +106,7 @@ struct btrfs_transaction {
 #define TRANS_EXTWRITERS	(__TRANS_START | __TRANS_ATTACH)
 
 #define BTRFS_SEND_TRANS_STUB	((void *)1)
+#define BTRFS_TRANS_STUB	((void *)2)
 
 struct btrfs_trans_handle {
 	u64 transid;
Signature · 1 line
2.14.3
#15 lore raw

Re: [PATCH 10/22] btrfs: alloc space cache inode with GFP_NOFS

Quoted 16 lines · Josef Bacik

If we're allocating a new space cache inode it's likely going to be under a transaction handle, so we need to use GFP_NOFS to keep from deadlocking. Otherwise GFP_KERNEL is fine.

---
 fs/btrfs/free-space-cache.c | 5 +++++
 fs/btrfs/inode.c            | 3 ++-
 fs/btrfs/transaction.h      | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
fs/btrfs/free-space-cache.c +0 -0
index d5f80cb300be..13bc514e4e16 100644

fs/btrfs/free-space-cache.c static struct inode *__lookup_free_space_inode(struct btrfs_root *root, · line 76

Quoted 9 lines
fs/btrfs/free-space-cache.c +5 -0
@@ -68,7 +68,12 @@ static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
 	btrfs_disk_key_to_cpu(&location, &disk_key);
 	btrfs_release_path(path);
 
+	/* We need this set so that we use GFP_NOFS when allocating our inode. */
+	if (current->journal_info == NULL)
+		current->journal_info = BTRFS_TRANS_STUB;
 	inode = btrfs_iget(fs_info->sb, &location, root, NULL);
+	if (current->journal_info == BTRFS_TRANS_STUB)
+		current->journal_info = NULL;

This is not safe in the face of stacked filesystem, i.e ext4 uses the journal_info.

Quoted 8 lines
fs/btrfs/free-space-cache.c +0 -0
 	if (IS_ERR(inode))
 		return inode;
 	if (is_bad_inode(inode)) {
fs/btrfs/inode.c +0 -0
index eba61bcb9bb3..14ecfe5d6110 100644

fs/btrfs/inode.c struct inode *btrfs_alloc_inode(struct super_block *sb) · line 9216

Quoted 7 lines
fs/btrfs/inode.c +2 -1
@@ -9211,8 +9211,9 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
 	struct btrfs_inode *ei;
 	struct inode *inode;
+	gfp_t flags = (current->journal_info) ? GFP_NOFS : GFP_KERNEL;
 
-	ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
+	ei = kmem_cache_alloc(btrfs_inode_cachep, flags);

Why don't you just hardcode GFP_NOFS? We should be striving at removing abuse of ->journal_info than proliferating it.

Quoted 16 lines
fs/btrfs/inode.c +0 -0
 	if (!ei)
 		return NULL;
 
fs/btrfs/transaction.h +1 -0
index 94439482a0ec..172ff923bf15 100644
@@ -106,6 +106,7 @@ struct btrfs_transaction {
 #define TRANS_EXTWRITERS	(__TRANS_START | __TRANS_ATTACH)
 
 #define BTRFS_SEND_TRANS_STUB	((void *)1)
+#define BTRFS_TRANS_STUB	((void *)2)
 
 struct btrfs_trans_handle {
 	u64 transid;
#16 lore raw

Re: [PATCH 10/22] btrfs: alloc space cache inode with GFP_NOFS

Quoted 44 lines · Nikolay Borisov
Quoted 16 lines · Josef Bacik

If we're allocating a new space cache inode it's likely going to be under a transaction handle, so we need to use GFP_NOFS to keep from deadlocking. Otherwise GFP_KERNEL is fine.

---
 fs/btrfs/free-space-cache.c | 5 +++++
 fs/btrfs/inode.c            | 3 ++-
 fs/btrfs/transaction.h      | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
fs/btrfs/free-space-cache.c +0 -0
index d5f80cb300be..13bc514e4e16 100644

fs/btrfs/free-space-cache.c static struct inode *__lookup_free_space_inode(struct btrfs_root *root, · line 76

Quoted 9 lines
fs/btrfs/free-space-cache.c +5 -0
@@ -68,7 +68,12 @@ static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
 	btrfs_disk_key_to_cpu(&location, &disk_key);
 	btrfs_release_path(path);
 
+	/* We need this set so that we use GFP_NOFS when allocating our inode. */
+	if (current->journal_info == NULL)
+		current->journal_info = BTRFS_TRANS_STUB;
 	inode = btrfs_iget(fs_info->sb, &location, root, NULL);
+	if (current->journal_info == BTRFS_TRANS_STUB)
+		current->journal_info = NULL;

This is not safe in the face of stacked filesystem, i.e ext4 uses the journal_info.

Quoted 13 lines
fs/btrfs/free-space-cache.c +0 -0
 	if (IS_ERR(inode))
 		return inode;
 	if (is_bad_inode(inode)) {
fs/btrfs/inode.c +1 -0
index eba61bcb9bb3..14ecfe5d6110 100644
@@ -9211,8 +9211,9 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
 	struct btrfs_inode *ei;
 	struct inode *inode;
+	gfp_t flags = (current->journal_info) ? GFP_NOFS : GFP_KERNEL;
 
Quoted 5 lines

fs/btrfs/inode.c struct inode *btrfs_alloc_inode(struct super_block *sb) · line 9216

Quoted 2 lines
fs/btrfs/inode.c +1 -1
-	ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
+	ei = kmem_cache_alloc(btrfs_inode_cachep, flags);

Why don't you just hardcode GFP_NOFS? We should be striving at removing abuse of ->journal_info than proliferating it.

We're also removing unnecessary use of GFP_NOFS. The right way here is to use memalloc_nofs_save/memalloc_nofs_restore around btrfs_iget.

#17 lore raw

Re: [PATCH 10/22] btrfs: alloc space cache inode with GFP_NOFS

Quoted 45 lines · David Sterba
Quoted 44 lines · Nikolay Borisov
Quoted 16 lines · Josef Bacik

If we're allocating a new space cache inode it's likely going to be under a transaction handle, so we need to use GFP_NOFS to keep from deadlocking. Otherwise GFP_KERNEL is fine.

---
 fs/btrfs/free-space-cache.c | 5 +++++
 fs/btrfs/inode.c            | 3 ++-
 fs/btrfs/transaction.h      | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
fs/btrfs/free-space-cache.c +0 -0
index d5f80cb300be..13bc514e4e16 100644

fs/btrfs/free-space-cache.c static struct inode *__lookup_free_space_inode(struct btrfs_root *root, · line 76

Quoted 9 lines
fs/btrfs/free-space-cache.c +5 -0
@@ -68,7 +68,12 @@ static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
 	btrfs_disk_key_to_cpu(&location, &disk_key);
 	btrfs_release_path(path);
 
+	/* We need this set so that we use GFP_NOFS when allocating our inode. */
+	if (current->journal_info == NULL)
+		current->journal_info = BTRFS_TRANS_STUB;
 	inode = btrfs_iget(fs_info->sb, &location, root, NULL);
+	if (current->journal_info == BTRFS_TRANS_STUB)
+		current->journal_info = NULL;

This is not safe in the face of stacked filesystem, i.e ext4 uses the journal_info.

Quoted 13 lines
fs/btrfs/free-space-cache.c +0 -0
 	if (IS_ERR(inode))
 		return inode;
 	if (is_bad_inode(inode)) {
fs/btrfs/inode.c +1 -0
index eba61bcb9bb3..14ecfe5d6110 100644
@@ -9211,8 +9211,9 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
 	struct btrfs_inode *ei;
 	struct inode *inode;
+	gfp_t flags = (current->journal_info) ? GFP_NOFS : GFP_KERNEL;
 
Quoted 8 lines
Quoted 5 lines

fs/btrfs/inode.c struct inode *btrfs_alloc_inode(struct super_block *sb) · line 9216

Quoted 2 lines
fs/btrfs/inode.c +1 -1
-	ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
+	ei = kmem_cache_alloc(btrfs_inode_cachep, flags);

Why don't you just hardcode GFP_NOFS? We should be striving at removing abuse of ->journal_info than proliferating it.

We're also removing unnecessary use of GFP_NOFS. The right way here is to use memalloc_nofs_save/memalloc_nofs_restore around btrfs_iget.

Huh I didn't know this existed, that's neat. I'll change this patch to do that, thanks,

Josef

#18 lore raw

Re: [PATCH 10/22] btrfs: alloc space cache inode with GFP_NOFS

In reply to Nikolay Borisov
Quoted 22 lines · Nikolay Borisov
Quoted 19 lines · Josef Bacik

If we're allocating a new space cache inode it's likely going to be under a transaction handle, so we need to use GFP_NOFS to keep from deadlocking. Otherwise GFP_KERNEL is fine.

---
 fs/btrfs/free-space-cache.c | 5 +++++
 fs/btrfs/inode.c            | 3 ++-
 fs/btrfs/transaction.h      | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
fs/btrfs/free-space-cache.c +0 -0
index d5f80cb300be..13bc514e4e16 100644
@@ -68,7 +68,12 @@ static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
 	btrfs_disk_key_to_cpu(&location, &disk_key);
 	btrfs_release_path(path);
 
Quoted 8 lines

fs/btrfs/free-space-cache.c static struct inode *__lookup_free_space_inode(struct btrfs_root *root, · line 76

Quoted 6 lines
fs/btrfs/free-space-cache.c +5 -0
+	/* We need this set so that we use GFP_NOFS when allocating our inode. */
+	if (current->journal_info == NULL)
+		current->journal_info = BTRFS_TRANS_STUB;
 	inode = btrfs_iget(fs_info->sb, &location, root, NULL);
+	if (current->journal_info == BTRFS_TRANS_STUB)
+		current->journal_info = NULL;

This is not safe in the face of stacked filesystem, i.e ext4 uses the journal_info.

Stacked file systems arent safe at all because we all use journal_info, this doesn't change that.

Quoted 14 lines
Quoted 14 lines
fs/btrfs/free-space-cache.c +0 -0
 	if (IS_ERR(inode))
 		return inode;
 	if (is_bad_inode(inode)) {
fs/btrfs/inode.c +1 -0
index eba61bcb9bb3..14ecfe5d6110 100644
@@ -9211,8 +9211,9 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
 	struct btrfs_inode *ei;
 	struct inode *inode;
+	gfp_t flags = (current->journal_info) ? GFP_NOFS : GFP_KERNEL;
 
Quoted 6 lines

fs/btrfs/inode.c struct inode *btrfs_alloc_inode(struct super_block *sb) · line 9216

Quoted 2 lines
fs/btrfs/inode.c +1 -1
-	ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
+	ei = kmem_cache_alloc(btrfs_inode_cachep, flags);

Why don't you just hardcode GFP_NOFS? We should be striving at removing abuse of ->journal_info than proliferating it.

Because every time I use GFP_NOFS some mm guy shows up and complains. I'm fine with doing GFP_NOFS, but the vast majority of allocations are in paths that GFP_KERNEL is fine. Thanks,

Josef

#19 lore raw

PATCH 11/22 btrfs: fix truncate throttling

In reply to Josef Bacik

We have a bunch of magic to make sure we're throttling delayed refs when truncating a file. Now that we have a delayed refs rsv and a mechanism for refilling that reserve simply use that instead of all of this magic.

---
 fs/btrfs/inode.c | 78 ++++++++++++--------------------------------------------
 1 file changed, 16 insertions(+), 62 deletions(-)
fs/btrfs/inode.c +16 -62
index 14ecfe5d6110..062807baf2ed 100644
@@ -4481,31 +4481,6 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
 	return err;
 }
 
-static int truncate_space_check(struct btrfs_trans_handle *trans,
-				struct btrfs_root *root,
-				u64 bytes_deleted)
-{
-	struct btrfs_fs_info *fs_info = root->fs_info;
-	int ret;
-
-	/*
-	 * This is only used to apply pressure to the enospc system, we don't
-	 * intend to use this reservation at all.
-	 */
-	bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
-	bytes_deleted *= fs_info->nodesize;
-	ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
-				  bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
-	if (!ret) {
-		trace_btrfs_space_reservation(fs_info, "transaction",
-					      trans->transid,
-					      bytes_deleted, 1);
-		trans->bytes_reserved += bytes_deleted;
-	}
-	return ret;
-
-}
-
 /*
  * Return this if we need to call truncate_block for the last bit of the
  * truncate.
@@ -4550,7 +4525,6 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
 	u64 bytes_deleted = 0;
 	bool be_nice = false;
 	bool should_throttle = false;
-	bool should_end = false;
 
 	BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
 
@@ -4763,15 +4737,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
 				btrfs_abort_transaction(trans, ret);
 				break;
 			}
-			if (btrfs_should_throttle_delayed_refs(trans, fs_info))
-				btrfs_async_run_delayed_refs(fs_info,
-					trans->delayed_ref_updates * 2,
-					trans->transid, 0);
 			if (be_nice) {
-				if (truncate_space_check(trans, root,
-							 extent_num_bytes)) {
-					should_end = true;
-				}
 				if (btrfs_should_throttle_delayed_refs(trans,
 								       fs_info))
 					should_throttle = true;
@@ -4783,7 +4749,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
 
 		if (path->slots[0] == 0 ||
 		    path->slots[0] != pending_del_slot ||
-		    should_throttle || should_end) {
+		    should_throttle) {
 			if (pending_del_nr) {
 				ret = btrfs_del_items(trans, root, path,
 						pending_del_slot,
@@ -4795,23 +4761,23 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
 				pending_del_nr = 0;
 			}
 			btrfs_release_path(path);
-			if (should_throttle) {
-				unsigned long updates = trans->delayed_ref_updates;
-				if (updates) {
-					trans->delayed_ref_updates = 0;
-					ret = btrfs_run_delayed_refs(trans,
-								   updates * 2);
-					if (ret)
-						break;
-				}
-			}
+
 			/*
-			 * if we failed to refill our space rsv, bail out
-			 * and let the transaction restart
+			 * We can generate a lot of delayed refs, so we need to
+			 * throttle every once and a while and make sure we're
+			 * adding enough space to keep up with the work we are
+			 * generating.  Since we hold a transaction here we can
+			 * only FLUSH_LIMIT, if this fails we just return EAGAIN
+			 * and let the normal space allocation stuff do it's
+			 * work.
 			 */
-			if (should_end) {
-				ret = -EAGAIN;
-				break;
+			if (should_throttle) {
+				ret = btrfs_refill_delayed_block_rsv(fs_info,
+							BTRFS_RESERVE_FLUSH_LIMIT);
+				if (ret) {
+					ret = -EAGAIN;
+					break;
+				}
 			}
 			goto search_again;
 		} else {
@@ -4837,18 +4803,6 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
 	}
 
 	btrfs_free_path(path);
-
-	if (be_nice && bytes_deleted > SZ_32M && (ret >= 0 || ret == -EAGAIN)) {
-		unsigned long updates = trans->delayed_ref_updates;
-		int err;
-
-		if (updates) {
-			trans->delayed_ref_updates = 0;
-			err = btrfs_run_delayed_refs(trans, updates * 2);
-			if (err)
-				ret = err;
-		}
-	}
 	return ret;
 }
 
Signature · 1 line
2.14.3
#20 lore raw

PATCH 12/22 btrfs: don't use global rsv for chunk allocation

In reply to Josef Bacik

We've done this forever because of the voodoo around knowing how much space we have. However we have better ways of doing this now, and on normal file systems we'll easily have a global reserve of 512MiB, and since metadata chunks are usually 1GiB that means we'll allocate metadata chunks more readily. Instead use the actual used amount when determining if we need to allocate a chunk or not.

---
 fs/btrfs/extent-tree.c | 9 ---------
 1 file changed, 9 deletions(-)
fs/btrfs/extent-tree.c +0 -9
index 35c1c1b7ba1d..c20675c265c5 100644
@@ -4459,21 +4459,12 @@ static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
 static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
 			      struct btrfs_space_info *sinfo, int force)
 {
-	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
 	u64 bytes_used = btrfs_space_info_used(sinfo, false);
 	u64 thresh;
 
 	if (force == CHUNK_ALLOC_FORCE)
 		return 1;
 
-	/*
-	 * We need to take into account the global rsv because for all intents
-	 * and purposes it's used space.  Don't worry about locking the
-	 * global_rsv, it doesn't change except when the transaction commits.
-	 */
-	if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
-		bytes_used += calc_global_rsv_need_space(global_rsv);
-
 	/*
 	 * in limited mode, we want to have some free space up to
 	 * about 1% of the FS size.
Signature · 1 line
2.14.3
#21 lore raw

PATCH 13/22 btrfs: reset max_extent_size properly

In reply to Josef Bacik

If we use up our block group before allocating a new one we'll easily get a max_extent_size that's set really really low, which will result in a lot of fragmentation. We need to make sure we're resetting the max_extent_size when we add a new chunk or add new space.

---
 fs/btrfs/extent-tree.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/btrfs/extent-tree.c +7 -0
index c20675c265c5..e057d9b2d959 100644
@@ -4657,6 +4657,7 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans,
 			goto out;
 	} else {
 		ret = 1;
+		space_info->max_extent_size = 0;
 	}
 
 	space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
@@ -8199,10 +8200,16 @@ static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
 	if (pin)
 		pin_down_extent(fs_info, cache, start, len, 1);
 	else {
+		struct btrfs_space_info *space_info = cache->space_info;
+
 		if (btrfs_test_opt(fs_info, DISCARD))
 			ret = btrfs_discard_extent(fs_info, start, len, NULL);
 		btrfs_add_free_space(cache, start, len);
 		btrfs_free_reserved_bytes(cache, len, delalloc);
+
+		spin_lock(&space_info->lock);
+		space_info->max_extent_size = 0;
+		spin_unlock(&space_info->lock);
 		trace_btrfs_reserved_extent_free(fs_info, start, len);
 	}
 
Signature · 1 line
2.14.3
#22 lore raw

PATCH 14/22 btrfs: don't enospc all tickets on flush failure

In reply to Josef Bacik

With the introduction of the per-inode block_rsv it became possible to have really really large reservation requests made because of data fragmentation. Since the ticket stuff assumed that we'd always have relatively small reservation requests it just killed all tickets if we were unable to satisfy the current request. However this is generally not the case anymore. So fix this logic to instead see if we had a ticket that we were able to give some reservation to, and if we were continue the flushing loop again. Likewise we make the tickets use the space_info_add_old_bytes() method of returning what reservation they did receive in hopes that it could satisfy reservations down the line.

---
 fs/btrfs/extent-tree.c | 45 +++++++++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 20 deletions(-)
fs/btrfs/extent-tree.c +25 -20
index e057d9b2d959..355884445f38 100644
@@ -4864,6 +4864,7 @@ static void shrink_delalloc(struct btrfs_fs_info *fs_info, u64 to_reclaim,
 }
 
 struct reserve_ticket {
+	u64 orig_bytes;
 	u64 bytes;
 	int error;
 	struct list_head list;
@@ -5081,7 +5082,7 @@ static inline int need_do_async_reclaim(struct btrfs_fs_info *fs_info,
 		!test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
 }
 
-static void wake_all_tickets(struct list_head *head)
+static bool wake_all_tickets(struct list_head *head)
 {
 	struct reserve_ticket *ticket;
 
@@ -5090,7 +5091,10 @@ static void wake_all_tickets(struct list_head *head)
 		list_del_init(&ticket->list);
 		ticket->error = -ENOSPC;
 		wake_up(&ticket->wait);
+		if (ticket->bytes != ticket->orig_bytes)
+			return true;
 	}
+	return false;
 }
 
 /*
@@ -5145,8 +5149,12 @@ static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
 		if (flush_state > COMMIT_TRANS) {
 			commit_cycles++;
 			if (commit_cycles > 2) {
-				wake_all_tickets(&space_info->tickets);
-				space_info->flush = 0;
+				if (wake_all_tickets(&space_info->tickets)) {
+					flush_state = FLUSH_DELAYED_ITEMS_NR;
+					commit_cycles--;
+				} else {
+					space_info->flush = 0;
+				}
 			} else {
 				flush_state = FLUSH_DELAYED_ITEMS_NR;
 			}
@@ -5198,10 +5206,11 @@ static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
 
 static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,
 			       struct btrfs_space_info *space_info,
-			       struct reserve_ticket *ticket, u64 orig_bytes)
+			       struct reserve_ticket *ticket)
 
 {
 	DEFINE_WAIT(wait);
+	u64 reclaim_bytes = 0;
 	int ret = 0;
 
 	spin_lock(&space_info->lock);
@@ -5222,14 +5231,12 @@ static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,
 		ret = ticket->error;
 	if (!list_empty(&ticket->list))
 		list_del_init(&ticket->list);
-	if (ticket->bytes && ticket->bytes < orig_bytes) {
-		u64 num_bytes = orig_bytes - ticket->bytes;
-		space_info->bytes_may_use -= num_bytes;
-		trace_btrfs_space_reservation(fs_info, "space_info",
-					      space_info->flags, num_bytes, 0);
-	}
+	if (ticket->bytes && ticket->bytes < ticket->orig_bytes)
+		reclaim_bytes = ticket->orig_bytes - ticket->bytes;
 	spin_unlock(&space_info->lock);
 
+	if (reclaim_bytes)
+		space_info_add_old_bytes(fs_info, space_info, reclaim_bytes);
 	return ret;
 }
 
@@ -5255,6 +5262,7 @@ static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
 {
 	struct reserve_ticket ticket;
 	u64 used;
+	u64 reclaim_bytes = 0;
 	int ret = 0;
 
 	ASSERT(orig_bytes);
@@ -5290,6 +5298,7 @@ static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
 	 * the list and we will do our own flushing further down.
 	 */
 	if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
+		ticket.orig_bytes = orig_bytes;
 		ticket.bytes = orig_bytes;
 		ticket.error = 0;
 		init_waitqueue_head(&ticket.wait);
@@ -5330,25 +5339,21 @@ static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
 		return ret;
 
 	if (flush == BTRFS_RESERVE_FLUSH_ALL)
-		return wait_reserve_ticket(fs_info, space_info, &ticket,
-					   orig_bytes);
+		return wait_reserve_ticket(fs_info, space_info, &ticket);
 
 	ret = 0;
 	priority_reclaim_metadata_space(fs_info, space_info, &ticket);
 	spin_lock(&space_info->lock);
 	if (ticket.bytes) {
-		if (ticket.bytes < orig_bytes) {
-			u64 num_bytes = orig_bytes - ticket.bytes;
-			space_info->bytes_may_use -= num_bytes;
-			trace_btrfs_space_reservation(fs_info, "space_info",
-						      space_info->flags,
-						      num_bytes, 0);
-
-		}
+		if (ticket.bytes < orig_bytes)
+			reclaim_bytes = orig_bytes - ticket.bytes;
 		list_del_init(&ticket.list);
 		ret = -ENOSPC;
 	}
 	spin_unlock(&space_info->lock);
+
+	if (reclaim_bytes)
+		space_info_add_old_bytes(fs_info, space_info, reclaim_bytes);
 	ASSERT(list_empty(&ticket.list));
 	return ret;
 }
Signature · 1 line
2.14.3
#23 lore raw

PATCH 15/22 btrfs: run delayed iputs before committing

In reply to Josef Bacik

We want to have a complete picture of any delayed inode updates before we make the decision to commit or not, so make sure we run delayed iputs before making the decision to commit or not.

---
 fs/btrfs/extent-tree.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/btrfs/extent-tree.c +4 -0
index 355884445f38..81396bac53f1 100644
@@ -4923,6 +4923,10 @@ static int may_commit_transaction(struct btrfs_fs_info *fs_info,
 		goto commit;
 	}
 
+	mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
+	btrfs_run_delayed_iputs(fs_info);
+	mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
+
 	spin_lock(&delayed_rsv->lock);
 	reclaim_bytes += delayed_rsv->reserved;
 	spin_unlock(&delayed_rsv->lock);
Signature · 1 line
2.14.3
#24 lore raw

Re: [PATCH 15/22] btrfs: run delayed iputs before committing

Quoted 3 lines · Josef Bacik

We want to have a complete picture of any delayed inode updates before we make the decision to commit or not, so make sure we run delayed iputs before making the decision to commit or not.

This basically says "we need to do X, so we do X", lacking the explanation why.

#25 lore raw

PATCH 16/22 btrfs: loop in inode_rsv_refill

In reply to Josef Bacik

With severe fragmentation we can end up with our inode rsv size being huge during writeout, which would cause us to need to make very large metadata reservations. However we may not actually need that much once writeout is complete. So instead try to make our reservation, and if we couldn't make it re-calculate our new reservation size and try again. If our reservation size doesn't change between tries then we know we are actually out of space and can error out.

---
 fs/btrfs/extent-tree.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
fs/btrfs/extent-tree.c +17 -2
index 81396bac53f1..66b28b29839b 100644
@@ -5860,10 +5860,11 @@ static int btrfs_inode_rsv_refill(struct btrfs_inode *inode,
 {
 	struct btrfs_root *root = inode->root;
 	struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
-	u64 num_bytes = 0;
+	u64 num_bytes = 0, last = 0;
 	u64 qgroup_num_bytes = 0;
 	int ret = -ENOSPC;
 
+again:
 	spin_lock(&block_rsv->lock);
 	if (block_rsv->reserved < block_rsv->size)
 		num_bytes = block_rsv->size - block_rsv->reserved;
@@ -5888,8 +5889,22 @@ static int btrfs_inode_rsv_refill(struct btrfs_inode *inode,
 		spin_lock(&block_rsv->lock);
 		block_rsv->qgroup_rsv_reserved += qgroup_num_bytes;
 		spin_unlock(&block_rsv->lock);
-	} else
+	} else {
 		btrfs_qgroup_free_meta_prealloc(root, qgroup_num_bytes);
+
+		/*
+		 * If we are fragmented we can end up with a lot of outstanding
+		 * extents which will make our size be much larger than our
+		 * reserved amount.  If we happen to try to do a reservation
+		 * here that may result in us trying to do a pretty hefty
+		 * reservation, which we may not need once delalloc flushing
+		 * happens.  If this is the case try and do the reserve again.
+		 */
+		if (flush == BTRFS_RESERVE_FLUSH_ALL && last != num_bytes) {
+			last = num_bytes;
+			goto again;
+		}
+	}
 	return ret;
 }
 
Signature · 1 line
2.14.3
#26 lore raw

PATCH 17/22 btrfs: don't take the dio_sem in the fsync path

In reply to Josef Bacik

Since we've changed the fsync() path to always run ordered extents before doing the tree log we no longer need to take the dio_sem in the tree log path. This gets rid of a lockdep splat that I was seeing with the AIO tests.

---
 fs/btrfs/tree-log.c | 2 --
 1 file changed, 2 deletions(-)
fs/btrfs/tree-log.c +0 -2
index f8220ec02036..aa06e1954b84 100644
@@ -4439,7 +4439,6 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
 
 	INIT_LIST_HEAD(&extents);
 
-	down_write(&inode->dio_sem);
 	write_lock(&tree->lock);
 	test_gen = root->fs_info->last_trans_committed;
 	logged_start = start;
@@ -4520,7 +4519,6 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
 	}
 	WARN_ON(!list_empty(&extents));
 	write_unlock(&tree->lock);
-	up_write(&inode->dio_sem);
 
 	btrfs_release_path(path);
 	if (!ret)
Signature · 1 line
2.14.3
#27 lore raw

Re: [PATCH 17/22] btrfs: don't take the dio_sem in the fsync path

Quoted 4 lines · Josef Bacik

Since we've changed the fsync() path to always run ordered extents before doing the tree log we no longer need to take the dio_sem in the tree log path. This gets rid of a lockdep splat that I was seeing with the AIO tests.

The dio_sem can be removed completely, it was added to synchronize lockless dio writes with fsync to avoid races.
I.e., just removing it here in the tree-log makes it useless.

thanks!

Quoted 33 lines
---
 fs/btrfs/tree-log.c | 2 --
 1 file changed, 2 deletions(-)
fs/btrfs/tree-log.c +0 -3
index f8220ec02036..aa06e1954b84 100644
@@ -4439,7 +4439,6 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
 
        INIT_LIST_HEAD(&extents);
 
-       down_write(&inode->dio_sem);
        write_lock(&tree->lock);
        test_gen = root->fs_info->last_trans_committed;
        logged_start = start;
@@ -4520,7 +4519,6 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
        }
        WARN_ON(!list_empty(&extents));
        write_unlock(&tree->lock);
-       up_write(&inode->dio_sem);
 
        btrfs_release_path(path);
        if (!ret)
--

2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”
#28 lore raw

Re: [PATCH 17/22] btrfs: don't take the dio_sem in the fsync path

In reply to Josef Bacik
Quoted 4 lines · Josef Bacik

Since we've changed the fsync() path to always run ordered extents before doing the tree log we no longer need to take the dio_sem in the tree log path. This gets rid of a lockdep splat that I was seeing with the AIO tests.

So actually, we still need it (or some other means of sync). Because even after the recent changes to fsync, the fast path still logs extent items based on the extent maps, and the dio write path creates first the extent map and then the ordered extent. So the old problem can still happen between concurrent fsync and lockless dio write, where fsync logs an extent item for an extent map whose ordered extent we never waited for.
The solution prior to the introduction of dio_sem solved this - make the dio write create first the ordered extent, and, only after it, create the extent map.

Quoted 33 lines
---
 fs/btrfs/tree-log.c | 2 --
 1 file changed, 2 deletions(-)
fs/btrfs/tree-log.c +0 -3
index f8220ec02036..aa06e1954b84 100644
@@ -4439,7 +4439,6 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
 
        INIT_LIST_HEAD(&extents);
 
-       down_write(&inode->dio_sem);
        write_lock(&tree->lock);
        test_gen = root->fs_info->last_trans_committed;
        logged_start = start;
@@ -4520,7 +4519,6 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
        }
        WARN_ON(!list_empty(&extents));
        write_unlock(&tree->lock);
-       up_write(&inode->dio_sem);
 
        btrfs_release_path(path);
        if (!ret)
--

2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”
#29 lore raw

Re: [PATCH 17/22] btrfs: don't take the dio_sem in the fsync path

Quoted 6 lines · Filipe Manana
Quoted 4 lines · Josef Bacik

Since we've changed the fsync() path to always run ordered extents before doing the tree log we no longer need to take the dio_sem in the tree log path. This gets rid of a lockdep splat that I was seeing with the AIO tests.

Quoted 11 lines

So actually, we still need it (or some other means of sync). Because even after the recent changes to fsync, the fast path still logs extent items based on the extent maps, and the dio write path creates first the extent map and then the ordered extent. So the old problem can still happen between concurrent fsync and lockless dio write, where fsync logs an extent item for an extent map whose ordered extent we never waited for.
The solution prior to the introduction of dio_sem solved this - make the dio write create first the ordered extent, and, only after it, create the extent map.

Oooh balls I see. This is still a problem even if we add the ordered extent first, because we can easily just start the lockless dio write after we've waited for ordered extents, so the order we create the extent map and ordered extent don't actually matter. We still have this lockdep thing, I think we just move the dio_sem to the start of fsync, if we're fsyncing you just don't get to do lockless dio writes while we're doing the fsync. What do you think? Thanks,

Josef

#30 lore raw

Re: [PATCH 17/22] btrfs: don't take the dio_sem in the fsync path

Quoted 19 lines · Josef Bacik
Quoted 6 lines · Filipe Manana
Quoted 4 lines · Josef Bacik

Since we've changed the fsync() path to always run ordered extents before doing the tree log we no longer need to take the dio_sem in the tree log path. This gets rid of a lockdep splat that I was seeing with the AIO tests.

Quoted 11 lines

So actually, we still need it (or some other means of sync). Because even after the recent changes to fsync, the fast path still logs extent items based on the extent maps, and the dio write path creates first the extent map and then the ordered extent. So the old problem can still happen between concurrent fsync and lockless dio write, where fsync logs an extent item for an extent map whose ordered extent we never waited for.
The solution prior to the introduction of dio_sem solved this - make the dio write create first the ordered extent, and, only after it, create the extent map.

Quoted 6 lines

Oooh balls I see. This is still a problem even if we add the ordered extent first, because we can easily just start the lockless dio write after we've waited for ordered extents, so the order we create the extent map and ordered extent don't actually matter. We still have this lockdep thing, I think we just move the dio_sem to the start of fsync, if we're fsyncing you just don't get to do lockless dio writes while we're doing the fsync. What do you think? Thanks,

On first though, it seems to be the simplest and sanest solution :) Thanks.

Quoted 2 lines

Josef

Signature · 3 lines
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”
#31 lore raw

PATCH 18/22 btrfs: add ALLOC_CHUNK_FORCE to the flushing code

In reply to Josef Bacik

With my change to no longer take into account the global reserve for metadata allocation chunks we have this side-effect for mixed block group fs'es where we are no longer allocating enough chunks for the data/metadata requirements. To deal with this add a ALLOC_CHUNK_FORCE step to the flushing state machine. This will only get used if we've already made a full loop through the flushing machinery and tried committing the transaction. If we have then we can try and force a chunk allocation since we likely need it to make progress. This resolves the issues I was seeing with the mixed bg tests in xfstests with my previous patch.

---
 fs/btrfs/ctree.h             | 3 ++-
 fs/btrfs/extent-tree.c       | 7 ++++++-
 include/trace/events/btrfs.h | 1 +
 3 files changed, 9 insertions(+), 2 deletions(-)
fs/btrfs/ctree.h +2 -1
index b33f851fb97d..95f841d8b27c 100644
@@ -2756,7 +2756,8 @@ enum btrfs_flush_state {
 	FLUSH_DELALLOC		=	5,
 	FLUSH_DELALLOC_WAIT	=	6,
 	ALLOC_CHUNK		=	7,
-	COMMIT_TRANS		=	8,
+	ALLOC_CHUNK_FORCE	=	8,
+	COMMIT_TRANS		=	9,
 };
 
 int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes);
fs/btrfs/extent-tree.c +6 -1
index 66b28b29839b..626fb6a92dda 100644
@@ -5004,6 +5004,7 @@ static void flush_space(struct btrfs_fs_info *fs_info,
 		btrfs_end_transaction(trans);
 		break;
 	case ALLOC_CHUNK:
+	case ALLOC_CHUNK_FORCE:
 		trans = btrfs_join_transaction(root);
 		if (IS_ERR(trans)) {
 			ret = PTR_ERR(trans);
@@ -5011,7 +5012,9 @@ static void flush_space(struct btrfs_fs_info *fs_info,
 		}
 		ret = do_chunk_alloc(trans, fs_info,
 				     btrfs_metadata_alloc_profile(fs_info),
-				     CHUNK_ALLOC_NO_FORCE);
+				     (state == ALLOC_CHUNK) ?
+				     CHUNK_ALLOC_NO_FORCE :
+				     CHUNK_ALLOC_FORCE);
 		btrfs_end_transaction(trans);
 		if (ret > 0 || ret == -ENOSPC)
 			ret = 0;
@@ -5164,6 +5167,8 @@ static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
 			}
 		}
 		spin_unlock(&space_info->lock);
+		if (flush_state == ALLOC_CHUNK_FORCE && !commit_cycles)
+			flush_state++;
 	} while (flush_state <= COMMIT_TRANS);
 }
 
include/trace/events/btrfs.h +1 -0
index c0c536d8848a..de38e849f963 100644
@@ -1052,6 +1052,7 @@ TRACE_EVENT(btrfs_trigger_flush,
 		{ FLUSH_DELAYED_REFS_NR,	"FLUSH_DELAYED_REFS_NR"},	\
 		{ FLUSH_DELAYED_REFS,		"FLUSH_ELAYED_REFS"},		\
 		{ ALLOC_CHUNK,			"ALLOC_CHUNK"},			\
+		{ ALLOC_CHUNK_FORCE,		"ALLOC_CHUNK_FORCE"},		\
 		{ COMMIT_TRANS,			"COMMIT_TRANS"})
 
 TRACE_EVENT(btrfs_flush_space,
Signature · 1 line
2.14.3
#32 lore raw

PATCH 19/22 btrfs: set max_extent_size properly

In reply to Josef Bacik

From: Josef Bacik <[email protected]>

We can't use entry->bytes if our entry is a bitmap entry, we need to use entry->max_extent_size in that case. Fix up all the logic to make this consistent.

---
 fs/btrfs/free-space-cache.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
fs/btrfs/free-space-cache.c +19 -10
index 13bc514e4e16..92e1c30356b9 100644
@@ -1774,6 +1774,18 @@ static int search_bitmap(struct btrfs_free_space_ctl *ctl,
 	return -1;
 }
 
+static void set_max_extent_size(struct btrfs_free_space *entry,
+				u64 *max_extent_size)
+{
+	if (entry->bitmap) {
+		if (entry->max_extent_size > *max_extent_size)
+			*max_extent_size = entry->max_extent_size;
+	} else {
+		if (entry->bytes > *max_extent_size)
+			*max_extent_size = entry->bytes;
+	}
+}
+
 /* Cache the size of the max extent in bytes */
 static struct btrfs_free_space *
 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
@@ -1795,8 +1807,7 @@ find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
 	for (node = &entry->offset_index; node; node = rb_next(node)) {
 		entry = rb_entry(node, struct btrfs_free_space, offset_index);
 		if (entry->bytes < *bytes) {
-			if (entry->bytes > *max_extent_size)
-				*max_extent_size = entry->bytes;
+			set_max_extent_size(entry, max_extent_size);
 			continue;
 		}
 
@@ -1814,8 +1825,7 @@ find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
 		}
 
 		if (entry->bytes < *bytes + align_off) {
-			if (entry->bytes > *max_extent_size)
-				*max_extent_size = entry->bytes;
+			set_max_extent_size(entry, max_extent_size);
 			continue;
 		}
 
@@ -1827,8 +1837,8 @@ find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
 				*offset = tmp;
 				*bytes = size;
 				return entry;
-			} else if (size > *max_extent_size) {
-				*max_extent_size = size;
+			} else {
+				set_max_extent_size(entry, max_extent_size);
 			}
 			continue;
 		}
@@ -2688,8 +2698,7 @@ static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
 
 	err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
 	if (err) {
-		if (search_bytes > *max_extent_size)
-			*max_extent_size = search_bytes;
+		set_max_extent_size(entry, max_extent_size);
 		return 0;
 	}
 
@@ -2726,8 +2735,8 @@ u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
 
 	entry = rb_entry(node, struct btrfs_free_space, offset_index);
 	while (1) {
-		if (entry->bytes < bytes && entry->bytes > *max_extent_size)
-			*max_extent_size = entry->bytes;
+		if (entry->bytes < bytes)
+			set_max_extent_size(entry, max_extent_size);
 
 		if (entry->bytes < bytes ||
 		    (!entry->bitmap && entry->offset < min_start)) {
Signature · 1 line
2.14.3
#33 lore raw

PATCH 20/22 btrfs: don't use ctl->free_space for max_extent_size

In reply to Josef Bacik

From: Josef Bacik <[email protected]>

max_extent_size is supposed to be the largest contiguous range for the space info, and ctl->free_space is the total free space in the block group. We need to keep track of these separately and _only_ use the max_free_space if we don't have a max_extent_size, as that means our original request was too large to search any of the block groups for and therefore wouldn't have a max_extent_size set.

---
 fs/btrfs/extent-tree.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/btrfs/extent-tree.c +5 -2
index 626fb6a92dda..7311a232e45d 100644
@@ -7598,6 +7598,7 @@ static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
 	struct btrfs_block_group_cache *block_group = NULL;
 	u64 search_start = 0;
 	u64 max_extent_size = 0;
+	u64 max_free_space = 0;
 	u64 empty_cluster = 0;
 	struct btrfs_space_info *space_info;
 	int loop = 0;
@@ -7893,8 +7894,8 @@ static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
 			spin_lock(&ctl->tree_lock);
 			if (ctl->free_space <
 			    num_bytes + empty_cluster + empty_size) {
-				if (ctl->free_space > max_extent_size)
-					max_extent_size = ctl->free_space;
+				max_free_space = max(max_free_space,
+						     ctl->free_space);
 				spin_unlock(&ctl->tree_lock);
 				goto loop;
 			}
@@ -8063,6 +8064,8 @@ static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
 	}
 out:
 	if (ret == -ENOSPC) {
+		if (!max_extent_size)
+			max_extent_size = max_free_space;
 		spin_lock(&space_info->lock);
 		space_info->max_extent_size = max_extent_size;
 		spin_unlock(&space_info->lock);
Signature · 1 line
2.14.3
#34 lore raw

PATCH 21/22 btrfs: reset max_extent_size on clear in a bitmap

In reply to Josef Bacik

From: Josef Bacik <[email protected]>

We need to clear the max_extent_size when we clear bits from a bitmap since it could have been from the range that contains the max_extent_size.

---
 fs/btrfs/free-space-cache.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/free-space-cache.c +2 -0
index 92e1c30356b9..0feb735a78a8 100644
@@ -1691,6 +1691,8 @@ static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
 	bitmap_clear(info->bitmap, start, count);
 
 	info->bytes -= bytes;
+	if (info->max_extent_size > ctl->unit)
+		info->max_extent_size = 0;
 }
 
 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
Signature · 1 line
2.14.3
#35 lore raw

PATCH 22/22 btrfs: only run delayed refs if we're committing

In reply to Josef Bacik

I noticed in a giant dbench run that we spent a lot of time on lock contention while running transaction commit. This is because dbench results in a lot of fsync()'s that do a btrfs_transaction_commit(), and they all run the delayed refs first thing, so they all contend with each other. This leads to seconds of 0 throughput. Change this to only run the delayed refs if we're the ones committing the transaction. This makes the latency go away and we get no more lock contention.

---
 fs/btrfs/transaction.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)
fs/btrfs/transaction.c +11 -15
index 4b171d8a7554..39ff9378b3db 100644
@@ -1919,15 +1919,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 	btrfs_trans_release_metadata(trans);
 	trans->block_rsv = NULL;
 
-	/* make a pass through all the delayed refs we have so far
-	 * any runnings procs may add more while we are here
-	 */
-	ret = btrfs_run_delayed_refs(trans, 0);
-	if (ret) {
-		btrfs_end_transaction(trans);
-		return ret;
-	}
-
 	cur_trans = trans->transaction;
 
 	/*
@@ -1940,12 +1931,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 	if (!list_empty(&trans->new_bgs))
 		btrfs_create_pending_block_groups(trans);
 
-	ret = btrfs_run_delayed_refs(trans, 0);
-	if (ret) {
-		btrfs_end_transaction(trans);
-		return ret;
-	}
-
 	if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
 		int run_it = 0;
 
@@ -2016,6 +2001,17 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 		spin_unlock(&fs_info->trans_lock);
 	}
 
+	/*
+	 * We are now the only one in the commit area, we can run delayed refs
+	 * without hitting a bunch of lock contention from a lot of people
+	 * trying to commit the transaction at once.
+	 */
+	ret = btrfs_run_delayed_refs(trans, 0);
+	if (ret) {
+		btrfs_end_transaction(trans);
+		return ret;
+	}
+
 	extwriter_counter_dec(cur_trans, trans->type);
 
 	ret = btrfs_start_delalloc_flush(fs_info);
Signature · 1 line
2.14.3
#36 lore raw

Re: [PATCH 22/22] btrfs: only run delayed refs if we're committing

Quoted 51 lines · Josef Bacik

I noticed in a giant dbench run that we spent a lot of time on lock contention while running transaction commit. This is because dbench results in a lot of fsync()'s that do a btrfs_transaction_commit(), and they all run the delayed refs first thing, so they all contend with each other. This leads to seconds of 0 throughput. Change this to only run the delayed refs if we're the ones committing the transaction. This makes the latency go away and we get no more lock contention.

---
 fs/btrfs/transaction.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)
fs/btrfs/transaction.c +1 -15
index 4b171d8a7554..39ff9378b3db 100644
@@ -1919,15 +1919,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 	btrfs_trans_release_metadata(trans);
 	trans->block_rsv = NULL;
 
-	/* make a pass through all the delayed refs we have so far
-	 * any runnings procs may add more while we are here
-	 */
-	ret = btrfs_run_delayed_refs(trans, 0);
-	if (ret) {
-		btrfs_end_transaction(trans);
-		return ret;
-	}
-
 	cur_trans = trans->transaction;
 
 	/*
@@ -1940,12 +1931,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 	if (!list_empty(&trans->new_bgs))
 		btrfs_create_pending_block_groups(trans);
 
-	ret = btrfs_run_delayed_refs(trans, 0);
-	if (ret) {
-		btrfs_end_transaction(trans);
-		return ret;
-	}
-
 	if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
 		int run_it = 0;
 
@@ -2016,6 +2001,17 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 		spin_unlock(&fs_info->trans_lock);
 	}
 
+	/*

fs/btrfs/transaction.c int btrfs_commit_transaction(struct btrfs_trans_handle *trans) · line 2014

Quoted 10 lines
fs/btrfs/transaction.c +10 -0
+	 * We are now the only one in the commit area, we can run delayed refs
+	 * without hitting a bunch of lock contention from a lot of people
+	 * trying to commit the transaction at once.
+	 */
+	ret = btrfs_run_delayed_refs(trans, 0);
+	if (ret) {
+		btrfs_end_transaction(trans);
+		return ret;
+	}
+

Is this really needed since we already have code which runs delayed refs in the transaction critical section right after btrfs_run_delayed_items. I'd rather have a single place in commit_transaction where delayed_refs are run. If this is needed for correctness reasons then document what this reason is for otherwise let's just remove it.

Quoted 4 lines
fs/btrfs/transaction.c +0 -0
 	extwriter_counter_dec(cur_trans, trans->type);
 
 	ret = btrfs_start_delalloc_flush(fs_info);
#37 lore raw
In reply to Josef Bacik

Please send cover letter for patchsets with more than 1 patch. Especially for a patchset with 20+ patches that does not seem to be doing anything trivial.

What problem does this fix, what's the overall idea behind the approach, what workload can trigger that etc.

Thanks.

#38 lore raw
Quoted 6 lines · David Sterba

Please send cover letter for patchsets with more than 1 patch. Especially for a patchset with 20+ patches that does not seem to be doing anything trivial.

What problem does this fix, what's the overall idea behind the approach, what workload can trigger that etc.

They are just fixes, there's no large over-arching theme, hence no cover letter,

Josef

#39 lore raw
Quoted 9 lines · Josef Bacik
Quoted 6 lines · David Sterba

Please send cover letter for patchsets with more than 1 patch. Especially for a patchset with 20+ patches that does not seem to be doing anything trivial.

What problem does this fix, what's the overall idea behind the approach, what workload can trigger that etc.

They are just fixes, there's no large over-arching theme, hence no cover letter,

Cover letter is a single point where I can address the whole patchset, like that it's added to for-next or merged, or report bugs. Also you are going to write what are the changes between the revisions.

The initial text does not need to be long if there's really nothing big behind, but please state that in advance. This sets the expectations before one is going through the patches.

#40 lore raw
In reply to Josef Bacik
Quoted 79 lines · Josef Bacik

From: Josef Bacik <[email protected]>

We do this dance in cleanup_ref_head and check_ref_cleanup, unify it into a helper and cleanup the calling functions.

---
 fs/btrfs/delayed-ref.c | 14 ++++++++++++++
 fs/btrfs/delayed-ref.h |  3 ++-
 fs/btrfs/extent-tree.c | 24 ++++--------------------
 3 files changed, 20 insertions(+), 21 deletions(-)
fs/btrfs/delayed-ref.c +14 -0
index 03dec673d12a..e1b322d651dd 100644
@@ -393,6 +393,20 @@ btrfs_select_ref_head(struct btrfs_trans_handle *trans)
 	return head;
 }
 
+void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
+			   struct btrfs_delayed_ref_head *head)
+{
+	lockdep_assert_held(&delayed_refs->lock);
+	lockdep_assert_held(&head->lock);
+
+	rb_erase(&head->href_node, &delayed_refs->href_root);
+	RB_CLEAR_NODE(&head->href_node);
+	atomic_dec(&delayed_refs->num_entries);
+	delayed_refs->num_heads--;
+	if (head->processing == 0)
+		delayed_refs->num_heads_ready--;
+}
+
 /*
  * Helper to insert the ref_node to the tail or merge with tail.
  *
fs/btrfs/delayed-ref.h +2 -1
index ea1aecb6a50d..36318182e4ec 100644
@@ -263,7 +263,8 @@ static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
 {
 	mutex_unlock(&head->mutex);
 }
-
+void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
+			   struct btrfs_delayed_ref_head *head);
 
 struct btrfs_delayed_ref_head *
 btrfs_select_ref_head(struct btrfs_trans_handle *trans);
fs/btrfs/extent-tree.c +2 -10
index 3d9fe58c0080..ccaccd78534e 100644
@@ -2577,12 +2577,9 @@ static int cleanup_ref_head(struct btrfs_trans_handle *trans,
 		spin_unlock(&delayed_refs->lock);
 		return 1;
 	}
-	delayed_refs->num_heads--;
-	rb_erase(&head->href_node, &delayed_refs->href_root);
-	RB_CLEAR_NODE(&head->href_node);
-	spin_unlock(&head->lock);
+	btrfs_delete_ref_head(delayed_refs, head);
 	spin_unlock(&delayed_refs->lock);
-	atomic_dec(&delayed_refs->num_entries);
+	spin_unlock(&head->lock);
 
 	trace_run_delayed_ref_head(fs_info, head, 0);
 
@@ -7122,22 +7119,9 @@ static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
 	if (!mutex_trylock(&head->mutex))
 		goto out;
 
-	/*
-	 * at this point we have a head with no other entries.  Go
-	 * ahead and process it.
-	 */
-	rb_erase(&head->href_node, &delayed_refs->href_root);

fs/btrfs/extent-tree.c static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, · line 7139

Quoted 10 lines
fs/btrfs/extent-tree.c +0 -10
-	RB_CLEAR_NODE(&head->href_node);
-	atomic_dec(&delayed_refs->num_entries);
-
-	/*
-	 * we don't take a ref on the node because we're removing it from the
-	 * tree, so we just steal the ref the tree was holding.
-	 */
-	delayed_refs->num_heads--;
-	if (head->processing == 0)
-		delayed_refs->num_heads_ready--;

In cleanup_ref_head we don't have the num_heads_ready-- code so this is not pure consolidation but changes the behavior to a certain extent. It seems this patch is also fixing a bug w.r.t num_heads_ready counts if so, this needs to be documented in the changelog.

Quoted 7 lines
fs/btrfs/extent-tree.c +2 -0
+	btrfs_delete_ref_head(delayed_refs, head);
 	head->processing = 0;
+
 	spin_unlock(&head->lock);
 	spin_unlock(&delayed_refs->lock);
 
#41 lore raw
Quoted 89 lines · Nikolay Borisov
Quoted 86 lines · Josef Bacik

From: Josef Bacik <[email protected]>

We do this dance in cleanup_ref_head and check_ref_cleanup, unify it into a helper and cleanup the calling functions.

---
 fs/btrfs/delayed-ref.c | 14 ++++++++++++++
 fs/btrfs/delayed-ref.h |  3 ++-
 fs/btrfs/extent-tree.c | 24 ++++--------------------
 3 files changed, 20 insertions(+), 21 deletions(-)
fs/btrfs/delayed-ref.c +14 -0
index 03dec673d12a..e1b322d651dd 100644
@@ -393,6 +393,20 @@ btrfs_select_ref_head(struct btrfs_trans_handle *trans)
 	return head;
 }
 
+void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
+			   struct btrfs_delayed_ref_head *head)
+{
+	lockdep_assert_held(&delayed_refs->lock);
+	lockdep_assert_held(&head->lock);
+
+	rb_erase(&head->href_node, &delayed_refs->href_root);
+	RB_CLEAR_NODE(&head->href_node);
+	atomic_dec(&delayed_refs->num_entries);
+	delayed_refs->num_heads--;
+	if (head->processing == 0)
+		delayed_refs->num_heads_ready--;
+}
+
 /*
  * Helper to insert the ref_node to the tail or merge with tail.
  *
fs/btrfs/delayed-ref.h +2 -1
index ea1aecb6a50d..36318182e4ec 100644
@@ -263,7 +263,8 @@ static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
 {
 	mutex_unlock(&head->mutex);
 }
-
+void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
+			   struct btrfs_delayed_ref_head *head);
 
 struct btrfs_delayed_ref_head *
 btrfs_select_ref_head(struct btrfs_trans_handle *trans);
fs/btrfs/extent-tree.c +2 -17
index 3d9fe58c0080..ccaccd78534e 100644
@@ -2577,12 +2577,9 @@ static int cleanup_ref_head(struct btrfs_trans_handle *trans,
 		spin_unlock(&delayed_refs->lock);
 		return 1;
 	}
-	delayed_refs->num_heads--;
-	rb_erase(&head->href_node, &delayed_refs->href_root);
-	RB_CLEAR_NODE(&head->href_node);
-	spin_unlock(&head->lock);
+	btrfs_delete_ref_head(delayed_refs, head);
 	spin_unlock(&delayed_refs->lock);
-	atomic_dec(&delayed_refs->num_entries);
+	spin_unlock(&head->lock);
 
 	trace_run_delayed_ref_head(fs_info, head, 0);
 
@@ -7122,22 +7119,9 @@ static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
 	if (!mutex_trylock(&head->mutex))
 		goto out;
 
-	/*
-	 * at this point we have a head with no other entries.  Go
-	 * ahead and process it.
-	 */
-	rb_erase(&head->href_node, &delayed_refs->href_root);
-	RB_CLEAR_NODE(&head->href_node);
-	atomic_dec(&delayed_refs->num_entries);
-
-	/*
-	 * we don't take a ref on the node because we're removing it from the
-	 * tree, so we just steal the ref the tree was holding.
-	 */
Quoted 9 lines

fs/btrfs/extent-tree.c static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, · line 7139

Quoted 3 lines
fs/btrfs/extent-tree.c +0 -3
-	delayed_refs->num_heads--;
-	if (head->processing == 0)
-		delayed_refs->num_heads_ready--;

In cleanup_ref_head we don't have the num_heads_ready-- code so this is not pure consolidation but changes the behavior to a certain extent. It seems this patch is also fixing a bug w.r.t num_heads_ready counts if so, this needs to be documented in the changelog.

No it's not, because cleanup_ref_head is called when running delayed refs, so head->processing == 1, which means there's no change. Thanks,

Josef

#42 lore raw
In reply to Josef Bacik
Quoted 4 lines · Josef Bacik
fs/btrfs/extent-tree.c +0 -0
@@ -2577,12 +2577,9 @@ static int cleanup_ref_head(struct btrfs_trans_handle *trans,
 		spin_unlock(&delayed_refs->lock);

fs/btrfs/extent-tree.c static int cleanup_ref_head(struct btrfs_trans_handle *trans, · line 2582

Quoted 10 lines
fs/btrfs/extent-tree.c +2 -5
 		return 1;
 	}
-	delayed_refs->num_heads--;
-	rb_erase(&head->href_node, &delayed_refs->href_root);
-	RB_CLEAR_NODE(&head->href_node);
-	spin_unlock(&head->lock);
+	btrfs_delete_ref_head(delayed_refs, head);
 	spin_unlock(&delayed_refs->lock);
-	atomic_dec(&delayed_refs->num_entries);
+	spin_unlock(&head->lock);

The order of unlocks is reversed, the head is nested to the delayed refs.

Quoted 14 lines
fs/btrfs/extent-tree.c +0 -10
@@ -7122,22 +7119,9 @@ static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
 	if (!mutex_trylock(&head->mutex))
 		goto out;
 
-	/*
-	 * at this point we have a head with no other entries.  Go
-	 * ahead and process it.
-	 */
-	rb_erase(&head->href_node, &delayed_refs->href_root);
-	RB_CLEAR_NODE(&head->href_node);
-	atomic_dec(&delayed_refs->num_entries);
-
-	/*
-	 * we don't take a ref on the node because we're removing it from the

fs/btrfs/extent-tree.c static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, · line 7126

Quoted 10 lines
fs/btrfs/extent-tree.c +2 -5
-	 * tree, so we just steal the ref the tree was holding.
-	 */
-	delayed_refs->num_heads--;
-	if (head->processing == 0)
-		delayed_refs->num_heads_ready--;
+	btrfs_delete_ref_head(delayed_refs, head);
 	head->processing = 0;
+
 	spin_unlock(&head->lock);
 	spin_unlock(&delayed_refs->lock);

The right order eg. here and everywhere else.

#43 lore raw
In reply to Josef Bacik

fstests run in the sequence from btrfs/001, in qemu, 2G memory, warning and crash at btrfs/124

btrfs/124 [22:58:39]

[10100.765898] run fstests btrfs/124 at 2018-07-19 22:58:39
...
[10110.113787] BTRFS: device fsid 8b6b700e-3346-4260-bb2e-c561ba4b9960 devid 1 transid 7 /dev/vdb
[10110.118673] BTRFS info (device vdb): allowing degraded mounts
[10110.120386] BTRFS info (device vdb): disk space caching is enabled
[10110.122070] BTRFS info (device vdb): has skinny extents
[10110.138082] BTRFS warning (device vdb): devid 2 uuid 1e933775-ded8-40fa-9b6a-6127afd4cc01 is missing
[10110.141411] BTRFS warning (device vdb): devid 2 uuid 1e933775-ded8-40fa-9b6a-6127afd4cc01 is missing
[10110.719342] WARNING: CPU: 3 PID: 28973 at fs/btrfs/extent-tree.c:6122 btrfs_trans_release_chunk_metadata+0x3f/0x50 [btrfs]
[10110.723171] Modules linked in: btrfs libcrc32c xor zstd_decompress zstd_compress xxhash raid6_pq dm_mod dax loop [last unloaded: libcrc32c]
[10110.727313] CPU: 3 PID: 28973 Comm: dd Not tainted 4.18.0-rc5-default+ #198
[10110.728504] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.0.0-prebuilt.qemu-project.org 04/01/2014
[10110.730236] RIP: 0010:btrfs_trans_release_chunk_metadata+0x3f/0x50 [btrfs]
[10110.734444] RSP: 0018:ffffb3edc08bfba0 EFLAGS: 00010202
[10110.735361] RAX: ffff9b68e5232dd8 RBX: ffff9b69315f98f0 RCX: 00000000000c0000
[10110.736539] RDX: ffff9b69315f9948 RSI: ffff9b68e52369d8 RDI: ffff9b69315f98f0
[10110.737721] RBP: ffff9b68e5236000 R08: 0000000000000001 R09: 0000000000000000
[10110.738849] R10: ffff9b691e93e4e0 R11: 0000000000000000 R12: ffff9b69359b0000
[10110.739966] R13: 0000000000000000 R14: 0000000000000201 R15: ffff9b6917e08528
[10110.741460] FS:  00007f15f5b14540(0000) GS:ffff9b693cc00000(0000) knlGS:0000000000000000
[10110.742795] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[10110.743679] CR2: 00007f15f5571690 CR3: 000000002d8a0000 CR4: 00000000000006e0
[10110.744692] Call Trace:
[10110.745096]  __btrfs_end_transaction+0x55/0x230 [btrfs]
[10110.745762]  btrfs_create+0xf2/0x1f0 [btrfs]
[10110.746564]  lookup_open+0x562/0x700
[10110.747216]  ? __wake_up_common_lock+0x63/0xc0
[10110.748021]  ? find_held_lock+0x34/0xa0
[10110.748737]  path_openat+0x653/0xf90
[10110.749411]  do_filp_open+0x93/0x100
[10110.750076]  ? _raw_spin_unlock+0x24/0x40
[10110.750793]  ? __alloc_fd+0xee/0x1d0
[10110.751438]  do_sys_open+0x186/0x210
[10110.752152]  do_syscall_64+0x5a/0x170
[10110.752774]  entry_SYSCALL_64_after_hwframe+0x49/0xbe
[10110.753582] RIP: 0033:0x7f15f562090e
[10110.757116] RSP: 002b:00007ffea876ae20 EFLAGS: 00000246 ORIG_RAX: 0000000000000101
[10110.758424] RAX: ffffffffffffffda RBX: 000055d6f74323e0 RCX: 00007f15f562090e
[10110.759523] RDX: 0000000000000241 RSI: 00007ffea876c286 RDI: 00000000ffffff9c
[10110.760501] RBP: 0000000000000001 R08: 00007ffea876c2a7 R09: 0000000000000000
[10110.761323] R10: 00000000000001b6 R11: 0000000000000246 R12: 0000000000000241
[10110.762400] R13: 00007ffea876c286 R14: 0000000000000001 R15: 00007ffea876b118
[10110.763533] irq event stamp: 2212
[10110.764139] hardirqs last  enabled at (2211): [<ffffffff9221b007>] slab_free_freelist_hook+0x97/0xe0
[10110.765566] hardirqs last disabled at (2212): [<ffffffff92800f9c>] error_entry+0x6c/0xc0
[10110.766932] softirqs last  enabled at (2006): [<ffffffff921c485e>] __set_page_dirty_nobuffers+0xfe/0x140
[10110.768464] softirqs last disabled at (2002): [<ffffffff921ddebe>] wb_wakeup_delayed+0x2e/0x70
[10110.769891] ---[ end trace 6fb571b51e7cab5c ]---
[10131.610680] assertion failed: list_empty(&block_group->bg_list), file: fs/btrfs/extent-tree.c, line: 10070
[10131.614113] ------------[ cut here ]------------
[10131.615675] kernel BUG at fs/btrfs/ctree.h:3472!
[10131.617376] invalid opcode: 0000 [#1] PREEMPT SMP
[10131.618968] CPU: 1 PID: 28975 Comm: umount Tainted: G        W         4.18.0-rc5-default+ #198
[10131.621170] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.0.0-prebuilt.qemu-project.org 04/01/2014
[10131.622672] RIP: 0010:assfail.constprop.84+0x18/0x1a [btrfs]
[10131.626514] RSP: 0018:ffffb3edc6bfbde0 EFLAGS: 00010246
[10131.627380] RAX: 000000000000005e RBX: ffff9b68e5232c00 RCX: 0000000000000006
[10131.628521] RDX: 0000000000000000 RSI: ffff9b68f9bcd9c8 RDI: 0000000000000246
[10131.629596] RBP: ffff9b69359b0000 R08: 00001016d930fc78 R09: 0000000000000000
[10131.630678] R10: 0000000000000000 R11: 0000000000000000 R12: ffff9b69359b0118
[10131.631754] R13: ffff9b69359b0168 R14: ffff9b68e5232d98 R15: dead000000000100
[10131.632823] FS:  00007fbc00b0dfc0(0000) GS:ffff9b693c800000(0000) knlGS:0000000000000000
[10131.634119] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[10131.635030] CR2: 00007fbc006e167e CR3: 0000000053e5c000 CR4: 00000000000006e0
[10131.636136] Call Trace:
[10131.636641]  btrfs_free_block_groups.cold.112+0x22/0x55 [btrfs]
[10131.637583]  close_ctree+0x159/0x330 [btrfs]
[10131.638292]  generic_shutdown_super+0x64/0x100
[10131.639031]  kill_anon_super+0xe/0x20
[10131.639663]  btrfs_kill_super+0x12/0xa0 [btrfs]
[10131.640408]  deactivate_locked_super+0x29/0x60
[10131.641159]  cleanup_mnt+0x3b/0x70
[10131.641764]  task_work_run+0x9b/0xd0
[10131.642406]  exit_to_usermode_loop+0xbd/0xc0
[10131.643124]  do_syscall_64+0x16c/0x170
[10131.643775]  entry_SYSCALL_64_after_hwframe+0x49/0xbe
[10131.644590] RIP: 0033:0x7fbc003d4d07
[10131.648070] RSP: 002b:00007fff6a865598 EFLAGS: 00000246 ORIG_RAX: 00000000000000a6
[10131.649331] RAX: 0000000000000000 RBX: 0000557a82414970 RCX: 00007fbc003d4d07
[10131.650417] RDX: 0000000000000001 RSI: 0000000000000000 RDI: 0000557a82414b50
[10131.651486] RBP: 0000000000000000 R08: 0000557a82414b70 R09: 00007fff6a863e00
[10131.652439] R10: 0000000000000000 R11: 0000000000000246 R12: 00007fbc008f11c4
[10131.653253] R13: 0000557a82414b50 R14: 0000000000000000 R15: 00007fff6a865808
[10131.654063] Modules linked in: btrfs libcrc32c xor zstd_decompress zstd_compress xxhash raid6_pq dm_mod dax loop [last unloaded: libcrc32c]
[10131.655964] ---[ end trace 6fb571b51e7cab5d ]---
[10131.656787] RIP: 0010:assfail.constprop.84+0x18/0x1a [btrfs]
[10131.660454] RSP: 0018:ffffb3edc6bfbde0 EFLAGS: 00010246
[10131.661302] RAX: 000000000000005e RBX: ffff9b68e5232c00 RCX: 0000000000000006
[10131.662353] RDX: 0000000000000000 RSI: ffff9b68f9bcd9c8 RDI: 0000000000000246
[10131.663413] RBP: ffff9b69359b0000 R08: 00001016d930fc78 R09: 0000000000000000
[10131.664508] R10: 0000000000000000 R11: 0000000000000000 R12: ffff9b69359b0118
[10131.665627] R13: ffff9b69359b0168 R14: ffff9b68e5232d98 R15: dead000000000100
[10131.666723] FS:  00007fbc00b0dfc0(0000) GS:ffff9b693c800000(0000) knlGS:0000000000000000
[10131.668029] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[10131.668943] CR2: 00007fbc006e167e CR3: 0000000053e5c000 CR4: 00000000000006e0
[10132.375092] BTRFS: device fsid fec9b6db-bc92-4981-85fa-f965b2e44059 devid 1 transid 234 /dev/vda