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