blob: 5e0a8a2d3ddf9751dd791b1db06076423c3e0bec [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'o77f464f2009-10-24 14:15:55 -040053 fprintf (stderr, _("Usage: %s [-bfhixV] [-o superblock=<num>] "
54 "[-o blocksize=<num>] 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'oefc6f622008-08-27 23:07:54 -040060 if (hex_format)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050061 printf("0x%04lx", num);
62 else
63 printf("%lu", num);
64}
65
66static void print_range(unsigned long a, unsigned long b)
67{
Theodore Ts'oefc6f622008-08-27 23:07:54 -040068 if (hex_format)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050069 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{
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400113 int first = 1, bg_flags = 0;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400114
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400115 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400116 bg_flags = fs->group_desc[i].bg_flags;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400117
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400118 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400119 &first);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400120 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
121 &first);
122 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400123 &first);
124 if (!first)
125 fputc(']', stdout);
126 fputc('\n', stdout);
127}
128
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500129static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
130 blk64_t first_block, blk64_t last_block)
131{
132 if ((block >= first_block) && (block <= last_block)) {
133 if (itable && block == first_block)
134 return;
135 printf(" (+%u)", (unsigned)(block - first_block));
136 } else if (fs->super->s_feature_incompat &
137 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
138 dgrp_t flex_grp = ext2fs_group_of_blk(fs, block);
139 printf(" (bg #%u + %u)", flex_grp,
140 (unsigned)(block-ext2fs_group_first_block(fs,flex_grp)));
141 }
142}
143
Theodore Ts'o3839e651997-04-26 13:21:57 +0000144static void list_desc (ext2_filsys fs)
145{
146 unsigned long i;
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400147 blk_t first_block, last_block;
Theodore Ts'oef344e12003-11-21 09:02:21 -0500148 blk_t super_blk, old_desc_blk, new_desc_blk;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400149 char *block_bitmap=NULL, *inode_bitmap=NULL;
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500150 int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400151 int block_nbytes, inode_nbytes;
Theodore Ts'oef344e12003-11-21 09:02:21 -0500152 int has_super;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400153 blk_t blk_itr = fs->super->s_first_data_block;
154 ext2_ino_t ino_itr = 1;
155
156 block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
157 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000158
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400159 if (fs->block_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400160 block_bitmap = malloc(block_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400161 if (fs->inode_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400162 inode_bitmap = malloc(inode_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400163
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000164 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
165 EXT2_INODE_SIZE(fs->super)) +
166 EXT2_BLOCK_SIZE(fs->super) - 1) /
167 EXT2_BLOCK_SIZE(fs->super);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500168 reserved_gdt = fs->super->s_reserved_gdt_blocks;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100169 fputc('\n', stdout);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400170 first_block = fs->super->s_first_data_block;
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500171 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
172 old_desc_blocks = fs->super->s_first_meta_bg;
173 else
174 old_desc_blocks = fs->desc_blocks;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000175 for (i = 0; i < fs->group_desc_count; i++) {
Eric Sandeenabf23432006-09-12 14:56:16 -0400176 first_block = ext2fs_group_first_block(fs, i);
177 last_block = ext2fs_group_last_block(fs, i);
178
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400179 ext2fs_super_and_bgd_loc(fs, i, &super_blk,
Theodore Ts'oef344e12003-11-21 09:02:21 -0500180 &old_desc_blk, &new_desc_blk, 0);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400181
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100182 printf (_("Group %lu: (Blocks "), i);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400183 print_range(first_block, last_block);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400184 fputs(")", stdout);
185 print_bg_opts(fs, i);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400186 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
187 printf(_(" Checksum 0x%04x, unused inodes %d\n"),
188 fs->group_desc[i].bg_checksum,
189 fs->group_desc[i].bg_itable_unused);
Theodore Ts'oef344e12003-11-21 09:02:21 -0500190 has_super = ((i==0) || super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400191 if (has_super) {
192 printf (_(" %s superblock at "),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100193 i == 0 ? _("Primary") : _("Backup"));
Theodore Ts'o54434922003-12-07 01:28:50 -0500194 print_number(super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400195 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500196 if (old_desc_blk) {
197 printf(_(", Group descriptors at "));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400198 print_range(old_desc_blk,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500199 old_desc_blk + old_desc_blocks - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500200 if (reserved_gdt) {
201 printf(_("\n Reserved GDT blocks at "));
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500202 print_range(old_desc_blk + old_desc_blocks,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400203 old_desc_blk + old_desc_blocks +
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500204 reserved_gdt - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500205 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500206 } else if (new_desc_blk) {
207 fputc(has_super ? ',' : ' ', stdout);
208 printf(_(" Group descriptor at "));
Theodore Ts'o54434922003-12-07 01:28:50 -0500209 print_number(new_desc_blk);
Theodore Ts'oef344e12003-11-21 09:02:21 -0500210 has_super++;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100211 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500212 if (has_super)
213 fputc('\n', stdout);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100214 fputs(_(" Block bitmap at "), stdout);
Theodore Ts'o54434922003-12-07 01:28:50 -0500215 print_number(fs->group_desc[i].bg_block_bitmap);
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500216 print_bg_rel_offset(fs, fs->group_desc[i].bg_block_bitmap, 0,
217 first_block, last_block);
218
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100219 fputs(_(", Inode bitmap at "), stdout);
Theodore Ts'o54434922003-12-07 01:28:50 -0500220 print_number(fs->group_desc[i].bg_inode_bitmap);
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500221 print_bg_rel_offset(fs, fs->group_desc[i].bg_inode_bitmap, 0,
222 first_block, last_block);
223
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100224 fputs(_("\n Inode table at "), stdout);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500225 print_range(fs->group_desc[i].bg_inode_table,
226 fs->group_desc[i].bg_inode_table +
227 inode_blocks_per_group - 1);
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500228 print_bg_rel_offset(fs, fs->group_desc[i].bg_inode_table, 1,
229 first_block, last_block);
230
Jose R. Santos777a8c12007-10-21 21:03:46 -0500231 printf (_("\n %u free blocks, %u free inodes, "
232 "%u directories%s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 fs->group_desc[i].bg_free_blocks_count,
234 fs->group_desc[i].bg_free_inodes_count,
Jose R. Santos777a8c12007-10-21 21:03:46 -0500235 fs->group_desc[i].bg_used_dirs_count,
236 fs->group_desc[i].bg_itable_unused ? "" : "\n");
237 if (fs->group_desc[i].bg_itable_unused)
238 printf (_(", %u unused inodes\n"),
239 fs->group_desc[i].bg_itable_unused);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400240 if (block_bitmap) {
241 fputs(_(" Free blocks: "), stdout);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400242 ext2fs_get_block_bitmap_range(fs->block_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400243 blk_itr, block_nbytes << 3, block_bitmap);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400244 print_free (i, block_bitmap,
245 fs->super->s_blocks_per_group,
246 fs->super->s_first_data_block);
247 fputc('\n', stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400248 blk_itr += fs->super->s_blocks_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400249 }
250 if (inode_bitmap) {
251 fputs(_(" Free inodes: "), stdout);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400252 ext2fs_get_inode_bitmap_range(fs->inode_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400253 ino_itr, inode_nbytes << 3, inode_bitmap);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400254 print_free (i, inode_bitmap,
255 fs->super->s_inodes_per_group, 1);
256 fputc('\n', stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400257 ino_itr += fs->super->s_inodes_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400258 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000259 }
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400260 if (block_bitmap)
261 free(block_bitmap);
262 if (inode_bitmap)
263 free(inode_bitmap);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000264}
265
Theodore Ts'o0655b102001-12-21 23:59:46 -0500266static void list_bad_blocks(ext2_filsys fs, int dump)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000267{
268 badblocks_list bb_list = 0;
269 badblocks_iterate bb_iter;
270 blk_t blk;
271 errcode_t retval;
Theodore Ts'o0655b102001-12-21 23:59:46 -0500272 const char *header, *fmt;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273
274 retval = ext2fs_read_bb_inode(fs, &bb_list);
275 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500276 com_err("ext2fs_read_bb_inode", retval, 0);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500277 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000279 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000280 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000281 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000282 _("while printing bad block list"));
Theodore Ts'o0655b102001-12-21 23:59:46 -0500283 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000284 }
Theodore Ts'o0655b102001-12-21 23:59:46 -0500285 if (dump) {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400286 header = fmt = "%u\n";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500287 } else {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400288 header = _("Bad blocks: %u");
289 fmt = ", %u";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500290 }
291 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
292 printf(header ? header : fmt, blk);
293 header = 0;
294 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000295 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500296 if (!dump)
297 fputc('\n', stdout);
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400298 ext2fs_badblocks_list_free(bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000299}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000300
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500301static void print_inline_journal_information(ext2_filsys fs)
302{
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400303 journal_superblock_t *jsb;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500304 struct ext2_inode inode;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400305 ext2_file_t journal_file;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500306 errcode_t retval;
307 ino_t ino = fs->super->s_journal_inum;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400308 char buf[1024];
309 __u32 *mask_ptr, mask, m;
310 int i, j, size, printed = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400311
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500312 retval = ext2fs_read_inode(fs, ino, &inode);
313 if (retval) {
314 com_err(program_name, retval,
315 _("while reading journal inode"));
316 exit(1);
317 }
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400318 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
319 if (retval) {
320 com_err(program_name, retval,
321 _("while opening journal inode"));
322 exit(1);
323 }
324 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
325 if (retval) {
326 com_err(program_name, retval,
327 _("while reading journal super block"));
328 exit(1);
329 }
330 ext2fs_file_close(journal_file);
331 jsb = (journal_superblock_t *) buf;
332 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
333 fprintf(stderr,
334 "Journal superblock magic number invalid!\n");
335 exit(1);
336 }
337 printf(_("Journal features: "));
338 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
339 mask = be32_to_cpu(*mask_ptr);
340 for (j=0,m=1; j < 32; j++, m<<=1) {
341 if (mask & m) {
342 printf(" %s", e2p_jrnl_feature2string(i, m));
343 printed++;
344 }
345 }
346 }
347 if (printed == 0)
348 printf(" (none)");
349 printf("\n");
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500350 fputs(_("Journal size: "), stdout);
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400351 if ((fs->super->s_feature_ro_compat &
352 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
353 (inode.i_flags & EXT4_HUGE_FILE_FL))
354 size = inode.i_blocks / (fs->blocksize / 1024);
355 else
356 size = inode.i_blocks >> 1;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500357 if (size < 8192)
358 printf("%uk\n", size);
359 else
360 printf("%uM\n", size >> 10);
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400361 printf(_("Journal length: %u\n"
362 "Journal sequence: 0x%08x\n"
363 "Journal start: %u\n"),
364 (unsigned int)ntohl(jsb->s_maxlen),
365 (unsigned int)ntohl(jsb->s_sequence),
366 (unsigned int)ntohl(jsb->s_start));
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500367}
368
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000369static void print_journal_information(ext2_filsys fs)
370{
371 errcode_t retval;
372 char buf[1024];
373 char str[80];
Theodore Ts'o54434922003-12-07 01:28:50 -0500374 unsigned int i;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000375 journal_superblock_t *jsb;
376
377 /* Get the journal superblock */
Theodore Ts'o02088862001-01-18 01:44:19 +0000378 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 +0000379 com_err(program_name, retval,
380 _("while reading journal superblock"));
381 exit(1);
382 }
383 jsb = (journal_superblock_t *) buf;
384 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
385 (jsb->s_header.h_blocktype !=
386 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
387 com_err(program_name, 0,
388 _("Couldn't find journal superblock magic numbers"));
389 exit(1);
390 }
391
Takashi Sato8deb80a2006-03-18 21:43:46 -0500392 printf(_("\nJournal block size: %u\n"
393 "Journal length: %u\n"
394 "Journal first block: %u\n"
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100395 "Journal sequence: 0x%08x\n"
Takashi Sato8deb80a2006-03-18 21:43:46 -0500396 "Journal start: %u\n"
Matthias Andreef6567a82006-05-30 16:26:29 +0200397 "Journal number of users: %u\n"),
398 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
399 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
400 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100401
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000402 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000403 uuid_unparse(&jsb->s_users[i*16], str);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100404 printf(i ? " %s\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400405 : _("Journal users: %s\n"),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100406 str);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000407 }
408}
409
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400410static void parse_extended_opts(const char *opts, blk_t *superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500411 int *blocksize)
412{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400413 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500414 int len;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400415 int do_usage = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500416
417 len = strlen(opts);
418 buf = malloc(len+1);
419 if (!buf) {
420 fprintf(stderr,
421 _("Couldn't allocate memory to parse options!\n"));
422 exit(1);
423 }
424 strcpy(buf, opts);
425 for (token = buf; token && *token; token = next) {
426 p = strchr(token, ',');
427 next = 0;
428 if (p) {
429 *p = 0;
430 next = p+1;
431 }
432 arg = strchr(token, '=');
433 if (arg) {
434 *arg = 0;
435 arg++;
436 }
437 if (strcmp(token, "superblock") == 0 ||
438 strcmp(token, "sb") == 0) {
439 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400440 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500441 badopt = token;
442 continue;
443 }
444 *superblock = strtoul(arg, &p, 0);
445 if (*p) {
446 fprintf(stderr,
447 _("Invalid superblock parameter: %s\n"),
448 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400449 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500450 continue;
451 }
452 } else if (strcmp(token, "blocksize") == 0 ||
453 strcmp(token, "bs") == 0) {
454 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400455 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500456 badopt = token;
457 continue;
458 }
459 *blocksize = strtoul(arg, &p, 0);
460 if (*p) {
461 fprintf(stderr,
462 _("Invalid blocksize parameter: %s\n"),
463 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400464 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500465 continue;
466 }
467 } else {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400468 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500469 badopt = token;
470 }
471 }
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400472 if (do_usage) {
Theodore Ts'odb197a82008-02-26 19:05:33 -0500473 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
474 "Extended options are separated by commas, "
475 "and may take an argument which\n"
476 "\tis set off by an equals ('=') sign.\n\n"
477 "Valid extended options are:\n"
478 "\tsuperblock=<superblock number>\n"
479 "\tblocksize=<blocksize>\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400480 badopt ? badopt : "");
Theodore Ts'odb197a82008-02-26 19:05:33 -0500481 free(buf);
482 exit(1);
483 }
484 free(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400485}
Theodore Ts'odb197a82008-02-26 19:05:33 -0500486
Theodore Ts'o00e54331997-09-16 02:13:52 +0000487int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488{
489 errcode_t retval;
490 ext2_filsys fs;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000491 int print_badblocks = 0;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400492 blk_t use_superblock = 0;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000493 int use_blocksize = 0;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000494 int image_dump = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000495 int force = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000496 int flags;
Theodore Ts'o27401561999-09-14 20:11:19 +0000497 int header_only = 0;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000498 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000500#ifdef ENABLE_NLS
501 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500502 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000503 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
504 textdomain(NLS_CAT_NAME);
505#endif
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500506 add_error_table(&et_ext2_error_table);
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400507 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
508 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 if (argc && *argv)
510 program_name = *argv;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400511
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000512 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000513 switch (c) {
514 case 'b':
515 print_badblocks++;
516 break;
Theodore Ts'o27401561999-09-14 20:11:19 +0000517 case 'f':
518 force++;
519 break;
520 case 'h':
521 header_only++;
522 break;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000523 case 'i':
524 image_dump++;
525 break;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000526 case 'o':
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400527 parse_extended_opts(optarg, &use_superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500528 &use_blocksize);
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000529 break;
Theodore Ts'o5c576471997-04-29 15:29:49 +0000530 case 'V':
531 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000532 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o5c576471997-04-29 15:29:49 +0000533 error_message(EXT2_ET_BASE));
534 exit(0);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000535 case 'x':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500536 hex_format++;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000537 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000538 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000539 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000540 }
541 }
542 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000543 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000544 device_name = argv[optind++];
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500545 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000546 if (force)
547 flags |= EXT2_FLAG_FORCE;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000548 if (image_dump)
549 flags |= EXT2_FLAG_IMAGE_FILE;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400550
Theodore Ts'odb197a82008-02-26 19:05:33 -0500551 if (use_superblock && !use_blocksize) {
552 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
553 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
554 use_blocksize *= 2) {
555 retval = ext2fs_open (device_name, flags,
556 use_superblock,
557 use_blocksize, unix_io_manager,
558 &fs);
559 if (!retval)
560 break;
561 }
562 } else
563 retval = ext2fs_open (device_name, flags, use_superblock,
564 use_blocksize, unix_io_manager, &fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000565 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000566 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567 device_name);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000568 printf (_("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000569 exit (1);
570 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000571 if (print_badblocks) {
Theodore Ts'o0655b102001-12-21 23:59:46 -0500572 list_bad_blocks(fs, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000573 } else {
Theodore Ts'o27401561999-09-14 20:11:19 +0000574 list_super (fs->super);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000575 if (fs->super->s_feature_incompat &
576 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
577 print_journal_information(fs);
578 ext2fs_close(fs);
579 exit(0);
580 }
Theodore Ts'oa11d0742008-11-15 15:05:51 -0500581 if ((fs->super->s_feature_compat &
582 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
583 (fs->super->s_journal_inum != 0))
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500584 print_inline_journal_information(fs);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500585 list_bad_blocks(fs, 0);
Theodore Ts'o27401561999-09-14 20:11:19 +0000586 if (header_only) {
587 ext2fs_close (fs);
588 exit (0);
589 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000590 retval = ext2fs_read_bitmaps (fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000591 list_desc (fs);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400592 if (retval) {
593 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
594 program_name, device_name,
595 error_message(retval));
596 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598 ext2fs_close (fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500599 remove_error_table(&et_ext2_error_table);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000600 exit (0);
601}