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