1From d37a9f1818fa04fc91a497b3541ed205804720af Mon Sep 17 00:00:00 2001
2From: "lihaoxiang (F)" <lihaoxiang9@huawei.com>
3Date: Tue, 15 Nov 2022 16:29:55 +0800
4Subject: [PATCH] debugfs: fix repeated output problem with `logdump -O -n
5 <num_trans>`
6
7Previously, patch 6e4cc3d5eeb2dfaa055e652b5390beaa6c3d05da introduces
8the function of printing the specified number of logs. But there exists
9a shortage when n is larger than the total number of logs, it dumped the
10duplicated records circulately.
11
12For example, the disk sda only has three records, but using instruction logdump
13-On5, it would output the result as follow:
14----------------------------------------------------------------------
15Journal starts at block 1, transaction 6
16Found expected sequence 6, type 1 (descriptor block) at block 1
17Found expected sequence 6, type 2 (commit block) at block 4
18No magic number at block 5: end of journal.
19Found sequence 2 (not 7) at block 7: end of journal.
20Found expected sequence 2, type 2 (commit block) at block 7
21Found sequence 3 (not 8) at block 8: end of journal.
22Found expected sequence 3, type 1 (descriptor block) at block 8
23Found sequence 3 (not 8) at block 15: end of journal.
24Found expected sequence 3, type 2 (commit block) at block 15
25Found sequence 6 (not 9) at block 1: end of journal.       <---------begin loop
26Found expected sequence 6, type 1 (descriptor block) at block 1
27Found sequence 6 (not 9) at block 4: end of journal.
28Found expected sequence 6, type 2 (commit block) at block 4
29Found sequence 2 (not 10) at block 7: end of journal.
30Found expected sequence 2, type 2 (commit block) at block 7
31logdump: short read (read 0, expected 1024) while reading journal
32
33In this commit, we solve the problem above by exiting dumping if the
34blocknr had already encountered, displayed the total number of logs
35that the disk only possessed.
36
37Signed-off-by: lihaoxiang <lihaoxiang9@huawei.com>
38Signed-off-by: Theodore Ts'o <tytso@mit.edu>
39---
40 debugfs/logdump.c | 9 +++++++++
41 1 file changed, 9 insertions(+)
42
43diff --git a/debugfs/logdump.c b/debugfs/logdump.c
44index 614414e..036b50b 100644
45--- a/debugfs/logdump.c
46+++ b/debugfs/logdump.c
47@@ -376,6 +376,7 @@ static void dump_journal(char *cmdname, FILE *out_file,
48 	journal_header_t	*header;
49 	tid_t			transaction;
50 	unsigned int		blocknr = 0;
51+	unsigned int		first_transaction_blocknr;
52 	int			fc_done;
53 	__u64			total_len;
54 	__u32			maxlen;
55@@ -470,10 +471,18 @@ static void dump_journal(char *cmdname, FILE *out_file,
56 			blocknr = 1;
57 	}
58 
59+	first_transaction_blocknr = blocknr;
60+
61 	while (1) {
62 		if (dump_old && (dump_counts != -1) && (cur_counts >= dump_counts))
63 			break;
64 
65+		if ((blocknr == first_transaction_blocknr) &&
66+		    (cur_counts != 0) && dump_old && (dump_counts != -1)) {
67+			fprintf(out_file, "Dump all %lld journal records.\n", cur_counts);
68+			break;
69+		}
70+
71 		retval = read_journal_block(cmdname, source,
72 				((ext2_loff_t) blocknr) * blocksize,
73 				buf, blocksize);
74-- 
751.8.3.1
76
77