blob: 4e2ce0f96d161127cd81caff5dc393fc1fc7558d [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * 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%
Theodore Ts'o3839e651997-04-26 13:21:57 +000015 */
16
17/*
18 * History:
19 * 94/01/09 - Creation
20 * 94/02/27 - Ported to use the ext2fs library
21 */
22
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000023#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000025#else
26extern char *optarg;
27extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000028#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000029#include <fcntl.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
Theodore Ts'o54c637d2001-05-14 11:45:38 +000035#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000036
37#include "ext2fs/ext2fs.h"
38#include "e2p/e2p.h"
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000039#include "jfs_user.h"
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -050040#include <uuid/uuid.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000041
42#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000043#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000044
Theodore Ts'o74becf31997-04-26 14:37:06 +000045#define in_use(m, x) (ext2fs_test_bit ((x), (m)))
Theodore Ts'o3839e651997-04-26 13:21:57 +000046
47const char * program_name = "dumpe2fs";
48char * device_name = NULL;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050049int hex_format = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000050
Theodore Ts'o818180c1998-06-27 05:11:14 +000051static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000052{
Theodore Ts'o348e43d2001-05-03 14:43:43 +000053 fprintf (stderr, _("Usage: %s [-bfhixV] [-ob superblock] "
Theodore Ts'od9c56d32000-02-08 00:47:55 +000054 "[-oB blocksize] device\n"), program_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +000055 exit (1);
56}
57
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050058static void print_number(unsigned long num)
Theodore Ts'o54434922003-12-07 01:28:50 -050059{
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050060 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);
Theodore Ts'o54434922003-12-07 01:28:50 -050072}
73
Theodore Ts'o3839e651997-04-26 13:21:57 +000074static 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
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010081 offset += group * nbytes;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 for (i = 0; i < nbytes; i++)
83 if (!in_use (bitmap, i))
84 {
85 if (p)
86 printf (", ");
Theodore Ts'o54434922003-12-07 01:28:50 -050087 print_number(i + offset);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010088 for (j = i; j < nbytes && !in_use (bitmap, j); j++)
89 ;
90 if (--j != i) {
91 fputc('-', stdout);
Theodore Ts'o54434922003-12-07 01:28:50 -050092 print_number(j + offset);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010093 i = j;
Theodore Ts'o3839e651997-04-26 13:21:57 +000094 }
95 p = 1;
96 }
97}
98
Theodore Ts'of5fa2002006-05-08 20:17:26 -040099static 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
Jose R. Santos777a8c12007-10-21 21:03:46 -0500115 if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_LAZY_BG ||
116 fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400117 bg_flags = fs->group_desc[i].bg_flags;
118 else
119 bg_flags = 0;
120
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400121 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400122 &first);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400123 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
124 &first);
125 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400126 &first);
127 if (!first)
128 fputc(']', stdout);
129 fputc('\n', stdout);
130}
131
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132static void list_desc (ext2_filsys fs)
133{
134 unsigned long i;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100135 long diff;
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400136 blk_t first_block, last_block;
Theodore Ts'oef344e12003-11-21 09:02:21 -0500137 blk_t super_blk, old_desc_blk, new_desc_blk;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400138 char *block_bitmap=NULL, *inode_bitmap=NULL;
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500139 int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400140 int block_nbytes, inode_nbytes;
Theodore Ts'oef344e12003-11-21 09:02:21 -0500141 int has_super;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400142 blk_t blk_itr = fs->super->s_first_data_block;
143 ext2_ino_t ino_itr = 1;
144
145 block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
146 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000147
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400148 if (fs->block_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400149 block_bitmap = malloc(block_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400150 if (fs->inode_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400151 inode_bitmap = malloc(inode_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400152
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000153 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
154 EXT2_INODE_SIZE(fs->super)) +
155 EXT2_BLOCK_SIZE(fs->super) - 1) /
156 EXT2_BLOCK_SIZE(fs->super);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500157 reserved_gdt = fs->super->s_reserved_gdt_blocks;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100158 fputc('\n', stdout);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400159 first_block = fs->super->s_first_data_block;
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500160 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
161 old_desc_blocks = fs->super->s_first_meta_bg;
162 else
163 old_desc_blocks = fs->desc_blocks;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000164 for (i = 0; i < fs->group_desc_count; i++) {
Eric Sandeenabf23432006-09-12 14:56:16 -0400165 first_block = ext2fs_group_first_block(fs, i);
166 last_block = ext2fs_group_last_block(fs, i);
167
Theodore Ts'oef344e12003-11-21 09:02:21 -0500168 ext2fs_super_and_bgd_loc(fs, i, &super_blk,
169 &old_desc_blk, &new_desc_blk, 0);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400170
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100171 printf (_("Group %lu: (Blocks "), i);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400172 print_range(first_block, last_block);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400173 fputs(")", stdout);
174 print_bg_opts(fs, i);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400175 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
176 printf(_(" Checksum 0x%04x, unused inodes %d\n"),
177 fs->group_desc[i].bg_checksum,
178 fs->group_desc[i].bg_itable_unused);
Theodore Ts'oef344e12003-11-21 09:02:21 -0500179 has_super = ((i==0) || super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400180 if (has_super) {
181 printf (_(" %s superblock at "),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100182 i == 0 ? _("Primary") : _("Backup"));
Theodore Ts'o54434922003-12-07 01:28:50 -0500183 print_number(super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400184 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500185 if (old_desc_blk) {
186 printf(_(", Group descriptors at "));
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500187 print_range(old_desc_blk,
188 old_desc_blk + old_desc_blocks - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500189 if (reserved_gdt) {
190 printf(_("\n Reserved GDT blocks at "));
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500191 print_range(old_desc_blk + old_desc_blocks,
192 old_desc_blk + old_desc_blocks +
193 reserved_gdt - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500194 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500195 } else if (new_desc_blk) {
196 fputc(has_super ? ',' : ' ', stdout);
197 printf(_(" Group descriptor at "));
Theodore Ts'o54434922003-12-07 01:28:50 -0500198 print_number(new_desc_blk);
Theodore Ts'oef344e12003-11-21 09:02:21 -0500199 has_super++;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100200 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500201 if (has_super)
202 fputc('\n', stdout);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100203 fputs(_(" Block bitmap at "), stdout);
Theodore Ts'o54434922003-12-07 01:28:50 -0500204 print_number(fs->group_desc[i].bg_block_bitmap);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400205 diff = fs->group_desc[i].bg_block_bitmap - first_block;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100206 if (diff >= 0)
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500207 printf(" (+%ld)", diff);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100208 fputs(_(", Inode bitmap at "), stdout);
Theodore Ts'o54434922003-12-07 01:28:50 -0500209 print_number(fs->group_desc[i].bg_inode_bitmap);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400210 diff = fs->group_desc[i].bg_inode_bitmap - first_block;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100211 if (diff >= 0)
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500212 printf(" (+%ld)", diff);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100213 fputs(_("\n Inode table at "), stdout);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500214 print_range(fs->group_desc[i].bg_inode_table,
215 fs->group_desc[i].bg_inode_table +
216 inode_blocks_per_group - 1);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400217 diff = fs->group_desc[i].bg_inode_table - first_block;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100218 if (diff > 0)
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500219 printf(" (+%ld)", diff);
Jose R. Santos777a8c12007-10-21 21:03:46 -0500220 printf (_("\n %u free blocks, %u free inodes, "
221 "%u directories%s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222 fs->group_desc[i].bg_free_blocks_count,
223 fs->group_desc[i].bg_free_inodes_count,
Jose R. Santos777a8c12007-10-21 21:03:46 -0500224 fs->group_desc[i].bg_used_dirs_count,
225 fs->group_desc[i].bg_itable_unused ? "" : "\n");
226 if (fs->group_desc[i].bg_itable_unused)
227 printf (_(", %u unused inodes\n"),
228 fs->group_desc[i].bg_itable_unused);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400229 if (block_bitmap) {
230 fputs(_(" Free blocks: "), stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400231 ext2fs_get_block_bitmap_range(fs->block_map,
232 blk_itr, block_nbytes << 3, block_bitmap);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400233 print_free (i, block_bitmap,
234 fs->super->s_blocks_per_group,
235 fs->super->s_first_data_block);
236 fputc('\n', stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400237 blk_itr += fs->super->s_blocks_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400238 }
239 if (inode_bitmap) {
240 fputs(_(" Free inodes: "), stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400241 ext2fs_get_inode_bitmap_range(fs->inode_map,
242 ino_itr, inode_nbytes << 3, inode_bitmap);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400243 print_free (i, inode_bitmap,
244 fs->super->s_inodes_per_group, 1);
245 fputc('\n', stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400246 ino_itr += fs->super->s_inodes_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400247 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 }
249}
250
Theodore Ts'o0655b102001-12-21 23:59:46 -0500251static void list_bad_blocks(ext2_filsys fs, int dump)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000252{
253 badblocks_list bb_list = 0;
254 badblocks_iterate bb_iter;
255 blk_t blk;
256 errcode_t retval;
Theodore Ts'o0655b102001-12-21 23:59:46 -0500257 const char *header, *fmt;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000258
259 retval = ext2fs_read_bb_inode(fs, &bb_list);
260 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500261 com_err("ext2fs_read_bb_inode", retval, 0);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500262 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000264 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000266 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000267 _("while printing bad block list"));
Theodore Ts'o0655b102001-12-21 23:59:46 -0500268 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269 }
Theodore Ts'o0655b102001-12-21 23:59:46 -0500270 if (dump) {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400271 header = fmt = "%u\n";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500272 } else {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400273 header = _("Bad blocks: %u");
274 fmt = ", %u";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500275 }
276 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
277 printf(header ? header : fmt, blk);
278 header = 0;
279 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000280 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500281 if (!dump)
282 fputc('\n', stdout);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000283}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000284
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500285static void print_inline_journal_information(ext2_filsys fs)
286{
287 struct ext2_inode inode;
288 errcode_t retval;
289 ino_t ino = fs->super->s_journal_inum;
290 int size;
291
292 retval = ext2fs_read_inode(fs, ino, &inode);
293 if (retval) {
294 com_err(program_name, retval,
295 _("while reading journal inode"));
296 exit(1);
297 }
298 fputs(_("Journal size: "), stdout);
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400299 if ((fs->super->s_feature_ro_compat &
300 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
301 (inode.i_flags & EXT4_HUGE_FILE_FL))
302 size = inode.i_blocks / (fs->blocksize / 1024);
303 else
304 size = inode.i_blocks >> 1;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500305 if (size < 8192)
306 printf("%uk\n", size);
307 else
308 printf("%uM\n", size >> 10);
309}
310
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000311static void print_journal_information(ext2_filsys fs)
312{
313 errcode_t retval;
314 char buf[1024];
315 char str[80];
Theodore Ts'o54434922003-12-07 01:28:50 -0500316 unsigned int i;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000317 journal_superblock_t *jsb;
318
319 /* Get the journal superblock */
Theodore Ts'o02088862001-01-18 01:44:19 +0000320 if ((retval = io_channel_read_blk(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000321 com_err(program_name, retval,
322 _("while reading journal superblock"));
323 exit(1);
324 }
325 jsb = (journal_superblock_t *) buf;
326 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
327 (jsb->s_header.h_blocktype !=
328 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
329 com_err(program_name, 0,
330 _("Couldn't find journal superblock magic numbers"));
331 exit(1);
332 }
333
Takashi Sato8deb80a2006-03-18 21:43:46 -0500334 printf(_("\nJournal block size: %u\n"
335 "Journal length: %u\n"
336 "Journal first block: %u\n"
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100337 "Journal sequence: 0x%08x\n"
Takashi Sato8deb80a2006-03-18 21:43:46 -0500338 "Journal start: %u\n"
Matthias Andreef6567a82006-05-30 16:26:29 +0200339 "Journal number of users: %u\n"),
340 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
341 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
342 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100343
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000344 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000345 uuid_unparse(&jsb->s_users[i*16], str);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100346 printf(i ? " %s\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400347 : _("Journal users: %s\n"),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100348 str);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000349 }
350}
351
Theodore Ts'odb197a82008-02-26 19:05:33 -0500352static void parse_extended_opts(const char *opts, blk_t *superblock,
353 int *blocksize)
354{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400355 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500356 int len;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400357 int do_usage = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500358
359 len = strlen(opts);
360 buf = malloc(len+1);
361 if (!buf) {
362 fprintf(stderr,
363 _("Couldn't allocate memory to parse options!\n"));
364 exit(1);
365 }
366 strcpy(buf, opts);
367 for (token = buf; token && *token; token = next) {
368 p = strchr(token, ',');
369 next = 0;
370 if (p) {
371 *p = 0;
372 next = p+1;
373 }
374 arg = strchr(token, '=');
375 if (arg) {
376 *arg = 0;
377 arg++;
378 }
379 if (strcmp(token, "superblock") == 0 ||
380 strcmp(token, "sb") == 0) {
381 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400382 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500383 badopt = token;
384 continue;
385 }
386 *superblock = strtoul(arg, &p, 0);
387 if (*p) {
388 fprintf(stderr,
389 _("Invalid superblock parameter: %s\n"),
390 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400391 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500392 continue;
393 }
394 } else if (strcmp(token, "blocksize") == 0 ||
395 strcmp(token, "bs") == 0) {
396 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400397 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500398 badopt = token;
399 continue;
400 }
401 *blocksize = strtoul(arg, &p, 0);
402 if (*p) {
403 fprintf(stderr,
404 _("Invalid blocksize parameter: %s\n"),
405 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400406 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500407 continue;
408 }
409 } else {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400410 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500411 badopt = token;
412 }
413 }
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400414 if (do_usage) {
Theodore Ts'odb197a82008-02-26 19:05:33 -0500415 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
416 "Extended options are separated by commas, "
417 "and may take an argument which\n"
418 "\tis set off by an equals ('=') sign.\n\n"
419 "Valid extended options are:\n"
420 "\tsuperblock=<superblock number>\n"
421 "\tblocksize=<blocksize>\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400422 badopt ? badopt : "");
Theodore Ts'odb197a82008-02-26 19:05:33 -0500423 free(buf);
424 exit(1);
425 }
426 free(buf);
427}
428
Theodore Ts'o00e54331997-09-16 02:13:52 +0000429int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430{
431 errcode_t retval;
432 ext2_filsys fs;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000433 int print_badblocks = 0;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400434 blk_t use_superblock = 0;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000435 int use_blocksize = 0;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000436 int image_dump = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000437 int force = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000438 int flags;
Theodore Ts'o27401561999-09-14 20:11:19 +0000439 int header_only = 0;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000440 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000442#ifdef ENABLE_NLS
443 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500444 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000445 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
446 textdomain(NLS_CAT_NAME);
447#endif
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500448 add_error_table(&et_ext2_error_table);
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400449 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
450 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451 if (argc && *argv)
452 program_name = *argv;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000453
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000454 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000455 switch (c) {
456 case 'b':
457 print_badblocks++;
458 break;
Theodore Ts'o27401561999-09-14 20:11:19 +0000459 case 'f':
460 force++;
461 break;
462 case 'h':
463 header_only++;
464 break;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000465 case 'i':
466 image_dump++;
467 break;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000468 case 'o':
Theodore Ts'odb197a82008-02-26 19:05:33 -0500469 parse_extended_opts(optarg, &use_superblock,
470 &use_blocksize);
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000471 break;
Theodore Ts'o5c576471997-04-29 15:29:49 +0000472 case 'V':
473 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000474 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o5c576471997-04-29 15:29:49 +0000475 error_message(EXT2_ET_BASE));
476 exit(0);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000477 case 'x':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500478 hex_format++;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000479 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000480 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000481 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000482 }
483 }
484 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000485 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000486 device_name = argv[optind++];
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500487 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000488 if (force)
489 flags |= EXT2_FLAG_FORCE;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000490 if (image_dump)
491 flags |= EXT2_FLAG_IMAGE_FILE;
492
Theodore Ts'odb197a82008-02-26 19:05:33 -0500493 if (use_superblock && !use_blocksize) {
494 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
495 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
496 use_blocksize *= 2) {
497 retval = ext2fs_open (device_name, flags,
498 use_superblock,
499 use_blocksize, unix_io_manager,
500 &fs);
501 if (!retval)
502 break;
503 }
504 } else
505 retval = ext2fs_open (device_name, flags, use_superblock,
506 use_blocksize, unix_io_manager, &fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000507 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000508 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 device_name);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000510 printf (_("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511 exit (1);
512 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000513 if (print_badblocks) {
Theodore Ts'o0655b102001-12-21 23:59:46 -0500514 list_bad_blocks(fs, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000515 } else {
Theodore Ts'o27401561999-09-14 20:11:19 +0000516 list_super (fs->super);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000517 if (fs->super->s_feature_incompat &
518 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
519 print_journal_information(fs);
520 ext2fs_close(fs);
521 exit(0);
522 }
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500523 if (fs->super->s_feature_compat &
524 EXT3_FEATURE_COMPAT_HAS_JOURNAL)
525 print_inline_journal_information(fs);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500526 list_bad_blocks(fs, 0);
Theodore Ts'o27401561999-09-14 20:11:19 +0000527 if (header_only) {
528 ext2fs_close (fs);
529 exit (0);
530 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000531 retval = ext2fs_read_bitmaps (fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000532 list_desc (fs);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400533 if (retval) {
534 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
535 program_name, device_name,
536 error_message(retval));
537 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 ext2fs_close (fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500540 remove_error_table(&et_ext2_error_table);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541 exit (0);
542}