e2fsprogs/misc/dumpe2fs.c

361 lines
9.2 KiB
C
Raw Normal View History

1997-04-26 21:21:57 +08:00
/*
* 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)
*
1997-04-30 00:17:09 +08:00
* 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%
1997-04-26 21:21:57 +08:00
*/
/*
* History:
* 94/01/09 - Creation
* 94/02/27 - Ported to use the ext2fs library
*/
1997-04-26 22:00:26 +08:00
#ifdef HAVE_GETOPT_H
1997-04-26 21:21:57 +08:00
#include <getopt.h>
#else
extern char *optarg;
extern int optind;
1997-04-26 22:00:26 +08:00
#endif
1997-04-26 21:21:57 +08:00
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ext2fs/ext2_fs.h"
1997-04-26 21:21:57 +08:00
#include "ext2fs/ext2fs.h"
#include "e2p/e2p.h"
#include "jfs_user.h"
#include <uuid/uuid.h>
1997-04-26 21:21:57 +08:00
#include "../version.h"
#include "nls-enable.h"
1997-04-26 21:21:57 +08:00
1997-04-26 22:37:06 +08:00
#define in_use(m, x) (ext2fs_test_bit ((x), (m)))
1997-04-26 21:21:57 +08:00
const char * program_name = "dumpe2fs";
char * device_name = NULL;
const char *num_format = "%lu";
1997-04-26 21:21:57 +08:00
static void usage(void)
1997-04-26 21:21:57 +08:00
{
fprintf (stderr, _("Usage: %s [-bfhixV] [-ob superblock] "
"[-oB blocksize] device\n"), program_name);
1997-04-26 21:21:57 +08:00
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;
offset += group * nbytes;
1997-04-26 21:21:57 +08:00
for (i = 0; i < nbytes; i++)
if (!in_use (bitmap, i))
{
if (p)
printf (", ");
printf (num_format, i + offset);
for (j = i; j < nbytes && !in_use (bitmap, j); j++)
;
if (--j != i) {
fputc('-', stdout);
printf(num_format, j + offset);
i = j;
1997-04-26 21:21:57 +08:00
}
p = 1;
}
}
static void list_desc (ext2_filsys fs)
{
unsigned long i;
long diff;
1997-04-30 01:48:10 +08:00
blk_t group_blk, next_blk;
1997-04-26 21:34:30 +08:00
char * block_bitmap = fs->block_map->bitmap;
char * inode_bitmap = fs->inode_map->bitmap;
int inode_blocks_per_group;
int group_desc_blocks;
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);
group_desc_blocks = ((fs->super->s_blocks_count -
fs->super->s_first_data_block +
EXT2_BLOCKS_PER_GROUP(fs->super) - 1) /
EXT2_BLOCKS_PER_GROUP(fs->super) +
EXT2_DESC_PER_BLOCK(fs->super) - 1) /
EXT2_DESC_PER_BLOCK(fs->super);
1997-04-26 21:21:57 +08:00
fputc('\n', stdout);
1997-04-30 01:48:10 +08:00
group_blk = fs->super->s_first_data_block;
for (i = 0; i < fs->group_desc_count; i++) {
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);
printf(num_format, group_blk);
fputc('-', stdout);
printf(num_format, next_blk - 1);
fputs(")\n", stdout);
if (ext2fs_bg_has_super (fs, i)) {
printf (_(" %s Superblock at "),
i == 0 ? _("Primary") : _("Backup"));
printf(num_format, group_blk);
printf(_(", Group Descriptors at "));
printf(num_format, group_blk+1);
fputc('-', stdout);
printf(num_format, group_blk + group_desc_blocks);
fputc('\n', stdout);
}
fputs(_(" Block bitmap at "), stdout);
printf(num_format, 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);
printf(num_format, 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);
printf(num_format, fs->group_desc[i].bg_inode_table);
fputc('-', stdout);
printf(num_format, 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 Free blocks: "),
1997-04-26 21:21:57 +08:00
fs->group_desc[i].bg_free_blocks_count,
fs->group_desc[i].bg_free_inodes_count,
fs->group_desc[i].bg_used_dirs_count);
print_free (i, block_bitmap, fs->super->s_blocks_per_group,
fs->super->s_first_data_block);
fputs(_("\n Free inodes: "), stdout);
1997-04-26 21:21:57 +08:00
print_free (i, inode_bitmap, fs->super->s_inodes_per_group, 1);
fputc('\n', stdout);
block_bitmap += fs->super->s_blocks_per_group / 8;
1997-04-26 21:21:57 +08:00
inode_bitmap += fs->super->s_inodes_per_group / 8;
1997-04-30 01:48:10 +08:00
group_blk = next_blk;
1997-04-26 21:21:57 +08:00
}
}
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 = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
1997-04-26 21:21:57 +08:00
if (retval) {
com_err("ext2fs_badblocks_list_iterate_begin", retval,
_("while printing bad block list"));
1997-04-26 21:21:57 +08:00
exit(1);
}
if (ext2fs_badblocks_list_iterate(bb_iter, &blk))
printf(_("Bad blocks: %d"), blk);
while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
1997-04-26 22:00:26 +08:00
printf(", %d", blk);
ext2fs_badblocks_list_iterate_end(bb_iter);
fputc('\n', stdout);
1997-04-26 21:21:57 +08:00
}
1997-04-26 21:34:30 +08:00
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 = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
1997-04-26 21:34:30 +08:00
if (retval) {
com_err("ext2fs_badblocks_list_iterate_begin", retval,
_("while printing bad block list"));
1997-04-26 21:34:30 +08:00
exit(1);
}
while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
1997-04-26 22:00:26 +08:00
printf("%d\n", blk);
ext2fs_badblocks_list_iterate_end(bb_iter);
1997-04-29 22:53:37 +08:00
}
1997-04-26 21:34:30 +08:00
static void print_journal_information(ext2_filsys fs)
{
errcode_t retval;
char buf[1024];
char str[80];
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: %d\n"
"Journal length: %d\n"
"Journal first block: %d\n"
"Journal sequence: 0x%08x\n"
"Journal start: %d\n"
"Journal number of users: %d\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)
1997-04-26 21:21:57 +08:00
{
errcode_t retval;
ext2_filsys fs;
1997-04-26 21:34:30 +08:00
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;
1997-04-29 22:53:37 +08:00
int big_endian;
int c;
1997-04-26 21:21:57 +08:00
#ifdef ENABLE_NLS
setlocale(LC_MESSAGES, "");
bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
textdomain(NLS_CAT_NAME);
#endif
1997-04-29 23:29:49 +08:00
initialize_ext2_error_table();
fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
E2FSPROGS_DATE);
1997-04-26 21:21:57 +08:00
if (argc && *argv)
program_name = *argv;
1997-04-26 21:34:30 +08:00
while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
1997-04-26 21:34:30 +08:00
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;
1997-04-29 23:29:49 +08:00
case 'V':
/* Print version number and exit */
fprintf(stderr, _("\tUsing %s\n"),
1997-04-29 23:29:49 +08:00
error_message(EXT2_ET_BASE));
exit(0);
case 'x':
num_format = "0x%04x";
break;
1997-04-26 21:34:30 +08:00
default:
usage();
1997-04-26 21:34:30 +08:00
}
}
if (optind > argc - 1)
usage();
1997-04-26 21:34:30 +08:00
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);
1997-04-26 21:34:30 +08:00
if (retval) {
com_err (program_name, retval, _("while trying to open %s"),
1997-04-26 21:21:57 +08:00
device_name);
printf (_("Couldn't find valid filesystem superblock.\n"));
1997-04-26 21:21:57 +08:00
exit (1);
}
1997-04-26 21:34:30 +08:00
if (print_badblocks) {
dump_bad_blocks(fs);
} 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);
}
list_bad_blocks (fs);
if (header_only) {
ext2fs_close (fs);
exit (0);
}
1997-04-26 21:34:30 +08:00
retval = ext2fs_read_bitmaps (fs);
if (retval) {
com_err (program_name, retval,
_("while trying to read the bitmaps"),
1997-04-26 21:34:30 +08:00
device_name);
ext2fs_close (fs);
exit (1);
}
list_desc (fs);
1997-04-26 21:21:57 +08:00
}
ext2fs_close (fs);
exit (0);
}