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 print_bg_opts(fs, i);
225 if (ext2fs_has_group_desc_csum(fs)) {
226 unsigned csum = ext2fs_bg_checksum(fs, i);
227 unsigned exp_csum = ext2fs_group_desc_csum(fs, i);
229 printf(_(" Checksum 0x%04x"), csum);
230 if (csum != exp_csum)
231 printf(_(" (EXPECTED 0x%04x)"), exp_csum);
232 printf(_(", unused inodes %u\n"),
233 ext2fs_bg_itable_unused(fs, i));
235 has_super = ((i==0) || super_blk);
237 printf (_(" %s superblock at "),
238 i == 0 ? _("Primary") : _("Backup"));
239 print_number(super_blk);
242 printf("%s", _(", Group descriptors at "));
243 print_range(old_desc_blk,
244 old_desc_blk + old_desc_blocks - 1);
246 printf("%s", _("\n Reserved GDT blocks at "));
247 print_range(old_desc_blk + old_desc_blocks,
248 old_desc_blk + old_desc_blocks +
251 } else if (new_desc_blk) {
252 fputc(has_super ? ',' : ' ', stdout);
253 printf("%s", _(" Group descriptor at "));
254 print_number(new_desc_blk);
259 fputs(_(" Block bitmap at "), stdout);
260 print_number(ext2fs_block_bitmap_loc(fs, i));
261 print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
262 first_block, last_block);
263 if (fs->super->s_feature_ro_compat &
264 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
265 printf(_(", csum 0x%08x"),
266 ext2fs_block_bitmap_checksum(fs, i));
267 fputs(_(", Inode bitmap at "), stdout);
268 print_number(ext2fs_inode_bitmap_loc(fs, i));
269 print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
270 first_block, last_block);
271 if (fs->super->s_feature_ro_compat &
272 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
273 printf(_(", csum 0x%08x"),
274 ext2fs_inode_bitmap_checksum(fs, i));
275 fputs(_("\n Inode table at "), stdout);
276 print_range(ext2fs_inode_table_loc(fs, i),
277 ext2fs_inode_table_loc(fs, i) +
278 inode_blocks_per_group - 1);
279 print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
280 first_block, last_block);
281 printf (_("\n %u free %s, %u free inodes, "
283 ext2fs_bg_free_blocks_count(fs, i), units,
284 ext2fs_bg_free_inodes_count(fs, i),
285 ext2fs_bg_used_dirs_count(fs, i),
286 ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
287 if (ext2fs_bg_itable_unused(fs, i))
288 printf (_(", %u unused inodes\n"),
289 ext2fs_bg_itable_unused(fs, i));
291 fputs(_(" Free blocks: "), stdout);
292 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
293 blk_itr, block_nbytes << 3, block_bitmap);
295 com_err("list_desc", retval,
296 "while reading block bitmap");
298 print_free(i, block_bitmap,
299 fs->super->s_clusters_per_group,
300 fs->super->s_first_data_block,
301 EXT2FS_CLUSTER_RATIO(fs));
303 blk_itr += fs->super->s_clusters_per_group;
306 fputs(_(" Free inodes: "), stdout);
307 retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
308 ino_itr, inode_nbytes << 3, inode_bitmap);
310 com_err("list_desc", retval,
311 "while reading inode bitmap");
313 print_free(i, inode_bitmap,
314 fs->super->s_inodes_per_group,
317 ino_itr += fs->super->s_inodes_per_group;
326 static void list_bad_blocks(ext2_filsys fs, int dump)
328 badblocks_list bb_list = 0;
329 badblocks_iterate bb_iter;
332 const char *header, *fmt;
334 retval = ext2fs_read_bb_inode(fs, &bb_list);
336 com_err("ext2fs_read_bb_inode", retval, 0);
339 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
341 com_err("ext2fs_badblocks_list_iterate_begin", retval,
342 "%s", _("while printing bad block list"));
346 header = fmt = "%u\n";
348 header = _("Bad blocks: %u");
351 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
352 printf(header ? header : fmt, blk);
355 ext2fs_badblocks_list_iterate_end(bb_iter);
358 ext2fs_badblocks_list_free(bb_list);
361 static const char *journal_checksum_type_str(__u8 type)
364 case JBD2_CRC32C_CHKSUM:
371 static void print_inline_journal_information(ext2_filsys fs)
373 journal_superblock_t *jsb;
374 struct ext2_inode inode;
375 ext2_file_t journal_file;
377 ino_t ino = fs->super->s_journal_inum;
379 __u32 *mask_ptr, mask, m;
380 int i, j, size, printed = 0;
382 if (fs->flags & EXT2_FLAG_IMAGE_FILE)
384 retval = ext2fs_read_inode(fs, ino, &inode);
386 com_err(program_name, retval, "%s",
387 _("while reading journal inode"));
390 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
392 com_err(program_name, retval, "%s",
393 _("while opening journal inode"));
396 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
398 com_err(program_name, retval, "%s",
399 _("while reading journal super block"));
402 ext2fs_file_close(journal_file);
403 jsb = (journal_superblock_t *) buf;
404 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
405 fprintf(stderr, "%s",
406 _("Journal superblock magic number invalid!\n"));
409 printf("%s", _("Journal features: "));
410 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
411 mask = be32_to_cpu(*mask_ptr);
412 for (j=0,m=1; j < 32; j++, m<<=1) {
414 printf(" %s", e2p_jrnl_feature2string(i, m));
422 fputs(_("Journal size: "), stdout);
423 if ((fs->super->s_feature_ro_compat &
424 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
425 (inode.i_flags & EXT4_HUGE_FILE_FL))
426 size = inode.i_blocks / (fs->blocksize / 1024);
428 size = inode.i_blocks >> 1;
430 printf("%uk\n", size);
432 printf("%uM\n", size >> 10);
433 printf(_("Journal length: %u\n"
434 "Journal sequence: 0x%08x\n"
435 "Journal start: %u\n"),
436 (unsigned int)ntohl(jsb->s_maxlen),
437 (unsigned int)ntohl(jsb->s_sequence),
438 (unsigned int)ntohl(jsb->s_start));
439 if (jsb->s_feature_compat &
440 ext2fs_cpu_to_be32(JFS_FEATURE_COMPAT_CHECKSUM))
441 printf("%s", _("Journal checksum type: crc32\n"));
442 if ((jsb->s_feature_incompat &
443 ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V3)) ||
444 (jsb->s_feature_incompat &
445 ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V2)))
446 printf(_("Journal checksum type: %s\n"
447 "Journal checksum: 0x%08x\n"),
448 journal_checksum_type_str(jsb->s_checksum_type),
449 ext2fs_be32_to_cpu(jsb->s_checksum));
450 if (jsb->s_errno != 0)
451 printf(_("Journal errno: %d\n"),
452 (int) ntohl(jsb->s_errno));
455 static void print_journal_information(ext2_filsys fs)
460 unsigned int i, j, printed = 0;
461 journal_superblock_t *jsb;
462 __u32 *mask_ptr, mask, m;
464 /* Get the journal superblock */
465 if ((retval = io_channel_read_blk64(fs->io,
466 fs->super->s_first_data_block + 1,
468 com_err(program_name, retval, "%s",
469 _("while reading journal superblock"));
472 jsb = (journal_superblock_t *) buf;
473 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
474 (jsb->s_header.h_blocktype !=
475 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
476 com_err(program_name, 0, "%s",
477 _("Couldn't find journal superblock magic numbers"));
481 if (jsb->s_feature_compat &
482 ext2fs_cpu_to_be32(JFS_FEATURE_COMPAT_CHECKSUM))
483 printf("%s", _("Journal checksum type: crc32\n"));
484 if ((jsb->s_feature_incompat &
485 ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V3)) ||
486 (jsb->s_feature_incompat &
487 ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V2)))
488 printf(_("Journal checksum type: %s\n"
489 "Journal checksum: 0x%08x\n"),
490 journal_checksum_type_str(jsb->s_checksum_type),
491 ext2fs_be32_to_cpu(jsb->s_checksum));
493 printf("%s", _("Journal features: "));
494 for (i = 0, mask_ptr = &jsb->s_feature_compat; i < 3; i++, mask_ptr++) {
495 mask = be32_to_cpu(*mask_ptr);
496 for (j = 0, m = 1; j < 32; j++, m <<= 1) {
498 printf(" %s", e2p_jrnl_feature2string(i, m));
504 printf(_("\nJournal block size: %u\n"
505 "Journal length: %u\n"
506 "Journal first block: %u\n"
507 "Journal sequence: 0x%08x\n"
508 "Journal start: %u\n"
509 "Journal number of users: %u\n"),
510 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
511 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
512 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
514 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
515 uuid_unparse(&jsb->s_users[i*16], str);
517 : _("Journal users: %s\n"),
522 static void parse_extended_opts(const char *opts, blk64_t *superblock,
525 char *buf, *token, *next, *p, *arg, *badopt = 0;
532 fprintf(stderr, "%s",
533 _("Couldn't allocate memory to parse options!\n"));
537 for (token = buf; token && *token; token = next) {
538 p = strchr(token, ',');
544 arg = strchr(token, '=');
549 if (strcmp(token, "superblock") == 0 ||
550 strcmp(token, "sb") == 0) {
556 *superblock = strtoul(arg, &p, 0);
559 _("Invalid superblock parameter: %s\n"),
564 } else if (strcmp(token, "blocksize") == 0 ||
565 strcmp(token, "bs") == 0) {
571 *blocksize = strtoul(arg, &p, 0);
574 _("Invalid blocksize parameter: %s\n"),
585 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
586 "Extended options are separated by commas, "
587 "and may take an argument which\n"
588 "\tis set off by an equals ('=') sign.\n\n"
589 "Valid extended options are:\n"
590 "\tsuperblock=<superblock number>\n"
591 "\tblocksize=<blocksize>\n"),
592 badopt ? badopt : "");
599 int main (int argc, char ** argv)
603 int print_badblocks = 0;
604 blk64_t use_superblock = 0;
605 int use_blocksize = 0;
614 setlocale(LC_MESSAGES, "");
615 setlocale(LC_CTYPE, "");
616 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
617 textdomain(NLS_CAT_NAME);
618 set_com_err_gettext(gettext);
620 add_error_table(&et_ext2_error_table);
621 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
624 program_name = *argv;
626 while ((c = getopt(argc, argv, "bfghixVo:")) != EOF) {
644 parse_extended_opts(optarg, &use_superblock,
648 /* Print version number and exit */
649 fprintf(stderr, _("\tUsing %s\n"),
650 error_message(EXT2_ET_BASE));
659 if (optind != argc - 1) {
663 device_name = argv[optind++];
664 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
666 flags |= EXT2_FLAG_FORCE;
668 flags |= EXT2_FLAG_IMAGE_FILE;
670 if (use_superblock && !use_blocksize) {
671 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
672 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
673 use_blocksize *= 2) {
674 retval = ext2fs_open (device_name, flags,
676 use_blocksize, unix_io_manager,
682 retval = ext2fs_open (device_name, flags, use_superblock,
683 use_blocksize, unix_io_manager, &fs);
684 if (retval && !(flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
685 flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
688 if (!retval && (fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS))
689 printf("%s", _("\n*** Checksum errors detected in filesystem! Run e2fsck now!\n\n"));
690 flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
692 com_err (program_name, retval, _("while trying to open %s"),
694 printf("%s", _("Couldn't find valid filesystem superblock.\n"));
695 if (retval == EXT2_ET_BAD_MAGIC)
696 check_plausibility(device_name, CHECK_FS_EXIST, NULL);
699 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
700 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
702 if (print_badblocks) {
703 list_bad_blocks(fs, 1);
706 goto just_descriptors;
707 list_super (fs->super);
708 if (fs->super->s_feature_incompat &
709 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
710 print_journal_information(fs);
711 ext2fs_close_free(&fs);
714 if ((fs->super->s_feature_compat &
715 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
716 (fs->super->s_journal_inum != 0))
717 print_inline_journal_information(fs);
718 list_bad_blocks(fs, 0);
720 ext2fs_close_free(&fs);
723 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
725 retval = ext2fs_read_bitmaps (fs);
726 if (retval && !(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
727 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
728 goto try_bitmaps_again;
730 if (!retval && (fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS))
731 printf("%s", _("\n*** Checksum errors detected in bitmaps! Run e2fsck now!\n\n"));
733 list_desc(fs, grp_only);
735 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
736 program_name, device_name,
737 error_message(retval));
740 ext2fs_close_free(&fs);
741 remove_error_table(&et_ext2_error_table);