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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/*
* 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)
*
* Copyright 1995, 1996, 1997 by Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
/*
* History:
* 94/01/09 - Creation
* 94/02/27 - Ported to use the ext2fs library
*/
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
extern char *optarg;
extern int optind;
#endif
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ext2fs/ext2_fs.h"
#include "ext2fs/ext2fs.h"
#include "e2p/e2p.h"
#include "jfs_user.h"
#include <uuid/uuid.h>
#include "../version.h"
#include "nls-enable.h"
#define in_use(m, x) (ext2fs_test_bit ((x), (m)))
const char * program_name = "dumpe2fs";
char * device_name = NULL;
int hex_format = 0;
static void usage(void)
{
fprintf (stderr, _("Usage: %s [-bfhixV] [-ob superblock] "
"[-oB blocksize] device\n"), program_name);
exit (1);
}
static void print_number(unsigned long num)
{
if (hex_format)
printf("0x%04lx", num);
else
printf("%lu", num);
}
static void print_range(unsigned long a, unsigned long b)
{
if (hex_format)
printf("0x%04lx-0x%04lx", a, b);
else
printf("%lu-%lu", a, b);
}
static void print_free (unsigned long group, char * bitmap,
unsigned long nbytes, unsigned long offset)
{
int p = 0;
unsigned long i;
unsigned long j;
offset += group * nbytes;
for (i = 0; i < nbytes; i++)
if (!in_use (bitmap, i))
{
if (p)
printf (", ");
print_number(i + offset);
for (j = i; j < nbytes && !in_use (bitmap, j); j++)
;
if (--j != i) {
fputc('-', stdout);
print_number(j + offset);
i = j;
}
p = 1;
}
}
static void print_bg_opt(int bg_flags, int mask,
const char *str, int *first)
{
if (bg_flags & mask) {
if (*first) {
fputs(" [", stdout);
*first = 0;
} else
fputs(", ", stdout);
fputs(str, stdout);
}
}
static void print_bg_opts(ext2_filsys fs, dgrp_t i)
{
int first = 1, bg_flags;
if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_LAZY_BG)
bg_flags = fs->group_desc[i].bg_flags;
else
bg_flags = 0;
print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "Inode not init",
&first);
print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "Block not init",
&first);
if (!first)
fputc(']', stdout);
fputc('\n', stdout);
}
static void list_desc (ext2_filsys fs)
{
unsigned long i;
long diff;
blk_t group_blk, next_blk;
blk_t super_blk, old_desc_blk, new_desc_blk;
char *block_bitmap=NULL, *inode_bitmap=NULL;
int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
int has_super;
if (fs->block_map)
block_bitmap = fs->block_map->bitmap;
if (fs->inode_map)
inode_bitmap = fs->inode_map->bitmap;
inode_blocks_per_group = ((fs->super->s_inodes_per_group *
EXT2_INODE_SIZE(fs->super)) +
EXT2_BLOCK_SIZE(fs->super) - 1) /
EXT2_BLOCK_SIZE(fs->super);
reserved_gdt = fs->super->s_reserved_gdt_blocks;
fputc('\n', stdout);
group_blk = fs->super->s_first_data_block;
if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
old_desc_blocks = fs->super->s_first_meta_bg;
else
old_desc_blocks = fs->desc_blocks;
for (i = 0; i < fs->group_desc_count; i++) {
ext2fs_super_and_bgd_loc(fs, i, &super_blk,
&old_desc_blk, &new_desc_blk, 0);
next_blk = group_blk + fs->super->s_blocks_per_group;
if (next_blk > fs->super->s_blocks_count)
next_blk = fs->super->s_blocks_count;
printf (_("Group %lu: (Blocks "), i);
print_range(group_blk, next_blk - 1);
fputs(")", stdout);
print_bg_opts(fs, i);
has_super = ((i==0) || super_blk);
if (has_super) {
printf (_(" %s superblock at "),
i == 0 ? _("Primary") : _("Backup"));
print_number(super_blk);
}
if (old_desc_blk) {
printf(_(", Group descriptors at "));
print_range(old_desc_blk,
old_desc_blk + old_desc_blocks - 1);
if (reserved_gdt) {
printf(_("\n Reserved GDT blocks at "));
print_range(old_desc_blk + old_desc_blocks,
old_desc_blk + old_desc_blocks +
reserved_gdt - 1);
}
} else if (new_desc_blk) {
fputc(has_super ? ',' : ' ', stdout);
printf(_(" Group descriptor at "));
print_number(new_desc_blk);
has_super++;
}
if (has_super)
fputc('\n', stdout);
fputs(_(" Block bitmap at "), stdout);
print_number(fs->group_desc[i].bg_block_bitmap);
diff = fs->group_desc[i].bg_block_bitmap - group_blk;
if (diff >= 0)
printf(" (+%ld)", diff);
fputs(_(", Inode bitmap at "), stdout);
print_number(fs->group_desc[i].bg_inode_bitmap);
diff = fs->group_desc[i].bg_inode_bitmap - group_blk;
if (diff >= 0)
printf(" (+%ld)", diff);
fputs(_("\n Inode table at "), stdout);
print_range(fs->group_desc[i].bg_inode_table,
fs->group_desc[i].bg_inode_table +
inode_blocks_per_group - 1);
diff = fs->group_desc[i].bg_inode_table - group_blk;
if (diff > 0)
printf(" (+%ld)", diff);
printf (_("\n %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);
if (block_bitmap) {
fputs(_(" Free blocks: "), stdout);
print_free (i, block_bitmap,
fs->super->s_blocks_per_group,
fs->super->s_first_data_block);
fputc('\n', stdout);
block_bitmap += fs->super->s_blocks_per_group / 8;
}
if (inode_bitmap) {
fputs(_(" Free inodes: "), stdout);
print_free (i, inode_bitmap,
fs->super->s_inodes_per_group, 1);
fputc('\n', stdout);
inode_bitmap += fs->super->s_inodes_per_group / 8;
}
group_blk = next_blk;
}
}
static void list_bad_blocks(ext2_filsys fs, int dump)
{
badblocks_list bb_list = 0;
badblocks_iterate bb_iter;
blk_t blk;
errcode_t retval;
const char *header, *fmt;
retval = ext2fs_read_bb_inode(fs, &bb_list);
if (retval) {
com_err("ext2fs_read_bb_inode", retval, 0);
return;
}
retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
if (retval) {
com_err("ext2fs_badblocks_list_iterate_begin", retval,
_("while printing bad block list"));
return;
}
if (dump) {
header = fmt = "%d\n";
} else {
header = _("Bad blocks: %d");
fmt = ", %d";
}
while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
printf(header ? header : fmt, blk);
header = 0;
}
ext2fs_badblocks_list_iterate_end(bb_iter);
if (!dump)
fputc('\n', stdout);
}
static void print_inline_journal_information(ext2_filsys fs)
{
struct ext2_inode inode;
errcode_t retval;
ino_t ino = fs->super->s_journal_inum;
int size;
retval = ext2fs_read_inode(fs, ino, &inode);
if (retval) {
com_err(program_name, retval,
_("while reading journal inode"));
exit(1);
}
fputs(_("Journal size: "), stdout);
size = inode.i_blocks >> 1;
if (size < 8192)
printf("%uk\n", size);
else
printf("%uM\n", size >> 10);
}
static void print_journal_information(ext2_filsys fs)
{
errcode_t retval;
char buf[1024];
char str[80];
unsigned int i;
journal_superblock_t *jsb;
/* Get the journal superblock */
if ((retval = io_channel_read_blk(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
com_err(program_name, retval,
_("while reading journal superblock"));
exit(1);
}
jsb = (journal_superblock_t *) buf;
if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
(jsb->s_header.h_blocktype !=
(unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
com_err(program_name, 0,
_("Couldn't find journal superblock magic numbers"));
exit(1);
}
printf(_("\nJournal block size: %u\n"
"Journal length: %u\n"
"Journal first block: %u\n"
"Journal sequence: 0x%08x\n"
"Journal start: %u\n"
"Journal number of users: %lu\n"),
ntohl(jsb->s_blocksize), ntohl(jsb->s_maxlen),
ntohl(jsb->s_first), ntohl(jsb->s_sequence),
ntohl(jsb->s_start), ntohl(jsb->s_nr_users));
for (i=0; i < ntohl(jsb->s_nr_users); i++) {
uuid_unparse(&jsb->s_users[i*16], str);
printf(i ? " %s\n"
: _("Journal users: %s\n"),
str);
}
}
int main (int argc, char ** argv)
{
errcode_t retval;
ext2_filsys fs;
int print_badblocks = 0;
int use_superblock = 0;
int use_blocksize = 0;
int image_dump = 0;
int force = 0;
int flags;
int header_only = 0;
int big_endian;
int c;
#ifdef ENABLE_NLS
setlocale(LC_MESSAGES, "");
setlocale(LC_CTYPE, "");
bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
textdomain(NLS_CAT_NAME);
#endif
initialize_ext2_error_table();
fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
E2FSPROGS_DATE);
if (argc && *argv)
program_name = *argv;
while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
switch (c) {
case 'b':
print_badblocks++;
break;
case 'f':
force++;
break;
case 'h':
header_only++;
break;
case 'i':
image_dump++;
break;
case 'o':
if (optarg[0] == 'b')
use_superblock = atoi(optarg+1);
else if (optarg[0] == 'B')
use_blocksize = atoi(optarg+1);
else
usage();
break;
case 'V':
/* Print version number and exit */
fprintf(stderr, _("\tUsing %s\n"),
error_message(EXT2_ET_BASE));
exit(0);
case 'x':
hex_format++;
break;
default:
usage();
}
}
if (optind > argc - 1)
usage();
device_name = argv[optind++];
if (use_superblock && !use_blocksize)
use_blocksize = 1024;
flags = EXT2_FLAG_JOURNAL_DEV_OK;
if (force)
flags |= EXT2_FLAG_FORCE;
if (image_dump)
flags |= EXT2_FLAG_IMAGE_FILE;
retval = ext2fs_open (device_name, flags, use_superblock,
use_blocksize, 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) {
list_bad_blocks(fs, 1);
} else {
big_endian = ((fs->flags & EXT2_FLAG_SWAP_BYTES) != 0);
#ifdef WORDS_BIGENDIAN
big_endian = !big_endian;
#endif
if (big_endian)
printf(_("Note: This is a byte-swapped filesystem\n"));
list_super (fs->super);
if (fs->super->s_feature_incompat &
EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
print_journal_information(fs);
ext2fs_close(fs);
exit(0);
}
if (fs->super->s_feature_compat &
EXT3_FEATURE_COMPAT_HAS_JOURNAL)
print_inline_journal_information(fs);
list_bad_blocks(fs, 0);
if (header_only) {
ext2fs_close (fs);
exit (0);
}
retval = ext2fs_read_bitmaps (fs);
list_desc (fs);
if (retval) {
printf(_("\n%s: %s: error reading bitmaps: %s\n"),
program_name, device_name,
error_message(retval));
}
}
ext2fs_close (fs);
exit (0);
}
|