1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/*
* dumpe2fs.c - List the control structures of a second
* extended filesystem
*
* Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
* Laboratoire MASI, Institut Blaise Pascal
* Universite Pierre et Marie Curie (Paris VI)
*
* This file can be redistributed under the terms of the GNU General
* Public License
*/
/*
* History:
* 94/01/09 - Creation
* 94/02/27 - Ported to use the ext2fs library
*/
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/ext2_fs.h>
#include "ext2fs/ext2fs.h"
#include "e2p/e2p.h"
#include "../version.h"
#define in_use(m, x) (ext2fs_test_bit ((x), (m)))
const char * program_name = "dumpe2fs";
char * device_name = NULL;
static volatile void usage (void)
{
fprintf (stderr, "usage: %s device\n", program_name);
exit (1);
}
static void print_free (unsigned long group, char * bitmap,
unsigned long nbytes, unsigned long offset)
{
int p = 0;
unsigned long i;
unsigned long j;
for (i = 0; i < nbytes; i++)
if (!in_use (bitmap, i))
{
if (p)
printf (", ");
if (i == nbytes - 1 || in_use (bitmap, i + 1))
printf ("%lu", group * nbytes + i + offset);
else
{
for (j = i; j < nbytes && !in_use (bitmap, j);
j++)
;
printf ("%lu-%lu", group * nbytes + i + offset,
group * nbytes + (j - 1) + offset);
i = j - 1;
}
p = 1;
}
}
static void list_desc (ext2_filsys fs)
{
unsigned long i;
char * block_bitmap = fs->block_map->bitmap;
char * inode_bitmap = fs->inode_map->bitmap;
printf ("\n");
for (i = 0; i < fs->group_desc_count; i++)
{
printf ("Group %lu:\n", i);
printf (" Block bitmap at %u, Inode bitmap at %u, "
"Inode table at %u\n",
fs->group_desc[i].bg_block_bitmap,
fs->group_desc[i].bg_inode_bitmap,
fs->group_desc[i].bg_inode_table);
printf (" %d free blocks, %d free inodes, %d directories\n",
fs->group_desc[i].bg_free_blocks_count,
fs->group_desc[i].bg_free_inodes_count,
fs->group_desc[i].bg_used_dirs_count);
printf (" Free blocks: ");
print_free (i, block_bitmap, fs->super->s_blocks_per_group,
fs->super->s_first_data_block);
block_bitmap += fs->super->s_blocks_per_group / 8;
printf ("\n");
printf (" Free inodes: ");
print_free (i, inode_bitmap, fs->super->s_inodes_per_group, 1);
inode_bitmap += fs->super->s_inodes_per_group / 8;
printf ("\n");
}
}
static void list_bad_blocks(ext2_filsys fs)
{
badblocks_list bb_list = 0;
badblocks_iterate bb_iter;
blk_t blk;
errcode_t retval;
retval = ext2fs_read_bb_inode(fs, &bb_list);
if (retval) {
com_err("ext2fs_read_bb_inode", retval, "");
exit(1);
}
retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
if (retval) {
com_err("badblocks_list_iterate_begin", retval,
"while printing bad block list");
exit(1);
}
if (badblocks_list_iterate(bb_iter, &blk))
printf("Bad blocks: %d", blk);
while (badblocks_list_iterate(bb_iter, &blk))
printf(", %d", blk);
badblocks_list_iterate_end(bb_iter);
printf("\n");
}
static void dump_bad_blocks(ext2_filsys fs)
{
badblocks_list bb_list = 0;
badblocks_iterate bb_iter;
blk_t blk;
errcode_t retval;
retval = ext2fs_read_bb_inode(fs, &bb_list);
if (retval) {
com_err("ext2fs_read_bb_inode", retval, "");
exit(1);
}
retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
if (retval) {
com_err("badblocks_list_iterate_begin", retval,
"while printing bad block list");
exit(1);
}
while (badblocks_list_iterate(bb_iter, &blk))
printf("%d\n", blk);
badblocks_list_iterate_end(bb_iter);
}
static int i386_byteorder(void)
{
int one = 1;
char *cp = (char *) &one;
return (*cp == 1);
}
void main (int argc, char ** argv)
{
errcode_t retval;
ext2_filsys fs;
int print_badblocks = 0;
int big_endian;
char c;
fprintf (stderr, "dumpe2fs %s, %s for EXT2 FS %s, %s\n",
E2FSPROGS_VERSION, E2FSPROGS_DATE,
EXT2FS_VERSION, EXT2FS_DATE);
if (argc && *argv)
program_name = *argv;
while ((c = getopt (argc, argv, "b")) != EOF) {
switch (c) {
case 'b':
print_badblocks++;
break;
default:
usage ();
}
}
if (optind > argc - 1)
usage ();
device_name = argv[optind++];
initialize_ext2_error_table();
retval = ext2fs_open (device_name, 0, 0, 0, unix_io_manager, &fs);
if (retval) {
com_err (program_name, retval, "while trying to open %s",
device_name);
printf ("Couldn't find valid filesystem superblock.\n");
exit (1);
}
if (print_badblocks) {
dump_bad_blocks(fs);
} else {
retval = ext2fs_read_bitmaps (fs);
if (retval) {
com_err (program_name, retval,
"while trying to read the bitmaps",
device_name);
ext2fs_close (fs);
exit (1);
}
big_endian = ((fs->flags & EXT2_SWAP_BYTES) != 0);
if (!i386_byteorder())
big_endian = !big_endian;
if (big_endian)
printf("Note: This is a byte-swapped filesystem\n");
list_super (fs->super);
list_bad_blocks (fs);
list_desc (fs);
}
ext2fs_close (fs);
exit (0);
}
|