2 * dumpe2fs.c - List the control structures of a second
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)
9 * Copyright 1995, 1996, 1997 by Theodore Ts'o.
12 * This file may be redistributed under the terms of the GNU Public
20 * 94/02/27 - Ported to use the ext2fs library
35 #include "ext2fs/ext2_fs.h"
37 #include "ext2fs/ext2fs.h"
40 #include <uuid/uuid.h>
42 #include "../version.h"
43 #include "nls-enable.h"
45 #define in_use(m, x) (ext2fs_test_bit ((x), (m)))
47 const char * program_name = "dumpe2fs";
48 char * device_name = NULL;
51 static void usage(void)
53 fprintf (stderr, _("Usage: %s [-bfhixV] [-ob superblock] "
54 "[-oB blocksize] device\n"), program_name);
58 static void print_number(unsigned long num)
61 printf("0x%04lx", num);
66 static void print_range(unsigned long a, unsigned long b)
69 printf("0x%04lx-0x%04lx", a, b);
71 printf("%lu-%lu", a, b);
74 static void print_free (unsigned long group, char * bitmap,
75 unsigned long nbytes, unsigned long offset)
81 offset += group * nbytes;
82 for (i = 0; i < nbytes; i++)
83 if (!in_use (bitmap, i))
87 print_number(i + offset);
88 for (j = i; j < nbytes && !in_use (bitmap, j); j++)
92 print_number(j + offset);
99 static void print_bg_opt(int bg_flags, int mask,
100 const char *str, int *first)
102 if (bg_flags & mask) {
111 static void print_bg_opts(ext2_filsys fs, dgrp_t i)
113 int first = 1, bg_flags;
115 if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_LAZY_BG)
116 bg_flags = fs->group_desc[i].bg_flags;
120 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "Inode not init",
122 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "Block not init",
129 static void list_desc (ext2_filsys fs)
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;
140 block_bitmap = fs->block_map->bitmap;
142 inode_bitmap = fs->inode_map->bitmap;
144 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
145 EXT2_INODE_SIZE(fs->super)) +
146 EXT2_BLOCK_SIZE(fs->super) - 1) /
147 EXT2_BLOCK_SIZE(fs->super);
148 reserved_gdt = fs->super->s_reserved_gdt_blocks;
150 first_block = fs->super->s_first_data_block;
151 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
152 old_desc_blocks = fs->super->s_first_meta_bg;
154 old_desc_blocks = fs->desc_blocks;
155 for (i = 0; i < fs->group_desc_count; i++) {
156 ext2fs_super_and_bgd_loc(fs, i, &super_blk,
157 &old_desc_blk, &new_desc_blk, 0);
158 if (i == fs->group_desc_count - 1)
159 last_block = fs->super->s_blocks_count - 1;
161 last_block = first_block +
162 fs->super->s_blocks_per_group - 1;
164 printf (_("Group %lu: (Blocks "), i);
165 print_range(first_block, last_block);
167 print_bg_opts(fs, i);
168 has_super = ((i==0) || super_blk);
170 printf (_(" %s superblock at "),
171 i == 0 ? _("Primary") : _("Backup"));
172 print_number(super_blk);
175 printf(_(", Group descriptors at "));
176 print_range(old_desc_blk,
177 old_desc_blk + old_desc_blocks - 1);
179 printf(_("\n Reserved GDT blocks at "));
180 print_range(old_desc_blk + old_desc_blocks,
181 old_desc_blk + old_desc_blocks +
184 } else if (new_desc_blk) {
185 fputc(has_super ? ',' : ' ', stdout);
186 printf(_(" Group descriptor at "));
187 print_number(new_desc_blk);
192 fputs(_(" Block bitmap at "), stdout);
193 print_number(fs->group_desc[i].bg_block_bitmap);
194 diff = fs->group_desc[i].bg_block_bitmap - first_block;
196 printf(" (+%ld)", diff);
197 fputs(_(", Inode bitmap at "), stdout);
198 print_number(fs->group_desc[i].bg_inode_bitmap);
199 diff = fs->group_desc[i].bg_inode_bitmap - first_block;
201 printf(" (+%ld)", diff);
202 fputs(_("\n Inode table at "), stdout);
203 print_range(fs->group_desc[i].bg_inode_table,
204 fs->group_desc[i].bg_inode_table +
205 inode_blocks_per_group - 1);
206 diff = fs->group_desc[i].bg_inode_table - first_block;
208 printf(" (+%ld)", diff);
209 printf (_("\n %d free blocks, %d free inodes, "
211 fs->group_desc[i].bg_free_blocks_count,
212 fs->group_desc[i].bg_free_inodes_count,
213 fs->group_desc[i].bg_used_dirs_count);
215 fputs(_(" Free blocks: "), stdout);
216 print_free (i, block_bitmap,
217 fs->super->s_blocks_per_group,
218 fs->super->s_first_data_block);
220 block_bitmap += fs->super->s_blocks_per_group / 8;
223 fputs(_(" Free inodes: "), stdout);
224 print_free (i, inode_bitmap,
225 fs->super->s_inodes_per_group, 1);
227 inode_bitmap += fs->super->s_inodes_per_group / 8;
229 first_block += fs->super->s_blocks_per_group;
233 static void list_bad_blocks(ext2_filsys fs, int dump)
235 badblocks_list bb_list = 0;
236 badblocks_iterate bb_iter;
239 const char *header, *fmt;
241 retval = ext2fs_read_bb_inode(fs, &bb_list);
243 com_err("ext2fs_read_bb_inode", retval, 0);
246 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
248 com_err("ext2fs_badblocks_list_iterate_begin", retval,
249 _("while printing bad block list"));
253 header = fmt = "%d\n";
255 header = _("Bad blocks: %d");
258 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
259 printf(header ? header : fmt, blk);
262 ext2fs_badblocks_list_iterate_end(bb_iter);
267 static void print_inline_journal_information(ext2_filsys fs)
269 struct ext2_inode inode;
271 ino_t ino = fs->super->s_journal_inum;
274 retval = ext2fs_read_inode(fs, ino, &inode);
276 com_err(program_name, retval,
277 _("while reading journal inode"));
280 fputs(_("Journal size: "), stdout);
281 size = inode.i_blocks >> 1;
283 printf("%uk\n", size);
285 printf("%uM\n", size >> 10);
288 static void print_journal_information(ext2_filsys fs)
294 journal_superblock_t *jsb;
296 /* Get the journal superblock */
297 if ((retval = io_channel_read_blk(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
298 com_err(program_name, retval,
299 _("while reading journal superblock"));
302 jsb = (journal_superblock_t *) buf;
303 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
304 (jsb->s_header.h_blocktype !=
305 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
306 com_err(program_name, 0,
307 _("Couldn't find journal superblock magic numbers"));
311 printf(_("\nJournal block size: %u\n"
312 "Journal length: %u\n"
313 "Journal first block: %u\n"
314 "Journal sequence: 0x%08x\n"
315 "Journal start: %u\n"
316 "Journal number of users: %u\n"),
317 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
318 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
319 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
321 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
322 uuid_unparse(&jsb->s_users[i*16], str);
324 : _("Journal users: %s\n"),
329 int main (int argc, char ** argv)
333 int print_badblocks = 0;
334 int use_superblock = 0;
335 int use_blocksize = 0;
344 setlocale(LC_MESSAGES, "");
345 setlocale(LC_CTYPE, "");
346 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
347 textdomain(NLS_CAT_NAME);
349 initialize_ext2_error_table();
350 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
353 program_name = *argv;
355 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
370 if (optarg[0] == 'b')
371 use_superblock = atoi(optarg+1);
372 else if (optarg[0] == 'B')
373 use_blocksize = atoi(optarg+1);
378 /* Print version number and exit */
379 fprintf(stderr, _("\tUsing %s\n"),
380 error_message(EXT2_ET_BASE));
389 if (optind > argc - 1)
391 device_name = argv[optind++];
392 if (use_superblock && !use_blocksize)
393 use_blocksize = 1024;
394 flags = EXT2_FLAG_JOURNAL_DEV_OK;
396 flags |= EXT2_FLAG_FORCE;
398 flags |= EXT2_FLAG_IMAGE_FILE;
400 retval = ext2fs_open (device_name, flags, use_superblock,
401 use_blocksize, unix_io_manager, &fs);
403 com_err (program_name, retval, _("while trying to open %s"),
405 printf (_("Couldn't find valid filesystem superblock.\n"));
408 if (print_badblocks) {
409 list_bad_blocks(fs, 1);
411 big_endian = ((fs->flags & EXT2_FLAG_SWAP_BYTES) != 0);
412 #ifdef WORDS_BIGENDIAN
413 big_endian = !big_endian;
416 printf(_("Note: This is a byte-swapped filesystem\n"));
417 list_super (fs->super);
418 if (fs->super->s_feature_incompat &
419 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
420 print_journal_information(fs);
424 if (fs->super->s_feature_compat &
425 EXT3_FEATURE_COMPAT_HAS_JOURNAL)
426 print_inline_journal_information(fs);
427 list_bad_blocks(fs, 0);
432 retval = ext2fs_read_bitmaps (fs);
435 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
436 program_name, device_name,
437 error_message(retval));