Skip to contentSkip to conversation

Btrfs: map the node block when looking for readahead targets

PATCH In review Josef Bacik 3 participants 9 messages May 11, 2011 – Jun 8, 2011 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 Btrfs: map the node block when looking for readahead targets

If we have particularly full nodes, we could call btrfs_node_blockptr up to 32 times, which is 32 pairs of kmap/kunmap, which _sucks_. So go ahead and map the extent buffer while we look for readahead targets. Thanks,

---
 fs/btrfs/ctree.c |   23 +++++++++++++++++++++--
 1 files changed, 21 insertions(+), 2 deletions(-)
fs/btrfs/ctree.c +21 -2
index 84d7ca1..009bcf7 100644
@@ -1229,6 +1229,7 @@ static void reada_for_search(struct btrfs_root *root,
 	u64 search;
 	u64 target;
 	u64 nread = 0;
+	u64 gen;
 	int direction = path->reada;
 	struct extent_buffer *eb;
 	u32 nr;
@@ -1256,6 +1257,15 @@ static void reada_for_search(struct btrfs_root *root,
 	nritems = btrfs_header_nritems(node);
 	nr = slot;
 	while (1) {
+		if (!node->map_token) {
+			unsigned long offset = btrfs_node_key_ptr_offset(nr);
+			map_private_extent_buffer(node, offset,
+						  sizeof(struct btrfs_key_ptr),
+						  &node->map_token,
+						  &node->kaddr,
+						  &node->map_start,
+						  &node->map_len, KM_USER1);
+		}
 		if (direction < 0) {
 			if (nr == 0)
 				break;
@@ -1273,14 +1283,23 @@ static void reada_for_search(struct btrfs_root *root,
 		search = btrfs_node_blockptr(node, nr);
 		if ((search <= target && target - search <= 65536) ||
 		    (search > target && search - target <= 65536)) {
-			readahead_tree_block(root, search, blocksize,
-				     btrfs_node_ptr_generation(node, nr));
+			gen = btrfs_node_ptr_generation(node, nr);
+			if (node->map_token) {
+				unmap_extent_buffer(node, node->map_token,
+						    KM_USER1);
+				node->map_token = NULL;
+			}
+			readahead_tree_block(root, search, blocksize, gen);
 			nread += blocksize;
 		}
 		nscan++;
 		if ((nread > 65536 || nscan > 32))
 			break;
 	}
+	if (node->map_token) {
+		unmap_extent_buffer(node, node->map_token, KM_USER1);
+		node->map_token = NULL;
+	}
 }
 
 /*
Signature · 1 line
1.7.2.3
#1 lore raw

PATCH 2/4 Btrfs: don't look at the extent buffer level 3 times in a row

We have a bit of debugging in btrfs_search_slot to make sure the level of the cow block is the same as the original block we were cow'ing. I don't think I've ever seen this tripped, so kill it. This saves us 2 kmap's per level in our search. Thanks,

---
 fs/btrfs/ctree.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
fs/btrfs/ctree.c +0 -3
index 009bcf7..f7a0a64 100644
@@ -1672,9 +1672,6 @@ again:
 		}
 cow_done:
 		BUG_ON(!cow && ins_len);
-		if (level != btrfs_header_level(b))
-			WARN_ON(1);
-		level = btrfs_header_level(b);
 
 		p->nodes[level] = b;
 		if (!p->skip_locking)
Signature · 1 line
1.7.2.3
#4 lore raw

PATCH 3/4 Btrfs: kill BTRFS_I(inode)->block_group

In reply to Josef Bacik

Originally this was going to be used as a way to give hints to the allocator, but frankly we can get much better hints elsewhere and it's not even used at all for anything usefull. In addition to be completely useless, when we initialize an inode we try and find a freeish block group to set as the inodes block group, and with a completely full 40gb fs this takes _forever_, so I imagine with say 1tb fs this is just unbearable. So just axe the thing altoghether, we don't need it and it saves us 8 bytes in the inode and saves us 500 microseconds per inode lookup in my testcase. Thanks,

---
 fs/btrfs/btrfs_inode.h |    3 --
 fs/btrfs/ctree.h       |    3 +-
 fs/btrfs/extent-tree.c |   10 +----
 fs/btrfs/inode.c       |   86 +++++-------------------------------------------
 fs/btrfs/ioctl.c       |    3 +-
 fs/btrfs/transaction.c |    1 -
 fs/btrfs/transaction.h |   14 --------
 fs/btrfs/xattr.c       |    2 -
 8 files changed, 13 insertions(+), 109 deletions(-)
fs/btrfs/btrfs_inode.h +0 -3
index 57c3bb2..4bc852d 100644
@@ -120,9 +120,6 @@ struct btrfs_inode {
 	 */
 	u64 index_cnt;
 
-	/* the start of block group preferred for allocations. */
-	u64 block_group;
-
 	/* the fsync log has some corner cases that mean we have to check
 	 * directories to see if any unlinks have been done before
 	 * the directory was logged.  See tree-log.c for all the
fs/btrfs/ctree.h +1 -2
index 1a41d77..c217b45 100644
@@ -2514,8 +2514,7 @@ int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
 int btrfs_writepages(struct address_space *mapping,
 		     struct writeback_control *wbc);
 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
-			     struct btrfs_root *new_root,
-			     u64 new_dirid, u64 alloc_hint);
+			     struct btrfs_root *new_root, u64 new_dirid);
 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
 			 size_t size, struct bio *bio, unsigned long bio_flags);
 
fs/btrfs/extent-tree.c +2 -8
index 64d2514..0d24414 100644
@@ -5273,6 +5273,7 @@ checks:
 			btrfs_add_free_space(block_group, offset,
 					     search_start - offset);
 		BUG_ON(offset > search_start);
+		btrfs_put_block_group(block_group);
 		break;
 loop:
 		failed_cluster_refill = false;
@@ -5363,14 +5364,7 @@ loop:
 		ret = -ENOSPC;
 	} else if (!ins->objectid) {
 		ret = -ENOSPC;
-	}
-
-	/* we found what we needed */
-	if (ins->objectid) {
-		if (!(data & BTRFS_BLOCK_GROUP_DATA))
-			trans->block_group = block_group->key.objectid;
-
-		btrfs_put_block_group(block_group);
+	} else if (ins->objectid) {
 		ret = 0;
 	}
 
fs/btrfs/inode.c +9 -77

335 lines not on this page · raw message

fs/btrfs/ioctl.c +1 -2
index 4a9a277..1783b25 100644
@@ -407,8 +407,7 @@ static noinline int create_subvol(struct btrfs_root *root,
 
 	btrfs_record_root_in_trans(trans, new_root);
 
-	ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
-				       BTRFS_I(dir)->block_group);
+	ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
 	/*
 	 * insert the directory item
 	 */
fs/btrfs/transaction.c +0 -1
index 43816f8..f4ea695 100644
@@ -241,7 +241,6 @@ again:
 	h->transid = cur_trans->transid;
 	h->transaction = cur_trans;
 	h->blocks_used = 0;
-	h->block_group = 0;
 	h->bytes_reserved = 0;
 	h->delayed_ref_updates = 0;
 	h->use_count = 1;
fs/btrfs/transaction.h +0 -14
index 11c6efc..da7289e 100644
@@ -47,7 +47,6 @@ struct btrfs_transaction {
 
 struct btrfs_trans_handle {
 	u64 transid;
-	u64 block_group;
 	u64 bytes_reserved;
 	unsigned long use_count;
 	unsigned long blocks_reserved;
@@ -70,19 +69,6 @@ struct btrfs_pending_snapshot {
 	struct list_head list;
 };
 
-static inline void btrfs_set_trans_block_group(struct btrfs_trans_handle *trans,
-					       struct inode *inode)
-{
-	trans->block_group = BTRFS_I(inode)->block_group;
-}
-
-static inline void btrfs_update_inode_block_group(
-					  struct btrfs_trans_handle *trans,
-					  struct inode *inode)
-{
-	BTRFS_I(inode)->block_group = trans->block_group;
-}
-
 static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
 					      struct inode *inode)
 {
fs/btrfs/xattr.c +0 -2
index a5303b8..8fff09a 100644
@@ -158,8 +158,6 @@ int __btrfs_setxattr(struct btrfs_trans_handle *trans,
 	if (IS_ERR(trans))
 		return PTR_ERR(trans);
 
-	btrfs_set_trans_block_group(trans, inode);
-
 	ret = do_setxattr(trans, inode, name, value, size, flags);
 	if (ret)
 		goto out;
Signature · 1 line
1.7.2.3
#5 lore raw

PATCH 4/4 Btrfs: try not to sleep as much when doing slow caching

In reply to Josef Bacik

When the fs is super full and we unmount the fs, we could get stuck in this thing where unmount is waiting for the caching kthread to make progress and the caching kthread keeps scheduling because we're in the middle of a commit. So instead just let the caching kthread keep going and only yeild if need_resched(). This makes my horrible umount case go from taking up to 10 minutes to taking less than 20 seconds. Thanks,

---
 fs/btrfs/extent-tree.c |   19 +++++++++++--------
 1 files changed, 11 insertions(+), 8 deletions(-)
fs/btrfs/extent-tree.c +11 -8
index 0d24414..edc1880 100644
@@ -359,15 +359,18 @@ again:
 			if (ret)
 				break;
 
-			caching_ctl->progress = last;
-			btrfs_release_path(extent_root, path);
-			up_read(&fs_info->extent_commit_sem);
-			mutex_unlock(&caching_ctl->mutex);
-			if (btrfs_transaction_in_commit(fs_info))
-				schedule_timeout(1);
-			else
+			if (need_resched() ||
+			    btrfs_next_leaf(extent_root, path)) {
+				caching_ctl->progress = last;
+				btrfs_release_path(extent_root, path);
+				up_read(&fs_info->extent_commit_sem);
+				mutex_unlock(&caching_ctl->mutex);
 				cond_resched();
-			goto again;
+				goto again;
+			}
+			leaf = path->nodes[0];
+			nritems = btrfs_header_nritems(leaf);
+			continue;
 		}
 
 		if (key.objectid < block_group->key.objectid) {
Signature · 1 line
1.7.2.3
#2 lore raw

Re: [PATCH 2/4] Btrfs: don't look at the extent buffer level 3 times in a row

In reply to Josef Bacik

Hi,

Quoted 4 lines · Josef Bacik

We have a bit of debugging in btrfs_search_slot to make sure the level of the cow block is the same as the original block we were cow'ing. I don't think I've ever seen this tripped, so kill it. This saves us 2 kmap's per level in our search. Thanks,

I think it may be worth to add a CONFIG_BTRFS_DEBUG to switch on some (even expensive) checks, eg. like this one, rather than deleting them.

david

#3 lore raw

Re: [PATCH 2/4] Btrfs: don't look at the extent buffer level 3 times in a row

Quoted 11 lines · David Sterba

Hi,

Quoted 4 lines · Josef Bacik

We have a bit of debugging in btrfs_search_slot to make sure the level of the cow block is the same as the original block we were cow'ing. I don't think I've ever seen this tripped, so kill it. This saves us 2 kmap's per level in our search. Thanks,

I think it may be worth to add a CONFIG_BTRFS_DEBUG to switch on some (even expensive) checks, eg. like this one, rather than deleting them.

I had thought about that, but then decided to be lazy. Thanks,

Josef

#6 lore raw
In reply to Josef Bacik
Quoted 23 lines · Josef Bacik

If we have particularly full nodes, we could call btrfs_node_blockptr up to 32 times, which is 32 pairs of kmap/kunmap, which _sucks_. So go ahead and map the extent buffer while we look for readahead targets. Thanks,

---
 fs/btrfs/ctree.c |   23 +++++++++++++++++++++--
 1 files changed, 21 insertions(+), 2 deletions(-)
fs/btrfs/ctree.c +1 -0
index 84d7ca1..009bcf7 100644
@@ -1229,6 +1229,7 @@ static void reada_for_search(struct btrfs_root *root,
 	u64 search;
 	u64 target;
 	u64 nread = 0;
+	u64 gen;
 	int direction = path->reada;
 	struct extent_buffer *eb;
 	u32 nr;
@@ -1256,6 +1257,15 @@ static void reada_for_search(struct btrfs_root *root,
 	nritems = btrfs_header_nritems(node);

fs/btrfs/ctree.c static void reada_for_search(struct btrfs_root *root, · line 1267

Quoted 10 lines
fs/btrfs/ctree.c +8 -0
 	nr = slot;
 	while (1) {
+		if (!node->map_token) {
+			unsigned long offset = btrfs_node_key_ptr_offset(nr);
+			map_private_extent_buffer(node, offset,
+						  sizeof(struct btrfs_key_ptr),
+						  &node->map_token,
+						  &node->kaddr,
+						  &node->map_start,
+						  &node->map_len, KM_USER1);

You can't do that. It puts us in atomic context, and the following readahead_tree_block will try a memory allocation with GFP_NOFS, which leads to a BUG: sleeping function called from invalid context. It didn't fall on our feet earlier because you also turned off readahead, but scrub still uses it.

-Arne

Quoted 30 lines
fs/btrfs/ctree.c +12 -2
+		}
 		if (direction < 0) {
 			if (nr == 0)
 				break;
@@ -1273,14 +1283,23 @@ static void reada_for_search(struct btrfs_root *root,
 		search = btrfs_node_blockptr(node, nr);
 		if ((search <= target && target - search <= 65536) ||
 		    (search > target && search - target <= 65536)) {
-			readahead_tree_block(root, search, blocksize,
-				     btrfs_node_ptr_generation(node, nr));
+			gen = btrfs_node_ptr_generation(node, nr);
+			if (node->map_token) {
+				unmap_extent_buffer(node, node->map_token,
+						    KM_USER1);
+				node->map_token = NULL;
+			}
+			readahead_tree_block(root, search, blocksize, gen);
 			nread += blocksize;
 		}
 		nscan++;
 		if ((nread > 65536 || nscan > 32))
 			break;
 	}
+	if (node->map_token) {
+		unmap_extent_buffer(node, node->map_token, KM_USER1);
+		node->map_token = NULL;
+	}
 }
 
 /*
#7 lore raw
Quoted 32 lines · Arne Jansen
Quoted 31 lines · Josef Bacik

If we have particularly full nodes, we could call btrfs_node_blockptr up to 32 times, which is 32 pairs of kmap/kunmap, which _sucks_. So go ahead and map the extent buffer while we look for readahead targets. Thanks,

---
 fs/btrfs/ctree.c |   23 +++++++++++++++++++++--
 1 files changed, 21 insertions(+), 2 deletions(-)
fs/btrfs/ctree.c +7 -0
index 84d7ca1..009bcf7 100644
@@ -1229,6 +1229,7 @@ static void reada_for_search(struct btrfs_root *root,
 	u64 search;
 	u64 target;
 	u64 nread = 0;
+	u64 gen;
 	int direction = path->reada;
 	struct extent_buffer *eb;
 	u32 nr;
@@ -1256,6 +1257,15 @@ static void reada_for_search(struct btrfs_root *root,
 	nritems = btrfs_header_nritems(node);
 	nr = slot;
 	while (1) {
+		if (!node->map_token) {
+			unsigned long offset = btrfs_node_key_ptr_offset(nr);
+			map_private_extent_buffer(node, offset,
+						  sizeof(struct btrfs_key_ptr),
+						  &node->map_token,
+						  &node->kaddr,
Quoted 8 lines

fs/btrfs/ctree.c static void reada_for_search(struct btrfs_root *root, · line 1267

Quoted 2 lines
fs/btrfs/ctree.c +2 -0
+						  &node->map_start,
+						  &node->map_len, KM_USER1);

You can't do that. It puts us in atomic context, and the following readahead_tree_block will try a memory allocation with GFP_NOFS, which leads to a BUG: sleeping function called from invalid context. It didn't fall on our feet earlier because you also turned off readahead, but scrub still uses it.

We don't make any memory allocations within the area that we've kmap_atomic()'ed, something else is going wrong, this patch is fine. Thanks,

Josef

#8 lore raw
Quoted 42 lines · Josef Bacik
Quoted 32 lines · Arne Jansen
Quoted 31 lines · Josef Bacik

If we have particularly full nodes, we could call btrfs_node_blockptr up to 32 times, which is 32 pairs of kmap/kunmap, which _sucks_. So go ahead and map the extent buffer while we look for readahead targets. Thanks,

---
 fs/btrfs/ctree.c |   23 +++++++++++++++++++++--
 1 files changed, 21 insertions(+), 2 deletions(-)
fs/btrfs/ctree.c +7 -0
index 84d7ca1..009bcf7 100644
@@ -1229,6 +1229,7 @@ static void reada_for_search(struct btrfs_root *root,
 	u64 search;
 	u64 target;
 	u64 nread = 0;
+	u64 gen;
 	int direction = path->reada;
 	struct extent_buffer *eb;
 	u32 nr;
@@ -1256,6 +1257,15 @@ static void reada_for_search(struct btrfs_root *root,
 	nritems = btrfs_header_nritems(node);
 	nr = slot;
 	while (1) {
+		if (!node->map_token) {
+			unsigned long offset = btrfs_node_key_ptr_offset(nr);
+			map_private_extent_buffer(node, offset,
+						  sizeof(struct btrfs_key_ptr),
+						  &node->map_token,
+						  &node->kaddr,
Quoted 8 lines

fs/btrfs/ctree.c static void reada_for_search(struct btrfs_root *root, · line 1267

Quoted 2 lines
fs/btrfs/ctree.c +2 -0
+						  &node->map_start,
+						  &node->map_len, KM_USER1);

You can't do that. It puts us in atomic context, and the following readahead_tree_block will try a memory allocation with GFP_NOFS, which leads to a BUG: sleeping function called from invalid context. It didn't fall on our feet earlier because you also turned off readahead, but scrub still uses it.

Quoted 2 lines

We don't make any memory allocations within the area that we've kmap_atomic()'ed, something else is going wrong, this patch is fine.

You're right, the problem is a different one, but still within this function. When there are multiple readers concurrently sneaking through the "if (!node->map_token)" check, the extent will get mapped multiple times, but only unmapped once.
Scrub is running in multiple threads, with path->skip_locking = 1, and it triggers this condition immediately.

-Arne

Quoted 7 lines

Thanks,

Josef
--
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