1From a1530ed61778e99da315cb10cea2c46d215b096f Mon Sep 17 00:00:00 2001
2From: Wenchao Hao <haowenchao2@huawei.com>
3Date: Tue, 19 Dec 2023 17:23:57 +0800
4Subject: [PATCH] debugfs: Fix infinite loop when dump log
5
6There are 2 scenarios which would trigger infinite loop:
7
81. None log is recorded, then dumplog with "-n", for example:
9   debugfs -R "logdump -O -n 10" /dev/xxx
10   while /dev/xxx has no valid log recorded.
112. The log area is full and cycle write is triggered, then dumplog with
12   debugfs -R "logdump -aOS" /dev/xxx
13
14This patch add a new flag "reverse_flag" to mark if logdump has reached
15to tail of logarea, it is default false, and set in macro WRAP().
16
17If reverse_flag is true, and we comes to first_transaction_blocknr
18again, just break the logdump loop.
19
20Signed-off-by: Wenchao Hao <haowenchao2@huawei.com>
21---
22 debugfs/logdump.c | 11 +++++++----
23 1 file changed, 7 insertions(+), 4 deletions(-)
24
25diff --git a/debugfs/logdump.c b/debugfs/logdump.c
26index 853be41..c4686ae 100644
27--- a/debugfs/logdump.c
28+++ b/debugfs/logdump.c
29@@ -45,6 +45,7 @@ static int64_t		dump_counts;
30 static blk64_t		block_to_dump, bitmap_to_dump, inode_block_to_dump;
31 static unsigned int	group_to_dump, inode_offset_to_dump;
32 static ext2_ino_t	inode_to_dump;
33+static bool		reverse_flag;
34 
35 struct journal_source
36 {
37@@ -73,8 +74,10 @@ static void dump_fc_block(FILE *out_file, char *buf, int blocksize,
38 static void do_hexdump (FILE *, char *, int);
39 
40 #define WRAP(jsb, blocknr, maxlen)					\
41-	if (blocknr >= (maxlen))					\
42-	    blocknr -= (maxlen - be32_to_cpu((jsb)->s_first));
43+	if (blocknr >= (maxlen)) {					\
44+		blocknr -= (maxlen - be32_to_cpu((jsb)->s_first));	\
45+		reverse_flag = true;					\
46+	}
47 
48 void do_logdump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
49 		    void *infop EXT2FS_ATTR((unused)))
50@@ -108,6 +111,7 @@ void do_logdump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
51 	inode_block_to_dump = ANY_BLOCK;
52 	inode_to_dump = -1;
53 	dump_counts = -1;
54+	reverse_flag = false;
55 
56 	reset_getopt();
57 	while ((c = getopt (argc, argv, "ab:ci:f:OsSn:")) != EOF) {
58@@ -470,8 +474,7 @@ static void dump_journal(char *cmdname, FILE *out_file,
59 		if (dump_old && (dump_counts != -1) && (cur_counts >= dump_counts))
60 			break;
61 
62-		if ((blocknr == first_transaction_blocknr) &&
63-		    (cur_counts != 0) && dump_old && (dump_counts != -1)) {
64+		if ((blocknr == first_transaction_blocknr) && dump_old && reverse_flag) {
65 			fprintf(out_file, "Dump all %lld journal records.\n", cur_counts);
66 			break;
67 		}
68-- 
692.32.0
70
71