Skip to contentSkip to conversation

fs: btrfs: Fix tree traversal with btrfs_next_slot()

PATCH In review Yevgeny Popovych 3 participants 7 messages Sep 7, 2018 – Oct 11, 2018 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: btrfs: Fix tree traversal with btrfs_next_slot()

When traversing slots in a btree (via btrfs_path) with btrfs_next_slot(), we didn't correctly identify that the last slot in the leaf was reached and we should jump to the next leaf.

This could lead to any kind of runtime errors or corruptions, like:
* file data not being read at all, or is read partially
* file is read but is corrupted
* (any) metadata being corrupted or not read at all, etc

The easiest way to reproduce this is to read a large enough file that its EXTENT_DATA items don't fit into a single leaf.

---
 fs/btrfs/ctree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ctree.c +1 -1
index 4da36a9..b44a47e 100644
@@ -270,7 +270,7 @@ int btrfs_next_slot(struct btrfs_path *p)
 {
 	struct btrfs_leaf *leaf = &p->nodes[0]->leaf;
 
-	if (p->slots[0] >= leaf->header.nritems)
+	if (p->slots[0] + 1 >= leaf->header.nritems)
 		return jump_leaf(p, 1);
 
 	p->slots[0]++;
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/1 fs: btrfs: Fix tree traversal with btrfs_next_slot()

Just a kindly reminder :)

Quoted 32 lines · Yevgeny Popovych

When traversing slots in a btree (via btrfs_path) with btrfs_next_slot(), we didn't correctly identify that the last slot in the leaf was reached and we should jump to the next leaf.

This could lead to any kind of runtime errors or corruptions, like:
* file data not being read at all, or is read partially
* file is read but is corrupted
* (any) metadata being corrupted or not read at all, etc

The easiest way to reproduce this is to read a large enough file that its EXTENT_DATA items don't fit into a single leaf.

---
 fs/btrfs/ctree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ctree.c +1 -1
index 4da36a9..b44a47e 100644
@@ -270,7 +270,7 @@ int btrfs_next_slot(struct btrfs_path *p)
 {
 	struct btrfs_leaf *leaf = &p->nodes[0]->leaf;
 
-	if (p->slots[0] >= leaf->header.nritems)
+	if (p->slots[0] + 1 >= leaf->header.nritems)
 		return jump_leaf(p, 1);
 
 	p->slots[0]++;
Signature · 2 lines
Sincerely,
Yevgeny Popovych
#2 lore raw

PATCH 1/1 fs: btrfs: Fix tree traversal with btrfs_next_slot()

I shall test this today or tomorrow, but have too many things on my head, sorry :(
You saying that this will fix reading large files? I haven't encountered such an error yet, but can try creating a large enough file. How large should it be?

Quoted 36 lines · On Mon, 1 Oct 2018 08:50:11 +0300 Yevgeny Popovych <[email protected]>…

Just a kindly reminder :)

Quoted 32 lines · Yevgeny Popovych

When traversing slots in a btree (via btrfs_path) with btrfs_next_slot(), we didn't correctly identify that the last slot in the leaf was reached and we should jump to the next leaf.

This could lead to any kind of runtime errors or corruptions, like:
* file data not being read at all, or is read partially
* file is read but is corrupted
* (any) metadata being corrupted or not read at all, etc

The easiest way to reproduce this is to read a large enough file that its EXTENT_DATA items don't fit into a single leaf.

---
 fs/btrfs/ctree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ctree.c +1 -1
index 4da36a9..b44a47e 100644
@@ -270,7 +270,7 @@ int btrfs_next_slot(struct btrfs_path *p)
 {
 	struct btrfs_leaf *leaf = &p->nodes[0]->leaf;
 
-	if (p->slots[0] >= leaf->header.nritems)
+	if (p->slots[0] + 1 >= leaf->header.nritems)
 		return jump_leaf(p, 1);
 
 	p->slots[0]++;
#3 lore raw

PATCH 1/1 fs: btrfs: Fix tree traversal with btrfs_next_slot()

Hi Marek,

No hurry from my side, please take your time!

I think that might be the case for every file that is > 32 MiB but it definitely depends on filesystem layout (other files) and maybe some options (my EXTENT_DATA describe blocks of 1MiB size)? This also can happen to almost any file that is > 1 MiB given that there are enough files + some luck.

Bottom line - create a 50MiB file and add 50 2 MiB files to the filesystem. I would expect that to create necessary conditions. After trying to read the file - check file checksum (no guarantee that it will end up with an error).

You can inspect FS with dump-tree to check if EXTENT_DATA items of this inode (large file) is split across the tree leaves.

Thanks!

Quoted 46 lines · Marek Behún

I shall test this today or tomorrow, but have too many things on my head, sorry :(
You saying that this will fix reading large files? I haven't encountered such an error yet, but can try creating a large enough file. How large should it be?

Quoted 36 lines · On Mon, 1 Oct 2018 08:50:11 +0300 Yevgeny Popovych <[email protected]>…

Just a kindly reminder :)

Quoted 32 lines · Yevgeny Popovych

When traversing slots in a btree (via btrfs_path) with btrfs_next_slot(), we didn't correctly identify that the last slot in the leaf was reached and we should jump to the next leaf.

This could lead to any kind of runtime errors or corruptions, like:
* file data not being read at all, or is read partially
* file is read but is corrupted
* (any) metadata being corrupted or not read at all, etc

The easiest way to reproduce this is to read a large enough file that its EXTENT_DATA items don't fit into a single leaf.

---
 fs/btrfs/ctree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ctree.c +1 -1
index 4da36a9..b44a47e 100644
@@ -270,7 +270,7 @@ int btrfs_next_slot(struct btrfs_path *p)
 {
 	struct btrfs_leaf *leaf = &p->nodes[0]->leaf;
 
-	if (p->slots[0] >= leaf->header.nritems)
+	if (p->slots[0] + 1 >= leaf->header.nritems)
 		return jump_leaf(p, 1);
 
 	p->slots[0]++;
Signature · 2 lines
Sincerely,
Yevgeny Popovych
#4 lore raw

PATCH 1/1 fs: btrfs: Fix tree traversal with btrfs_next_slot()

In reply to Yevgeny Popovych

Hello Tom, could you please apply the patch by Yevgeny?

Marek

Quoted 31 lines · On Fri, 7 Sep 2018 12:59:30 +0300 Yevgeny Popovych <[email protected]>…

When traversing slots in a btree (via btrfs_path) with btrfs_next_slot(), we didn't correctly identify that the last slot in the leaf was reached and we should jump to the next leaf.

This could lead to any kind of runtime errors or corruptions, like:
* file data not being read at all, or is read partially
* file is read but is corrupted
* (any) metadata being corrupted or not read at all, etc

The easiest way to reproduce this is to read a large enough file that its EXTENT_DATA items don't fit into a single leaf.

---
 fs/btrfs/ctree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ctree.c +1 -1
index 4da36a9..b44a47e 100644
@@ -270,7 +270,7 @@ int btrfs_next_slot(struct btrfs_path *p)
 {
 	struct btrfs_leaf *leaf = &p->nodes[0]->leaf;
 
-	if (p->slots[0] >= leaf->header.nritems)
+	if (p->slots[0] + 1 >= leaf->header.nritems)
 		return jump_leaf(p, 1);
 
 	p->slots[0]++;
#5 lore raw

PATCH 1/1 fs: btrfs: Fix tree traversal with btrfs_next_slot()

Quoted 3 lines · Marek Behún

Hello Tom, could you please apply the patch by Yevgeny?

Sorry, I'll just lament that patchwork no longer has the ability to list unassigned patches anymore. I've given this to myself in patchwork and will pick it up shortly. Thanks!

Quoted 38 lines

Marek

Quoted 31 lines · On Fri, 7 Sep 2018 12:59:30 +0300 Yevgeny Popovych <[email protected]>…

When traversing slots in a btree (via btrfs_path) with btrfs_next_slot(), we didn't correctly identify that the last slot in the leaf was reached and we should jump to the next leaf.

This could lead to any kind of runtime errors or corruptions, like:
* file data not being read at all, or is read partially
* file is read but is corrupted
* (any) metadata being corrupted or not read at all, etc

The easiest way to reproduce this is to read a large enough file that its EXTENT_DATA items don't fit into a single leaf.

---
 fs/btrfs/ctree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/ctree.c +1 -1
index 4da36a9..b44a47e 100644
@@ -270,7 +270,7 @@ int btrfs_next_slot(struct btrfs_path *p)
 {
 	struct btrfs_leaf *leaf = &p->nodes[0]->leaf;
 
-	if (p->slots[0] >= leaf->header.nritems)
+	if (p->slots[0] + 1 >= leaf->header.nritems)
 		return jump_leaf(p, 1);
 
 	p->slots[0]++;
Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20181008/0bf06144/attachment.sig>
#6 lore raw
In reply to Yevgeny Popovych
Quoted 9 lines · Yevgeny Popovych

When traversing slots in a btree (via btrfs_path) with btrfs_next_slot(), we didn't correctly identify that the last slot in the leaf was reached and we should jump to the next leaf.

This could lead to any kind of runtime errors or corruptions, like:
* file data not being read at all, or is read partially
* file is read but is corrupted
* (any) metadata being corrupted or not read at all, etc

Quoted 6 lines

The easiest way to reproduce this is to read a large enough file that its EXTENT_DATA items don't fit into a single leaf.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20181011/6d5093cd/attachment.sig>