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 group_blk, next_blk;
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 group_blk = 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 next_blk = group_blk + fs->super->s_blocks_per_group;
159 if (next_blk > fs->super->s_blocks_count)
160 next_blk = fs->super->s_blocks_count;
161 printf (_("Group %lu: (Blocks "), i);
162 print_range(group_blk, next_blk - 1);
164 print_bg_opts(fs, i);
165 has_super = ((i==0) || super_blk);
167 printf (_(" %s superblock at "),
168 i == 0 ? _("Primary") : _("Backup"));
169 print_number(super_blk);
172 printf(_(", Group descriptors at "));
173 print_range(old_desc_blk,
174 old_desc_blk + old_desc_blocks - 1);
176 printf(_("\n Reserved GDT blocks at "));
177 print_range(old_desc_blk + old_desc_blocks,
178 old_desc_blk + old_desc_blocks +
181 } else if (new_desc_blk) {
182 fputc(has_super ? ',' : ' ', stdout);
183 printf(_(" Group descriptor at "));
184 print_number(new_desc_blk);
189 fputs(_(" Block bitmap at "), stdout);
190 print_number(fs->group_desc[i].bg_block_bitmap);
191 diff = fs->group_desc[i].bg_block_bitmap - group_blk;
193 printf(" (+%ld)", diff);
194 fputs(_(", Inode bitmap at "), stdout);
195 print_number(fs->group_desc[i].bg_inode_bitmap);
196 diff = fs->group_desc[i].bg_inode_bitmap - group_blk;
198 printf(" (+%ld)", diff);
199 fputs(_("\n Inode table at "), stdout);
200 print_range(fs->group_desc[i].bg_inode_table,
201 fs->group_desc[i].bg_inode_table +
202 inode_blocks_per_group - 1);
203 diff = fs->group_desc[i].bg_inode_table - group_blk;
205 printf(" (+%ld)", diff);
206 printf (_("\n %d free blocks, %d free inodes, "
208 fs->group_desc[i].bg_free_blocks_count,
209 fs->group_desc[i].bg_free_inodes_count,
210 fs->group_desc[i].bg_used_dirs_count);
212 fputs(_(" Free blocks: "), stdout);
213 print_free (i, block_bitmap,
214 fs->super->s_blocks_per_group,
215 fs->super->s_first_data_block);
217 block_bitmap += fs->super->s_blocks_per_group / 8;
220 fputs(_(" Free inodes: "), stdout);
221 print_free (i, inode_bitmap,
222 fs->super->s_inodes_per_group, 1);
224 inode_bitmap += fs->super->s_inodes_per_group / 8;
226 group_blk = next_blk;
230 static void list_bad_blocks(ext2_filsys fs, int dump)
232 badblocks_list bb_list = 0;
233 badblocks_iterate bb_iter;
236 const char *header, *fmt;
238 retval = ext2fs_read_bb_inode(fs, &bb_list);
240 com_err("ext2fs_read_bb_inode", retval, 0);
243 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
245 com_err("ext2fs_badblocks_list_iterate_begin", retval,
246 _("while printing bad block list"));
250 header = fmt = "%d\n";
252 header = _("Bad blocks: %d");
255 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
256 printf(header ? header : fmt, blk);
259 ext2fs_badblocks_list_iterate_end(bb_iter);
264 static void print_inline_journal_information(ext2_filsys fs)
266 struct ext2_inode inode;
268 ino_t ino = fs->super->s_journal_inum;
271 retval = ext2fs_read_inode(fs, ino, &inode);
273 com_err(program_name, retval,
274 _("while reading journal inode"));
277 fputs(_("Journal size: "), stdout);
278 size = inode.i_blocks >> 1;
280 printf("%uk\n", size);
282 printf("%uM\n", size >> 10);
285 static void print_journal_information(ext2_filsys fs)
291 journal_superblock_t *jsb;
293 /* Get the journal superblock */
294 if ((retval = io_channel_read_blk(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
295 com_err(program_name, retval,
296 _("while reading journal superblock"));
299 jsb = (journal_superblock_t *) buf;
300 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
301 (jsb->s_header.h_blocktype !=
302 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
303 com_err(program_name, 0,
304 _("Couldn't find journal superblock magic numbers"));
308 printf(_("\nJournal block size: %u\n"
309 "Journal length: %u\n"
310 "Journal first block: %u\n"
311 "Journal sequence: 0x%08x\n"
312 "Journal start: %u\n"
313 "Journal number of users: %u\n"),
314 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
315 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
316 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
318 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
319 uuid_unparse(&jsb->s_users[i*16], str);
321 : _("Journal users: %s\n"),
326 int main (int argc, char ** argv)
330 int print_badblocks = 0;
331 int use_superblock = 0;
332 int use_blocksize = 0;
341 setlocale(LC_MESSAGES, "");
342 setlocale(LC_CTYPE, "");
343 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
344 textdomain(NLS_CAT_NAME);
346 initialize_ext2_error_table();
347 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
350 program_name = *argv;
352 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
367 if (optarg[0] == 'b')
368 use_superblock = atoi(optarg+1);
369 else if (optarg[0] == 'B')
370 use_blocksize = atoi(optarg+1);
375 /* Print version number and exit */
376 fprintf(stderr, _("\tUsing %s\n"),
377 error_message(EXT2_ET_BASE));
386 if (optind > argc - 1)
388 device_name = argv[optind++];
389 if (use_superblock && !use_blocksize)
390 use_blocksize = 1024;
391 flags = EXT2_FLAG_JOURNAL_DEV_OK;
393 flags |= EXT2_FLAG_FORCE;
395 flags |= EXT2_FLAG_IMAGE_FILE;
397 retval = ext2fs_open (device_name, flags, use_superblock,
398 use_blocksize, unix_io_manager, &fs);
400 com_err (program_name, retval, _("while trying to open %s"),
402 printf (_("Couldn't find valid filesystem superblock.\n"));
405 if (print_badblocks) {
406 list_bad_blocks(fs, 1);
408 big_endian = ((fs->flags & EXT2_FLAG_SWAP_BYTES) != 0);
409 #ifdef WORDS_BIGENDIAN
410 big_endian = !big_endian;
413 printf(_("Note: This is a byte-swapped filesystem\n"));
414 list_super (fs->super);
415 if (fs->super->s_feature_incompat &
416 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
417 print_journal_information(fs);
421 if (fs->super->s_feature_compat &
422 EXT3_FEATURE_COMPAT_HAS_JOURNAL)
423 print_inline_journal_information(fs);
424 list_bad_blocks(fs, 0);
429 retval = ext2fs_read_bitmaps (fs);
432 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
433 program_name, device_name,
434 error_message(retval));