2 * dumpe2fs.c - List the control structures of a second
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)
9 * Copyright 1995, 1996, 1997 by Theodore Ts'o.
12 * This file may be redistributed under the terms of the GNU Public
20 * 94/02/27 - Ported to use the ext2fs library
36 #include "ext2fs/ext2_fs.h"
38 #include "ext2fs/ext2fs.h"
41 #include <uuid/uuid.h>
43 #include "../version.h"
44 #include "nls-enable.h"
45 #include "plausible.h"
47 #define in_use(m, x) (ext2fs_test_bit ((x), (m)))
49 static const char * program_name = "dumpe2fs";
50 static char * device_name = NULL;
51 static int hex_format = 0;
52 static int blocks64 = 0;
54 static void usage(void)
56 fprintf(stderr, _("Usage: %s [-bfghixV] [-o superblock=<num>] "
57 "[-o blocksize=<num>] device\n"), program_name);
61 static void print_number(unsigned long long num)
65 printf("0x%08llx", num);
67 printf("0x%04llx", num);
72 static void print_range(unsigned long long a, unsigned long long b)
76 printf("0x%08llx-0x%08llx", a, b);
78 printf("0x%04llx-0x%04llx", a, b);
80 printf("%llu-%llu", a, b);
83 static void print_free(unsigned long group, char * bitmap,
84 unsigned long num, unsigned long offset, int ratio)
91 offset += group * num;
92 for (i = 0; i < num; i++)
93 if (!in_use (bitmap, i))
97 print_number((i + offset) * ratio);
98 for (j = i; j < num && !in_use (bitmap, j); j++)
102 print_number((j + offset) * ratio);
109 static void print_bg_opt(int bg_flags, int mask,
110 const char *str, int *first)
112 if (bg_flags & mask) {
121 static void print_bg_opts(ext2_filsys fs, dgrp_t i)
123 int first = 1, bg_flags = 0;
125 if (ext2fs_has_group_desc_csum(fs))
126 bg_flags = ext2fs_bg_flags(fs, i);
128 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
130 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
132 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
139 static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
140 blk64_t first_block, blk64_t last_block)
142 if ((block >= first_block) && (block <= last_block)) {
143 if (itable && block == first_block)
145 printf(" (+%u)", (unsigned)(block - first_block));
146 } else if (fs->super->s_feature_incompat &
147 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
148 dgrp_t flex_grp = ext2fs_group_of_blk2(fs, block);
149 printf(" (bg #%u + %u)", flex_grp,
150 (unsigned)(block-ext2fs_group_first_block2(fs,flex_grp)));
154 static void list_desc(ext2_filsys fs, int grp_only)
157 blk64_t first_block, last_block;
158 blk64_t super_blk, old_desc_blk, new_desc_blk;
159 char *block_bitmap=NULL, *inode_bitmap=NULL;
160 const char *units = _("blocks");
161 int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
162 int block_nbytes, inode_nbytes;
164 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
165 ext2_ino_t ino_itr = 1;
168 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
169 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
170 units = _("clusters");
172 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
173 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
176 block_bitmap = malloc(block_nbytes);
178 inode_bitmap = malloc(inode_nbytes);
180 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
181 EXT2_INODE_SIZE(fs->super)) +
182 EXT2_BLOCK_SIZE(fs->super) - 1) /
183 EXT2_BLOCK_SIZE(fs->super);
184 reserved_gdt = fs->super->s_reserved_gdt_blocks;
186 first_block = fs->super->s_first_data_block;
187 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
188 old_desc_blocks = fs->super->s_first_meta_bg;
190 old_desc_blocks = fs->desc_blocks;
192 printf("group:block:super:gdt:bbitmap:ibitmap:itable\n");
193 for (i = 0; i < fs->group_desc_count; i++) {
194 first_block = ext2fs_group_first_block2(fs, i);
195 last_block = ext2fs_group_last_block2(fs, i);
197 ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
198 &old_desc_blk, &new_desc_blk, 0);
201 printf("%lu:%llu:", i, first_block);
202 if (i == 0 || super_blk)
203 printf("%llu:", super_blk);
207 print_range(old_desc_blk,
208 old_desc_blk + old_desc_blocks - 1);
210 } else if (new_desc_blk)
211 printf("%llu:", new_desc_blk);
214 printf("%llu:%llu:%llu\n",
215 ext2fs_block_bitmap_loc(fs, i),
216 ext2fs_inode_bitmap_loc(fs, i),
217 ext2fs_inode_table_loc(fs, i));
221 printf(_("Group %lu: (Blocks "), i);
222 print_range(first_block, last_block);
224 if (ext2fs_has_group_desc_csum(fs)) {
225 unsigned csum = ext2fs_bg_checksum(fs, i);
226 unsigned exp_csum = ext2fs_group_desc_csum(fs, i);
228 printf(_(" csum 0x%04x"), csum);
229 if (csum != exp_csum)
230 printf(_(" (EXPECTED 0x%04x)"), exp_csum);
232 print_bg_opts(fs, i);
233 has_super = ((i==0) || super_blk);
235 printf (_(" %s superblock at "),
236 i == 0 ? _("Primary") : _("Backup"));
237 print_number(super_blk);
240 printf("%s", _(", Group descriptors at "));
241 print_range(old_desc_blk,
242 old_desc_blk + old_desc_blocks - 1);
244 printf("%s", _("\n Reserved GDT blocks at "));
245 print_range(old_desc_blk + old_desc_blocks,
246 old_desc_blk + old_desc_blocks +
249 } else if (new_desc_blk) {
250 fputc(has_super ? ',' : ' ', stdout);
251 printf("%s", _(" Group descriptor at "));
252 print_number(new_desc_blk);
257 fputs(_(" Block bitmap at "), stdout);
258 print_number(ext2fs_block_bitmap_loc(fs, i));
259 print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
260 first_block, last_block);
261 if (fs->super->s_feature_ro_compat &
262 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
263 printf(_(", csum 0x%08x"),
264 ext2fs_block_bitmap_checksum(fs, i));
265 if (getenv("DUMPE2FS_IGNORE_80COL"))
266 fputs(_(","), stdout);
268 fputs(_("\n "), stdout);
269 fputs(_(" Inode bitmap at "), stdout);
270 print_number(ext2fs_inode_bitmap_loc(fs, i));
271 print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
272 first_block, last_block);
273 if (fs->super->s_feature_ro_compat &
274 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
275 printf(_(", csum 0x%08x"),
276 ext2fs_inode_bitmap_checksum(fs, i));
277 fputs(_("\n Inode table at "), stdout);
278 print_range(ext2fs_inode_table_loc(fs, i),
279 ext2fs_inode_table_loc(fs, i) +
280 inode_blocks_per_group - 1);
281 print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
282 first_block, last_block);
283 printf (_("\n %u free %s, %u free inodes, "
285 ext2fs_bg_free_blocks_count(fs, i), units,
286 ext2fs_bg_free_inodes_count(fs, i),
287 ext2fs_bg_used_dirs_count(fs, i),
288 ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
289 if (ext2fs_bg_itable_unused(fs, i))
290 printf (_(", %u unused inodes\n"),
291 ext2fs_bg_itable_unused(fs, i));
293 fputs(_(" Free blocks: "), stdout);
294 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
295 blk_itr, block_nbytes << 3, block_bitmap);
297 com_err("list_desc", retval,
298 "while reading block bitmap");
300 print_free(i, block_bitmap,
301 fs->super->s_clusters_per_group,
302 fs->super->s_first_data_block,
303 EXT2FS_CLUSTER_RATIO(fs));
305 blk_itr += fs->super->s_clusters_per_group;
308 fputs(_(" Free inodes: "), stdout);
309 retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
310 ino_itr, inode_nbytes << 3, inode_bitmap);
312 com_err("list_desc", retval,
313 "while reading inode bitmap");
315 print_free(i, inode_bitmap,
316 fs->super->s_inodes_per_group,
319 ino_itr += fs->super->s_inodes_per_group;
328 static void list_bad_blocks(ext2_filsys fs, int dump)
330 badblocks_list bb_list = 0;
331 badblocks_iterate bb_iter;
334 const char *header, *fmt;
336 retval = ext2fs_read_bb_inode(fs, &bb_list);
338 com_err("ext2fs_read_bb_inode", retval, 0);
341 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
343 com_err("ext2fs_badblocks_list_iterate_begin", retval,
344 "%s", _("while printing bad block list"));
348 header = fmt = "%u\n";
350 header = _("Bad blocks: %u");
353 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
354 printf(header ? header : fmt, blk);
357 ext2fs_badblocks_list_iterate_end(bb_iter);
360 ext2fs_badblocks_list_free(bb_list);
363 static const char *journal_checksum_type_str(__u8 type)
366 case JBD2_CRC32C_CHKSUM:
373 static void print_inline_journal_information(ext2_filsys fs)
375 journal_superblock_t *jsb;
376 struct ext2_inode inode;
377 ext2_file_t journal_file;
379 ino_t ino = fs->super->s_journal_inum;
381 __u32 *mask_ptr, mask, m;
382 int i, j, size, printed = 0;
384 if (fs->flags & EXT2_FLAG_IMAGE_FILE)
386 retval = ext2fs_read_inode(fs, ino, &inode);
388 com_err(program_name, retval, "%s",
389 _("while reading journal inode"));
392 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
394 com_err(program_name, retval, "%s",
395 _("while opening journal inode"));
398 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
400 com_err(program_name, retval, "%s",
401 _("while reading journal super block"));
404 ext2fs_file_close(journal_file);
405 jsb = (journal_superblock_t *) buf;
406 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
407 fprintf(stderr, "%s",
408 _("Journal superblock magic number invalid!\n"));
411 printf("%s", _("Journal features: "));
412 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
413 mask = be32_to_cpu(*mask_ptr);
414 for (j=0,m=1; j < 32; j++, m<<=1) {
416 printf(" %s", e2p_jrnl_feature2string(i, m));
424 fputs(_("Journal size: "), stdout);
425 if ((fs->super->s_feature_ro_compat &
426 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
427 (inode.i_flags & EXT4_HUGE_FILE_FL))
428 size = inode.i_blocks / (fs->blocksize / 1024);
430 size = inode.i_blocks >> 1;
432 printf("%uk\n", size);
434 printf("%uM\n", size >> 10);
435 printf(_("Journal length: %u\n"
436 "Journal sequence: 0x%08x\n"
437 "Journal start: %u\n"),
438 (unsigned int)ntohl(jsb->s_maxlen),
439 (unsigned int)ntohl(jsb->s_sequence),
440 (unsigned int)ntohl(jsb->s_start));
441 if (jsb->s_feature_compat &
442 ext2fs_cpu_to_be32(JFS_FEATURE_COMPAT_CHECKSUM))
443 printf("%s", _("Journal checksum type: crc32\n"));
444 if ((jsb->s_feature_incompat &
445 ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V3)) ||
446 (jsb->s_feature_incompat &
447 ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V2)))
448 printf(_("Journal checksum type: %s\n"
449 "Journal checksum: 0x%08x\n"),
450 journal_checksum_type_str(jsb->s_checksum_type),
451 ext2fs_be32_to_cpu(jsb->s_checksum));
452 if (jsb->s_errno != 0)
453 printf(_("Journal errno: %d\n"),
454 (int) ntohl(jsb->s_errno));
457 static void print_journal_information(ext2_filsys fs)
462 unsigned int i, j, printed = 0;
463 journal_superblock_t *jsb;
464 __u32 *mask_ptr, mask, m;
466 /* Get the journal superblock */
467 if ((retval = io_channel_read_blk64(fs->io,
468 fs->super->s_first_data_block + 1,
470 com_err(program_name, retval, "%s",
471 _("while reading journal superblock"));
474 jsb = (journal_superblock_t *) buf;
475 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
476 (jsb->s_header.h_blocktype !=
477 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
478 com_err(program_name, 0, "%s",
479 _("Couldn't find journal superblock magic numbers"));
483 if (jsb->s_feature_compat &
484 ext2fs_cpu_to_be32(JFS_FEATURE_COMPAT_CHECKSUM))
485 printf("%s", _("Journal checksum type: crc32\n"));
486 if ((jsb->s_feature_incompat &
487 ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V3)) ||
488 (jsb->s_feature_incompat &
489 ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V2)))
490 printf(_("Journal checksum type: %s\n"
491 "Journal checksum: 0x%08x\n"),
492 journal_checksum_type_str(jsb->s_checksum_type),
493 ext2fs_be32_to_cpu(jsb->s_checksum));
495 printf("%s", _("Journal features: "));
496 for (i = 0, mask_ptr = &jsb->s_feature_compat; i < 3; i++, mask_ptr++) {
497 mask = be32_to_cpu(*mask_ptr);
498 for (j = 0, m = 1; j < 32; j++, m <<= 1) {
500 printf(" %s", e2p_jrnl_feature2string(i, m));
506 printf(_("\nJournal block size: %u\n"
507 "Journal length: %u\n"
508 "Journal first block: %u\n"
509 "Journal sequence: 0x%08x\n"
510 "Journal start: %u\n"
511 "Journal number of users: %u\n"),
512 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
513 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
514 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
516 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
517 uuid_unparse(&jsb->s_users[i*16], str);
519 : _("Journal users: %s\n"),
524 static void parse_extended_opts(const char *opts, blk64_t *superblock,
527 char *buf, *token, *next, *p, *arg, *badopt = 0;
534 fprintf(stderr, "%s",
535 _("Couldn't allocate memory to parse options!\n"));
539 for (token = buf; token && *token; token = next) {
540 p = strchr(token, ',');
546 arg = strchr(token, '=');
551 if (strcmp(token, "superblock") == 0 ||
552 strcmp(token, "sb") == 0) {
558 *superblock = strtoul(arg, &p, 0);
561 _("Invalid superblock parameter: %s\n"),
566 } else if (strcmp(token, "blocksize") == 0 ||
567 strcmp(token, "bs") == 0) {
573 *blocksize = strtoul(arg, &p, 0);
576 _("Invalid blocksize parameter: %s\n"),
587 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
588 "Extended options are separated by commas, "
589 "and may take an argument which\n"
590 "\tis set off by an equals ('=') sign.\n\n"
591 "Valid extended options are:\n"
592 "\tsuperblock=<superblock number>\n"
593 "\tblocksize=<blocksize>\n"),
594 badopt ? badopt : "");
601 int main (int argc, char ** argv)
605 int print_badblocks = 0;
606 blk64_t use_superblock = 0;
607 int use_blocksize = 0;
616 setlocale(LC_MESSAGES, "");
617 setlocale(LC_CTYPE, "");
618 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
619 textdomain(NLS_CAT_NAME);
620 set_com_err_gettext(gettext);
622 add_error_table(&et_ext2_error_table);
623 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
626 program_name = *argv;
628 while ((c = getopt(argc, argv, "bfghixVo:")) != EOF) {
646 parse_extended_opts(optarg, &use_superblock,
650 /* Print version number and exit */
651 fprintf(stderr, _("\tUsing %s\n"),
652 error_message(EXT2_ET_BASE));
661 if (optind != argc - 1) {
665 device_name = argv[optind++];
666 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
668 flags |= EXT2_FLAG_FORCE;
670 flags |= EXT2_FLAG_IMAGE_FILE;
672 if (use_superblock && !use_blocksize) {
673 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
674 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
675 use_blocksize *= 2) {
676 retval = ext2fs_open (device_name, flags,
678 use_blocksize, unix_io_manager,
684 retval = ext2fs_open (device_name, flags, use_superblock,
685 use_blocksize, unix_io_manager, &fs);
686 if (retval && !(flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
687 flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
690 if (!retval && (fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS))
691 printf("%s", _("\n*** Checksum errors detected in filesystem! Run e2fsck now!\n\n"));
692 flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
694 com_err (program_name, retval, _("while trying to open %s"),
696 printf("%s", _("Couldn't find valid filesystem superblock.\n"));
697 if (retval == EXT2_ET_BAD_MAGIC)
698 check_plausibility(device_name, CHECK_FS_EXIST, NULL);
701 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
702 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
704 if (print_badblocks) {
705 list_bad_blocks(fs, 1);
708 goto just_descriptors;
709 list_super (fs->super);
710 if (fs->super->s_feature_incompat &
711 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
712 print_journal_information(fs);
713 ext2fs_close_free(&fs);
716 if ((fs->super->s_feature_compat &
717 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
718 (fs->super->s_journal_inum != 0))
719 print_inline_journal_information(fs);
720 list_bad_blocks(fs, 0);
722 ext2fs_close_free(&fs);
725 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
727 retval = ext2fs_read_bitmaps (fs);
728 if (retval && !(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
729 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
730 goto try_bitmaps_again;
732 if (!retval && (fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS))
733 printf("%s", _("\n*** Checksum errors detected in bitmaps! Run e2fsck now!\n\n"));
735 list_desc(fs, grp_only);
737 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
738 program_name, device_name,
739 error_message(retval));
742 ext2fs_close_free(&fs);
743 remove_error_table(&et_ext2_error_table);