[PATCH] btrfs-progs: Do proper error handling in add_cache_extent()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If we have memory allocation failure in add_cache_extent(), it will
simply exit with one error message.

That's definitely not proper, especially when all but one call sites
have handled the error.

This patch will return -ENOMEM for add_cache_extent(), and fix the only
call site which doesn't handle error from it.

Signed-off-by: Qu Wenruo <wqu@xxxxxxxx>
---
 check/main.c   | 12 +++++++++++-
 extent-cache.c |  6 ++----
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/check/main.c b/check/main.c
index a41117fe79f9..c51dad8f2c89 100644
--- a/check/main.c
+++ b/check/main.c
@@ -4924,7 +4924,17 @@ static int add_pending(struct cache_tree *pending,
 	ret = add_cache_extent(seen, bytenr, size);
 	if (ret)
 		return ret;
-	add_cache_extent(pending, bytenr, size);
+	ret = add_cache_extent(pending, bytenr, size);
+	if (ret) {
+		struct cache_extent *entry;
+
+		entry = lookup_cache_extent(seen, bytenr, size);
+		if (entry && entry->start == bytenr && entry->size == size) {
+			remove_cache_extent(seen, entry);
+			free(entry);
+		}
+		return ret;
+	}
 	return 0;
 }
 
diff --git a/extent-cache.c b/extent-cache.c
index 4065522aafcf..927597b31978 100644
--- a/extent-cache.c
+++ b/extent-cache.c
@@ -111,10 +111,8 @@ int add_cache_extent(struct cache_tree *tree, u64 start, u64 size)
 	struct cache_extent *pe = alloc_cache_extent(start, size);
 	int ret;
 
-	if (!pe) {
-		fprintf(stderr, "memory allocation failed\n");
-		exit(1);
-	}
+	if (!pe)
+		return -ENOMEM;
 
 	ret = insert_cache_extent(tree, pe);
 	if (ret)
-- 
2.26.0




[Index of Archives]     [Linux Filesystem Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux