diff mbox series

debugfs:add logdump with option -n that display n records

Message ID 608df030-593f-8c69-cb65-632a34729d23@huawei.com
State Accepted
Headers show
Series debugfs:add logdump with option -n that display n records | expand

Commit Message

lihaoxiang (F) July 14, 2022, 1:32 a.m. UTC
The current version's debugfs possessed the function logdump. Executing
with option -O could output the log history. But when it occurred the block
which had no magic number in it's header, the program would exit.

Sometimes we were locating problems, needed for more transactions that
had replayed instead of the latest batch of transactions and we weren't
hope to display all the history in the meanwhile. So we introduced
the option -n used for controlling the print of history transactions.
Specially, this parameter was depending on the option -O otherwise it
couldn't work.

So in this modification, we used logdump with -O -n <history trans num>.
It would not stop searching even if occurred no magic blocks or not
corherent transactions. The only terminated condition was that all logs
had been outputed or the outputed log counts reached the limitation of -n.

Signed-off-by: lihaoxiang <lihaoxiang9@huawei.com>
---
 debugfs/logdump.c | 32 ++++++++++++++++++++++++++++----
 1 file changed, 28 insertions(+), 4 deletions(-)

Comments

lihaoxiang (F) Aug. 11, 2022, 12:33 p.m. UTC | #1
Friendly ping...

On 2022/7/14 9:32, lihaoxiang (F) wrote:
> The current version's debugfs possessed the function logdump. Executing
> with option -O could output the log history. But when it occurred the block
> which had no magic number in it's header, the program would exit.
> 
> Sometimes we were locating problems, needed for more transactions that
> had replayed instead of the latest batch of transactions and we weren't
> hope to display all the history in the meanwhile. So we introduced
> the option -n used for controlling the print of history transactions.
> Specially, this parameter was depending on the option -O otherwise it
> couldn't work.
> 
> So in this modification, we used logdump with -O -n <history trans num>.
> It would not stop searching even if occurred no magic blocks or not
> corherent transactions. The only terminated condition was that all logs
> had been outputed or the outputed log counts reached the limitation of -n.
> 
> Signed-off-by: lihaoxiang <lihaoxiang9@huawei.com>
> ---
>  debugfs/logdump.c | 32 ++++++++++++++++++++++++++++----
>  1 file changed, 28 insertions(+), 4 deletions(-)
> 
> diff --git a/debugfs/logdump.c b/debugfs/logdump.c
> index 4154ef2a..1067961f 100644
> --- a/debugfs/logdump.c
> +++ b/debugfs/logdump.c
> @@ -48,6 +48,7 @@ enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
>  #define ANY_BLOCK ((blk64_t) -1)
> 
>  static int		dump_all, dump_super, dump_old, dump_contents, dump_descriptors;
> +static int64_t		dump_counts;
>  static blk64_t		block_to_dump, bitmap_to_dump, inode_block_to_dump;
>  static unsigned int	group_to_dump, inode_offset_to_dump;
>  static ext2_ino_t	inode_to_dump;
> @@ -113,9 +114,10 @@ void do_logdump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
>  	bitmap_to_dump = -1;
>  	inode_block_to_dump = ANY_BLOCK;
>  	inode_to_dump = -1;
> +	dump_counts = -1;
> 
>  	reset_getopt();
> -	while ((c = getopt (argc, argv, "ab:ci:f:OsS")) != EOF) {
> +	while ((c = getopt (argc, argv, "ab:ci:f:OsSn:")) != EOF) {
>  		switch (c) {
>  		case 'a':
>  			dump_all++;
> @@ -148,6 +150,14 @@ void do_logdump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
>  		case 'S':
>  			dump_super++;
>  			break;
> +		case 'n':
> +			dump_counts = strtol(optarg, &tmp, 10);
> +			if (*tmp) {
> +				com_err(argv[0], 0,
> +					"Bad log counts number - %s", optarg);
> +				return;
> +			}
> +			break;
>  		default:
>  			goto print_usage;
>  		}
> @@ -289,7 +299,7 @@ cleanup:
>  	return;
> 
>  print_usage:
> -	fprintf(stderr, "%s: Usage: logdump [-acsOS] [-b<block>] [-i<filespec>]\n\t"
> +	fprintf(stderr, "%s: Usage: logdump [-acsOS] [-n<num_trans>] [-b<block>] [-i<filespec>]\n\t"
>  		"[-f<journal_file>] [output_file]\n", argv[0]);
>  }
> 
> @@ -369,6 +379,8 @@ static void dump_journal(char *cmdname, FILE *out_file,
>  	int			fc_done;
>  	__u64			total_len;
>  	__u32			maxlen;
> +	int64_t			cur_counts = 0;			
> +	bool			exist_no_magic = false;
> 
>  	/* First, check to see if there's an ext2 superblock header */
>  	retval = read_journal_block(cmdname, source, 0, buf, 2048);
> @@ -459,6 +471,9 @@ static void dump_journal(char *cmdname, FILE *out_file,
>  	}
> 
>  	while (1) {
> +		if (dump_old && (dump_counts != -1) && (cur_counts >= dump_counts))
> +			break;
> +		
>  		retval = read_journal_block(cmdname, source,
>  				((ext2_loff_t) blocknr) * blocksize,
>  				buf, blocksize);
> @@ -472,8 +487,16 @@ static void dump_journal(char *cmdname, FILE *out_file,
>  		blocktype = be32_to_cpu(header->h_blocktype);
> 
>  		if (magic != JBD2_MAGIC_NUMBER) {
> -			fprintf (out_file, "No magic number at block %u: "
> -				 "end of journal.\n", blocknr);
> +			if (exist_no_magic == false) {
> +				exist_no_magic = true;
> +				fprintf (out_file, "No magic number at block %u: "
> +					"end of journal.\n", blocknr);
> +			}
> +			if (dump_old && (dump_counts != -1)) {
> +				blocknr++;
> +				WRAP(jsb, blocknr, maxlen);
> +				continue;
> +			}
>  			break;
>  		}
> 
> @@ -500,6 +523,7 @@ static void dump_journal(char *cmdname, FILE *out_file,
>  			continue;
> 
>  		case JBD2_COMMIT_BLOCK:
> +			cur_counts++;
>  			transaction++;
>  			blocknr++;
>  			WRAP(jsb, blocknr, maxlen);
Theodore Ts'o Aug. 13, 2022, 2:58 p.m. UTC | #2
On Thu, 14 Jul 2022 09:32:48 +0800, lihaoxiang (F) wrote:
> The current version's debugfs possessed the function logdump. Executing
> with option -O could output the log history. But when it occurred the block
> which had no magic number in it's header, the program would exit.
> 
> Sometimes we were locating problems, needed for more transactions that
> had replayed instead of the latest batch of transactions and we weren't
> hope to display all the history in the meanwhile. So we introduced
> the option -n used for controlling the print of history transactions.
> Specially, this parameter was depending on the option -O otherwise it
> couldn't work.
> 
> [...]

Applied, thanks!

[1/1] debugfs:add logdump with option -n that display n records
      commit: 6e4cc3d5eeb2dfaa055e652b5390beaa6c3d05da

Best regards,
diff mbox series

Patch

diff --git a/debugfs/logdump.c b/debugfs/logdump.c
index 4154ef2a..1067961f 100644
--- a/debugfs/logdump.c
+++ b/debugfs/logdump.c
@@ -48,6 +48,7 @@  enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
 #define ANY_BLOCK ((blk64_t) -1)

 static int		dump_all, dump_super, dump_old, dump_contents, dump_descriptors;
+static int64_t		dump_counts;
 static blk64_t		block_to_dump, bitmap_to_dump, inode_block_to_dump;
 static unsigned int	group_to_dump, inode_offset_to_dump;
 static ext2_ino_t	inode_to_dump;
@@ -113,9 +114,10 @@  void do_logdump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
 	bitmap_to_dump = -1;
 	inode_block_to_dump = ANY_BLOCK;
 	inode_to_dump = -1;
+	dump_counts = -1;

 	reset_getopt();
-	while ((c = getopt (argc, argv, "ab:ci:f:OsS")) != EOF) {
+	while ((c = getopt (argc, argv, "ab:ci:f:OsSn:")) != EOF) {
 		switch (c) {
 		case 'a':
 			dump_all++;
@@ -148,6 +150,14 @@  void do_logdump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
 		case 'S':
 			dump_super++;
 			break;
+		case 'n':
+			dump_counts = strtol(optarg, &tmp, 10);
+			if (*tmp) {
+				com_err(argv[0], 0,
+					"Bad log counts number - %s", optarg);
+				return;
+			}
+			break;
 		default:
 			goto print_usage;
 		}
@@ -289,7 +299,7 @@  cleanup:
 	return;

 print_usage:
-	fprintf(stderr, "%s: Usage: logdump [-acsOS] [-b<block>] [-i<filespec>]\n\t"
+	fprintf(stderr, "%s: Usage: logdump [-acsOS] [-n<num_trans>] [-b<block>] [-i<filespec>]\n\t"
 		"[-f<journal_file>] [output_file]\n", argv[0]);
 }

@@ -369,6 +379,8 @@  static void dump_journal(char *cmdname, FILE *out_file,
 	int			fc_done;
 	__u64			total_len;
 	__u32			maxlen;
+	int64_t			cur_counts = 0;			
+	bool			exist_no_magic = false;

 	/* First, check to see if there's an ext2 superblock header */
 	retval = read_journal_block(cmdname, source, 0, buf, 2048);
@@ -459,6 +471,9 @@  static void dump_journal(char *cmdname, FILE *out_file,
 	}

 	while (1) {
+		if (dump_old && (dump_counts != -1) && (cur_counts >= dump_counts))
+			break;
+		
 		retval = read_journal_block(cmdname, source,
 				((ext2_loff_t) blocknr) * blocksize,
 				buf, blocksize);
@@ -472,8 +487,16 @@  static void dump_journal(char *cmdname, FILE *out_file,
 		blocktype = be32_to_cpu(header->h_blocktype);

 		if (magic != JBD2_MAGIC_NUMBER) {
-			fprintf (out_file, "No magic number at block %u: "
-				 "end of journal.\n", blocknr);
+			if (exist_no_magic == false) {
+				exist_no_magic = true;
+				fprintf (out_file, "No magic number at block %u: "
+					"end of journal.\n", blocknr);
+			}
+			if (dump_old && (dump_counts != -1)) {
+				blocknr++;
+				WRAP(jsb, blocknr, maxlen);
+				continue;
+			}
 			break;
 		}

@@ -500,6 +523,7 @@  static void dump_journal(char *cmdname, FILE *out_file,
 			continue;

 		case JBD2_COMMIT_BLOCK:
+			cur_counts++;
 			transaction++;
 			blocknr++;
 			WRAP(jsb, blocknr, maxlen);