• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * dumpe2fs.c		- List the control structures of a second
3  *			  extended filesystem
4  *
5  * Copyright (C) 1992, 1993, 1994  Remy Card <card@masi.ibp.fr>
6  *                                 Laboratoire MASI, Institut Blaise Pascal
7  *                                 Universite Pierre et Marie Curie (Paris VI)
8  *
9  * Copyright 1995, 1996, 1997 by Theodore Ts'o.
10  *
11  * %Begin-Header%
12  * This file may be redistributed under the terms of the GNU Public
13  * License.
14  * %End-Header%
15  */
16 
17 /*
18  * History:
19  * 94/01/09	- Creation
20  * 94/02/27	- Ported to use the ext2fs library
21  */
22 
23 #include "config.h"
24 #ifdef HAVE_GETOPT_H
25 #include <getopt.h>
26 #else
27 extern char *optarg;
28 extern int optind;
29 #endif
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include "ext2fs/ext2_fs.h"
37 
38 #include "ext2fs/ext2fs.h"
39 #include "e2p/e2p.h"
40 #include "ext2fs/kernel-jbd.h"
41 #include <uuid/uuid.h>
42 
43 #include "support/nls-enable.h"
44 #include "support/plausible.h"
45 #include "../version.h"
46 
47 #define in_use(m, x)	(ext2fs_test_bit ((x), (m)))
48 
49 static const char * program_name = "dumpe2fs";
50 static char * device_name = NULL;
51 static int hex_format = 0;
52 static int blocks64 = 0;
53 
usage(void)54 static void usage(void)
55 {
56 	fprintf(stderr, _("Usage: %s [-bfghimxV] [-o superblock=<num>] "
57 		 "[-o blocksize=<num>] device\n"), program_name);
58 	exit(1);
59 }
60 
print_number(unsigned long long num)61 static void print_number(unsigned long long num)
62 {
63 	if (hex_format) {
64 		if (blocks64)
65 			printf("0x%08llx", num);
66 		else
67 			printf("0x%04llx", num);
68 	} else
69 		printf("%llu", num);
70 }
71 
print_range(unsigned long long a,unsigned long long b)72 static void print_range(unsigned long long a, unsigned long long b)
73 {
74 	if (hex_format) {
75 		if (blocks64)
76 			printf("0x%08llx-0x%08llx", a, b);
77 		else
78 			printf("0x%04llx-0x%04llx", a, b);
79 	} else
80 		printf("%llu-%llu", a, b);
81 }
82 
print_free(unsigned long group,char * bitmap,unsigned long num,unsigned long offset,int ratio)83 static void print_free(unsigned long group, char * bitmap,
84 		       unsigned long num, unsigned long offset, int ratio)
85 {
86 	int p = 0;
87 	unsigned long i;
88 	unsigned long j;
89 
90 	offset /= ratio;
91 	offset += group * num;
92 	for (i = 0; i < num; i++)
93 		if (!in_use (bitmap, i))
94 		{
95 			if (p)
96 				printf (", ");
97 			print_number((i + offset) * ratio);
98 			for (j = i; j < num && !in_use (bitmap, j); j++)
99 				;
100 			if (--j != i) {
101 				fputc('-', stdout);
102 				print_number((j + offset) * ratio);
103 				i = j;
104 			}
105 			p = 1;
106 		}
107 }
108 
print_bg_opt(int bg_flags,int mask,const char * str,int * first)109 static void print_bg_opt(int bg_flags, int mask,
110 			  const char *str, int *first)
111 {
112 	if (bg_flags & mask) {
113 		if (*first) {
114 			fputs(" [", stdout);
115 			*first = 0;
116 		} else
117 			fputs(", ", stdout);
118 		fputs(str, stdout);
119 	}
120 }
print_bg_opts(ext2_filsys fs,dgrp_t i)121 static void print_bg_opts(ext2_filsys fs, dgrp_t i)
122 {
123 	int first = 1, bg_flags = 0;
124 
125 	if (ext2fs_has_group_desc_csum(fs))
126 		bg_flags = ext2fs_bg_flags(fs, i);
127 
128 	print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
129  		     &first);
130 	print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
131  		     &first);
132 	print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
133  		     &first);
134 	if (!first)
135 		fputc(']', stdout);
136 	fputc('\n', stdout);
137 }
138 
print_bg_rel_offset(ext2_filsys fs,blk64_t block,int itable,blk64_t first_block,blk64_t last_block)139 static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
140 				blk64_t first_block, blk64_t last_block)
141 {
142 	if ((block >= first_block) && (block <= last_block)) {
143 		if (itable && block == first_block)
144 			return;
145 		printf(" (+%u)", (unsigned)(block - first_block));
146 	} else if (ext2fs_has_feature_flex_bg(fs->super)) {
147 		dgrp_t flex_grp = ext2fs_group_of_blk2(fs, block);
148 		printf(" (bg #%u + %u)", flex_grp,
149 		       (unsigned)(block-ext2fs_group_first_block2(fs,flex_grp)));
150 	}
151 }
152 
list_desc(ext2_filsys fs,int grp_only)153 static void list_desc(ext2_filsys fs, int grp_only)
154 {
155 	unsigned long i;
156 	blk64_t	first_block, last_block;
157 	blk64_t	super_blk, old_desc_blk, new_desc_blk;
158 	char *block_bitmap=NULL, *inode_bitmap=NULL;
159 	const char *units = _("blocks");
160 	int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
161 	int		block_nbytes, inode_nbytes;
162 	int has_super;
163 	blk64_t		blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
164 	ext2_ino_t	ino_itr = 1;
165 	errcode_t	retval;
166 
167 	if (ext2fs_has_feature_bigalloc(fs->super))
168 		units = _("clusters");
169 
170 	block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
171 	inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
172 
173 	if (fs->block_map)
174 		block_bitmap = malloc(block_nbytes);
175 	if (fs->inode_map)
176 		inode_bitmap = malloc(inode_nbytes);
177 
178 	inode_blocks_per_group = ((fs->super->s_inodes_per_group *
179 				   EXT2_INODE_SIZE(fs->super)) +
180 				  EXT2_BLOCK_SIZE(fs->super) - 1) /
181 				 EXT2_BLOCK_SIZE(fs->super);
182 	reserved_gdt = fs->super->s_reserved_gdt_blocks;
183 	fputc('\n', stdout);
184 	first_block = fs->super->s_first_data_block;
185 	if (ext2fs_has_feature_meta_bg(fs->super))
186 		old_desc_blocks = fs->super->s_first_meta_bg;
187 	else
188 		old_desc_blocks = fs->desc_blocks;
189 	if (grp_only)
190 		printf("group:block:super:gdt:bbitmap:ibitmap:itable\n");
191 	for (i = 0; i < fs->group_desc_count; i++) {
192 		first_block = ext2fs_group_first_block2(fs, i);
193 		last_block = ext2fs_group_last_block2(fs, i);
194 
195 		ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
196 					  &old_desc_blk, &new_desc_blk, 0);
197 
198 		if (grp_only) {
199 			printf("%lu:%llu:", i, (unsigned long long) first_block);
200 			if (i == 0 || super_blk)
201 				printf("%llu:", (unsigned long long) super_blk);
202 			else
203 				printf("-1:");
204 			if (old_desc_blk) {
205 				print_range(old_desc_blk,
206 					    old_desc_blk + old_desc_blocks - 1);
207 				printf(":");
208 			} else if (new_desc_blk)
209 				printf("%llu:", (unsigned long long) new_desc_blk);
210 			else
211 				printf("-1:");
212 			printf("%llu:%llu:%llu\n",
213 			       (unsigned long long) ext2fs_block_bitmap_loc(fs, i),
214 			       (unsigned long long) ext2fs_inode_bitmap_loc(fs, i),
215 			       (unsigned long long) ext2fs_inode_table_loc(fs, i));
216 			continue;
217 		}
218 
219 		printf(_("Group %lu: (Blocks "), i);
220 		print_range(first_block, last_block);
221 		fputs(")", stdout);
222 		if (ext2fs_has_group_desc_csum(fs)) {
223 			unsigned csum = ext2fs_bg_checksum(fs, i);
224 			unsigned exp_csum = ext2fs_group_desc_csum(fs, i);
225 
226 			printf(_(" csum 0x%04x"), csum);
227 			if (csum != exp_csum)
228 				printf(_(" (EXPECTED 0x%04x)"), exp_csum);
229 		}
230 		print_bg_opts(fs, i);
231 		has_super = ((i==0) || super_blk);
232 		if (has_super) {
233 			printf (_("  %s superblock at "),
234 				i == 0 ? _("Primary") : _("Backup"));
235 			print_number(super_blk);
236 		}
237 		if (old_desc_blk) {
238 			printf("%s", _(", Group descriptors at "));
239 			print_range(old_desc_blk,
240 				    old_desc_blk + old_desc_blocks - 1);
241 			if (reserved_gdt) {
242 				printf("%s", _("\n  Reserved GDT blocks at "));
243 				print_range(old_desc_blk + old_desc_blocks,
244 					    old_desc_blk + old_desc_blocks +
245 					    reserved_gdt - 1);
246 			}
247 		} else if (new_desc_blk) {
248 			fputc(has_super ? ',' : ' ', stdout);
249 			printf("%s", _(" Group descriptor at "));
250 			print_number(new_desc_blk);
251 			has_super++;
252 		}
253 		if (has_super)
254 			fputc('\n', stdout);
255 		fputs(_("  Block bitmap at "), stdout);
256 		print_number(ext2fs_block_bitmap_loc(fs, i));
257 		print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
258 				    first_block, last_block);
259 		if (ext2fs_has_feature_metadata_csum(fs->super))
260 			printf(_(", csum 0x%08x"),
261 			       ext2fs_block_bitmap_checksum(fs, i));
262 		if (getenv("DUMPE2FS_IGNORE_80COL"))
263 			fputs(_(","), stdout);
264 		else
265 			fputs(_("\n "), stdout);
266 		fputs(_(" Inode bitmap at "), stdout);
267 		print_number(ext2fs_inode_bitmap_loc(fs, i));
268 		print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
269 				    first_block, last_block);
270 		if (ext2fs_has_feature_metadata_csum(fs->super))
271 			printf(_(", csum 0x%08x"),
272 			       ext2fs_inode_bitmap_checksum(fs, i));
273 		fputs(_("\n  Inode table at "), stdout);
274 		print_range(ext2fs_inode_table_loc(fs, i),
275 			    ext2fs_inode_table_loc(fs, i) +
276 			    inode_blocks_per_group - 1);
277 		print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
278 				    first_block, last_block);
279 		printf (_("\n  %u free %s, %u free inodes, "
280 			  "%u directories%s"),
281 			ext2fs_bg_free_blocks_count(fs, i), units,
282 			ext2fs_bg_free_inodes_count(fs, i),
283 			ext2fs_bg_used_dirs_count(fs, i),
284 			ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
285 		if (ext2fs_bg_itable_unused(fs, i))
286 			printf (_(", %u unused inodes\n"),
287 				ext2fs_bg_itable_unused(fs, i));
288 		if (block_bitmap) {
289 			fputs(_("  Free blocks: "), stdout);
290 			retval = ext2fs_get_block_bitmap_range2(fs->block_map,
291 				 blk_itr, block_nbytes << 3, block_bitmap);
292 			if (retval)
293 				com_err("list_desc", retval,
294 					"while reading block bitmap");
295 			else
296 				print_free(i, block_bitmap,
297 					   fs->super->s_clusters_per_group,
298 					   fs->super->s_first_data_block,
299 					   EXT2FS_CLUSTER_RATIO(fs));
300 			fputc('\n', stdout);
301 			blk_itr += fs->super->s_clusters_per_group;
302 		}
303 		if (inode_bitmap) {
304 			fputs(_("  Free inodes: "), stdout);
305 			retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
306 				 ino_itr, inode_nbytes << 3, inode_bitmap);
307 			if (retval)
308 				com_err("list_desc", retval,
309 					"while reading inode bitmap");
310 			else
311 				print_free(i, inode_bitmap,
312 					   fs->super->s_inodes_per_group,
313 					   1, 1);
314 			fputc('\n', stdout);
315 			ino_itr += fs->super->s_inodes_per_group;
316 		}
317 	}
318 	if (block_bitmap)
319 		free(block_bitmap);
320 	if (inode_bitmap)
321 		free(inode_bitmap);
322 }
323 
list_bad_blocks(ext2_filsys fs,int dump)324 static void list_bad_blocks(ext2_filsys fs, int dump)
325 {
326 	badblocks_list		bb_list = 0;
327 	badblocks_iterate	bb_iter;
328 	blk_t			blk;
329 	errcode_t		retval;
330 	const char		*header, *fmt;
331 
332 	retval = ext2fs_read_bb_inode(fs, &bb_list);
333 	if (retval) {
334 		com_err("ext2fs_read_bb_inode", retval, 0);
335 		return;
336 	}
337 	retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
338 	if (retval) {
339 		com_err("ext2fs_badblocks_list_iterate_begin", retval,
340 			"%s", _("while printing bad block list"));
341 		return;
342 	}
343 	if (dump) {
344 		header = fmt = "%u\n";
345 	} else {
346 		header =  _("Bad blocks: %u");
347 		fmt = ", %u";
348 	}
349 	while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
350 		printf(header ? header : fmt, blk);
351 		header = 0;
352 	}
353 	ext2fs_badblocks_list_iterate_end(bb_iter);
354 	if (!dump)
355 		fputc('\n', stdout);
356 	ext2fs_badblocks_list_free(bb_list);
357 }
358 
print_inline_journal_information(ext2_filsys fs)359 static void print_inline_journal_information(ext2_filsys fs)
360 {
361 	journal_superblock_t	*jsb;
362 	struct ext2_inode	inode;
363 	ext2_file_t		journal_file;
364 	errcode_t		retval;
365 	ext2_ino_t		ino = fs->super->s_journal_inum;
366 	char			buf[1024];
367 	int			flags;
368 
369 	if (fs->flags & EXT2_FLAG_IMAGE_FILE)
370 		return;
371 	retval = ext2fs_read_inode(fs, ino,  &inode);
372 	if (retval) {
373 		com_err(program_name, retval, "%s",
374 			_("while reading journal inode"));
375 		exit(1);
376 	}
377 	retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
378 	if (retval) {
379 		com_err(program_name, retval, "%s",
380 			_("while opening journal inode"));
381 		exit(1);
382 	}
383 	retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
384 	if (retval) {
385 		com_err(program_name, retval, "%s",
386 			_("while reading journal super block"));
387 		exit(1);
388 	}
389 	ext2fs_file_close(journal_file);
390 	jsb = (journal_superblock_t *) buf;
391 	if (be32_to_cpu(jsb->s_header.h_magic) != JBD2_MAGIC_NUMBER) {
392 		fprintf(stderr, "%s",
393 			_("Journal superblock magic number invalid!\n"));
394 		exit(1);
395 	}
396 	flags = ext2fs_has_feature_fast_commit(fs->super) ?
397 			E2P_LIST_JOURNAL_FLAG_FC : 0;
398 	e2p_list_journal_super(stdout, buf, fs->blocksize, flags);
399 }
400 
print_journal_information(ext2_filsys fs)401 static void print_journal_information(ext2_filsys fs)
402 {
403 	errcode_t	retval;
404 	char		buf[1024];
405 	journal_superblock_t	*jsb;
406 	int		flags;
407 
408 	/* Get the journal superblock */
409 	if ((retval = io_channel_read_blk64(fs->io,
410 					    fs->super->s_first_data_block + 1,
411 					    -1024, buf))) {
412 		com_err(program_name, retval, "%s",
413 			_("while reading journal superblock"));
414 		exit(1);
415 	}
416 	jsb = (journal_superblock_t *) buf;
417 	if ((jsb->s_header.h_magic != (unsigned) ntohl(JBD2_MAGIC_NUMBER)) ||
418 	    (jsb->s_header.h_blocktype !=
419 	     (unsigned) ntohl(JBD2_SUPERBLOCK_V2))) {
420 		com_err(program_name, 0, "%s",
421 			_("Couldn't find journal superblock magic numbers"));
422 		exit(1);
423 	}
424 	flags = ext2fs_has_feature_fast_commit(fs->super) ?
425 			E2P_LIST_JOURNAL_FLAG_FC : 0;
426 	e2p_list_journal_super(stdout, buf, fs->blocksize, flags);
427 }
428 
check_mmp(ext2_filsys fs)429 static int check_mmp(ext2_filsys fs)
430 {
431 	int retval;
432 
433 	/* This won't actually start MMP on the filesystem, since fs is opened
434 	 * readonly, but it will do the proper activity checking for us. */
435 	retval = ext2fs_mmp_start(fs);
436 	if (retval) {
437 		com_err(program_name, retval, _("while trying to open %s"),
438 			fs->device_name);
439 		if (retval == EXT2_ET_MMP_FAILED ||
440 		    retval == EXT2_ET_MMP_FSCK_ON ||
441 		    retval == EXT2_ET_MMP_CSUM_INVALID ||
442 		    retval == EXT2_ET_MMP_UNKNOWN_SEQ) {
443 			if (fs->mmp_buf) {
444 				struct mmp_struct *mmp = fs->mmp_buf;
445 				time_t mmp_time = mmp->mmp_time;
446 
447 				fprintf(stderr,
448 					"%s: MMP update by '%.*s%.*s' at %s",
449 					program_name,
450 					EXT2_LEN_STR(mmp->mmp_nodename),
451 					EXT2_LEN_STR(mmp->mmp_bdevname),
452 					ctime(&mmp_time));
453 			}
454 			retval = 1;
455 		} else {
456 			retval = 2;
457 		}
458 	} else {
459 		printf("%s: it is safe to mount '%s', MMP is clean\n",
460 		       program_name, fs->device_name);
461 	}
462 
463 	return retval;
464 }
465 
print_mmp_block(ext2_filsys fs)466 static void print_mmp_block(ext2_filsys fs)
467 {
468 	struct mmp_struct *mmp;
469 	time_t mmp_time;
470 	errcode_t retval;
471 
472 	if (fs->mmp_buf == NULL) {
473 		retval = ext2fs_get_mem(fs->blocksize, &fs->mmp_buf);
474 		if (retval) {
475 			com_err(program_name, retval,
476 				_("failed to alloc MMP buffer\n"));
477 			return;
478 		}
479 	}
480 
481 	retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, fs->mmp_buf);
482 	/* this is only dumping, not checking status, so OK to skip this */
483 	if (retval == EXT2_ET_OP_NOT_SUPPORTED)
484 		return;
485 	if (retval) {
486 		com_err(program_name, retval,
487 			_("reading MMP block %llu from '%s'\n"),
488 			(unsigned long long) fs->super->s_mmp_block,
489 			fs->device_name);
490 		return;
491 	}
492 
493 	mmp = fs->mmp_buf;
494 	mmp_time = mmp->mmp_time;
495 	printf("MMP_block:\n");
496 	printf("    mmp_magic: 0x%x\n", mmp->mmp_magic);
497 	printf("    mmp_check_interval: %d\n", mmp->mmp_check_interval);
498 	printf("    mmp_sequence: %#08x\n", mmp->mmp_seq);
499 	printf("    mmp_update_date: %s", ctime(&mmp_time));
500 	printf("    mmp_update_time: %llu\n",
501 	       (unsigned long long) mmp->mmp_time);
502 	printf("    mmp_node_name: %.*s\n",
503 	       EXT2_LEN_STR(mmp->mmp_nodename));
504 	printf("    mmp_device_name: %.*s\n",
505 	       EXT2_LEN_STR(mmp->mmp_bdevname));
506 }
507 
parse_extended_opts(const char * opts,blk64_t * superblock,int * blocksize)508 static void parse_extended_opts(const char *opts, blk64_t *superblock,
509 				int *blocksize)
510 {
511 	char	*buf, *token, *next, *p, *arg, *badopt = 0;
512 	int	len;
513 	int	do_usage = 0;
514 
515 	len = strlen(opts);
516 	buf = malloc(len+1);
517 	if (!buf) {
518 		fprintf(stderr, "%s",
519 			_("Couldn't allocate memory to parse options!\n"));
520 		exit(1);
521 	}
522 	strcpy(buf, opts);
523 	for (token = buf; token && *token; token = next) {
524 		p = strchr(token, ',');
525 		next = 0;
526 		if (p) {
527 			*p = 0;
528 			next = p+1;
529 		}
530 		arg = strchr(token, '=');
531 		if (arg) {
532 			*arg = 0;
533 			arg++;
534 		}
535 		if (strcmp(token, "superblock") == 0 ||
536 		    strcmp(token, "sb") == 0) {
537 			if (!arg) {
538 				do_usage++;
539 				badopt = token;
540 				continue;
541 			}
542 			*superblock = strtoul(arg, &p, 0);
543 			if (*p) {
544 				fprintf(stderr,
545 					_("Invalid superblock parameter: %s\n"),
546 					arg);
547 				do_usage++;
548 				continue;
549 			}
550 		} else if (strcmp(token, "blocksize") == 0 ||
551 			   strcmp(token, "bs") == 0) {
552 			if (!arg) {
553 				do_usage++;
554 				badopt = token;
555 				continue;
556 			}
557 			*blocksize = strtoul(arg, &p, 0);
558 			if (*p) {
559 				fprintf(stderr,
560 					_("Invalid blocksize parameter: %s\n"),
561 					arg);
562 				do_usage++;
563 				continue;
564 			}
565 		} else {
566 			do_usage++;
567 			badopt = token;
568 		}
569 	}
570 	if (do_usage) {
571 		fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
572 			"Extended options are separated by commas, "
573 			"and may take an argument which\n"
574 			"\tis set off by an equals ('=') sign.\n\n"
575 			"Valid extended options are:\n"
576 			"\tsuperblock=<superblock number>\n"
577 			"\tblocksize=<blocksize>\n"),
578 			badopt ? badopt : "");
579 		free(buf);
580 		exit(1);
581 	}
582 	free(buf);
583 }
584 
main(int argc,char ** argv)585 int main (int argc, char ** argv)
586 {
587 	errcode_t	retval;
588 	errcode_t	retval_csum = 0;
589 	const char	*error_csum = NULL;
590 	ext2_filsys	fs;
591 	int		print_badblocks = 0;
592 	blk64_t		use_superblock = 0;
593 	int		use_blocksize = 0;
594 	int		image_dump = 0;
595 	int		mmp_check = 0;
596 	int		mmp_info = 0;
597 	int		force = 0;
598 	int		flags;
599 	int		header_only = 0;
600 	int		c;
601 	int		grp_only = 0;
602 
603 #ifdef ENABLE_NLS
604 	setlocale(LC_MESSAGES, "");
605 	setlocale(LC_CTYPE, "");
606 	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
607 	textdomain(NLS_CAT_NAME);
608 	set_com_err_gettext(gettext);
609 #endif
610 	add_error_table(&et_ext2_error_table);
611 	if (argc && *argv) {
612 		if (strrchr(*argv, '/'))
613 			program_name = strrchr(*argv, '/') + 1;
614 		else
615 			program_name = *argv;
616 
617 		if (strstr(program_name, "mmpstatus") != NULL) {
618 			mmp_check = 1;
619 			header_only = 1;
620 		}
621 	}
622 
623 	if (!mmp_check)
624 		fprintf(stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
625 			E2FSPROGS_DATE);
626 
627 	while ((c = getopt(argc, argv, "bfghimxVo:")) != EOF) {
628 		switch (c) {
629 		case 'b':
630 			print_badblocks++;
631 			break;
632 		case 'f':
633 			force++;
634 			break;
635 		case 'g':
636 			grp_only++;
637 			break;
638 		case 'h':
639 			header_only++;
640 			break;
641 		case 'i':
642 			if (mmp_check)
643 				mmp_info++;
644 			else
645 				image_dump++;
646 			break;
647 		case 'm':
648 			mmp_check++;
649 			header_only++;
650 			if (image_dump) {
651 				mmp_info = image_dump;
652 				image_dump = 0;
653 			}
654 			break;
655 		case 'o':
656 			parse_extended_opts(optarg, &use_superblock,
657 					    &use_blocksize);
658 			break;
659 		case 'V':
660 			/* Print version number and exit */
661 			fprintf(stderr, _("\tUsing %s\n"),
662 				error_message(EXT2_ET_BASE));
663 			exit(0);
664 		case 'x':
665 			hex_format++;
666 			break;
667 		default:
668 			usage();
669 		}
670 	}
671 	if (optind != argc - 1)
672 		usage();
673 
674 	device_name = argv[optind++];
675 	flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES |
676 		EXT2_FLAG_64BITS | EXT2_FLAG_THREADS;
677 	if (force)
678 		flags |= EXT2_FLAG_FORCE;
679 	if (image_dump)
680 		flags |= EXT2_FLAG_IMAGE_FILE;
681 	if (header_only)
682 		flags |= EXT2_FLAG_SUPER_ONLY;
683 try_open_again:
684 	if (use_superblock && !use_blocksize) {
685 		for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
686 		     use_blocksize <= EXT2_MAX_BLOCK_SIZE;
687 		     use_blocksize *= 2) {
688 			retval = ext2fs_open (device_name, flags,
689 					      use_superblock,
690 					      use_blocksize, unix_io_manager,
691 					      &fs);
692 			if (!retval)
693 				break;
694 		}
695 	} else {
696 		retval = ext2fs_open(device_name, flags, use_superblock,
697 				     use_blocksize, unix_io_manager, &fs);
698 	}
699 	flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
700 	if (retval && !retval_csum) {
701 		retval_csum = retval;
702 		error_csum = _("while trying to open %s");
703 		goto try_open_again;
704 	}
705 	if (retval) {
706 		com_err(program_name, retval, _("while trying to open %s"),
707 			device_name);
708 		printf("%s", _("Couldn't find valid filesystem superblock.\n"));
709 		if (retval == EXT2_ET_BAD_MAGIC)
710 			check_plausibility(device_name, CHECK_FS_EXIST, NULL);
711 		goto out;
712 	}
713 	fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
714 	if (ext2fs_has_feature_64bit(fs->super))
715 		blocks64 = 1;
716 	if (mmp_check) {
717 		if (ext2fs_has_feature_mmp(fs->super) &&
718 		    fs->super->s_mmp_block != 0) {
719 			if (mmp_info) {
720 				print_mmp_block(fs);
721 				printf("    mmp_block_number: ");
722 				print_number(fs->super->s_mmp_block);
723 				printf("\n");
724 			} else {
725 				retval = check_mmp(fs);
726 			}
727 			if (!retval && retval_csum)
728 				retval = 2;
729 		} else {
730 			fprintf(stderr, _("%s: MMP feature not enabled.\n"),
731 				program_name);
732 			retval = 2;
733 		}
734 	} else if (print_badblocks) {
735 		list_bad_blocks(fs, 1);
736 	} else {
737 		if (grp_only)
738 			goto just_descriptors;
739 		list_super(fs->super);
740 		if (ext2fs_has_feature_journal_dev(fs->super)) {
741 			print_journal_information(fs);
742 
743 			goto out_close;
744 		}
745 		if (ext2fs_has_feature_journal(fs->super) &&
746 		    (fs->super->s_journal_inum != 0))
747 			print_inline_journal_information(fs);
748 		if (ext2fs_has_feature_mmp(fs->super) &&
749 		    fs->super->s_mmp_block != 0)
750 			print_mmp_block(fs);
751 		list_bad_blocks(fs, 0);
752 		if (header_only)
753 			goto out_close;
754 
755 		fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
756 try_bitmaps_again:
757 		retval = ext2fs_read_bitmaps(fs);
758 		if (retval && !retval_csum) {
759 			fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
760 			retval_csum = retval;
761 			error_csum = _("while trying to read '%s' bitmaps\n");
762 			goto try_bitmaps_again;
763 		}
764 just_descriptors:
765 		list_desc(fs, grp_only);
766 	}
767 out_close:
768 	if (retval_csum) {
769 		com_err(program_name, retval_csum, error_csum, device_name);
770 		printf("%s", _("*** Run e2fsck now!\n\n"));
771 		if (!retval)
772 			retval = retval_csum;
773 	}
774 	ext2fs_close_free(&fs);
775 	remove_error_table(&et_ext2_error_table);
776 out:
777 	return retval;
778 }
779