]>
Commit | Line | Data |
---|---|---|
3839e657 TT |
1 | /* |
2 | * dumpe2fs.c - List the control structures of a second | |
3 | * extended filesystem | |
4 | * | |
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) | |
8 | * | |
19c78dc0 TT |
9 | * Copyright 1995, 1996, 1997 by Theodore Ts'o. |
10 | * | |
11 | * %Begin-Header% | |
12 | * This file may be redistributed under the terms of the GNU Public | |
13 | * License. | |
14 | * %End-Header% | |
3839e657 TT |
15 | */ |
16 | ||
17 | /* | |
18 | * History: | |
19 | * 94/01/09 - Creation | |
20 | * 94/02/27 - Ported to use the ext2fs library | |
21 | */ | |
22 | ||
d1154eb4 | 23 | #include "config.h" |
a418d3ad | 24 | #ifdef HAVE_GETOPT_H |
3839e657 | 25 | #include <getopt.h> |
373b8337 TT |
26 | #else |
27 | extern char *optarg; | |
28 | extern int optind; | |
a418d3ad | 29 | #endif |
3839e657 TT |
30 | #include <fcntl.h> |
31 | #include <stdio.h> | |
32 | #include <stdlib.h> | |
33 | #include <string.h> | |
34 | #include <unistd.h> | |
35 | ||
54c637d4 | 36 | #include "ext2fs/ext2_fs.h" |
3839e657 TT |
37 | |
38 | #include "ext2fs/ext2fs.h" | |
39 | #include "e2p/e2p.h" | |
16ed5b3a | 40 | #include "jfs_user.h" |
4ea7bd04 | 41 | #include <uuid/uuid.h> |
3839e657 TT |
42 | |
43 | #include "../version.h" | |
d9c56d3c | 44 | #include "nls-enable.h" |
3839e657 | 45 | |
74becf3c | 46 | #define in_use(m, x) (ext2fs_test_bit ((x), (m))) |
3839e657 TT |
47 | |
48 | const char * program_name = "dumpe2fs"; | |
49 | char * device_name = NULL; | |
9b9a780f | 50 | int hex_format = 0; |
ce10c313 | 51 | int blocks64 = 0; |
3839e657 | 52 | |
818180cd | 53 | static void usage(void) |
3839e657 | 54 | { |
77f464fb TT |
55 | fprintf (stderr, _("Usage: %s [-bfhixV] [-o superblock=<num>] " |
56 | "[-o blocksize=<num>] device\n"), program_name); | |
3839e657 TT |
57 | exit (1); |
58 | } | |
59 | ||
ce10c313 | 60 | static void print_number(unsigned long long num) |
54434927 | 61 | { |
ce10c313 TT |
62 | if (hex_format) { |
63 | if (blocks64) | |
64 | printf("0x%08llx", num); | |
65 | else | |
66 | printf("0x%04llx", num); | |
67 | } else | |
68 | printf("%llu", num); | |
9b9a780f TT |
69 | } |
70 | ||
295c3e03 | 71 | static void print_range(unsigned long long a, unsigned long long b) |
9b9a780f | 72 | { |
ce10c313 TT |
73 | if (hex_format) { |
74 | if (blocks64) | |
75 | printf("0x%08llx-0x%08llx", a, b); | |
76 | else | |
77 | printf("0x%04llx-0x%04llx", a, b); | |
78 | } else | |
295c3e03 | 79 | printf("%llu-%llu", a, b); |
54434927 TT |
80 | } |
81 | ||
bcb942c2 TT |
82 | static void print_free(unsigned long group, char * bitmap, |
83 | unsigned long num, unsigned long offset, int ratio) | |
3839e657 TT |
84 | { |
85 | int p = 0; | |
86 | unsigned long i; | |
87 | unsigned long j; | |
88 | ||
bcb942c2 TT |
89 | offset /= ratio; |
90 | offset += group * num; | |
91 | for (i = 0; i < num; i++) | |
3839e657 TT |
92 | if (!in_use (bitmap, i)) |
93 | { | |
94 | if (p) | |
95 | printf (", "); | |
bcb942c2 TT |
96 | print_number((i + offset) * ratio); |
97 | for (j = i; j < num && !in_use (bitmap, j); j++) | |
a5f0bb9d TT |
98 | ; |
99 | if (--j != i) { | |
100 | fputc('-', stdout); | |
bcb942c2 | 101 | print_number((j + offset) * ratio); |
a5f0bb9d | 102 | i = j; |
3839e657 TT |
103 | } |
104 | p = 1; | |
105 | } | |
106 | } | |
107 | ||
f5fa2007 TT |
108 | static void print_bg_opt(int bg_flags, int mask, |
109 | const char *str, int *first) | |
110 | { | |
111 | if (bg_flags & mask) { | |
112 | if (*first) { | |
113 | fputs(" [", stdout); | |
114 | *first = 0; | |
115 | } else | |
116 | fputs(", ", stdout); | |
117 | fputs(str, stdout); | |
118 | } | |
119 | } | |
120 | static void print_bg_opts(ext2_filsys fs, dgrp_t i) | |
121 | { | |
16b851cd | 122 | int first = 1, bg_flags = 0; |
f5fa2007 | 123 | |
16b851cd | 124 | if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) |
732c8cd5 | 125 | bg_flags = ext2fs_bg_flags(fs, i); |
f5fa2007 | 126 | |
b89fc30d | 127 | print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT", |
f5fa2007 | 128 | &first); |
b89fc30d TT |
129 | print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT", |
130 | &first); | |
131 | print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED", | |
f5fa2007 TT |
132 | &first); |
133 | if (!first) | |
134 | fputc(']', stdout); | |
135 | fputc('\n', stdout); | |
136 | } | |
137 | ||
0e2afdba AD |
138 | static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable, |
139 | blk64_t first_block, blk64_t last_block) | |
140 | { | |
141 | if ((block >= first_block) && (block <= last_block)) { | |
142 | if (itable && block == first_block) | |
143 | return; | |
144 | printf(" (+%u)", (unsigned)(block - first_block)); | |
145 | } else if (fs->super->s_feature_incompat & | |
146 | EXT4_FEATURE_INCOMPAT_FLEX_BG) { | |
4dbfd79d | 147 | dgrp_t flex_grp = ext2fs_group_of_blk2(fs, block); |
0e2afdba | 148 | printf(" (bg #%u + %u)", flex_grp, |
26da6614 | 149 | (unsigned)(block-ext2fs_group_first_block2(fs,flex_grp))); |
0e2afdba AD |
150 | } |
151 | } | |
152 | ||
3839e657 TT |
153 | static void list_desc (ext2_filsys fs) |
154 | { | |
155 | unsigned long i; | |
295c3e03 TT |
156 | blk64_t first_block, last_block; |
157 | blk64_t super_blk, old_desc_blk, new_desc_blk; | |
d90a23e2 | 158 | char *block_bitmap=NULL, *inode_bitmap=NULL; |
2418dfd7 | 159 | const char *units = _("blocks"); |
35238dd1 | 160 | int inode_blocks_per_group, old_desc_blocks, reserved_gdt; |
f1f115a7 | 161 | int block_nbytes, inode_nbytes; |
ef344e13 | 162 | int has_super; |
6a6337c3 | 163 | blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block); |
f1f115a7 | 164 | ext2_ino_t ino_itr = 1; |
6908c0c3 | 165 | errcode_t retval; |
f1f115a7 | 166 | |
2418dfd7 TT |
167 | if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
168 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) | |
169 | units = _("clusters"); | |
170 | ||
bcb942c2 | 171 | block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8; |
f1f115a7 | 172 | inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8; |
80c22c90 | 173 | |
d90a23e2 | 174 | if (fs->block_map) |
f1f115a7 | 175 | block_bitmap = malloc(block_nbytes); |
d90a23e2 | 176 | if (fs->inode_map) |
f1f115a7 | 177 | inode_bitmap = malloc(inode_nbytes); |
d90a23e2 | 178 | |
80c22c90 TT |
179 | inode_blocks_per_group = ((fs->super->s_inodes_per_group * |
180 | EXT2_INODE_SIZE(fs->super)) + | |
181 | EXT2_BLOCK_SIZE(fs->super) - 1) / | |
182 | EXT2_BLOCK_SIZE(fs->super); | |
35238dd1 | 183 | reserved_gdt = fs->super->s_reserved_gdt_blocks; |
a5f0bb9d | 184 | fputc('\n', stdout); |
bb1a46a4 | 185 | first_block = fs->super->s_first_data_block; |
76dd5e5c TT |
186 | if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) |
187 | old_desc_blocks = fs->super->s_first_meta_bg; | |
188 | else | |
189 | old_desc_blocks = fs->desc_blocks; | |
521e3685 | 190 | for (i = 0; i < fs->group_desc_count; i++) { |
b49f78fe TT |
191 | first_block = ext2fs_group_first_block2(fs, i); |
192 | last_block = ext2fs_group_last_block2(fs, i); | |
abf23439 | 193 | |
295c3e03 TT |
194 | ext2fs_super_and_bgd_loc2(fs, i, &super_blk, |
195 | &old_desc_blk, &new_desc_blk, 0); | |
bb1a46a4 | 196 | |
a5f0bb9d | 197 | printf (_("Group %lu: (Blocks "), i); |
bb1a46a4 | 198 | print_range(first_block, last_block); |
f5fa2007 TT |
199 | fputs(")", stdout); |
200 | print_bg_opts(fs, i); | |
87141781 TT |
201 | if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) { |
202 | unsigned csum = ext2fs_bg_checksum(fs, i); | |
203 | unsigned exp_csum = ext2fs_group_desc_csum(fs, i); | |
204 | ||
205 | printf(_(" Checksum 0x%04x"), csum); | |
206 | if (csum != exp_csum) | |
207 | printf(_(" (EXPECTED 0x%04x)"), exp_csum); | |
208 | printf(_(", unused inodes %u\n"), | |
d7cca6b0 | 209 | ext2fs_bg_itable_unused(fs, i)); |
87141781 | 210 | } |
ef344e13 | 211 | has_super = ((i==0) || super_blk); |
c046ac7f TT |
212 | if (has_super) { |
213 | printf (_(" %s superblock at "), | |
a5f0bb9d | 214 | i == 0 ? _("Primary") : _("Backup")); |
54434927 | 215 | print_number(super_blk); |
c046ac7f | 216 | } |
ef344e13 | 217 | if (old_desc_blk) { |
45ff69ff | 218 | printf("%s", _(", Group descriptors at ")); |
efc6f628 | 219 | print_range(old_desc_blk, |
9b9a780f | 220 | old_desc_blk + old_desc_blocks - 1); |
35238dd1 | 221 | if (reserved_gdt) { |
45ff69ff | 222 | printf("%s", _("\n Reserved GDT blocks at ")); |
9b9a780f | 223 | print_range(old_desc_blk + old_desc_blocks, |
efc6f628 | 224 | old_desc_blk + old_desc_blocks + |
9b9a780f | 225 | reserved_gdt - 1); |
35238dd1 | 226 | } |
ef344e13 TT |
227 | } else if (new_desc_blk) { |
228 | fputc(has_super ? ',' : ' ', stdout); | |
45ff69ff | 229 | printf("%s", _(" Group descriptor at ")); |
54434927 | 230 | print_number(new_desc_blk); |
ef344e13 | 231 | has_super++; |
a5f0bb9d | 232 | } |
ef344e13 TT |
233 | if (has_super) |
234 | fputc('\n', stdout); | |
a5f0bb9d | 235 | fputs(_(" Block bitmap at "), stdout); |
d7cca6b0 | 236 | print_number(ext2fs_block_bitmap_loc(fs, i)); |
0358c9f9 | 237 | print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0, |
0e2afdba | 238 | first_block, last_block); |
a5f0bb9d | 239 | fputs(_(", Inode bitmap at "), stdout); |
d7cca6b0 | 240 | print_number(ext2fs_inode_bitmap_loc(fs, i)); |
0358c9f9 | 241 | print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0, |
0e2afdba | 242 | first_block, last_block); |
a5f0bb9d | 243 | fputs(_("\n Inode table at "), stdout); |
d7cca6b0 VAH |
244 | print_range(ext2fs_inode_table_loc(fs, i), |
245 | ext2fs_inode_table_loc(fs, i) + | |
9b9a780f | 246 | inode_blocks_per_group - 1); |
0358c9f9 | 247 | print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1, |
0e2afdba | 248 | first_block, last_block); |
2418dfd7 | 249 | printf (_("\n %u free %s, %u free inodes, " |
777a8c1b | 250 | "%u directories%s"), |
2418dfd7 | 251 | ext2fs_bg_free_blocks_count(fs, i), units, |
d7cca6b0 VAH |
252 | ext2fs_bg_free_inodes_count(fs, i), |
253 | ext2fs_bg_used_dirs_count(fs, i), | |
254 | ext2fs_bg_itable_unused(fs, i) ? "" : "\n"); | |
255 | if (ext2fs_bg_itable_unused(fs, i)) | |
777a8c1b | 256 | printf (_(", %u unused inodes\n"), |
d7cca6b0 | 257 | ext2fs_bg_itable_unused(fs, i)); |
d90a23e2 TT |
258 | if (block_bitmap) { |
259 | fputs(_(" Free blocks: "), stdout); | |
6908c0c3 | 260 | retval = ext2fs_get_block_bitmap_range2(fs->block_map, |
f1f115a7 | 261 | blk_itr, block_nbytes << 3, block_bitmap); |
6908c0c3 DW |
262 | if (retval) |
263 | com_err("list_desc", retval, | |
264 | "while reading block bitmap"); | |
265 | else | |
266 | print_free(i, block_bitmap, | |
267 | fs->super->s_clusters_per_group, | |
268 | fs->super->s_first_data_block, | |
269 | EXT2FS_CLUSTER_RATIO(fs)); | |
d90a23e2 | 270 | fputc('\n', stdout); |
bcb942c2 | 271 | blk_itr += fs->super->s_clusters_per_group; |
d90a23e2 TT |
272 | } |
273 | if (inode_bitmap) { | |
274 | fputs(_(" Free inodes: "), stdout); | |
6908c0c3 | 275 | retval = ext2fs_get_inode_bitmap_range2(fs->inode_map, |
f1f115a7 | 276 | ino_itr, inode_nbytes << 3, inode_bitmap); |
6908c0c3 DW |
277 | if (retval) |
278 | com_err("list_desc", retval, | |
279 | "while reading inode bitmap"); | |
280 | else | |
281 | print_free(i, inode_bitmap, | |
282 | fs->super->s_inodes_per_group, | |
283 | 1, 1); | |
d90a23e2 | 284 | fputc('\n', stdout); |
f1f115a7 | 285 | ino_itr += fs->super->s_inodes_per_group; |
d90a23e2 | 286 | } |
3839e657 | 287 | } |
1acde2b2 TT |
288 | if (block_bitmap) |
289 | free(block_bitmap); | |
290 | if (inode_bitmap) | |
291 | free(inode_bitmap); | |
3839e657 TT |
292 | } |
293 | ||
0655b104 | 294 | static void list_bad_blocks(ext2_filsys fs, int dump) |
3839e657 TT |
295 | { |
296 | badblocks_list bb_list = 0; | |
297 | badblocks_iterate bb_iter; | |
298 | blk_t blk; | |
299 | errcode_t retval; | |
0655b104 | 300 | const char *header, *fmt; |
3839e657 TT |
301 | |
302 | retval = ext2fs_read_bb_inode(fs, &bb_list); | |
303 | if (retval) { | |
9b9a780f | 304 | com_err("ext2fs_read_bb_inode", retval, 0); |
0655b104 | 305 | return; |
3839e657 | 306 | } |
cbbf031b | 307 | retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter); |
3839e657 | 308 | if (retval) { |
cbbf031b | 309 | com_err("ext2fs_badblocks_list_iterate_begin", retval, |
45ff69ff | 310 | "%s", _("while printing bad block list")); |
0655b104 | 311 | return; |
3839e657 | 312 | } |
0655b104 | 313 | if (dump) { |
d0ff90d5 | 314 | header = fmt = "%u\n"; |
0655b104 | 315 | } else { |
d0ff90d5 ES |
316 | header = _("Bad blocks: %u"); |
317 | fmt = ", %u"; | |
f3db3566 | 318 | } |
0655b104 TT |
319 | while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) { |
320 | printf(header ? header : fmt, blk); | |
321 | header = 0; | |
f3db3566 | 322 | } |
cbbf031b | 323 | ext2fs_badblocks_list_iterate_end(bb_iter); |
0655b104 TT |
324 | if (!dump) |
325 | fputc('\n', stdout); | |
1acde2b2 | 326 | ext2fs_badblocks_list_free(bb_list); |
1e3472c5 | 327 | } |
f3db3566 | 328 | |
6515a6f1 TT |
329 | static void print_inline_journal_information(ext2_filsys fs) |
330 | { | |
1d9b8183 | 331 | journal_superblock_t *jsb; |
6515a6f1 | 332 | struct ext2_inode inode; |
1d9b8183 | 333 | ext2_file_t journal_file; |
6515a6f1 TT |
334 | errcode_t retval; |
335 | ino_t ino = fs->super->s_journal_inum; | |
1d9b8183 TT |
336 | char buf[1024]; |
337 | __u32 *mask_ptr, mask, m; | |
338 | int i, j, size, printed = 0; | |
efc6f628 | 339 | |
6515a6f1 TT |
340 | retval = ext2fs_read_inode(fs, ino, &inode); |
341 | if (retval) { | |
45ff69ff | 342 | com_err(program_name, retval, "%s", |
6515a6f1 TT |
343 | _("while reading journal inode")); |
344 | exit(1); | |
345 | } | |
1d9b8183 TT |
346 | retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file); |
347 | if (retval) { | |
45ff69ff | 348 | com_err(program_name, retval, "%s", |
1d9b8183 TT |
349 | _("while opening journal inode")); |
350 | exit(1); | |
351 | } | |
352 | retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0); | |
353 | if (retval) { | |
45ff69ff | 354 | com_err(program_name, retval, "%s", |
1d9b8183 TT |
355 | _("while reading journal super block")); |
356 | exit(1); | |
357 | } | |
358 | ext2fs_file_close(journal_file); | |
359 | jsb = (journal_superblock_t *) buf; | |
360 | if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) { | |
45ff69ff AD |
361 | fprintf(stderr, "%s", |
362 | _("Journal superblock magic number invalid!\n")); | |
1d9b8183 TT |
363 | exit(1); |
364 | } | |
45ff69ff | 365 | printf("%s", _("Journal features: ")); |
1d9b8183 TT |
366 | for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) { |
367 | mask = be32_to_cpu(*mask_ptr); | |
368 | for (j=0,m=1; j < 32; j++, m<<=1) { | |
369 | if (mask & m) { | |
370 | printf(" %s", e2p_jrnl_feature2string(i, m)); | |
371 | printed++; | |
372 | } | |
373 | } | |
374 | } | |
375 | if (printed == 0) | |
376 | printf(" (none)"); | |
377 | printf("\n"); | |
6515a6f1 | 378 | fputs(_("Journal size: "), stdout); |
1ca1059f TT |
379 | if ((fs->super->s_feature_ro_compat & |
380 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE) && | |
381 | (inode.i_flags & EXT4_HUGE_FILE_FL)) | |
382 | size = inode.i_blocks / (fs->blocksize / 1024); | |
383 | else | |
384 | size = inode.i_blocks >> 1; | |
6515a6f1 TT |
385 | if (size < 8192) |
386 | printf("%uk\n", size); | |
387 | else | |
388 | printf("%uM\n", size >> 10); | |
1d9b8183 TT |
389 | printf(_("Journal length: %u\n" |
390 | "Journal sequence: 0x%08x\n" | |
391 | "Journal start: %u\n"), | |
392 | (unsigned int)ntohl(jsb->s_maxlen), | |
393 | (unsigned int)ntohl(jsb->s_sequence), | |
394 | (unsigned int)ntohl(jsb->s_start)); | |
9906409e TT |
395 | if (jsb->s_errno != 0) |
396 | printf(_("Journal errno: %d\n"), | |
397 | (int) ntohl(jsb->s_errno)); | |
6515a6f1 TT |
398 | } |
399 | ||
16ed5b3a TT |
400 | static void print_journal_information(ext2_filsys fs) |
401 | { | |
402 | errcode_t retval; | |
403 | char buf[1024]; | |
404 | char str[80]; | |
54434927 | 405 | unsigned int i; |
16ed5b3a TT |
406 | journal_superblock_t *jsb; |
407 | ||
408 | /* Get the journal superblock */ | |
45ff69ff AD |
409 | if ((retval = io_channel_read_blk64(fs->io, |
410 | fs->super->s_first_data_block + 1, | |
411 | -1024, buf))) { | |
412 | com_err(program_name, retval, "%s", | |
16ed5b3a TT |
413 | _("while reading journal superblock")); |
414 | exit(1); | |
415 | } | |
416 | jsb = (journal_superblock_t *) buf; | |
417 | if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) || | |
418 | (jsb->s_header.h_blocktype != | |
419 | (unsigned) ntohl(JFS_SUPERBLOCK_V2))) { | |
45ff69ff | 420 | com_err(program_name, 0, "%s", |
16ed5b3a TT |
421 | _("Couldn't find journal superblock magic numbers")); |
422 | exit(1); | |
423 | } | |
424 | ||
8deb80a5 TS |
425 | printf(_("\nJournal block size: %u\n" |
426 | "Journal length: %u\n" | |
427 | "Journal first block: %u\n" | |
a5f0bb9d | 428 | "Journal sequence: 0x%08x\n" |
8deb80a5 | 429 | "Journal start: %u\n" |
f6567a88 MA |
430 | "Journal number of users: %u\n"), |
431 | (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen), | |
432 | (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence), | |
433 | (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users)); | |
a5f0bb9d | 434 | |
16ed5b3a | 435 | for (i=0; i < ntohl(jsb->s_nr_users); i++) { |
16ed5b3a | 436 | uuid_unparse(&jsb->s_users[i*16], str); |
a5f0bb9d | 437 | printf(i ? " %s\n" |
bb145b01 | 438 | : _("Journal users: %s\n"), |
a5f0bb9d | 439 | str); |
16ed5b3a TT |
440 | } |
441 | } | |
442 | ||
295c3e03 | 443 | static void parse_extended_opts(const char *opts, blk64_t *superblock, |
db197a81 TT |
444 | int *blocksize) |
445 | { | |
2d328bb7 | 446 | char *buf, *token, *next, *p, *arg, *badopt = 0; |
db197a81 | 447 | int len; |
2d328bb7 | 448 | int do_usage = 0; |
db197a81 TT |
449 | |
450 | len = strlen(opts); | |
451 | buf = malloc(len+1); | |
452 | if (!buf) { | |
45ff69ff | 453 | fprintf(stderr, "%s", |
db197a81 TT |
454 | _("Couldn't allocate memory to parse options!\n")); |
455 | exit(1); | |
456 | } | |
457 | strcpy(buf, opts); | |
458 | for (token = buf; token && *token; token = next) { | |
459 | p = strchr(token, ','); | |
460 | next = 0; | |
461 | if (p) { | |
462 | *p = 0; | |
463 | next = p+1; | |
464 | } | |
465 | arg = strchr(token, '='); | |
466 | if (arg) { | |
467 | *arg = 0; | |
468 | arg++; | |
469 | } | |
470 | if (strcmp(token, "superblock") == 0 || | |
471 | strcmp(token, "sb") == 0) { | |
472 | if (!arg) { | |
2d328bb7 | 473 | do_usage++; |
db197a81 TT |
474 | badopt = token; |
475 | continue; | |
476 | } | |
477 | *superblock = strtoul(arg, &p, 0); | |
478 | if (*p) { | |
479 | fprintf(stderr, | |
480 | _("Invalid superblock parameter: %s\n"), | |
481 | arg); | |
2d328bb7 | 482 | do_usage++; |
db197a81 TT |
483 | continue; |
484 | } | |
485 | } else if (strcmp(token, "blocksize") == 0 || | |
486 | strcmp(token, "bs") == 0) { | |
487 | if (!arg) { | |
2d328bb7 | 488 | do_usage++; |
db197a81 TT |
489 | badopt = token; |
490 | continue; | |
491 | } | |
492 | *blocksize = strtoul(arg, &p, 0); | |
493 | if (*p) { | |
494 | fprintf(stderr, | |
495 | _("Invalid blocksize parameter: %s\n"), | |
496 | arg); | |
2d328bb7 | 497 | do_usage++; |
db197a81 TT |
498 | continue; |
499 | } | |
500 | } else { | |
2d328bb7 | 501 | do_usage++; |
db197a81 TT |
502 | badopt = token; |
503 | } | |
504 | } | |
2d328bb7 | 505 | if (do_usage) { |
db197a81 TT |
506 | fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n" |
507 | "Extended options are separated by commas, " | |
508 | "and may take an argument which\n" | |
509 | "\tis set off by an equals ('=') sign.\n\n" | |
510 | "Valid extended options are:\n" | |
511 | "\tsuperblock=<superblock number>\n" | |
512 | "\tblocksize=<blocksize>\n"), | |
2d328bb7 | 513 | badopt ? badopt : ""); |
db197a81 TT |
514 | free(buf); |
515 | exit(1); | |
516 | } | |
517 | free(buf); | |
efc6f628 | 518 | } |
db197a81 | 519 | |
00e5433e | 520 | int main (int argc, char ** argv) |
3839e657 TT |
521 | { |
522 | errcode_t retval; | |
523 | ext2_filsys fs; | |
f3db3566 | 524 | int print_badblocks = 0; |
295c3e03 | 525 | blk64_t use_superblock = 0; |
02e7dd9a | 526 | int use_blocksize = 0; |
348e43dc | 527 | int image_dump = 0; |
2740156b | 528 | int force = 0; |
16ed5b3a | 529 | int flags; |
2740156b | 530 | int header_only = 0; |
519149fb | 531 | int c; |
3839e657 | 532 | |
d9c56d3c TT |
533 | #ifdef ENABLE_NLS |
534 | setlocale(LC_MESSAGES, ""); | |
14308a53 | 535 | setlocale(LC_CTYPE, ""); |
d9c56d3c TT |
536 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
537 | textdomain(NLS_CAT_NAME); | |
9d4507c5 | 538 | set_com_err_gettext(gettext); |
d9c56d3c | 539 | #endif |
a6d8302b | 540 | add_error_table(&et_ext2_error_table); |
0f8973fb TT |
541 | fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION, |
542 | E2FSPROGS_DATE); | |
3839e657 TT |
543 | if (argc && *argv) |
544 | program_name = *argv; | |
efc6f628 | 545 | |
348e43dc | 546 | while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) { |
f3db3566 TT |
547 | switch (c) { |
548 | case 'b': | |
549 | print_badblocks++; | |
550 | break; | |
2740156b TT |
551 | case 'f': |
552 | force++; | |
553 | break; | |
554 | case 'h': | |
555 | header_only++; | |
556 | break; | |
348e43dc TT |
557 | case 'i': |
558 | image_dump++; | |
559 | break; | |
02e7dd9a | 560 | case 'o': |
efc6f628 | 561 | parse_extended_opts(optarg, &use_superblock, |
db197a81 | 562 | &use_blocksize); |
02e7dd9a | 563 | break; |
5c576477 TT |
564 | case 'V': |
565 | /* Print version number and exit */ | |
d9c56d3c | 566 | fprintf(stderr, _("\tUsing %s\n"), |
5c576477 TT |
567 | error_message(EXT2_ET_BASE)); |
568 | exit(0); | |
80c22c90 | 569 | case 'x': |
9b9a780f | 570 | hex_format++; |
80c22c90 | 571 | break; |
f3db3566 | 572 | default: |
818180cd | 573 | usage(); |
f3db3566 TT |
574 | } |
575 | } | |
576 | if (optind > argc - 1) | |
818180cd | 577 | usage(); |
f3db3566 | 578 | device_name = argv[optind++]; |
295c3e03 | 579 | flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; |
16ed5b3a TT |
580 | if (force) |
581 | flags |= EXT2_FLAG_FORCE; | |
348e43dc TT |
582 | if (image_dump) |
583 | flags |= EXT2_FLAG_IMAGE_FILE; | |
efc6f628 | 584 | |
db197a81 TT |
585 | if (use_superblock && !use_blocksize) { |
586 | for (use_blocksize = EXT2_MIN_BLOCK_SIZE; | |
587 | use_blocksize <= EXT2_MAX_BLOCK_SIZE; | |
588 | use_blocksize *= 2) { | |
589 | retval = ext2fs_open (device_name, flags, | |
590 | use_superblock, | |
591 | use_blocksize, unix_io_manager, | |
592 | &fs); | |
593 | if (!retval) | |
594 | break; | |
595 | } | |
596 | } else | |
597 | retval = ext2fs_open (device_name, flags, use_superblock, | |
598 | use_blocksize, unix_io_manager, &fs); | |
f3db3566 | 599 | if (retval) { |
d9c56d3c | 600 | com_err (program_name, retval, _("while trying to open %s"), |
3839e657 | 601 | device_name); |
45ff69ff | 602 | printf("%s", _("Couldn't find valid filesystem superblock.\n")); |
3839e657 TT |
603 | exit (1); |
604 | } | |
24dea554 | 605 | fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; |
ce10c313 TT |
606 | if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) |
607 | blocks64 = 1; | |
f3db3566 | 608 | if (print_badblocks) { |
0655b104 | 609 | list_bad_blocks(fs, 1); |
f3db3566 | 610 | } else { |
2740156b | 611 | list_super (fs->super); |
16ed5b3a TT |
612 | if (fs->super->s_feature_incompat & |
613 | EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { | |
614 | print_journal_information(fs); | |
615 | ext2fs_close(fs); | |
616 | exit(0); | |
617 | } | |
a11d0746 TT |
618 | if ((fs->super->s_feature_compat & |
619 | EXT3_FEATURE_COMPAT_HAS_JOURNAL) && | |
620 | (fs->super->s_journal_inum != 0)) | |
6515a6f1 | 621 | print_inline_journal_information(fs); |
0655b104 | 622 | list_bad_blocks(fs, 0); |
2740156b TT |
623 | if (header_only) { |
624 | ext2fs_close (fs); | |
625 | exit (0); | |
626 | } | |
f3db3566 | 627 | retval = ext2fs_read_bitmaps (fs); |
d90a23e2 | 628 | list_desc (fs); |
f3db3566 | 629 | if (retval) { |
d90a23e2 TT |
630 | printf(_("\n%s: %s: error reading bitmaps: %s\n"), |
631 | program_name, device_name, | |
632 | error_message(retval)); | |
f3db3566 | 633 | } |
3839e657 | 634 | } |
3839e657 | 635 | ext2fs_close (fs); |
a6d8302b | 636 | remove_error_table(&et_ext2_error_table); |
3839e657 TT |
637 | exit (0); |
638 | } |