Skip to content

Commit 8c594ea

Browse files
committed
Btrfs: use the right node in reada_for_balance
reada_for_balance was using the wrong index into the path node array, so it wasn't reading the right blocks. We never directly used the results of the read done by this function because the btree search is started over at the end. This fixes reada_for_balance to reada in the correct node and to avoid searching past the last slot in the node. It also makes sure to hold the parent lock while we are finding the nodes to read. Signed-off-by: Chris Mason <[email protected]>
1 parent 11c8349 commit 8c594ea

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

fs/btrfs/ctree.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,12 +1325,12 @@ static noinline int reada_for_balance(struct btrfs_root *root,
13251325
int ret = 0;
13261326
int blocksize;
13271327

1328-
parent = path->nodes[level - 1];
1328+
parent = path->nodes[level + 1];
13291329
if (!parent)
13301330
return 0;
13311331

13321332
nritems = btrfs_header_nritems(parent);
1333-
slot = path->slots[level];
1333+
slot = path->slots[level + 1];
13341334
blocksize = btrfs_level_size(root, level);
13351335

13361336
if (slot > 0) {
@@ -1341,7 +1341,7 @@ static noinline int reada_for_balance(struct btrfs_root *root,
13411341
block1 = 0;
13421342
free_extent_buffer(eb);
13431343
}
1344-
if (slot < nritems) {
1344+
if (slot + 1 < nritems) {
13451345
block2 = btrfs_node_blockptr(parent, slot + 1);
13461346
gen = btrfs_node_ptr_generation(parent, slot + 1);
13471347
eb = btrfs_find_tree_block(root, block2, blocksize);
@@ -1351,7 +1351,11 @@ static noinline int reada_for_balance(struct btrfs_root *root,
13511351
}
13521352
if (block1 || block2) {
13531353
ret = -EAGAIN;
1354+
1355+
/* release the whole path */
13541356
btrfs_release_path(root, path);
1357+
1358+
/* read the blocks */
13551359
if (block1)
13561360
readahead_tree_block(root, block1, blocksize, 0);
13571361
if (block2)
@@ -1361,7 +1365,7 @@ static noinline int reada_for_balance(struct btrfs_root *root,
13611365
eb = read_tree_block(root, block1, blocksize, 0);
13621366
free_extent_buffer(eb);
13631367
}
1364-
if (block1) {
1368+
if (block2) {
13651369
eb = read_tree_block(root, block2, blocksize, 0);
13661370
free_extent_buffer(eb);
13671371
}
@@ -1481,12 +1485,15 @@ read_block_for_search(struct btrfs_trans_handle *trans,
14811485
* of the btree by dropping locks before
14821486
* we read.
14831487
*/
1484-
btrfs_release_path(NULL, p);
1488+
btrfs_unlock_up_safe(p, level + 1);
1489+
btrfs_set_path_blocking(p);
1490+
14851491
if (tmp)
14861492
free_extent_buffer(tmp);
14871493
if (p->reada)
14881494
reada_for_search(root, p, level, slot, key->objectid);
14891495

1496+
btrfs_release_path(NULL, p);
14901497
tmp = read_tree_block(root, blocknr, blocksize, gen);
14911498
if (tmp)
14921499
free_extent_buffer(tmp);

0 commit comments

Comments
 (0)