Skip to contentSkip to conversation

btrfs: use ASSERT to report logical error in cow_file_range()

PATCH Superseded Anand Jain 3 participants 5 messages Feb 15, 2018 – Feb 19, 2018 Btrfs file system

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

PATCH 1/1 btrfs: use ASSERT to report logical error in cow_file_range()

Use ASSERT to report logical error in cow_file_range(), also move it a bit closer to when the num_bytes is derived.

---
 fs/btrfs/inode.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/btrfs/inode.c +2 -3
index 4b156e191592..5648c4425f1e 100644
@@ -976,6 +976,8 @@ static noinline int cow_file_range(struct inode *inode,
 
 	num_bytes = ALIGN(end - start + 1, blocksize);
 	num_bytes = max(blocksize,  num_bytes);
+	ASSERT(!(num_bytes >
+		 btrfs_super_total_bytes(fs_info->super_copy)));
 
 	inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
 
@@ -1006,9 +1008,6 @@ static noinline int cow_file_range(struct inode *inode,
 		}
 	}
 
-	BUG_ON(num_bytes >
-	       btrfs_super_total_bytes(fs_info->super_copy));
-
 	alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
 	btrfs_drop_extent_cache(BTRFS_I(inode), start,
 			start + num_bytes - 1, 0);
Signature · 1 line
2.15.0
#1 lore raw
Quoted 13 lines · Anand Jain

Use ASSERT to report logical error in cow_file_range(), also move it a bit closer to when the num_bytes is derived.

---
 fs/btrfs/inode.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/btrfs/inode.c +0 -0
index 4b156e191592..5648c4425f1e 100644

fs/btrfs/inode.c static noinline int cow_file_range(struct inode *inode, · line 980

Quoted 5 lines
fs/btrfs/inode.c +2 -0
@@ -976,6 +976,8 @@ static noinline int cow_file_range(struct inode *inode,
 
 	num_bytes = ALIGN(end - start + 1, blocksize);
 	num_bytes = max(blocksize,  num_bytes);
+	ASSERT(!(num_bytes >
+		 btrfs_super_total_bytes(fs_info->super_copy)));

The gist of the assert is to see if the range we want to cow is not larger than the actual filesystem size. So just write it like:

ASSERT(numbytes <= btrfs_super_total_bytes(fs_info->super_copy))

It's a lot easier to grok.

Quoted 14 lines
fs/btrfs/inode.c +0 -3
 	inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
 
@@ -1006,9 +1008,6 @@ static noinline int cow_file_range(struct inode *inode,
 		}
 	}
 
-	BUG_ON(num_bytes >
-	       btrfs_super_total_bytes(fs_info->super_copy));
-
 	alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
 	btrfs_drop_extent_cache(BTRFS_I(inode), start,
 			start + num_bytes - 1, 0);
author
#2 lore raw

PATCH v2 1/1 btrfs: use ASSERT to report logical error in cow_file_range()

Use ASSERT to report logical error in cow_file_range(), also move it a bit closer to when the num_bytes is derived.

---
v1->v2:
ASSERT logic changed. Thanks Nikolay.

 fs/btrfs/inode.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/btrfs/inode.c +1 -3
index 4b156e191592..260fd8139951 100644
@@ -976,6 +976,7 @@ static noinline int cow_file_range(struct inode *inode,
 
 	num_bytes = ALIGN(end - start + 1, blocksize);
 	num_bytes = max(blocksize,  num_bytes);
+	ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
 
 	inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
 
@@ -1006,9 +1007,6 @@ static noinline int cow_file_range(struct inode *inode,
 		}
 	}
 
-	BUG_ON(num_bytes >
-	       btrfs_super_total_bytes(fs_info->super_copy));
-
 	alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
 	btrfs_drop_extent_cache(BTRFS_I(inode), start,
 			start + num_bytes - 1, 0);
Signature · 1 line
2.15.0
Reviewed-by on v2 1/1
#3 lore raw
Quoted 4 lines · Anand Jain

Use ASSERT to report logical error in cow_file_range(), also move it a bit closer to when the num_bytes is derived.

Quoted 30 lines

---
v1->v2:
ASSERT logic changed. Thanks Nikolay.

 fs/btrfs/inode.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/btrfs/inode.c +1 -3
index 4b156e191592..260fd8139951 100644
@@ -976,6 +976,7 @@ static noinline int cow_file_range(struct inode *inode,
 
 	num_bytes = ALIGN(end - start + 1, blocksize);
 	num_bytes = max(blocksize,  num_bytes);
+	ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
 
 	inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
 
@@ -1006,9 +1007,6 @@ static noinline int cow_file_range(struct inode *inode,
 		}
 	}
 
-	BUG_ON(num_bytes >
-	       btrfs_super_total_bytes(fs_info->super_copy));
-
 	alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
 	btrfs_drop_extent_cache(BTRFS_I(inode), start,
 			start + num_bytes - 1, 0);
Reviewed-by on v2 1/1
#4 lore raw
In reply to Anand Jain
Quoted 16 lines · Anand Jain

Use ASSERT to report logical error in cow_file_range(), also move it a bit closer to when the num_bytes is derived.

---
v1->v2:
ASSERT logic changed. Thanks Nikolay.

 fs/btrfs/inode.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/btrfs/inode.c +0 -0
index 4b156e191592..260fd8139951 100644

fs/btrfs/inode.c static noinline int cow_file_range(struct inode *inode, · line 979

Quoted 4 lines
fs/btrfs/inode.c +1 -0
@@ -976,6 +976,7 @@ static noinline int cow_file_range(struct inode *inode,
 
 	num_bytes = ALIGN(end - start + 1, blocksize);
 	num_bytes = max(blocksize,  num_bytes);
+	ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));

I was looking how if this assert is valid and theoretically possible. Yes it seems so, extent start of range could be (u64)-1 in some cases and this must not enter cow_file_range. So the assert is the right way to check the parameter mismatch.