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"
46 #define in_use(m, x) (ext2fs_test_bit ((x), (m)))
48 static const char * program_name = "dumpe2fs";
49 static char * device_name = NULL;
50 static int hex_format = 0;
51 static int blocks64 = 0;
53 static void usage(void)
55 fprintf (stderr, _("Usage: %s [-bfhixV] [-o superblock=<num>] "
56 "[-o blocksize=<num>] device\n"), program_name);
60 static void print_number(unsigned long long num)
64 printf("0x%08llx", num);
66 printf("0x%04llx", num);
71 static void print_range(unsigned long long a, unsigned long long b)
75 printf("0x%08llx-0x%08llx", a, b);
77 printf("0x%04llx-0x%04llx", a, b);
79 printf("%llu-%llu", a, b);
82 static void print_free(unsigned long group, char * bitmap,
83 unsigned long num, unsigned long offset, int ratio)
90 offset += group * num;
91 for (i = 0; i < num; i++)
92 if (!in_use (bitmap, i))
96 print_number((i + offset) * ratio);
97 for (j = i; j < num && !in_use (bitmap, j); j++)
101 print_number((j + offset) * ratio);
108 static void print_bg_opt(int bg_flags, int mask,
109 const char *str, int *first)
111 if (bg_flags & mask) {
120 static void print_bg_opts(ext2_filsys fs, dgrp_t i)
122 int first = 1, bg_flags = 0;
124 if (ext2fs_has_group_desc_csum(fs))
125 bg_flags = ext2fs_bg_flags(fs, i);
127 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
129 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
131 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
138 static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
139 blk64_t first_block, blk64_t last_block)
141 if ((block >= first_block) && (block <= last_block)) {
142 if (itable && block == first_block)
144 printf(" (+%u)", (unsigned)(block - first_block));
145 } else if (fs->super->s_feature_incompat &
146 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
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)));
153 static void list_desc (ext2_filsys fs)
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;
163 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
164 ext2_ino_t ino_itr = 1;
167 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
168 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
169 units = _("clusters");
171 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
172 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
175 block_bitmap = malloc(block_nbytes);
177 inode_bitmap = malloc(inode_nbytes);
179 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
180 EXT2_INODE_SIZE(fs->super)) +
181 EXT2_BLOCK_SIZE(fs->super) - 1) /
182 EXT2_BLOCK_SIZE(fs->super);
183 reserved_gdt = fs->super->s_reserved_gdt_blocks;
185 first_block = fs->super->s_first_data_block;
186 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
187 old_desc_blocks = fs->super->s_first_meta_bg;
189 old_desc_blocks = fs->desc_blocks;
190 for (i = 0; i < fs->group_desc_count; i++) {
191 first_block = ext2fs_group_first_block2(fs, i);
192 last_block = ext2fs_group_last_block2(fs, i);
194 ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
195 &old_desc_blk, &new_desc_blk, 0);
197 printf (_("Group %lu: (Blocks "), i);
198 print_range(first_block, last_block);
200 print_bg_opts(fs, i);
201 if (ext2fs_has_group_desc_csum(fs)) {
202 unsigned csum = ext2fs_bg_checksum(fs, i);
203 unsigned exp_csum = ext2fs_group_desc_csum(fs, i);
205 printf(_(" Checksum 0x%04x"), csum);
206 if (csum != exp_csum)
207 printf(_(" (EXPECTED 0x%04x)"), exp_csum);
208 printf(_(", unused inodes %u\n"),
209 ext2fs_bg_itable_unused(fs, i));
211 has_super = ((i==0) || super_blk);
213 printf (_(" %s superblock at "),
214 i == 0 ? _("Primary") : _("Backup"));
215 print_number(super_blk);
218 printf("%s", _(", Group descriptors at "));
219 print_range(old_desc_blk,
220 old_desc_blk + old_desc_blocks - 1);
222 printf("%s", _("\n Reserved GDT blocks at "));
223 print_range(old_desc_blk + old_desc_blocks,
224 old_desc_blk + old_desc_blocks +
227 } else if (new_desc_blk) {
228 fputc(has_super ? ',' : ' ', stdout);
229 printf("%s", _(" Group descriptor at "));
230 print_number(new_desc_blk);
235 fputs(_(" Block bitmap at "), stdout);
236 print_number(ext2fs_block_bitmap_loc(fs, i));
237 print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
238 first_block, last_block);
239 if (fs->super->s_feature_ro_compat &
240 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
241 printf(_(", csum 0x%08x"),
242 ext2fs_block_bitmap_checksum(fs, i));
243 fputs(_(", Inode bitmap at "), stdout);
244 print_number(ext2fs_inode_bitmap_loc(fs, i));
245 print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
246 first_block, last_block);
247 if (fs->super->s_feature_ro_compat &
248 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
249 printf(_(", csum 0x%08x"),
250 ext2fs_inode_bitmap_checksum(fs, i));
251 fputs(_("\n Inode table at "), stdout);
252 print_range(ext2fs_inode_table_loc(fs, i),
253 ext2fs_inode_table_loc(fs, i) +
254 inode_blocks_per_group - 1);
255 print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
256 first_block, last_block);
257 printf (_("\n %u free %s, %u free inodes, "
259 ext2fs_bg_free_blocks_count(fs, i), units,
260 ext2fs_bg_free_inodes_count(fs, i),
261 ext2fs_bg_used_dirs_count(fs, i),
262 ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
263 if (ext2fs_bg_itable_unused(fs, i))
264 printf (_(", %u unused inodes\n"),
265 ext2fs_bg_itable_unused(fs, i));
267 fputs(_(" Free blocks: "), stdout);
268 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
269 blk_itr, block_nbytes << 3, block_bitmap);
271 com_err("list_desc", retval,
272 "while reading block bitmap");
274 print_free(i, block_bitmap,
275 fs->super->s_clusters_per_group,
276 fs->super->s_first_data_block,
277 EXT2FS_CLUSTER_RATIO(fs));
279 blk_itr += fs->super->s_clusters_per_group;
282 fputs(_(" Free inodes: "), stdout);
283 retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
284 ino_itr, inode_nbytes << 3, inode_bitmap);
286 com_err("list_desc", retval,
287 "while reading inode bitmap");
289 print_free(i, inode_bitmap,
290 fs->super->s_inodes_per_group,
293 ino_itr += fs->super->s_inodes_per_group;
302 static void list_bad_blocks(ext2_filsys fs, int dump)
304 badblocks_list bb_list = 0;
305 badblocks_iterate bb_iter;
308 const char *header, *fmt;
310 retval = ext2fs_read_bb_inode(fs, &bb_list);
312 com_err("ext2fs_read_bb_inode", retval, 0);
315 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
317 com_err("ext2fs_badblocks_list_iterate_begin", retval,
318 "%s", _("while printing bad block list"));
322 header = fmt = "%u\n";
324 header = _("Bad blocks: %u");
327 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
328 printf(header ? header : fmt, blk);
331 ext2fs_badblocks_list_iterate_end(bb_iter);
334 ext2fs_badblocks_list_free(bb_list);
337 static const char *journal_checksum_type_str(__u8 type)
340 case JBD2_CRC32C_CHKSUM:
347 static void print_inline_journal_information(ext2_filsys fs)
349 journal_superblock_t *jsb;
350 struct ext2_inode inode;
351 ext2_file_t journal_file;
353 ino_t ino = fs->super->s_journal_inum;
355 __u32 *mask_ptr, mask, m;
356 int i, j, size, printed = 0;
358 if (fs->flags & EXT2_FLAG_IMAGE_FILE)
360 retval = ext2fs_read_inode(fs, ino, &inode);
362 com_err(program_name, retval, "%s",
363 _("while reading journal inode"));
366 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
368 com_err(program_name, retval, "%s",
369 _("while opening journal inode"));
372 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
374 com_err(program_name, retval, "%s",
375 _("while reading journal super block"));
378 ext2fs_file_close(journal_file);
379 jsb = (journal_superblock_t *) buf;
380 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
381 fprintf(stderr, "%s",
382 _("Journal superblock magic number invalid!\n"));
385 printf("%s", _("Journal features: "));
386 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
387 mask = be32_to_cpu(*mask_ptr);
388 for (j=0,m=1; j < 32; j++, m<<=1) {
390 printf(" %s", e2p_jrnl_feature2string(i, m));
398 fputs(_("Journal size: "), stdout);
399 if ((fs->super->s_feature_ro_compat &
400 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
401 (inode.i_flags & EXT4_HUGE_FILE_FL))
402 size = inode.i_blocks / (fs->blocksize / 1024);
404 size = inode.i_blocks >> 1;
406 printf("%uk\n", size);
408 printf("%uM\n", size >> 10);
409 printf(_("Journal length: %u\n"
410 "Journal sequence: 0x%08x\n"
411 "Journal start: %u\n"),
412 (unsigned int)ntohl(jsb->s_maxlen),
413 (unsigned int)ntohl(jsb->s_sequence),
414 (unsigned int)ntohl(jsb->s_start));
415 if (jsb->s_feature_compat &
416 ext2fs_cpu_to_be32(JFS_FEATURE_COMPAT_CHECKSUM))
417 printf(_("Journal checksum type: crc32\n"));
418 if (jsb->s_feature_incompat &
419 ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V2))
420 printf(_("Journal checksum type: %s\n"
421 "Journal checksum: 0x%08x\n"),
422 journal_checksum_type_str(jsb->s_checksum_type),
423 ext2fs_be32_to_cpu(jsb->s_checksum));
424 if (jsb->s_errno != 0)
425 printf(_("Journal errno: %d\n"),
426 (int) ntohl(jsb->s_errno));
429 static void print_journal_information(ext2_filsys fs)
435 journal_superblock_t *jsb;
437 /* Get the journal superblock */
438 if ((retval = io_channel_read_blk64(fs->io,
439 fs->super->s_first_data_block + 1,
441 com_err(program_name, retval, "%s",
442 _("while reading journal superblock"));
445 jsb = (journal_superblock_t *) buf;
446 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
447 (jsb->s_header.h_blocktype !=
448 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
449 com_err(program_name, 0, "%s",
450 _("Couldn't find journal superblock magic numbers"));
454 if (jsb->s_feature_compat &
455 ext2fs_cpu_to_be32(JFS_FEATURE_COMPAT_CHECKSUM))
456 printf(_("Journal checksum type: crc32\n"));
457 if (jsb->s_feature_incompat &
458 ext2fs_cpu_to_be32(JFS_FEATURE_INCOMPAT_CSUM_V2))
459 printf(_("Journal checksum type: %s\n"
460 "Journal checksum: 0x%08x\n"),
461 journal_checksum_type_str(jsb->s_checksum_type),
462 ext2fs_be32_to_cpu(jsb->s_checksum));
464 printf(_("\nJournal block size: %u\n"
465 "Journal length: %u\n"
466 "Journal first block: %u\n"
467 "Journal sequence: 0x%08x\n"
468 "Journal start: %u\n"
469 "Journal number of users: %u\n"),
470 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
471 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
472 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
474 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
475 uuid_unparse(&jsb->s_users[i*16], str);
477 : _("Journal users: %s\n"),
482 static void parse_extended_opts(const char *opts, blk64_t *superblock,
485 char *buf, *token, *next, *p, *arg, *badopt = 0;
492 fprintf(stderr, "%s",
493 _("Couldn't allocate memory to parse options!\n"));
497 for (token = buf; token && *token; token = next) {
498 p = strchr(token, ',');
504 arg = strchr(token, '=');
509 if (strcmp(token, "superblock") == 0 ||
510 strcmp(token, "sb") == 0) {
516 *superblock = strtoul(arg, &p, 0);
519 _("Invalid superblock parameter: %s\n"),
524 } else if (strcmp(token, "blocksize") == 0 ||
525 strcmp(token, "bs") == 0) {
531 *blocksize = strtoul(arg, &p, 0);
534 _("Invalid blocksize parameter: %s\n"),
545 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
546 "Extended options are separated by commas, "
547 "and may take an argument which\n"
548 "\tis set off by an equals ('=') sign.\n\n"
549 "Valid extended options are:\n"
550 "\tsuperblock=<superblock number>\n"
551 "\tblocksize=<blocksize>\n"),
552 badopt ? badopt : "");
559 int main (int argc, char ** argv)
563 int print_badblocks = 0;
564 blk64_t use_superblock = 0;
565 int use_blocksize = 0;
573 setlocale(LC_MESSAGES, "");
574 setlocale(LC_CTYPE, "");
575 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
576 textdomain(NLS_CAT_NAME);
577 set_com_err_gettext(gettext);
579 add_error_table(&et_ext2_error_table);
580 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
583 program_name = *argv;
585 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
600 parse_extended_opts(optarg, &use_superblock,
604 /* Print version number and exit */
605 fprintf(stderr, _("\tUsing %s\n"),
606 error_message(EXT2_ET_BASE));
615 if (optind > argc - 1)
617 device_name = argv[optind++];
618 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
620 flags |= EXT2_FLAG_FORCE;
622 flags |= EXT2_FLAG_IMAGE_FILE;
624 if (use_superblock && !use_blocksize) {
625 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
626 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
627 use_blocksize *= 2) {
628 retval = ext2fs_open (device_name, flags,
630 use_blocksize, unix_io_manager,
636 retval = ext2fs_open (device_name, flags, use_superblock,
637 use_blocksize, unix_io_manager, &fs);
639 com_err (program_name, retval, _("while trying to open %s"),
641 printf("%s", _("Couldn't find valid filesystem superblock.\n"));
644 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
645 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
647 if (print_badblocks) {
648 list_bad_blocks(fs, 1);
650 list_super (fs->super);
651 if (fs->super->s_feature_incompat &
652 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
653 print_journal_information(fs);
657 if ((fs->super->s_feature_compat &
658 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
659 (fs->super->s_journal_inum != 0))
660 print_inline_journal_information(fs);
661 list_bad_blocks(fs, 0);
666 retval = ext2fs_read_bitmaps (fs);
669 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
670 program_name, device_name,
671 error_message(retval));
675 remove_error_table(&et_ext2_error_table);