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 const char * program_name = "dumpe2fs";
49 char * device_name = NULL;
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 (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
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_blk(fs, block);
148 printf(" (bg #%u + %u)", flex_grp,
149 (unsigned)(block-ext2fs_group_first_block(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;
166 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
167 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
168 units = _("clusters");
170 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
171 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
174 block_bitmap = malloc(block_nbytes);
176 inode_bitmap = malloc(inode_nbytes);
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;
184 first_block = fs->super->s_first_data_block;
185 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
186 old_desc_blocks = fs->super->s_first_meta_bg;
188 old_desc_blocks = fs->desc_blocks;
189 for (i = 0; i < fs->group_desc_count; i++) {
190 first_block = ext2fs_group_first_block2(fs, i);
191 last_block = ext2fs_group_last_block2(fs, i);
193 ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
194 &old_desc_blk, &new_desc_blk, 0);
196 printf (_("Group %lu: (Blocks "), i);
197 print_range(first_block, last_block);
199 print_bg_opts(fs, i);
200 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
201 unsigned csum = ext2fs_bg_checksum(fs, i);
202 unsigned exp_csum = ext2fs_group_desc_csum(fs, i);
204 printf(_(" Checksum 0x%04x"), csum);
205 if (csum != exp_csum)
206 printf(_(" (EXPECTED 0x%04x)"), exp_csum);
207 printf(_(", unused inodes %u\n"),
208 ext2fs_bg_itable_unused(fs, i));
210 has_super = ((i==0) || super_blk);
212 printf (_(" %s superblock at "),
213 i == 0 ? _("Primary") : _("Backup"));
214 print_number(super_blk);
217 printf(_(", Group descriptors at "));
218 print_range(old_desc_blk,
219 old_desc_blk + old_desc_blocks - 1);
221 printf(_("\n Reserved GDT blocks at "));
222 print_range(old_desc_blk + old_desc_blocks,
223 old_desc_blk + old_desc_blocks +
226 } else if (new_desc_blk) {
227 fputc(has_super ? ',' : ' ', stdout);
228 printf(_(" Group descriptor at "));
229 print_number(new_desc_blk);
234 fputs(_(" Block bitmap at "), stdout);
235 print_number(ext2fs_block_bitmap_loc(fs, i));
236 print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
237 first_block, last_block);
238 fputs(_(", Inode bitmap at "), stdout);
239 print_number(ext2fs_inode_bitmap_loc(fs, i));
240 print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
241 first_block, last_block);
242 if (fs->super->s_feature_ro_compat &
243 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
244 printf(_(", csum 0x%08x"),
245 ext2fs_inode_bitmap_checksum(fs, i));
246 fputs(_("\n Inode table at "), stdout);
247 print_range(ext2fs_inode_table_loc(fs, i),
248 ext2fs_inode_table_loc(fs, i) +
249 inode_blocks_per_group - 1);
250 print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
251 first_block, last_block);
252 printf (_("\n %u free %s, %u free inodes, "
254 ext2fs_bg_free_blocks_count(fs, i), units,
255 ext2fs_bg_free_inodes_count(fs, i),
256 ext2fs_bg_used_dirs_count(fs, i),
257 ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
258 if (ext2fs_bg_itable_unused(fs, i))
259 printf (_(", %u unused inodes\n"),
260 ext2fs_bg_itable_unused(fs, i));
262 fputs(_(" Free blocks: "), stdout);
263 ext2fs_get_block_bitmap_range2(fs->block_map,
264 blk_itr, block_nbytes << 3, block_bitmap);
265 print_free(i, block_bitmap,
266 fs->super->s_clusters_per_group,
267 fs->super->s_first_data_block,
268 EXT2FS_CLUSTER_RATIO(fs));
270 blk_itr += fs->super->s_clusters_per_group;
273 fputs(_(" Free inodes: "), stdout);
274 ext2fs_get_inode_bitmap_range2(fs->inode_map,
275 ino_itr, inode_nbytes << 3, inode_bitmap);
276 print_free(i, inode_bitmap,
277 fs->super->s_inodes_per_group, 1, 1);
279 ino_itr += fs->super->s_inodes_per_group;
288 static void list_bad_blocks(ext2_filsys fs, int dump)
290 badblocks_list bb_list = 0;
291 badblocks_iterate bb_iter;
294 const char *header, *fmt;
296 retval = ext2fs_read_bb_inode(fs, &bb_list);
298 com_err("ext2fs_read_bb_inode", retval, 0);
301 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
303 com_err("ext2fs_badblocks_list_iterate_begin", retval,
304 _("while printing bad block list"));
308 header = fmt = "%u\n";
310 header = _("Bad blocks: %u");
313 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
314 printf(header ? header : fmt, blk);
317 ext2fs_badblocks_list_iterate_end(bb_iter);
320 ext2fs_badblocks_list_free(bb_list);
323 static void print_inline_journal_information(ext2_filsys fs)
325 journal_superblock_t *jsb;
326 struct ext2_inode inode;
327 ext2_file_t journal_file;
329 ino_t ino = fs->super->s_journal_inum;
331 __u32 *mask_ptr, mask, m;
332 int i, j, size, printed = 0;
334 retval = ext2fs_read_inode(fs, ino, &inode);
336 com_err(program_name, retval,
337 _("while reading journal inode"));
340 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
342 com_err(program_name, retval,
343 _("while opening journal inode"));
346 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
348 com_err(program_name, retval,
349 _("while reading journal super block"));
352 ext2fs_file_close(journal_file);
353 jsb = (journal_superblock_t *) buf;
354 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
356 "Journal superblock magic number invalid!\n");
359 printf(_("Journal features: "));
360 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
361 mask = be32_to_cpu(*mask_ptr);
362 for (j=0,m=1; j < 32; j++, m<<=1) {
364 printf(" %s", e2p_jrnl_feature2string(i, m));
372 fputs(_("Journal size: "), stdout);
373 if ((fs->super->s_feature_ro_compat &
374 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
375 (inode.i_flags & EXT4_HUGE_FILE_FL))
376 size = inode.i_blocks / (fs->blocksize / 1024);
378 size = inode.i_blocks >> 1;
380 printf("%uk\n", size);
382 printf("%uM\n", size >> 10);
383 printf(_("Journal length: %u\n"
384 "Journal sequence: 0x%08x\n"
385 "Journal start: %u\n"),
386 (unsigned int)ntohl(jsb->s_maxlen),
387 (unsigned int)ntohl(jsb->s_sequence),
388 (unsigned int)ntohl(jsb->s_start));
389 if (jsb->s_errno != 0)
390 printf(_("Journal errno: %d\n"),
391 (int) ntohl(jsb->s_errno));
394 static void print_journal_information(ext2_filsys fs)
400 journal_superblock_t *jsb;
402 /* Get the journal superblock */
403 if ((retval = io_channel_read_blk64(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
404 com_err(program_name, retval,
405 _("while reading journal superblock"));
408 jsb = (journal_superblock_t *) buf;
409 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
410 (jsb->s_header.h_blocktype !=
411 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
412 com_err(program_name, 0,
413 _("Couldn't find journal superblock magic numbers"));
417 printf(_("\nJournal block size: %u\n"
418 "Journal length: %u\n"
419 "Journal first block: %u\n"
420 "Journal sequence: 0x%08x\n"
421 "Journal start: %u\n"
422 "Journal number of users: %u\n"),
423 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
424 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
425 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
427 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
428 uuid_unparse(&jsb->s_users[i*16], str);
430 : _("Journal users: %s\n"),
435 static void parse_extended_opts(const char *opts, blk64_t *superblock,
438 char *buf, *token, *next, *p, *arg, *badopt = 0;
446 _("Couldn't allocate memory to parse options!\n"));
450 for (token = buf; token && *token; token = next) {
451 p = strchr(token, ',');
457 arg = strchr(token, '=');
462 if (strcmp(token, "superblock") == 0 ||
463 strcmp(token, "sb") == 0) {
469 *superblock = strtoul(arg, &p, 0);
472 _("Invalid superblock parameter: %s\n"),
477 } else if (strcmp(token, "blocksize") == 0 ||
478 strcmp(token, "bs") == 0) {
484 *blocksize = strtoul(arg, &p, 0);
487 _("Invalid blocksize parameter: %s\n"),
498 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
499 "Extended options are separated by commas, "
500 "and may take an argument which\n"
501 "\tis set off by an equals ('=') sign.\n\n"
502 "Valid extended options are:\n"
503 "\tsuperblock=<superblock number>\n"
504 "\tblocksize=<blocksize>\n"),
505 badopt ? badopt : "");
512 int main (int argc, char ** argv)
516 int print_badblocks = 0;
517 blk64_t use_superblock = 0;
518 int use_blocksize = 0;
526 setlocale(LC_MESSAGES, "");
527 setlocale(LC_CTYPE, "");
528 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
529 textdomain(NLS_CAT_NAME);
530 set_com_err_gettext(gettext);
532 add_error_table(&et_ext2_error_table);
533 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
536 program_name = *argv;
538 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
553 parse_extended_opts(optarg, &use_superblock,
557 /* Print version number and exit */
558 fprintf(stderr, _("\tUsing %s\n"),
559 error_message(EXT2_ET_BASE));
568 if (optind > argc - 1)
570 device_name = argv[optind++];
571 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
573 flags |= EXT2_FLAG_FORCE;
575 flags |= EXT2_FLAG_IMAGE_FILE;
577 if (use_superblock && !use_blocksize) {
578 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
579 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
580 use_blocksize *= 2) {
581 retval = ext2fs_open (device_name, flags,
583 use_blocksize, unix_io_manager,
589 retval = ext2fs_open (device_name, flags, use_superblock,
590 use_blocksize, unix_io_manager, &fs);
592 com_err (program_name, retval, _("while trying to open %s"),
594 printf (_("Couldn't find valid filesystem superblock.\n"));
597 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
598 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
600 if (print_badblocks) {
601 list_bad_blocks(fs, 1);
603 list_super (fs->super);
604 if (fs->super->s_feature_incompat &
605 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
606 print_journal_information(fs);
610 if ((fs->super->s_feature_compat &
611 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
612 (fs->super->s_journal_inum != 0))
613 print_inline_journal_information(fs);
614 list_bad_blocks(fs, 0);
619 retval = ext2fs_read_bitmaps (fs);
622 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
623 program_name, device_name,
624 error_message(retval));
628 remove_error_table(&et_ext2_error_table);