Skip to contentSkip to conversation

btrfs: Merge inode_can_compress in inode_need_compress

PATCH In review Nikolay Borisov 3 participants 5 messages Aug 18, 2020 – Aug 31, 2020 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: Merge inode_can_compress in inode_need_compress

The latter is the only caller of the former. Just open code can_compress into need_compress and also remove the warning since it's made redundant by this change.

---
 fs/btrfs/inode.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
fs/btrfs/inode.c +3 -18
index 15dc8b6871ac..19e1918bad5c 100644
@@ -410,17 +410,6 @@ static noinline int add_async_extent(struct async_chunk *cow,
 	return 0;
 }
 
-/*
- * Check if the inode has flags compatible with compression
- */
-static inline bool inode_can_compress(struct btrfs_inode *inode)
-{
-	if (inode->flags & BTRFS_INODE_NODATACOW ||
-	    inode->flags & BTRFS_INODE_NODATASUM)
-		return false;
-	return true;
-}
-
 /*
  * Check if the inode needs to be submitted to compression, based on mount
  * options, defragmentation, properties or heuristics.
@@ -430,12 +419,9 @@ static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
 {
 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
 
-	if (!inode_can_compress(inode)) {
-		WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
-			KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
-			btrfs_ino(inode));
+	if (inode->flags & BTRFS_INODE_NODATACOW ||
+	    inode->flags & BTRFS_INODE_NODATASUM)
 		return 0;
-	}
 	/* force compress */
 	if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
 		return 1;
@@ -1833,8 +1819,7 @@ int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page
 	} else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
 		ret = run_delalloc_nocow(inode, locked_page, start, end,
 					 page_started, 0, nr_written);
-	} else if (!inode_can_compress(inode) ||
-		   !inode_need_compress(inode, start, end)) {
+	} else if (!inode_need_compress(inode, start, end)) {
 		ret = cow_file_range(inode, locked_page, start, end,
 				     page_started, nr_written, 1);
 	} else {
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Nikolay Borisov

The latter is the only caller of the former. Just open code can_compress into need_compress and also remove the warning since it's made redundant by this change.

Thanks,
Qu

Quoted 52 lines
---
 fs/btrfs/inode.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
fs/btrfs/inode.c +3 -18
index 15dc8b6871ac..19e1918bad5c 100644
@@ -410,17 +410,6 @@ static noinline int add_async_extent(struct async_chunk *cow,
 	return 0;
 }
 
-/*
- * Check if the inode has flags compatible with compression
- */
-static inline bool inode_can_compress(struct btrfs_inode *inode)
-{
-	if (inode->flags & BTRFS_INODE_NODATACOW ||
-	    inode->flags & BTRFS_INODE_NODATASUM)
-		return false;
-	return true;
-}
-
 /*
  * Check if the inode needs to be submitted to compression, based on mount
  * options, defragmentation, properties or heuristics.
@@ -430,12 +419,9 @@ static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
 {
 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
 
-	if (!inode_can_compress(inode)) {
-		WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
-			KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
-			btrfs_ino(inode));
+	if (inode->flags & BTRFS_INODE_NODATACOW ||
+	    inode->flags & BTRFS_INODE_NODATASUM)
 		return 0;
-	}
 	/* force compress */
 	if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
 		return 1;
@@ -1833,8 +1819,7 @@ int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page
 	} else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
 		ret = run_delalloc_nocow(inode, locked_page, start, end,
 					 page_started, 0, nr_written);
-	} else if (!inode_can_compress(inode) ||
-		   !inode_need_compress(inode, start, end)) {
+	} else if (!inode_need_compress(inode, start, end)) {
 		ret = cow_file_range(inode, locked_page, start, end,
 				     page_started, nr_written, 1);
 	} else {
#2 lore raw
In reply to Nikolay Borisov
Quoted 3 lines · Nikolay Borisov

The latter is the only caller of the former. Just open code can_compress into need_compress and also remove the warning since it's made redundant by this change.

We had discussions and some disagreements in the past regarding the need/can compression helpers

(v1) https://lore.kernel.org/linux-btrfs/[email protected]/
(v2) https://lore.kernel.org/linux-btrfs/[email protected]/

The fix got merged as there was a pending user report and bug to fix, but I remember there's more to do.

While this patch cleans up some of the code, I don't think it's going the right way, we have 2 semantics for the compression decision and the helpers codify that.

I haven't read the discussions yet again and will have to do that before merging this patch.

#3 lore raw
In reply to Nikolay Borisov
Quoted 32 lines · Nikolay Borisov

The latter is the only caller of the former. Just open code can_compress into need_compress and also remove the warning since it's made redundant by this change.

---
 fs/btrfs/inode.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
fs/btrfs/inode.c +0 -11
index 15dc8b6871ac..19e1918bad5c 100644
@@ -410,17 +410,6 @@ static noinline int add_async_extent(struct async_chunk *cow,
 	return 0;
 }
 
-/*
- * Check if the inode has flags compatible with compression
- */
-static inline bool inode_can_compress(struct btrfs_inode *inode)
-{
-	if (inode->flags & BTRFS_INODE_NODATACOW ||
-	    inode->flags & BTRFS_INODE_NODATASUM)
-		return false;
-	return true;
-}
-
 /*
  * Check if the inode needs to be submitted to compression, based on mount
  * options, defragmentation, properties or heuristics.

fs/btrfs/inode.c static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, · line 424

Quoted 10 lines
fs/btrfs/inode.c +2 -4
@@ -430,12 +419,9 @@ static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
 {
 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
 
-	if (!inode_can_compress(inode)) {
-		WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
-			KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
-			btrfs_ino(inode));
+	if (inode->flags & BTRFS_INODE_NODATACOW ||
+	    inode->flags & BTRFS_INODE_NODATASUM)
 		return 0;

This serves as a runtime friendly assert, when inode_need_compress is called from compress_file_range.

First call in btrfs_run_delalloc_range duplicates the nodatacow/nodatasum which would be cleaned up by this patch but we'd lose the warning in case compress_file_range detects the unexpected flags.

#4 lore raw
Quoted 47 lines · David Sterba
Quoted 32 lines · Nikolay Borisov

The latter is the only caller of the former. Just open code can_compress into need_compress and also remove the warning since it's made redundant by this change.

---
 fs/btrfs/inode.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
fs/btrfs/inode.c +0 -11
index 15dc8b6871ac..19e1918bad5c 100644
@@ -410,17 +410,6 @@ static noinline int add_async_extent(struct async_chunk *cow,
 	return 0;
 }
 
-/*
- * Check if the inode has flags compatible with compression
- */
-static inline bool inode_can_compress(struct btrfs_inode *inode)
-{
-	if (inode->flags & BTRFS_INODE_NODATACOW ||
-	    inode->flags & BTRFS_INODE_NODATASUM)
-		return false;
-	return true;
-}
-
 /*
  * Check if the inode needs to be submitted to compression, based on mount
  * options, defragmentation, properties or heuristics.

fs/btrfs/inode.c static inline int inode_need_compress(struct btrfs_inode *inode, u64 start, · line 424

Quoted 10 lines
fs/btrfs/inode.c +2 -4
@@ -430,12 +419,9 @@ static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
 {
 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
 
-	if (!inode_can_compress(inode)) {
-		WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
-			KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
-			btrfs_ino(inode));
+	if (inode->flags & BTRFS_INODE_NODATACOW ||
+	    inode->flags & BTRFS_INODE_NODATASUM)
 		return 0;

This serves as a runtime friendly assert, when inode_need_compress is called from compress_file_range.

Quoted 5 lines

First call in btrfs_run_delalloc_range duplicates the nodatacow/nodatasum which would be cleaned up by this patch but we'd lose the warning in case compress_file_range detects the unexpected flags.

I think we should simply come to an agreement at which stage of a write shall we check for the presence of flags. IMO the less places we have the better. This will obviate the need for a run-time assert... Frankly I think we should be checking only at the time of btrfs_run_delalloc_range