dumpe2fs.c revision 1acde2b2776fca4d9386a04909296c2e6b11f242
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 * 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% 15 */ 16 17/* 18 * History: 19 * 94/01/09 - Creation 20 * 94/02/27 - Ported to use the ext2fs library 21 */ 22 23#ifdef HAVE_GETOPT_H 24#include <getopt.h> 25#else 26extern char *optarg; 27extern int optind; 28#endif 29#include <fcntl.h> 30#include <stdio.h> 31#include <stdlib.h> 32#include <string.h> 33#include <unistd.h> 34 35#include "ext2fs/ext2_fs.h" 36 37#include "ext2fs/ext2fs.h" 38#include "e2p/e2p.h" 39#include "jfs_user.h" 40#include <uuid/uuid.h> 41 42#include "../version.h" 43#include "nls-enable.h" 44 45#define in_use(m, x) (ext2fs_test_bit ((x), (m))) 46 47const char * program_name = "dumpe2fs"; 48char * device_name = NULL; 49int hex_format = 0; 50 51static void usage(void) 52{ 53 fprintf (stderr, _("Usage: %s [-bfhixV] [-ob superblock] " 54 "[-oB blocksize] device\n"), program_name); 55 exit (1); 56} 57 58static void print_number(unsigned long num) 59{ 60 if (hex_format) 61 printf("0x%04lx", num); 62 else 63 printf("%lu", num); 64} 65 66static void print_range(unsigned long a, unsigned long b) 67{ 68 if (hex_format) 69 printf("0x%04lx-0x%04lx", a, b); 70 else 71 printf("%lu-%lu", a, b); 72} 73 74static void print_free (unsigned long group, char * bitmap, 75 unsigned long nbytes, unsigned long offset) 76{ 77 int p = 0; 78 unsigned long i; 79 unsigned long j; 80 81 offset += group * nbytes; 82 for (i = 0; i < nbytes; i++) 83 if (!in_use (bitmap, i)) 84 { 85 if (p) 86 printf (", "); 87 print_number(i + offset); 88 for (j = i; j < nbytes && !in_use (bitmap, j); j++) 89 ; 90 if (--j != i) { 91 fputc('-', stdout); 92 print_number(j + offset); 93 i = j; 94 } 95 p = 1; 96 } 97} 98 99static void print_bg_opt(int bg_flags, int mask, 100 const char *str, int *first) 101{ 102 if (bg_flags & mask) { 103 if (*first) { 104 fputs(" [", stdout); 105 *first = 0; 106 } else 107 fputs(", ", stdout); 108 fputs(str, stdout); 109 } 110} 111static void print_bg_opts(ext2_filsys fs, dgrp_t i) 112{ 113 int first = 1, bg_flags = 0; 114 115 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) 116 bg_flags = fs->group_desc[i].bg_flags; 117 118 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT", 119 &first); 120 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT", 121 &first); 122 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED", 123 &first); 124 if (!first) 125 fputc(']', stdout); 126 fputc('\n', stdout); 127} 128 129static void list_desc (ext2_filsys fs) 130{ 131 unsigned long i; 132 long diff; 133 blk_t first_block, last_block; 134 blk_t super_blk, old_desc_blk, new_desc_blk; 135 char *block_bitmap=NULL, *inode_bitmap=NULL; 136 int inode_blocks_per_group, old_desc_blocks, reserved_gdt; 137 int block_nbytes, inode_nbytes; 138 int has_super; 139 blk_t blk_itr = fs->super->s_first_data_block; 140 ext2_ino_t ino_itr = 1; 141 142 block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8; 143 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8; 144 145 if (fs->block_map) 146 block_bitmap = malloc(block_nbytes); 147 if (fs->inode_map) 148 inode_bitmap = malloc(inode_nbytes); 149 150 inode_blocks_per_group = ((fs->super->s_inodes_per_group * 151 EXT2_INODE_SIZE(fs->super)) + 152 EXT2_BLOCK_SIZE(fs->super) - 1) / 153 EXT2_BLOCK_SIZE(fs->super); 154 reserved_gdt = fs->super->s_reserved_gdt_blocks; 155 fputc('\n', stdout); 156 first_block = fs->super->s_first_data_block; 157 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) 158 old_desc_blocks = fs->super->s_first_meta_bg; 159 else 160 old_desc_blocks = fs->desc_blocks; 161 for (i = 0; i < fs->group_desc_count; i++) { 162 first_block = ext2fs_group_first_block(fs, i); 163 last_block = ext2fs_group_last_block(fs, i); 164 165 ext2fs_super_and_bgd_loc(fs, i, &super_blk, 166 &old_desc_blk, &new_desc_blk, 0); 167 168 printf (_("Group %lu: (Blocks "), i); 169 print_range(first_block, last_block); 170 fputs(")", stdout); 171 print_bg_opts(fs, i); 172 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) 173 printf(_(" Checksum 0x%04x, unused inodes %d\n"), 174 fs->group_desc[i].bg_checksum, 175 fs->group_desc[i].bg_itable_unused); 176 has_super = ((i==0) || super_blk); 177 if (has_super) { 178 printf (_(" %s superblock at "), 179 i == 0 ? _("Primary") : _("Backup")); 180 print_number(super_blk); 181 } 182 if (old_desc_blk) { 183 printf(_(", Group descriptors at ")); 184 print_range(old_desc_blk, 185 old_desc_blk + old_desc_blocks - 1); 186 if (reserved_gdt) { 187 printf(_("\n Reserved GDT blocks at ")); 188 print_range(old_desc_blk + old_desc_blocks, 189 old_desc_blk + old_desc_blocks + 190 reserved_gdt - 1); 191 } 192 } else if (new_desc_blk) { 193 fputc(has_super ? ',' : ' ', stdout); 194 printf(_(" Group descriptor at ")); 195 print_number(new_desc_blk); 196 has_super++; 197 } 198 if (has_super) 199 fputc('\n', stdout); 200 fputs(_(" Block bitmap at "), stdout); 201 print_number(fs->group_desc[i].bg_block_bitmap); 202 diff = fs->group_desc[i].bg_block_bitmap - first_block; 203 if (diff >= 0) 204 printf(" (+%ld)", diff); 205 fputs(_(", Inode bitmap at "), stdout); 206 print_number(fs->group_desc[i].bg_inode_bitmap); 207 diff = fs->group_desc[i].bg_inode_bitmap - first_block; 208 if (diff >= 0) 209 printf(" (+%ld)", diff); 210 fputs(_("\n Inode table at "), stdout); 211 print_range(fs->group_desc[i].bg_inode_table, 212 fs->group_desc[i].bg_inode_table + 213 inode_blocks_per_group - 1); 214 diff = fs->group_desc[i].bg_inode_table - first_block; 215 if (diff > 0) 216 printf(" (+%ld)", diff); 217 printf (_("\n %u free blocks, %u free inodes, " 218 "%u directories%s"), 219 fs->group_desc[i].bg_free_blocks_count, 220 fs->group_desc[i].bg_free_inodes_count, 221 fs->group_desc[i].bg_used_dirs_count, 222 fs->group_desc[i].bg_itable_unused ? "" : "\n"); 223 if (fs->group_desc[i].bg_itable_unused) 224 printf (_(", %u unused inodes\n"), 225 fs->group_desc[i].bg_itable_unused); 226 if (block_bitmap) { 227 fputs(_(" Free blocks: "), stdout); 228 ext2fs_get_block_bitmap_range(fs->block_map, 229 blk_itr, block_nbytes << 3, block_bitmap); 230 print_free (i, block_bitmap, 231 fs->super->s_blocks_per_group, 232 fs->super->s_first_data_block); 233 fputc('\n', stdout); 234 blk_itr += fs->super->s_blocks_per_group; 235 } 236 if (inode_bitmap) { 237 fputs(_(" Free inodes: "), stdout); 238 ext2fs_get_inode_bitmap_range(fs->inode_map, 239 ino_itr, inode_nbytes << 3, inode_bitmap); 240 print_free (i, inode_bitmap, 241 fs->super->s_inodes_per_group, 1); 242 fputc('\n', stdout); 243 ino_itr += fs->super->s_inodes_per_group; 244 } 245 } 246 if (block_bitmap) 247 free(block_bitmap); 248 if (inode_bitmap) 249 free(inode_bitmap); 250} 251 252static void list_bad_blocks(ext2_filsys fs, int dump) 253{ 254 badblocks_list bb_list = 0; 255 badblocks_iterate bb_iter; 256 blk_t blk; 257 errcode_t retval; 258 const char *header, *fmt; 259 260 retval = ext2fs_read_bb_inode(fs, &bb_list); 261 if (retval) { 262 com_err("ext2fs_read_bb_inode", retval, 0); 263 return; 264 } 265 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter); 266 if (retval) { 267 com_err("ext2fs_badblocks_list_iterate_begin", retval, 268 _("while printing bad block list")); 269 return; 270 } 271 if (dump) { 272 header = fmt = "%u\n"; 273 } else { 274 header = _("Bad blocks: %u"); 275 fmt = ", %u"; 276 } 277 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) { 278 printf(header ? header : fmt, blk); 279 header = 0; 280 } 281 ext2fs_badblocks_list_iterate_end(bb_iter); 282 if (!dump) 283 fputc('\n', stdout); 284 ext2fs_badblocks_list_free(bb_list); 285} 286 287static void print_inline_journal_information(ext2_filsys fs) 288{ 289 struct ext2_inode inode; 290 errcode_t retval; 291 ino_t ino = fs->super->s_journal_inum; 292 int size; 293 294 retval = ext2fs_read_inode(fs, ino, &inode); 295 if (retval) { 296 com_err(program_name, retval, 297 _("while reading journal inode")); 298 exit(1); 299 } 300 fputs(_("Journal size: "), stdout); 301 if ((fs->super->s_feature_ro_compat & 302 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) && 303 (inode.i_flags & EXT4_HUGE_FILE_FL)) 304 size = inode.i_blocks / (fs->blocksize / 1024); 305 else 306 size = inode.i_blocks >> 1; 307 if (size < 8192) 308 printf("%uk\n", size); 309 else 310 printf("%uM\n", size >> 10); 311} 312 313static void print_journal_information(ext2_filsys fs) 314{ 315 errcode_t retval; 316 char buf[1024]; 317 char str[80]; 318 unsigned int i; 319 journal_superblock_t *jsb; 320 321 /* Get the journal superblock */ 322 if ((retval = io_channel_read_blk(fs->io, fs->super->s_first_data_block+1, -1024, buf))) { 323 com_err(program_name, retval, 324 _("while reading journal superblock")); 325 exit(1); 326 } 327 jsb = (journal_superblock_t *) buf; 328 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) || 329 (jsb->s_header.h_blocktype != 330 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) { 331 com_err(program_name, 0, 332 _("Couldn't find journal superblock magic numbers")); 333 exit(1); 334 } 335 336 printf(_("\nJournal block size: %u\n" 337 "Journal length: %u\n" 338 "Journal first block: %u\n" 339 "Journal sequence: 0x%08x\n" 340 "Journal start: %u\n" 341 "Journal number of users: %u\n"), 342 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen), 343 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence), 344 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users)); 345 346 for (i=0; i < ntohl(jsb->s_nr_users); i++) { 347 uuid_unparse(&jsb->s_users[i*16], str); 348 printf(i ? " %s\n" 349 : _("Journal users: %s\n"), 350 str); 351 } 352} 353 354static void parse_extended_opts(const char *opts, blk_t *superblock, 355 int *blocksize) 356{ 357 char *buf, *token, *next, *p, *arg, *badopt = 0; 358 int len; 359 int do_usage = 0; 360 361 len = strlen(opts); 362 buf = malloc(len+1); 363 if (!buf) { 364 fprintf(stderr, 365 _("Couldn't allocate memory to parse options!\n")); 366 exit(1); 367 } 368 strcpy(buf, opts); 369 for (token = buf; token && *token; token = next) { 370 p = strchr(token, ','); 371 next = 0; 372 if (p) { 373 *p = 0; 374 next = p+1; 375 } 376 arg = strchr(token, '='); 377 if (arg) { 378 *arg = 0; 379 arg++; 380 } 381 if (strcmp(token, "superblock") == 0 || 382 strcmp(token, "sb") == 0) { 383 if (!arg) { 384 do_usage++; 385 badopt = token; 386 continue; 387 } 388 *superblock = strtoul(arg, &p, 0); 389 if (*p) { 390 fprintf(stderr, 391 _("Invalid superblock parameter: %s\n"), 392 arg); 393 do_usage++; 394 continue; 395 } 396 } else if (strcmp(token, "blocksize") == 0 || 397 strcmp(token, "bs") == 0) { 398 if (!arg) { 399 do_usage++; 400 badopt = token; 401 continue; 402 } 403 *blocksize = strtoul(arg, &p, 0); 404 if (*p) { 405 fprintf(stderr, 406 _("Invalid blocksize parameter: %s\n"), 407 arg); 408 do_usage++; 409 continue; 410 } 411 } else { 412 do_usage++; 413 badopt = token; 414 } 415 } 416 if (do_usage) { 417 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n" 418 "Extended options are separated by commas, " 419 "and may take an argument which\n" 420 "\tis set off by an equals ('=') sign.\n\n" 421 "Valid extended options are:\n" 422 "\tsuperblock=<superblock number>\n" 423 "\tblocksize=<blocksize>\n"), 424 badopt ? badopt : ""); 425 free(buf); 426 exit(1); 427 } 428 free(buf); 429} 430 431int main (int argc, char ** argv) 432{ 433 errcode_t retval; 434 ext2_filsys fs; 435 int print_badblocks = 0; 436 blk_t use_superblock = 0; 437 int use_blocksize = 0; 438 int image_dump = 0; 439 int force = 0; 440 int flags; 441 int header_only = 0; 442 int c; 443 444#ifdef ENABLE_NLS 445 setlocale(LC_MESSAGES, ""); 446 setlocale(LC_CTYPE, ""); 447 bindtextdomain(NLS_CAT_NAME, LOCALEDIR); 448 textdomain(NLS_CAT_NAME); 449#endif 450 add_error_table(&et_ext2_error_table); 451 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION, 452 E2FSPROGS_DATE); 453 if (argc && *argv) 454 program_name = *argv; 455 456 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) { 457 switch (c) { 458 case 'b': 459 print_badblocks++; 460 break; 461 case 'f': 462 force++; 463 break; 464 case 'h': 465 header_only++; 466 break; 467 case 'i': 468 image_dump++; 469 break; 470 case 'o': 471 parse_extended_opts(optarg, &use_superblock, 472 &use_blocksize); 473 break; 474 case 'V': 475 /* Print version number and exit */ 476 fprintf(stderr, _("\tUsing %s\n"), 477 error_message(EXT2_ET_BASE)); 478 exit(0); 479 case 'x': 480 hex_format++; 481 break; 482 default: 483 usage(); 484 } 485 } 486 if (optind > argc - 1) 487 usage(); 488 device_name = argv[optind++]; 489 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES; 490 if (force) 491 flags |= EXT2_FLAG_FORCE; 492 if (image_dump) 493 flags |= EXT2_FLAG_IMAGE_FILE; 494 495 if (use_superblock && !use_blocksize) { 496 for (use_blocksize = EXT2_MIN_BLOCK_SIZE; 497 use_blocksize <= EXT2_MAX_BLOCK_SIZE; 498 use_blocksize *= 2) { 499 retval = ext2fs_open (device_name, flags, 500 use_superblock, 501 use_blocksize, unix_io_manager, 502 &fs); 503 if (!retval) 504 break; 505 } 506 } else 507 retval = ext2fs_open (device_name, flags, use_superblock, 508 use_blocksize, unix_io_manager, &fs); 509 if (retval) { 510 com_err (program_name, retval, _("while trying to open %s"), 511 device_name); 512 printf (_("Couldn't find valid filesystem superblock.\n")); 513 exit (1); 514 } 515 if (print_badblocks) { 516 list_bad_blocks(fs, 1); 517 } else { 518 list_super (fs->super); 519 if (fs->super->s_feature_incompat & 520 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 521 print_journal_information(fs); 522 ext2fs_close(fs); 523 exit(0); 524 } 525 if ((fs->super->s_feature_compat & 526 EXT3_FEATURE_COMPAT_HAS_JOURNAL) && 527 (fs->super->s_journal_inum != 0)) 528 print_inline_journal_information(fs); 529 list_bad_blocks(fs, 0); 530 if (header_only) { 531 ext2fs_close (fs); 532 exit (0); 533 } 534 retval = ext2fs_read_bitmaps (fs); 535 list_desc (fs); 536 if (retval) { 537 printf(_("\n%s: %s: error reading bitmaps: %s\n"), 538 program_name, device_name, 539 error_message(retval)); 540 } 541 } 542 ext2fs_close (fs); 543 remove_error_table(&et_ext2_error_table); 544 exit (0); 545} 546