• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * logdump.c --- dump the contents of the journal out to a file
3  *
4  * Author: Stephen C. Tweedie, 2001  <sct@redhat.com>
5  * Copyright (C) 2001 Red Hat, Inc.
6  * Based on portions  Copyright (C) 1994 Theodore Ts'o.
7  *
8  * This file may be redistributed under the terms of the GNU Public
9  * License.
10  */
11 
12 #include "config.h"
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <time.h>
19 #ifdef HAVE_ERRNO_H
20 #include <errno.h>
21 #endif
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <utime.h>
26 #ifdef HAVE_GETOPT_H
27 #include <getopt.h>
28 #else
29 extern int optind;
30 extern char *optarg;
31 #endif
32 
33 #include "debugfs.h"
34 #include "blkid/blkid.h"
35 #include "jfs_user.h"
36 #include "ext2fs/fast_commit.h"
37 #include <uuid/uuid.h>
38 
39 enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
40 
41 #define ANY_BLOCK ((blk64_t) -1)
42 
43 static int		dump_all, dump_super, dump_old, dump_contents, dump_descriptors;
44 static blk64_t		block_to_dump, bitmap_to_dump, inode_block_to_dump;
45 static unsigned int	group_to_dump, inode_offset_to_dump;
46 static ext2_ino_t	inode_to_dump;
47 
48 struct journal_source
49 {
50 	enum journal_location where;
51 	int fd;
52 	ext2_file_t file;
53 };
54 
55 static void dump_journal(char *, FILE *, struct journal_source *);
56 
57 static void dump_descriptor_block(FILE *, struct journal_source *,
58 				  char *, journal_superblock_t *,
59 				  unsigned int *, int, __u32, tid_t);
60 
61 static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
62 				  unsigned int, int, tid_t);
63 
64 static void dump_metadata_block(FILE *, struct journal_source *,
65 				journal_superblock_t*,
66 				unsigned int, unsigned int, unsigned int,
67 				int, tid_t);
68 
69 static void dump_fc_block(FILE *out_file, char *buf, int blocksize,
70 			  int transaction, int *fc_done, int dump_old);
71 
72 static void do_hexdump (FILE *, char *, int);
73 
74 #define WRAP(jsb, blocknr, maxlen)					\
75 	if (blocknr >= (maxlen))					\
76 	    blocknr -= (maxlen - be32_to_cpu((jsb)->s_first));
77 
do_logdump(int argc,char ** argv,int sci_idx EXT2FS_ATTR ((unused)),void * infop EXT2FS_ATTR ((unused)))78 void do_logdump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
79 		    void *infop EXT2FS_ATTR((unused)))
80 {
81 	int		c;
82 	int		retval;
83 	char		*out_fn;
84 	FILE		*out_file;
85 
86 	char		*inode_spec = NULL;
87 	char		*journal_fn = NULL;
88 	int		journal_fd = -1;
89 	int		use_sb = 0;
90 	ext2_ino_t	journal_inum;
91 	struct ext2_inode journal_inode;
92 	ext2_file_t 	journal_file = NULL;
93 	char		*tmp;
94 	struct journal_source journal_source;
95 	struct ext2_super_block *es = NULL;
96 
97 	journal_source.where = JOURNAL_IS_INTERNAL;
98 	journal_source.fd = 0;
99 	journal_source.file = 0;
100 	dump_all = 0;
101 	dump_old = 0;
102 	dump_contents = 0;
103 	dump_super = 0;
104 	dump_descriptors = 1;
105 	block_to_dump = ANY_BLOCK;
106 	bitmap_to_dump = -1;
107 	inode_block_to_dump = ANY_BLOCK;
108 	inode_to_dump = -1;
109 
110 	reset_getopt();
111 	while ((c = getopt (argc, argv, "ab:ci:f:OsS")) != EOF) {
112 		switch (c) {
113 		case 'a':
114 			dump_all++;
115 			break;
116 		case 'b':
117 			block_to_dump = strtoul(optarg, &tmp, 0);
118 			if (*tmp) {
119 				com_err(argv[0], 0,
120 					"Bad block number - %s", optarg);
121 				return;
122 			}
123 			dump_descriptors = 0;
124 			break;
125 		case 'c':
126 			dump_contents++;
127 			break;
128 		case 'f':
129 			journal_fn = optarg;
130 			break;
131 		case 'i':
132 			inode_spec = optarg;
133 			dump_descriptors = 0;
134 			break;
135 		case 'O':
136 			dump_old++;
137 			break;
138 		case 's':
139 			use_sb++;
140 			break;
141 		case 'S':
142 			dump_super++;
143 			break;
144 		default:
145 			goto print_usage;
146 		}
147 	}
148 	if (optind != argc && optind != argc-1) {
149 		goto print_usage;
150 	}
151 
152 	if (inode_spec) {
153 		int inode_group, group_offset, inodes_per_block;
154 
155 		if (check_fs_open(argv[0]))
156 			return;
157 
158 		inode_to_dump = string_to_inode(inode_spec);
159 		if (!inode_to_dump)
160 			return;
161 
162 		es = current_fs->super;
163 		inode_group = ((inode_to_dump - 1)
164 			       / es->s_inodes_per_group);
165 		group_offset = ((inode_to_dump - 1)
166 				% es->s_inodes_per_group);
167 		inodes_per_block = (current_fs->blocksize
168 				    / sizeof(struct ext2_inode));
169 
170 		inode_block_to_dump =
171 			ext2fs_inode_table_loc(current_fs, inode_group) +
172 			(group_offset / inodes_per_block);
173 		inode_offset_to_dump = ((group_offset % inodes_per_block)
174 					* sizeof(struct ext2_inode));
175 		printf("Inode %u is at group %u, block %llu, offset %u\n",
176 		       inode_to_dump, inode_group,
177 		       (unsigned long long) inode_block_to_dump,
178 		       inode_offset_to_dump);
179 	}
180 
181 	if (optind == argc) {
182 		out_file = stdout;
183 	} else {
184 		out_fn = argv[optind];
185 		out_file = fopen(out_fn, "w");
186 		if (!out_file) {
187 			com_err(argv[0], errno, "while opening %s for logdump",
188 				out_fn);
189 			goto cleanup;
190 		}
191 	}
192 
193 	if (block_to_dump != ANY_BLOCK) {
194 		if (check_fs_open(argv[0]))
195 			goto cleanup;
196 		es = current_fs->super;
197 		group_to_dump = ((block_to_dump -
198 				  es->s_first_data_block)
199 				 / es->s_blocks_per_group);
200 		bitmap_to_dump = ext2fs_block_bitmap_loc(current_fs, group_to_dump);
201 	}
202 
203 	if (journal_fn) {
204 		/* Set up to read journal from a regular file somewhere */
205 		journal_fd = open(journal_fn, O_RDONLY, 0);
206 		if (journal_fd < 0) {
207 			com_err(argv[0], errno, "while opening %s for logdump",
208 				journal_fn);
209 			goto cleanup;
210 		}
211 		journal_source.where = JOURNAL_IS_EXTERNAL;
212 		journal_source.fd = journal_fd;
213 		dump_journal(argv[0], out_file, &journal_source);
214 		goto cleanup;
215 
216 	}
217 	if (check_fs_open(argv[0]))
218 		goto cleanup;
219 	es = current_fs->super;
220 
221 	if ((journal_inum = es->s_journal_inum)) {
222 		if (use_sb) {
223 			if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) {
224 				com_err(argv[0], 0,
225 					"no journal backup in super block\n");
226 				goto cleanup;
227 			}
228 			memset(&journal_inode, 0, sizeof(struct ext2_inode));
229 			memcpy(&journal_inode.i_block[0], es->s_jnl_blocks,
230 			       EXT2_N_BLOCKS*4);
231 			journal_inode.i_size_high = es->s_jnl_blocks[15];
232 			journal_inode.i_size = es->s_jnl_blocks[16];
233 			journal_inode.i_links_count = 1;
234 			journal_inode.i_mode = LINUX_S_IFREG | 0600;
235 		} else {
236 			if (debugfs_read_inode(journal_inum, &journal_inode,
237 					       argv[0]))
238 				goto cleanup;
239 		}
240 
241 		retval = ext2fs_file_open2(current_fs, journal_inum,
242 					   &journal_inode, 0, &journal_file);
243 		if (retval) {
244 			com_err(argv[0], retval, "while opening ext2 file");
245 			goto cleanup;
246 		}
247 		journal_source.where = JOURNAL_IS_INTERNAL;
248 		journal_source.file = journal_file;
249 	} else {
250 		char uuid[37];
251 
252 		uuid_unparse(es->s_journal_uuid, uuid);
253 		journal_fn = blkid_get_devname(NULL, "UUID", uuid);
254 		if (!journal_fn)
255 				journal_fn = blkid_devno_to_devname(es->s_journal_dev);
256 		if (!journal_fn) {
257 			com_err(argv[0], 0, "filesystem has no journal");
258 			goto cleanup;
259 		}
260 		journal_fd = open(journal_fn, O_RDONLY, 0);
261 		if (journal_fd < 0) {
262 			com_err(argv[0], errno, "while opening %s for logdump",
263 				journal_fn);
264 			free(journal_fn);
265 			goto cleanup;
266 		}
267 		fprintf(out_file, "Using external journal found at %s\n",
268 			journal_fn);
269 		free(journal_fn);
270 		journal_source.where = JOURNAL_IS_EXTERNAL;
271 		journal_source.fd = journal_fd;
272 	}
273 	dump_journal(argv[0], out_file, &journal_source);
274 cleanup:
275 	if (journal_fd >= 0)
276 		close(journal_fd);
277 	if (journal_file)
278 		ext2fs_file_close(journal_file);
279 	if (out_file && (out_file != stdout))
280 		fclose(out_file);
281 
282 	return;
283 
284 print_usage:
285 	fprintf(stderr, "%s: Usage: logdump [-acsOS] [-b<block>] [-i<filespec>]\n\t"
286 		"[-f<journal_file>] [output_file]\n", argv[0]);
287 }
288 
289 
read_journal_block(const char * cmd,struct journal_source * source,ext2_loff_t offset,char * buf,unsigned int size)290 static int read_journal_block(const char *cmd, struct journal_source *source,
291 			      ext2_loff_t offset, char *buf, unsigned int size)
292 {
293 	int retval;
294 	unsigned int got;
295 
296 	if (source->where == JOURNAL_IS_EXTERNAL) {
297 		if (lseek(source->fd, offset, SEEK_SET) < 0) {
298 			retval = errno;
299 			goto seek_err;
300 		}
301 		retval = read(source->fd, buf, size);
302 		if (retval < 0) {
303 			retval = errno;
304 			goto read_err;
305 		}
306 		got = retval;
307 		retval = 0;
308 	} else {
309 		retval = ext2fs_file_llseek(source->file, offset,
310 					    EXT2_SEEK_SET, NULL);
311 		if (retval) {
312 		seek_err:
313 			com_err(cmd, retval, "while seeking in reading journal");
314 			return retval;
315 		}
316 		retval = ext2fs_file_read(source->file, buf, size, &got);
317 		if (retval) {
318 		read_err:
319 			com_err(cmd, retval, "while reading journal");
320 			return retval;
321 		}
322 	}
323 	if (got != size) {
324 		com_err(cmd, 0, "short read (read %u, expected %u) "
325 			"while reading journal", got, size);
326 		retval = -1;
327 	}
328 	return retval;
329 }
330 
type_to_name(int btype)331 static const char *type_to_name(int btype)
332 {
333 	switch (btype) {
334 	case JBD2_DESCRIPTOR_BLOCK:
335 		return "descriptor block";
336 	case JBD2_COMMIT_BLOCK:
337 		return "commit block";
338 	case JBD2_SUPERBLOCK_V1:
339 		return "V1 superblock";
340 	case JBD2_SUPERBLOCK_V2:
341 		return "V2 superblock";
342 	case JBD2_REVOKE_BLOCK:
343 		return "revoke table";
344 	}
345 	return "unrecognised type";
346 }
347 
348 
dump_journal(char * cmdname,FILE * out_file,struct journal_source * source)349 static void dump_journal(char *cmdname, FILE *out_file,
350 			 struct journal_source *source)
351 {
352 	struct ext2_super_block *sb;
353 	char			jsb_buffer[1024];
354 	char			buf[EXT2_MAX_BLOCK_SIZE];
355 	journal_superblock_t	*jsb;
356 	unsigned int		blocksize = 1024;
357 	int			retval;
358 	__u32			magic, sequence, blocktype;
359 	journal_header_t	*header;
360 	tid_t			transaction;
361 	unsigned int		blocknr = 0;
362 	int			fc_done;
363 	__u64			total_len;
364 	__u32			maxlen;
365 
366 	/* First, check to see if there's an ext2 superblock header */
367 	retval = read_journal_block(cmdname, source, 0, buf, 2048);
368 	if (retval)
369 		return;
370 
371 	jsb = (journal_superblock_t *) buf;
372 	sb = (struct ext2_super_block *) (buf+1024);
373 #ifdef WORDS_BIGENDIAN
374 	if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
375 		ext2fs_swap_super(sb);
376 #endif
377 
378 	if ((be32_to_cpu(jsb->s_header.h_magic) != JBD2_MAGIC_NUMBER) &&
379 	    (sb->s_magic == EXT2_SUPER_MAGIC) &&
380 	    ext2fs_has_feature_journal_dev(sb)) {
381 		blocksize = EXT2_BLOCK_SIZE(sb);
382 		blocknr = (blocksize == 1024) ? 2 : 1;
383 		uuid_unparse(sb->s_uuid, jsb_buffer);
384 		fprintf(out_file, "Ext2 superblock header found.\n");
385 		if (dump_all) {
386 			fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
387 			fprintf(out_file, "\tblocksize=%d\n", blocksize);
388 			fprintf(out_file, "\tjournal data size %lu\n",
389 				(unsigned long) ext2fs_blocks_count(sb));
390 		}
391 	}
392 
393 	/* Next, read the journal superblock */
394 	retval = read_journal_block(cmdname, source,
395 				    ((ext2_loff_t) blocknr) * blocksize,
396 				    jsb_buffer, 1024);
397 	if (retval)
398 		return;
399 
400 	if (dump_super) {
401 		e2p_list_journal_super(out_file, jsb_buffer,
402 				       current_fs->blocksize, 0);
403 		fputc('\n', out_file);
404 	}
405 
406 	jsb = (journal_superblock_t *) jsb_buffer;
407 	if (be32_to_cpu(jsb->s_header.h_magic) != JBD2_MAGIC_NUMBER) {
408 		fprintf(out_file,
409 			"Journal superblock magic number invalid!\n");
410 		return;
411 	}
412 	blocksize = be32_to_cpu(jsb->s_blocksize);
413 	if ((current_fs && (blocksize != current_fs->blocksize)) ||
414 	    (!current_fs && (!blocksize || (blocksize & (blocksize - 1)) ||
415 			     (blocksize > EXT2_MAX_BLOCK_SIZE)))) {
416 		fprintf(out_file,
417 			"Journal block size invalid: %u (%u)\n",
418 			be32_to_cpu(jsb->s_blocksize), blocksize);
419 		return;
420 	}
421 	transaction = be32_to_cpu(jsb->s_sequence);
422 	blocknr = be32_to_cpu(jsb->s_start);
423 	if (source->where == JOURNAL_IS_INTERNAL) {
424 		retval = ext2fs_file_get_lsize(source->file, &total_len);
425 		if (retval) {
426 		stat_err:
427 			com_err("dump_journal", retval,
428 				"while getting journal inode size");
429 			return;
430 		}
431 		total_len /= blocksize;
432 	} else {
433 			struct stat st;
434 
435 			if (fstat(source->fd, &st) < 0)
436 				goto stat_err;
437 			total_len = st.st_size / blocksize;
438 	}
439 	maxlen = be32_to_cpu(jsb->s_maxlen);
440 	if (maxlen > total_len)
441 		maxlen = total_len;
442 
443 	fprintf(out_file, "Journal starts at block %u, transaction %u\n",
444 		blocknr, transaction);
445 
446 	if (!blocknr) {
447 		/* Empty journal, nothing to do. */
448 		if (!dump_old)
449 			goto fc;
450 		else
451 			blocknr = 1;
452 	}
453 
454 	while (1) {
455 		retval = read_journal_block(cmdname, source,
456 				((ext2_loff_t) blocknr) * blocksize,
457 				buf, blocksize);
458 		if (retval)
459 			break;
460 
461 		header = (journal_header_t *) buf;
462 
463 		magic = be32_to_cpu(header->h_magic);
464 		sequence = be32_to_cpu(header->h_sequence);
465 		blocktype = be32_to_cpu(header->h_blocktype);
466 
467 		if (magic != JBD2_MAGIC_NUMBER) {
468 			fprintf (out_file, "No magic number at block %u: "
469 				 "end of journal.\n", blocknr);
470 			break;
471 		}
472 
473 		if (sequence != transaction) {
474 			fprintf (out_file, "Found sequence %u (not %u) at "
475 				 "block %u: end of journal.\n",
476 				 sequence, transaction, blocknr);
477 			if (!dump_old)
478 				break;
479 		}
480 
481 		if (dump_descriptors) {
482 			fprintf (out_file, "Found expected sequence %u, "
483 				 "type %u (%s) at block %u\n",
484 				 sequence, blocktype,
485 				 type_to_name(blocktype), blocknr);
486 		}
487 
488 		switch (blocktype) {
489 		case JBD2_DESCRIPTOR_BLOCK:
490 			dump_descriptor_block(out_file, source, buf, jsb,
491 					      &blocknr, blocksize, maxlen,
492 					      transaction);
493 			continue;
494 
495 		case JBD2_COMMIT_BLOCK:
496 			transaction++;
497 			blocknr++;
498 			WRAP(jsb, blocknr, maxlen);
499 			continue;
500 
501 		case JBD2_REVOKE_BLOCK:
502 			dump_revoke_block(out_file, buf, jsb,
503 					  blocknr, blocksize,
504 					  transaction);
505 			blocknr++;
506 			WRAP(jsb, blocknr, maxlen);
507 			continue;
508 
509 		default:
510 			fprintf (out_file, "Unexpected block type %u at "
511 				 "block %u.\n", blocktype, blocknr);
512 			break;
513 		}
514 	}
515 
516 fc:
517 	blocknr = maxlen - jbd2_journal_get_num_fc_blks(jsb) + 1;
518 	while (blocknr <= maxlen) {
519 		retval = read_journal_block(cmdname, source,
520 				((ext2_loff_t) blocknr) * blocksize,
521 				buf, blocksize);
522 		if (retval)
523 			return;
524 
525 		dump_fc_block(out_file, buf, blocksize, transaction, &fc_done,
526 			dump_old);
527 		if (!dump_old && fc_done)
528 			break;
529 		blocknr++;
530 	}
531 }
532 
journal_super_tag_bytes(journal_superblock_t * jsb)533 static inline size_t journal_super_tag_bytes(journal_superblock_t *jsb)
534 {
535 	size_t sz;
536 
537 	if (JSB_HAS_INCOMPAT_FEATURE(jsb, JBD2_FEATURE_INCOMPAT_CSUM_V3))
538 		return sizeof(journal_block_tag3_t);
539 
540 	sz = sizeof(journal_block_tag_t);
541 
542 	if (JSB_HAS_INCOMPAT_FEATURE(jsb, JBD2_FEATURE_INCOMPAT_CSUM_V2))
543 		sz += sizeof(__u16);
544 
545 	if (JSB_HAS_INCOMPAT_FEATURE(jsb, JBD2_FEATURE_INCOMPAT_64BIT))
546 		return sz;
547 
548 	return sz - sizeof(__u32);
549 }
550 
dump_fc_block(FILE * out_file,char * buf,int blocksize,int transaction,int * fc_done,int dump_old)551 static void dump_fc_block(FILE *out_file, char *buf, int blocksize,
552 	int transaction, int *fc_done, int dump_old)
553 {
554 	struct ext4_fc_tl	tl;
555 	struct ext4_fc_head	*head;
556 	struct ext4_fc_add_range	*add_range;
557 	struct ext4_fc_del_range	*del_range;
558 	struct ext4_fc_dentry_info	*dentry_info;
559 	struct ext4_fc_tail		*tail;
560 	struct ext3_extent	*ex;
561 	__u8			*cur, *val;
562 
563 	*fc_done = 0;
564 	for (cur = (__u8 *)buf; cur < (__u8 *)buf + blocksize;
565 	     cur = cur + sizeof(tl) + le16_to_cpu(tl.fc_len)) {
566 		memcpy(&tl, cur, sizeof(tl));
567 		val = cur + sizeof(tl);
568 
569 		switch (le16_to_cpu(tl.fc_tag)) {
570 		case EXT4_FC_TAG_ADD_RANGE:
571 			add_range = (struct ext4_fc_add_range *)val;
572 			ex = (struct ext3_extent *)add_range->fc_ex;
573 			fprintf(out_file,
574 				"tag %s, inode %d, lblk %u, pblk %llu, len %lu\n",
575 				tag2str(tl.fc_tag),
576 				le32_to_cpu(add_range->fc_ino),
577 				le32_to_cpu(ex->ee_block),
578 				le32_to_cpu(ex->ee_start) +
579 				(((unsigned long long) le16_to_cpu(ex->ee_start_hi)) << 32),
580 				le16_to_cpu(ex->ee_len) > EXT_INIT_MAX_LEN ?
581 				le16_to_cpu(ex->ee_len) - EXT_INIT_MAX_LEN :
582 				le16_to_cpu(ex->ee_len));
583 			break;
584 		case EXT4_FC_TAG_DEL_RANGE:
585 			del_range = (struct ext4_fc_del_range *)val;
586 			fprintf(out_file, "tag %s, inode %d, lblk %d, len %d\n",
587 				tag2str(tl.fc_tag),
588 				le32_to_cpu(del_range->fc_ino),
589 				le32_to_cpu(del_range->fc_lblk),
590 				le32_to_cpu(del_range->fc_len));
591 			break;
592 		case EXT4_FC_TAG_LINK:
593 		case EXT4_FC_TAG_UNLINK:
594 		case EXT4_FC_TAG_CREAT:
595 			dentry_info = (struct ext4_fc_dentry_info *)val;
596 			fprintf(out_file,
597 				"tag %s, parent %d, ino %d, name \"%s\"\n",
598 				tag2str(tl.fc_tag),
599 				le32_to_cpu(dentry_info->fc_parent_ino),
600 				le32_to_cpu(dentry_info->fc_ino),
601 				dentry_info->fc_dname);
602 			break;
603 		case EXT4_FC_TAG_INODE:
604 			fprintf(out_file, "tag %s, inode %d\n",
605 				tag2str(tl.fc_tag),
606 				le32_to_cpu(((struct ext4_fc_inode *)val)->fc_ino));
607 			break;
608 		case EXT4_FC_TAG_PAD:
609 			fprintf(out_file, "tag %s\n", tag2str(tl.fc_tag));
610 			break;
611 		case EXT4_FC_TAG_TAIL:
612 			tail = (struct ext4_fc_tail *)val;
613 			fprintf(out_file, "tag %s, tid %d\n",
614 				tag2str(tl.fc_tag),
615 				le32_to_cpu(tail->fc_tid));
616 			if (!dump_old &&
617 				le32_to_cpu(tail->fc_tid) < transaction) {
618 				*fc_done = 1;
619 				return;
620 			}
621 			break;
622 		case EXT4_FC_TAG_HEAD:
623 			fprintf(out_file, "\n*** Fast Commit Area ***\n");
624 			head = (struct ext4_fc_head *)val;
625 			fprintf(out_file, "tag %s, features 0x%x, tid %d\n",
626 				tag2str(tl.fc_tag),
627 				le32_to_cpu(head->fc_features),
628 				le32_to_cpu(head->fc_tid));
629 			if (!dump_old &&
630 				le32_to_cpu(head->fc_tid) < transaction) {
631 				*fc_done = 1;
632 				return;
633 			}
634 			break;
635 		default:
636 			*fc_done = 1;
637 			break;
638 		}
639 	}
640 }
641 
dump_descriptor_block(FILE * out_file,struct journal_source * source,char * buf,journal_superblock_t * jsb,unsigned int * blockp,int blocksize,__u32 maxlen,tid_t transaction)642 static void dump_descriptor_block(FILE *out_file,
643 				  struct journal_source *source,
644 				  char *buf,
645 				  journal_superblock_t *jsb,
646 				  unsigned int *blockp, int blocksize,
647 				  __u32 maxlen,
648 				  tid_t transaction)
649 {
650 	int			offset, tag_size, csum_size = 0;
651 	char			*tagp;
652 	journal_block_tag_t	*tag;
653 	unsigned int		blocknr;
654 	__u32			tag_block;
655 	__u32			tag_flags;
656 
657 	tag_size = journal_super_tag_bytes(jsb);
658 	offset = sizeof(journal_header_t);
659 	blocknr = *blockp;
660 
661 	if (JSB_HAS_INCOMPAT_FEATURE(jsb, JBD2_FEATURE_INCOMPAT_CSUM_V3) ||
662 	    JSB_HAS_INCOMPAT_FEATURE(jsb, JBD2_FEATURE_INCOMPAT_CSUM_V2))
663 		csum_size = sizeof(struct jbd2_journal_block_tail);
664 
665 	if (dump_all)
666 		fprintf(out_file, "Dumping descriptor block, sequence %u, at "
667 			"block %u:\n", transaction, blocknr);
668 
669 	++blocknr;
670 	WRAP(jsb, blocknr, maxlen);
671 
672 	do {
673 		/* Work out the location of the current tag, and skip to
674 		 * the next one... */
675 		tagp = &buf[offset];
676 		tag = (journal_block_tag_t *) tagp;
677 		offset += tag_size;
678 
679 		/* ... and if we have gone too far, then we've reached the
680 		   end of this block. */
681 		if (offset > blocksize - csum_size)
682 			break;
683 
684 		tag_block = be32_to_cpu(tag->t_blocknr);
685 		tag_flags = be16_to_cpu(tag->t_flags);
686 
687 		if (!(tag_flags & JBD2_FLAG_SAME_UUID))
688 			offset += 16;
689 
690 		dump_metadata_block(out_file, source, jsb,
691 				    blocknr, tag_block, tag_flags, blocksize,
692 				    transaction);
693 
694 		++blocknr;
695 		WRAP(jsb, blocknr, maxlen);
696 
697 	} while (!(tag_flags & JBD2_FLAG_LAST_TAG));
698 
699 	*blockp = blocknr;
700 }
701 
702 
dump_revoke_block(FILE * out_file,char * buf,journal_superblock_t * jsb EXT2FS_ATTR ((unused)),unsigned int blocknr,int blocksize EXT2FS_ATTR ((unused)),tid_t transaction)703 static void dump_revoke_block(FILE *out_file, char *buf,
704 			      journal_superblock_t *jsb EXT2FS_ATTR((unused)),
705 			      unsigned int blocknr,
706 			      int blocksize EXT2FS_ATTR((unused)),
707 			      tid_t transaction)
708 {
709 	int			offset, max;
710 	jbd2_journal_revoke_header_t *header;
711 	unsigned long long	rblock;
712 	int			tag_size = sizeof(__u32);
713 
714 	if (dump_all)
715 		fprintf(out_file, "Dumping revoke block, sequence %u, at "
716 			"block %u:\n", transaction, blocknr);
717 
718 	if (be32_to_cpu(jsb->s_feature_incompat) & JBD2_FEATURE_INCOMPAT_64BIT)
719 		tag_size = sizeof(__u64);
720 
721 	header = (jbd2_journal_revoke_header_t *) buf;
722 	offset = sizeof(jbd2_journal_revoke_header_t);
723 	max = be32_to_cpu(header->r_count);
724 	if (max > be32_to_cpu(jsb->s_blocksize)) {
725 		fprintf(out_file, "Revoke block's r_count invalid: %u\b",
726 			max);
727 		max = be32_to_cpu(jsb->s_blocksize);
728 	}
729 
730 	while (offset < max) {
731 		if (tag_size == sizeof(__u32)) {
732 			__u32 *entry = (__u32 *) (buf + offset);
733 			rblock = be32_to_cpu(*entry);
734 		} else {
735 			__u64 *entry = (__u64 *) (buf + offset);
736 			rblock = ext2fs_be64_to_cpu(*entry);
737 		}
738 		if (dump_all || rblock == block_to_dump) {
739 			fprintf(out_file, "  Revoke FS block %llu",
740 				(unsigned long long) rblock);
741 			if (dump_all)
742 				fprintf(out_file, "\n");
743 			else
744 				fprintf(out_file," at block %u, sequence %u\n",
745 					blocknr, transaction);
746 		}
747 		offset += tag_size;
748 	}
749 }
750 
751 
show_extent(FILE * out_file,int start_extent,int end_extent,__u32 first_block)752 static void show_extent(FILE *out_file, int start_extent, int end_extent,
753 			__u32 first_block)
754 {
755 	if (start_extent >= 0 && first_block != 0)
756 		fprintf(out_file, "(%d+%u): %u ",
757 			start_extent, end_extent-start_extent, first_block);
758 }
759 
show_indirect(FILE * out_file,const char * name,__u32 where)760 static void show_indirect(FILE *out_file, const char *name, __u32 where)
761 {
762 	if (where)
763 		fprintf(out_file, "(%s): %u ", name, where);
764 }
765 
766 
dump_metadata_block(FILE * out_file,struct journal_source * source,journal_superblock_t * jsb EXT2FS_ATTR ((unused)),unsigned int log_blocknr,unsigned int fs_blocknr,unsigned int log_tag_flags,int blocksize,tid_t transaction)767 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
768 				journal_superblock_t *jsb EXT2FS_ATTR((unused)),
769 				unsigned int log_blocknr,
770 				unsigned int fs_blocknr,
771 				unsigned int log_tag_flags,
772 				int blocksize,
773 				tid_t transaction)
774 {
775 	int		retval;
776 	char 		buf[EXT2_MAX_BLOCK_SIZE];
777 
778 	if (!(dump_all
779 	      || (fs_blocknr == block_to_dump)
780 	      || (fs_blocknr == inode_block_to_dump)
781 	      || (fs_blocknr == bitmap_to_dump)))
782 		return;
783 
784 	fprintf(out_file, "  FS block %u logged at ", fs_blocknr);
785 	if (!dump_all)
786 		fprintf(out_file, "sequence %u, ", transaction);
787 	fprintf(out_file, "journal block %u (flags 0x%x)\n", log_blocknr,
788 		log_tag_flags);
789 
790 	/* There are two major special cases to parse:
791 	 *
792 	 * If this block is a block
793 	 * bitmap block, we need to give it special treatment so that we
794 	 * can log any allocates and deallocates which affect the
795 	 * block_to_dump query block.
796 	 *
797 	 * If the block is an inode block for the inode being searched
798 	 * for, then we need to dump the contents of that inode
799 	 * structure symbolically.
800 	 */
801 
802 	if (!(dump_contents && dump_all)
803 	    && fs_blocknr != block_to_dump
804 	    && fs_blocknr != bitmap_to_dump
805 	    && fs_blocknr != inode_block_to_dump)
806 		return;
807 
808 	retval = read_journal_block("logdump", source,
809 				    ((ext2_loff_t) log_blocknr) * blocksize,
810 				    buf, blocksize);
811 	if (retval)
812 		return;
813 
814 	if (fs_blocknr == bitmap_to_dump) {
815 		struct ext2_super_block *super;
816 		int offset;
817 
818 		super = current_fs->super;
819 		offset = ((block_to_dump - super->s_first_data_block) %
820 			  super->s_blocks_per_group);
821 
822 		fprintf(out_file, "    (block bitmap for block %llu: "
823 			"block is %s)\n",
824 			(unsigned long long) block_to_dump,
825 			ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
826 	}
827 
828 	if (fs_blocknr == inode_block_to_dump) {
829 		struct ext2_inode *inode;
830 		int first, prev, this, start_extent, i;
831 
832 		fprintf(out_file, "    (inode block for inode %u):\n",
833 			inode_to_dump);
834 
835 		inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
836 		internal_dump_inode(out_file, "    ", inode_to_dump, inode, 0);
837 
838 		/* Dump out the direct/indirect blocks here:
839 		 * internal_dump_inode can only dump them from the main
840 		 * on-disk inode, not from the journaled copy of the
841 		 * inode. */
842 
843 		fprintf (out_file, "    Blocks:  ");
844 		first = prev = start_extent = -1;
845 
846 		for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
847 			this = inode->i_block[i];
848 			if (start_extent >= 0  && this == prev+1) {
849 				prev = this;
850 				continue;
851 			} else {
852 				show_extent(out_file, start_extent, i, first);
853 				start_extent = i;
854 				first = prev = this;
855 			}
856 		}
857 		show_extent(out_file, start_extent, i, first);
858 		show_indirect(out_file, "IND", inode->i_block[i++]);
859 		show_indirect(out_file, "DIND", inode->i_block[i++]);
860 		show_indirect(out_file, "TIND", inode->i_block[i++]);
861 
862 		fprintf(out_file, "\n");
863 	}
864 
865 	if (dump_contents)
866 		do_hexdump(out_file, buf, blocksize);
867 
868 }
869 
do_hexdump(FILE * out_file,char * buf,int blocksize)870 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
871 {
872 	int i,j;
873 	int *intp;
874 	char *charp;
875 	unsigned char c;
876 
877 	intp = (int *) buf;
878 	charp = (char *) buf;
879 
880 	for (i=0; i<blocksize; i+=16) {
881 		fprintf(out_file, "    %04x:  ", i);
882 		for (j=0; j<16; j+=4)
883 			fprintf(out_file, "%08x ", *intp++);
884 		for (j=0; j<16; j++) {
885 			c = *charp++;
886 			if (c < ' ' || c >= 127)
887 				c = '.';
888 			fprintf(out_file, "%c", c);
889 		}
890 		fprintf(out_file, "\n");
891 	}
892 }
893 
894