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;
52 static void usage(void)
54 fprintf (stderr, _("Usage: %s [-bfhixV] [-o superblock=<num>] "
55 "[-o blocksize=<num>] device\n"), program_name);
59 static void print_number(unsigned long num)
62 printf("0x%04lx", num);
67 static void print_range(unsigned long long a, unsigned long long b)
70 printf("0x%llx-0x%llx", a, b);
72 printf("%llu-%llu", a, b);
75 static void print_free(unsigned long group, char * bitmap,
76 unsigned long num, unsigned long offset, int ratio)
83 offset += group * num;
84 for (i = 0; i < num; i++)
85 if (!in_use (bitmap, i))
89 print_number((i + offset) * ratio);
90 for (j = i; j < num && !in_use (bitmap, j); j++)
94 print_number((j + offset) * ratio);
101 static void print_bg_opt(int bg_flags, int mask,
102 const char *str, int *first)
104 if (bg_flags & mask) {
113 static void print_bg_opts(ext2_filsys fs, dgrp_t i)
115 int first = 1, bg_flags = 0;
117 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
118 bg_flags = ext2fs_bg_flags(fs, i);
120 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
122 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
124 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
131 static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
132 blk64_t first_block, blk64_t last_block)
134 if ((block >= first_block) && (block <= last_block)) {
135 if (itable && block == first_block)
137 printf(" (+%u)", (unsigned)(block - first_block));
138 } else if (fs->super->s_feature_incompat &
139 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
140 dgrp_t flex_grp = ext2fs_group_of_blk(fs, block);
141 printf(" (bg #%u + %u)", flex_grp,
142 (unsigned)(block-ext2fs_group_first_block(fs,flex_grp)));
146 static void list_desc (ext2_filsys fs)
149 blk64_t first_block, last_block;
150 blk64_t super_blk, old_desc_blk, new_desc_blk;
151 char *block_bitmap=NULL, *inode_bitmap=NULL;
152 const char *units = _("blocks");
153 int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
154 int block_nbytes, inode_nbytes;
156 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
157 ext2_ino_t ino_itr = 1;
159 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
160 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
161 units = _("clusters");
163 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
164 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
167 block_bitmap = malloc(block_nbytes);
169 inode_bitmap = malloc(inode_nbytes);
171 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
172 EXT2_INODE_SIZE(fs->super)) +
173 EXT2_BLOCK_SIZE(fs->super) - 1) /
174 EXT2_BLOCK_SIZE(fs->super);
175 reserved_gdt = fs->super->s_reserved_gdt_blocks;
177 first_block = fs->super->s_first_data_block;
178 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
179 old_desc_blocks = fs->super->s_first_meta_bg;
181 old_desc_blocks = fs->desc_blocks;
182 for (i = 0; i < fs->group_desc_count; i++) {
183 first_block = ext2fs_group_first_block2(fs, i);
184 last_block = ext2fs_group_last_block2(fs, i);
186 ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
187 &old_desc_blk, &new_desc_blk, 0);
189 printf (_("Group %lu: (Blocks "), i);
190 print_range(first_block, last_block);
192 print_bg_opts(fs, i);
193 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
194 printf(_(" Checksum 0x%04x, unused inodes %u\n"),
195 ext2fs_bg_checksum(fs, i),
196 ext2fs_bg_itable_unused(fs, i));
197 has_super = ((i==0) || super_blk);
199 printf (_(" %s superblock at "),
200 i == 0 ? _("Primary") : _("Backup"));
201 print_number(super_blk);
204 printf(_(", Group descriptors at "));
205 print_range(old_desc_blk,
206 old_desc_blk + old_desc_blocks - 1);
208 printf(_("\n Reserved GDT blocks at "));
209 print_range(old_desc_blk + old_desc_blocks,
210 old_desc_blk + old_desc_blocks +
213 } else if (new_desc_blk) {
214 fputc(has_super ? ',' : ' ', stdout);
215 printf(_(" Group descriptor at "));
216 print_number(new_desc_blk);
221 fputs(_(" Block bitmap at "), stdout);
222 print_number(ext2fs_block_bitmap_loc(fs, i));
223 print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
224 first_block, last_block);
225 fputs(_(", Inode bitmap at "), stdout);
226 print_number(ext2fs_inode_bitmap_loc(fs, i));
227 print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
228 first_block, last_block);
229 fputs(_("\n Inode table at "), stdout);
230 print_range(ext2fs_inode_table_loc(fs, i),
231 ext2fs_inode_table_loc(fs, i) +
232 inode_blocks_per_group - 1);
233 print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
234 first_block, last_block);
235 printf (_("\n %u free %s, %u free inodes, "
237 ext2fs_bg_free_blocks_count(fs, i), units,
238 ext2fs_bg_free_inodes_count(fs, i),
239 ext2fs_bg_used_dirs_count(fs, i),
240 ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
241 if (ext2fs_bg_itable_unused(fs, i))
242 printf (_(", %u unused inodes\n"),
243 ext2fs_bg_itable_unused(fs, i));
245 fputs(_(" Free blocks: "), stdout);
246 ext2fs_get_block_bitmap_range2(fs->block_map,
247 blk_itr, block_nbytes << 3, block_bitmap);
248 print_free(i, block_bitmap,
249 fs->super->s_clusters_per_group,
250 fs->super->s_first_data_block,
251 EXT2FS_CLUSTER_RATIO(fs));
253 blk_itr += fs->super->s_clusters_per_group;
256 fputs(_(" Free inodes: "), stdout);
257 ext2fs_get_inode_bitmap_range2(fs->inode_map,
258 ino_itr, inode_nbytes << 3, inode_bitmap);
259 print_free(i, inode_bitmap,
260 fs->super->s_inodes_per_group, 1, 1);
262 ino_itr += fs->super->s_inodes_per_group;
271 static void list_bad_blocks(ext2_filsys fs, int dump)
273 badblocks_list bb_list = 0;
274 badblocks_iterate bb_iter;
277 const char *header, *fmt;
279 retval = ext2fs_read_bb_inode(fs, &bb_list);
281 com_err("ext2fs_read_bb_inode", retval, 0);
284 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
286 com_err("ext2fs_badblocks_list_iterate_begin", retval,
287 _("while printing bad block list"));
291 header = fmt = "%u\n";
293 header = _("Bad blocks: %u");
296 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
297 printf(header ? header : fmt, blk);
300 ext2fs_badblocks_list_iterate_end(bb_iter);
303 ext2fs_badblocks_list_free(bb_list);
306 static void print_inline_journal_information(ext2_filsys fs)
308 journal_superblock_t *jsb;
309 struct ext2_inode inode;
310 ext2_file_t journal_file;
312 ino_t ino = fs->super->s_journal_inum;
314 __u32 *mask_ptr, mask, m;
315 int i, j, size, printed = 0;
317 retval = ext2fs_read_inode(fs, ino, &inode);
319 com_err(program_name, retval,
320 _("while reading journal inode"));
323 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
325 com_err(program_name, retval,
326 _("while opening journal inode"));
329 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
331 com_err(program_name, retval,
332 _("while reading journal super block"));
335 ext2fs_file_close(journal_file);
336 jsb = (journal_superblock_t *) buf;
337 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
339 "Journal superblock magic number invalid!\n");
342 printf(_("Journal features: "));
343 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
344 mask = be32_to_cpu(*mask_ptr);
345 for (j=0,m=1; j < 32; j++, m<<=1) {
347 printf(" %s", e2p_jrnl_feature2string(i, m));
355 fputs(_("Journal size: "), stdout);
356 if ((fs->super->s_feature_ro_compat &
357 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
358 (inode.i_flags & EXT4_HUGE_FILE_FL))
359 size = inode.i_blocks / (fs->blocksize / 1024);
361 size = inode.i_blocks >> 1;
363 printf("%uk\n", size);
365 printf("%uM\n", size >> 10);
366 printf(_("Journal length: %u\n"
367 "Journal sequence: 0x%08x\n"
368 "Journal start: %u\n"),
369 (unsigned int)ntohl(jsb->s_maxlen),
370 (unsigned int)ntohl(jsb->s_sequence),
371 (unsigned int)ntohl(jsb->s_start));
374 static void print_journal_information(ext2_filsys fs)
380 journal_superblock_t *jsb;
382 /* Get the journal superblock */
383 if ((retval = io_channel_read_blk64(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
384 com_err(program_name, retval,
385 _("while reading journal superblock"));
388 jsb = (journal_superblock_t *) buf;
389 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
390 (jsb->s_header.h_blocktype !=
391 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
392 com_err(program_name, 0,
393 _("Couldn't find journal superblock magic numbers"));
397 printf(_("\nJournal block size: %u\n"
398 "Journal length: %u\n"
399 "Journal first block: %u\n"
400 "Journal sequence: 0x%08x\n"
401 "Journal start: %u\n"
402 "Journal number of users: %u\n"),
403 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
404 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
405 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
407 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
408 uuid_unparse(&jsb->s_users[i*16], str);
410 : _("Journal users: %s\n"),
415 static void parse_extended_opts(const char *opts, blk64_t *superblock,
418 char *buf, *token, *next, *p, *arg, *badopt = 0;
426 _("Couldn't allocate memory to parse options!\n"));
430 for (token = buf; token && *token; token = next) {
431 p = strchr(token, ',');
437 arg = strchr(token, '=');
442 if (strcmp(token, "superblock") == 0 ||
443 strcmp(token, "sb") == 0) {
449 *superblock = strtoul(arg, &p, 0);
452 _("Invalid superblock parameter: %s\n"),
457 } else if (strcmp(token, "blocksize") == 0 ||
458 strcmp(token, "bs") == 0) {
464 *blocksize = strtoul(arg, &p, 0);
467 _("Invalid blocksize parameter: %s\n"),
478 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
479 "Extended options are separated by commas, "
480 "and may take an argument which\n"
481 "\tis set off by an equals ('=') sign.\n\n"
482 "Valid extended options are:\n"
483 "\tsuperblock=<superblock number>\n"
484 "\tblocksize=<blocksize>\n"),
485 badopt ? badopt : "");
492 int main (int argc, char ** argv)
496 int print_badblocks = 0;
497 blk64_t use_superblock = 0;
498 int use_blocksize = 0;
506 setlocale(LC_MESSAGES, "");
507 setlocale(LC_CTYPE, "");
508 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
509 textdomain(NLS_CAT_NAME);
510 set_com_err_gettext(gettext);
512 add_error_table(&et_ext2_error_table);
513 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
516 program_name = *argv;
518 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
533 parse_extended_opts(optarg, &use_superblock,
537 /* Print version number and exit */
538 fprintf(stderr, _("\tUsing %s\n"),
539 error_message(EXT2_ET_BASE));
548 if (optind > argc - 1)
550 device_name = argv[optind++];
551 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
553 flags |= EXT2_FLAG_FORCE;
555 flags |= EXT2_FLAG_IMAGE_FILE;
557 if (use_superblock && !use_blocksize) {
558 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
559 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
560 use_blocksize *= 2) {
561 retval = ext2fs_open (device_name, flags,
563 use_blocksize, unix_io_manager,
569 retval = ext2fs_open (device_name, flags, use_superblock,
570 use_blocksize, unix_io_manager, &fs);
572 com_err (program_name, retval, _("while trying to open %s"),
574 printf (_("Couldn't find valid filesystem superblock.\n"));
577 if (print_badblocks) {
578 list_bad_blocks(fs, 1);
580 list_super (fs->super);
581 if (fs->super->s_feature_incompat &
582 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
583 print_journal_information(fs);
587 if ((fs->super->s_feature_compat &
588 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
589 (fs->super->s_journal_inum != 0))
590 print_inline_journal_information(fs);
591 list_bad_blocks(fs, 0);
596 retval = ext2fs_read_bitmaps (fs);
599 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
600 program_name, device_name,
601 error_message(retval));
605 remove_error_table(&et_ext2_error_table);