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 * This file can be redistributed under the terms of the GNU General
16 * 94/02/27 - Ported to use the ext2fs library
28 #include <linux/ext2_fs.h>
30 #include "ext2fs/ext2fs.h"
33 #include "../version.h"
35 #define in_use(m, x) (test_bit ((x), (m)))
37 const char * program_name = "dumpe2fs";
38 char * device_name = NULL;
40 static volatile void usage (void)
42 fprintf (stderr, "usage: %s device\n", program_name);
46 static void print_free (unsigned long group, char * bitmap,
47 unsigned long nbytes, unsigned long offset)
53 for (i = 0; i < nbytes; i++)
54 if (!in_use (bitmap, i))
58 if (i == nbytes - 1 || in_use (bitmap, i + 1))
59 printf ("%lu", group * nbytes + i + offset);
62 for (j = i; j < nbytes && !in_use (bitmap, j);
65 printf ("%lu-%lu", group * nbytes + i + offset,
66 group * nbytes + (j - 1) + offset);
73 static void list_desc (ext2_filsys fs)
76 char * block_bitmap = fs->block_map->bitmap;
77 char * inode_bitmap = fs->inode_map->bitmap;
80 for (i = 0; i < fs->group_desc_count; i++)
82 printf ("Group %lu:\n", i);
83 printf (" Block bitmap at %u, Inode bitmap at %u, "
84 "Inode table at %u\n",
85 fs->group_desc[i].bg_block_bitmap,
86 fs->group_desc[i].bg_inode_bitmap,
87 fs->group_desc[i].bg_inode_table);
88 printf (" %d free blocks, %d free inodes, %d directories\n",
89 fs->group_desc[i].bg_free_blocks_count,
90 fs->group_desc[i].bg_free_inodes_count,
91 fs->group_desc[i].bg_used_dirs_count);
92 printf (" Free blocks: ");
93 print_free (i, block_bitmap, fs->super->s_blocks_per_group,
94 fs->super->s_first_data_block);
95 block_bitmap += fs->super->s_blocks_per_group / 8;
97 printf (" Free inodes: ");
98 print_free (i, inode_bitmap, fs->super->s_inodes_per_group, 1);
99 inode_bitmap += fs->super->s_inodes_per_group / 8;
104 static void list_bad_blocks(ext2_filsys fs)
106 badblocks_list bb_list = 0;
107 badblocks_iterate bb_iter;
111 retval = ext2fs_read_bb_inode(fs, &bb_list);
113 com_err("ext2fs_read_bb_inode", retval, "");
116 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
118 com_err("badblocks_list_iterate_begin", retval,
119 "while printing bad block list");
122 if (badblocks_list_iterate(bb_iter, &blk))
123 printf("Bad blocks: %d", blk);
124 while (badblocks_list_iterate(bb_iter, &blk))
126 badblocks_list_iterate_end(bb_iter);
130 static void dump_bad_blocks(ext2_filsys fs)
132 badblocks_list bb_list = 0;
133 badblocks_iterate bb_iter;
137 retval = ext2fs_read_bb_inode(fs, &bb_list);
139 com_err("ext2fs_read_bb_inode", retval, "");
142 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
144 com_err("badblocks_list_iterate_begin", retval,
145 "while printing bad block list");
148 while (badblocks_list_iterate(bb_iter, &blk))
150 badblocks_list_iterate_end(bb_iter);
154 void main (int argc, char ** argv)
158 int print_badblocks = 0;
161 fprintf (stderr, "dumpe2fs %s, %s for EXT2 FS %s, %s\n",
162 E2FSPROGS_VERSION, E2FSPROGS_DATE,
163 EXT2FS_VERSION, EXT2FS_DATE);
165 program_name = *argv;
167 while ((c = getopt (argc, argv, "b")) != EOF) {
176 if (optind > argc - 1)
178 device_name = argv[optind++];
179 initialize_ext2_error_table();
180 retval = ext2fs_open (device_name, 0, 0, 0, unix_io_manager, &fs);
182 com_err (program_name, retval, "while trying to open %s",
184 printf ("Couldn't find valid filesystem superblock.\n");
187 if (print_badblocks) {
190 retval = ext2fs_read_bitmaps (fs);
192 com_err (program_name, retval,
193 "while trying to read the bitmaps",
198 list_super (fs->super);
199 list_bad_blocks (fs);