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
35 #include "ext2fs/ext2_fs.h"
37 #include "ext2fs/ext2fs.h"
40 #include <uuid/uuid.h>
42 #include "../version.h"
43 #include "nls-enable.h"
45 #define in_use(m, x) (ext2fs_test_bit ((x), (m)))
47 const char * program_name = "dumpe2fs";
48 char * device_name = NULL;
51 static void usage(void)
53 fprintf (stderr, _("Usage: %s [-bfhixV] [-o superblock=<num>] "
54 "[-o blocksize=<num>] device\n"), program_name);
58 static void print_number(unsigned long num)
61 printf("0x%04lx", num);
66 static void print_range(unsigned long long a, unsigned long long b)
69 printf("0x%llx-0x%llx", a, b);
71 printf("%llu-%llu", a, b);
74 static void print_free (unsigned long group, char * bitmap,
75 unsigned long nbytes, unsigned long offset)
81 offset += group * nbytes;
82 for (i = 0; i < nbytes; i++)
83 if (!in_use (bitmap, i))
87 print_number(i + offset);
88 for (j = i; j < nbytes && !in_use (bitmap, j); j++)
92 print_number(j + offset);
99 static void print_bg_opt(int bg_flags, int mask,
100 const char *str, int *first)
102 if (bg_flags & mask) {
111 static void print_bg_opts(ext2_filsys fs, dgrp_t i)
113 int first = 1, bg_flags = 0;
115 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
116 bg_flags = ext2fs_bg_flags(fs, i);
118 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
120 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
122 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
129 static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
130 blk64_t first_block, blk64_t last_block)
132 if ((block >= first_block) && (block <= last_block)) {
133 if (itable && block == first_block)
135 printf(" (+%u)", (unsigned)(block - first_block));
136 } else if (fs->super->s_feature_incompat &
137 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
138 dgrp_t flex_grp = ext2fs_group_of_blk(fs, block);
139 printf(" (bg #%u + %u)", flex_grp,
140 (unsigned)(block-ext2fs_group_first_block(fs,flex_grp)));
144 static void list_desc (ext2_filsys fs)
147 blk64_t first_block, last_block;
148 blk64_t super_blk, old_desc_blk, new_desc_blk;
149 char *block_bitmap=NULL, *inode_bitmap=NULL;
150 int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
151 int block_nbytes, inode_nbytes;
153 blk64_t blk_itr = fs->super->s_first_data_block;
154 ext2_ino_t ino_itr = 1;
156 block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
157 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
160 block_bitmap = malloc(block_nbytes);
162 inode_bitmap = malloc(inode_nbytes);
164 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
165 EXT2_INODE_SIZE(fs->super)) +
166 EXT2_BLOCK_SIZE(fs->super) - 1) /
167 EXT2_BLOCK_SIZE(fs->super);
168 reserved_gdt = fs->super->s_reserved_gdt_blocks;
170 first_block = fs->super->s_first_data_block;
171 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
172 old_desc_blocks = fs->super->s_first_meta_bg;
174 old_desc_blocks = fs->desc_blocks;
175 for (i = 0; i < fs->group_desc_count; i++) {
176 first_block = ext2fs_group_first_block2(fs, i);
177 last_block = ext2fs_group_last_block2(fs, i);
179 ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
180 &old_desc_blk, &new_desc_blk, 0);
182 printf (_("Group %lu: (Blocks "), i);
183 print_range(first_block, last_block);
185 print_bg_opts(fs, i);
186 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
187 printf(_(" Checksum 0x%04x, unused inodes %u\n"),
188 ext2fs_bg_checksum(fs, i),
189 ext2fs_bg_itable_unused(fs, i));
190 has_super = ((i==0) || super_blk);
192 printf (_(" %s superblock at "),
193 i == 0 ? _("Primary") : _("Backup"));
194 print_number(super_blk);
197 printf(_(", Group descriptors at "));
198 print_range(old_desc_blk,
199 old_desc_blk + old_desc_blocks - 1);
201 printf(_("\n Reserved GDT blocks at "));
202 print_range(old_desc_blk + old_desc_blocks,
203 old_desc_blk + old_desc_blocks +
206 } else if (new_desc_blk) {
207 fputc(has_super ? ',' : ' ', stdout);
208 printf(_(" Group descriptor at "));
209 print_number(new_desc_blk);
214 fputs(_(" Block bitmap at "), stdout);
215 print_number(ext2fs_block_bitmap_loc(fs, i));
216 print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
217 first_block, last_block);
218 fputs(_(", Inode bitmap at "), stdout);
219 print_number(ext2fs_inode_bitmap_loc(fs, i));
220 print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
221 first_block, last_block);
222 fputs(_("\n Inode table at "), stdout);
223 print_range(ext2fs_inode_table_loc(fs, i),
224 ext2fs_inode_table_loc(fs, i) +
225 inode_blocks_per_group - 1);
226 print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
227 first_block, last_block);
228 printf (_("\n %u free blocks, %u free inodes, "
230 ext2fs_bg_free_blocks_count(fs, i),
231 ext2fs_bg_free_inodes_count(fs, i),
232 ext2fs_bg_used_dirs_count(fs, i),
233 ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
234 if (ext2fs_bg_itable_unused(fs, i))
235 printf (_(", %u unused inodes\n"),
236 ext2fs_bg_itable_unused(fs, i));
238 fputs(_(" Free blocks: "), stdout);
239 ext2fs_get_block_bitmap_range2(fs->block_map,
240 blk_itr, block_nbytes << 3, block_bitmap);
241 print_free (i, block_bitmap,
242 fs->super->s_blocks_per_group,
243 fs->super->s_first_data_block);
245 blk_itr += fs->super->s_blocks_per_group;
248 fputs(_(" Free inodes: "), stdout);
249 ext2fs_get_inode_bitmap_range2(fs->inode_map,
250 ino_itr, inode_nbytes << 3, inode_bitmap);
251 print_free (i, inode_bitmap,
252 fs->super->s_inodes_per_group, 1);
254 ino_itr += fs->super->s_inodes_per_group;
263 static void list_bad_blocks(ext2_filsys fs, int dump)
265 badblocks_list bb_list = 0;
266 badblocks_iterate bb_iter;
269 const char *header, *fmt;
271 retval = ext2fs_read_bb_inode(fs, &bb_list);
273 com_err("ext2fs_read_bb_inode", retval, 0);
276 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
278 com_err("ext2fs_badblocks_list_iterate_begin", retval,
279 _("while printing bad block list"));
283 header = fmt = "%u\n";
285 header = _("Bad blocks: %u");
288 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
289 printf(header ? header : fmt, blk);
292 ext2fs_badblocks_list_iterate_end(bb_iter);
295 ext2fs_badblocks_list_free(bb_list);
298 static void print_inline_journal_information(ext2_filsys fs)
300 journal_superblock_t *jsb;
301 struct ext2_inode inode;
302 ext2_file_t journal_file;
304 ino_t ino = fs->super->s_journal_inum;
306 __u32 *mask_ptr, mask, m;
307 int i, j, size, printed = 0;
309 retval = ext2fs_read_inode(fs, ino, &inode);
311 com_err(program_name, retval,
312 _("while reading journal inode"));
315 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
317 com_err(program_name, retval,
318 _("while opening journal inode"));
321 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
323 com_err(program_name, retval,
324 _("while reading journal super block"));
327 ext2fs_file_close(journal_file);
328 jsb = (journal_superblock_t *) buf;
329 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
331 "Journal superblock magic number invalid!\n");
334 printf(_("Journal features: "));
335 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
336 mask = be32_to_cpu(*mask_ptr);
337 for (j=0,m=1; j < 32; j++, m<<=1) {
339 printf(" %s", e2p_jrnl_feature2string(i, m));
347 fputs(_("Journal size: "), stdout);
348 if ((fs->super->s_feature_ro_compat &
349 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
350 (inode.i_flags & EXT4_HUGE_FILE_FL))
351 size = inode.i_blocks / (fs->blocksize / 1024);
353 size = inode.i_blocks >> 1;
355 printf("%uk\n", size);
357 printf("%uM\n", size >> 10);
358 printf(_("Journal length: %u\n"
359 "Journal sequence: 0x%08x\n"
360 "Journal start: %u\n"),
361 (unsigned int)ntohl(jsb->s_maxlen),
362 (unsigned int)ntohl(jsb->s_sequence),
363 (unsigned int)ntohl(jsb->s_start));
366 static void print_journal_information(ext2_filsys fs)
372 journal_superblock_t *jsb;
374 /* Get the journal superblock */
375 if ((retval = io_channel_read_blk64(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
376 com_err(program_name, retval,
377 _("while reading journal superblock"));
380 jsb = (journal_superblock_t *) buf;
381 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
382 (jsb->s_header.h_blocktype !=
383 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
384 com_err(program_name, 0,
385 _("Couldn't find journal superblock magic numbers"));
389 printf(_("\nJournal block size: %u\n"
390 "Journal length: %u\n"
391 "Journal first block: %u\n"
392 "Journal sequence: 0x%08x\n"
393 "Journal start: %u\n"
394 "Journal number of users: %u\n"),
395 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
396 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
397 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
399 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
400 uuid_unparse(&jsb->s_users[i*16], str);
402 : _("Journal users: %s\n"),
407 static void parse_extended_opts(const char *opts, blk64_t *superblock,
410 char *buf, *token, *next, *p, *arg, *badopt = 0;
418 _("Couldn't allocate memory to parse options!\n"));
422 for (token = buf; token && *token; token = next) {
423 p = strchr(token, ',');
429 arg = strchr(token, '=');
434 if (strcmp(token, "superblock") == 0 ||
435 strcmp(token, "sb") == 0) {
441 *superblock = strtoul(arg, &p, 0);
444 _("Invalid superblock parameter: %s\n"),
449 } else if (strcmp(token, "blocksize") == 0 ||
450 strcmp(token, "bs") == 0) {
456 *blocksize = strtoul(arg, &p, 0);
459 _("Invalid blocksize parameter: %s\n"),
470 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
471 "Extended options are separated by commas, "
472 "and may take an argument which\n"
473 "\tis set off by an equals ('=') sign.\n\n"
474 "Valid extended options are:\n"
475 "\tsuperblock=<superblock number>\n"
476 "\tblocksize=<blocksize>\n"),
477 badopt ? badopt : "");
484 int main (int argc, char ** argv)
488 int print_badblocks = 0;
489 blk64_t use_superblock = 0;
490 int use_blocksize = 0;
498 setlocale(LC_MESSAGES, "");
499 setlocale(LC_CTYPE, "");
500 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
501 textdomain(NLS_CAT_NAME);
503 add_error_table(&et_ext2_error_table);
504 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
507 program_name = *argv;
509 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
524 parse_extended_opts(optarg, &use_superblock,
528 /* Print version number and exit */
529 fprintf(stderr, _("\tUsing %s\n"),
530 error_message(EXT2_ET_BASE));
539 if (optind > argc - 1)
541 device_name = argv[optind++];
542 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
544 flags |= EXT2_FLAG_FORCE;
546 flags |= EXT2_FLAG_IMAGE_FILE;
548 if (use_superblock && !use_blocksize) {
549 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
550 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
551 use_blocksize *= 2) {
552 retval = ext2fs_open (device_name, flags,
554 use_blocksize, unix_io_manager,
560 retval = ext2fs_open (device_name, flags, use_superblock,
561 use_blocksize, unix_io_manager, &fs);
563 com_err (program_name, retval, _("while trying to open %s"),
565 printf (_("Couldn't find valid filesystem superblock.\n"));
568 if (print_badblocks) {
569 list_bad_blocks(fs, 1);
571 list_super (fs->super);
572 if (fs->super->s_feature_incompat &
573 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
574 print_journal_information(fs);
578 if ((fs->super->s_feature_compat &
579 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
580 (fs->super->s_journal_inum != 0))
581 print_inline_journal_information(fs);
582 list_bad_blocks(fs, 0);
587 retval = ext2fs_read_bitmaps (fs);
590 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
591 program_name, device_name,
592 error_message(retval));
596 remove_error_table(&et_ext2_error_table);