blob: df241c217ca07d8b708066c66b721df54fa21320 [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'od1154eb2011-09-18 17:34:37 -040023#include "config.h"
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000024#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000026#else
27extern char *optarg;
28extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000029#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000030#include <fcntl.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
Theodore Ts'o54c637d2001-05-14 11:45:38 +000036#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000037
38#include "ext2fs/ext2fs.h"
39#include "e2p/e2p.h"
Theodore Ts'o16ed5b32001-01-16 07:47:31 +000040#include "jfs_user.h"
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -050041#include <uuid/uuid.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000042
43#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000044#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000045
Theodore Ts'o74becf31997-04-26 14:37:06 +000046#define in_use(m, x) (ext2fs_test_bit ((x), (m)))
Theodore Ts'o3839e651997-04-26 13:21:57 +000047
48const char * program_name = "dumpe2fs";
49char * device_name = NULL;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050050int hex_format = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000051
Theodore Ts'o818180c1998-06-27 05:11:14 +000052static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000053{
Theodore Ts'o77f464f2009-10-24 14:15:55 -040054 fprintf (stderr, _("Usage: %s [-bfhixV] [-o superblock=<num>] "
55 "[-o blocksize=<num>] device\n"), program_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +000056 exit (1);
57}
58
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050059static void print_number(unsigned long num)
Theodore Ts'o54434922003-12-07 01:28:50 -050060{
Theodore Ts'oefc6f622008-08-27 23:07:54 -040061 if (hex_format)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050062 printf("0x%04lx", num);
63 else
64 printf("%lu", num);
65}
66
Theodore Ts'o295c3e02010-06-13 15:00:00 -040067static void print_range(unsigned long long a, unsigned long long b)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050068{
Theodore Ts'oefc6f622008-08-27 23:07:54 -040069 if (hex_format)
Theodore Ts'o295c3e02010-06-13 15:00:00 -040070 printf("0x%llx-0x%llx", a, b);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050071 else
Theodore Ts'o295c3e02010-06-13 15:00:00 -040072 printf("%llu-%llu", a, b);
Theodore Ts'o54434922003-12-07 01:28:50 -050073}
74
Theodore Ts'obcb942c2011-06-04 20:06:32 -040075static void print_free(unsigned long group, char * bitmap,
76 unsigned long num, unsigned long offset, int ratio)
Theodore Ts'o3839e651997-04-26 13:21:57 +000077{
78 int p = 0;
79 unsigned long i;
80 unsigned long j;
81
Theodore Ts'obcb942c2011-06-04 20:06:32 -040082 offset /= ratio;
83 offset += group * num;
84 for (i = 0; i < num; i++)
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 if (!in_use (bitmap, i))
86 {
87 if (p)
88 printf (", ");
Theodore Ts'obcb942c2011-06-04 20:06:32 -040089 print_number((i + offset) * ratio);
90 for (j = i; j < num && !in_use (bitmap, j); j++)
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010091 ;
92 if (--j != i) {
93 fputc('-', stdout);
Theodore Ts'obcb942c2011-06-04 20:06:32 -040094 print_number((j + offset) * ratio);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010095 i = j;
Theodore Ts'o3839e651997-04-26 13:21:57 +000096 }
97 p = 1;
98 }
99}
100
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400101static void print_bg_opt(int bg_flags, int mask,
102 const char *str, int *first)
103{
104 if (bg_flags & mask) {
105 if (*first) {
106 fputs(" [", stdout);
107 *first = 0;
108 } else
109 fputs(", ", stdout);
110 fputs(str, stdout);
111 }
112}
113static void print_bg_opts(ext2_filsys fs, dgrp_t i)
114{
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400115 int first = 1, bg_flags = 0;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400116
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400117 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
Theodore Ts'o732c8cd2009-09-07 21:15:12 -0400118 bg_flags = ext2fs_bg_flags(fs, i);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400119
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400120 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400121 &first);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400122 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
123 &first);
124 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400125 &first);
126 if (!first)
127 fputc(']', stdout);
128 fputc('\n', stdout);
129}
130
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500131static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
132 blk64_t first_block, blk64_t last_block)
133{
134 if ((block >= first_block) && (block <= last_block)) {
135 if (itable && block == first_block)
136 return;
137 printf(" (+%u)", (unsigned)(block - first_block));
138 } else if (fs->super->s_feature_incompat &
139 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
140 dgrp_t flex_grp = ext2fs_group_of_blk(fs, block);
141 printf(" (bg #%u + %u)", flex_grp,
142 (unsigned)(block-ext2fs_group_first_block(fs,flex_grp)));
143 }
144}
145
Theodore Ts'o3839e651997-04-26 13:21:57 +0000146static void list_desc (ext2_filsys fs)
147{
148 unsigned long i;
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400149 blk64_t first_block, last_block;
150 blk64_t super_blk, old_desc_blk, new_desc_blk;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400151 char *block_bitmap=NULL, *inode_bitmap=NULL;
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400152 const char *units = _("blocks");
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500153 int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400154 int block_nbytes, inode_nbytes;
Theodore Ts'oef344e12003-11-21 09:02:21 -0500155 int has_super;
Theodore Ts'o6a6337c2011-06-04 20:24:36 -0400156 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400157 ext2_ino_t ino_itr = 1;
158
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400159 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
160 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
161 units = _("clusters");
162
Theodore Ts'obcb942c2011-06-04 20:06:32 -0400163 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400164 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000165
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400166 if (fs->block_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400167 block_bitmap = malloc(block_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400168 if (fs->inode_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400169 inode_bitmap = malloc(inode_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400170
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000171 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
172 EXT2_INODE_SIZE(fs->super)) +
173 EXT2_BLOCK_SIZE(fs->super) - 1) /
174 EXT2_BLOCK_SIZE(fs->super);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500175 reserved_gdt = fs->super->s_reserved_gdt_blocks;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100176 fputc('\n', stdout);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400177 first_block = fs->super->s_first_data_block;
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500178 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
179 old_desc_blocks = fs->super->s_first_meta_bg;
180 else
181 old_desc_blocks = fs->desc_blocks;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000182 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'ob49f78f2009-10-25 21:24:06 -0400183 first_block = ext2fs_group_first_block2(fs, i);
184 last_block = ext2fs_group_last_block2(fs, i);
Eric Sandeenabf23432006-09-12 14:56:16 -0400185
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400186 ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
187 &old_desc_blk, &new_desc_blk, 0);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400188
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100189 printf (_("Group %lu: (Blocks "), i);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400190 print_range(first_block, last_block);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400191 fputs(")", stdout);
192 print_bg_opts(fs, i);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400193 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400194 printf(_(" Checksum 0x%04x, unused inodes %u\n"),
195 ext2fs_bg_checksum(fs, i),
196 ext2fs_bg_itable_unused(fs, i));
Theodore Ts'oef344e12003-11-21 09:02:21 -0500197 has_super = ((i==0) || super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400198 if (has_super) {
199 printf (_(" %s superblock at "),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100200 i == 0 ? _("Primary") : _("Backup"));
Theodore Ts'o54434922003-12-07 01:28:50 -0500201 print_number(super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400202 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500203 if (old_desc_blk) {
204 printf(_(", Group descriptors at "));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400205 print_range(old_desc_blk,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500206 old_desc_blk + old_desc_blocks - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500207 if (reserved_gdt) {
208 printf(_("\n Reserved GDT blocks at "));
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500209 print_range(old_desc_blk + old_desc_blocks,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400210 old_desc_blk + old_desc_blocks +
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500211 reserved_gdt - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500212 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500213 } else if (new_desc_blk) {
214 fputc(has_super ? ',' : ' ', stdout);
215 printf(_(" Group descriptor at "));
Theodore Ts'o54434922003-12-07 01:28:50 -0500216 print_number(new_desc_blk);
Theodore Ts'oef344e12003-11-21 09:02:21 -0500217 has_super++;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100218 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500219 if (has_super)
220 fputc('\n', stdout);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100221 fputs(_(" Block bitmap at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400222 print_number(ext2fs_block_bitmap_loc(fs, i));
Theodore Ts'o0358c9f2010-12-13 09:16:09 -0500223 print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500224 first_block, last_block);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100225 fputs(_(", Inode bitmap at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400226 print_number(ext2fs_inode_bitmap_loc(fs, i));
Theodore Ts'o0358c9f2010-12-13 09:16:09 -0500227 print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500228 first_block, last_block);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100229 fputs(_("\n Inode table at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400230 print_range(ext2fs_inode_table_loc(fs, i),
231 ext2fs_inode_table_loc(fs, i) +
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500232 inode_blocks_per_group - 1);
Theodore Ts'o0358c9f2010-12-13 09:16:09 -0500233 print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500234 first_block, last_block);
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400235 printf (_("\n %u free %s, %u free inodes, "
Jose R. Santos777a8c12007-10-21 21:03:46 -0500236 "%u directories%s"),
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400237 ext2fs_bg_free_blocks_count(fs, i), units,
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400238 ext2fs_bg_free_inodes_count(fs, i),
239 ext2fs_bg_used_dirs_count(fs, i),
240 ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
241 if (ext2fs_bg_itable_unused(fs, i))
Jose R. Santos777a8c12007-10-21 21:03:46 -0500242 printf (_(", %u unused inodes\n"),
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400243 ext2fs_bg_itable_unused(fs, i));
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400244 if (block_bitmap) {
245 fputs(_(" Free blocks: "), stdout);
Valerie Aurora Henson3c041a52009-08-22 21:15:30 -0400246 ext2fs_get_block_bitmap_range2(fs->block_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400247 blk_itr, block_nbytes << 3, block_bitmap);
Theodore Ts'obcb942c2011-06-04 20:06:32 -0400248 print_free(i, block_bitmap,
249 fs->super->s_clusters_per_group,
250 fs->super->s_first_data_block,
251 EXT2FS_CLUSTER_RATIO(fs));
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400252 fputc('\n', stdout);
Theodore Ts'obcb942c2011-06-04 20:06:32 -0400253 blk_itr += fs->super->s_clusters_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400254 }
255 if (inode_bitmap) {
256 fputs(_(" Free inodes: "), stdout);
Valerie Aurora Henson3c041a52009-08-22 21:15:30 -0400257 ext2fs_get_inode_bitmap_range2(fs->inode_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400258 ino_itr, inode_nbytes << 3, inode_bitmap);
Theodore Ts'obcb942c2011-06-04 20:06:32 -0400259 print_free(i, inode_bitmap,
260 fs->super->s_inodes_per_group, 1, 1);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400261 fputc('\n', stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400262 ino_itr += fs->super->s_inodes_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400263 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000264 }
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400265 if (block_bitmap)
266 free(block_bitmap);
267 if (inode_bitmap)
268 free(inode_bitmap);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269}
270
Theodore Ts'o0655b102001-12-21 23:59:46 -0500271static void list_bad_blocks(ext2_filsys fs, int dump)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272{
273 badblocks_list bb_list = 0;
274 badblocks_iterate bb_iter;
275 blk_t blk;
276 errcode_t retval;
Theodore Ts'o0655b102001-12-21 23:59:46 -0500277 const char *header, *fmt;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278
279 retval = ext2fs_read_bb_inode(fs, &bb_list);
280 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500281 com_err("ext2fs_read_bb_inode", retval, 0);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500282 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000283 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000284 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000285 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000286 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000287 _("while printing bad block list"));
Theodore Ts'o0655b102001-12-21 23:59:46 -0500288 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000289 }
Theodore Ts'o0655b102001-12-21 23:59:46 -0500290 if (dump) {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400291 header = fmt = "%u\n";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500292 } else {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400293 header = _("Bad blocks: %u");
294 fmt = ", %u";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500295 }
296 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
297 printf(header ? header : fmt, blk);
298 header = 0;
299 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000300 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500301 if (!dump)
302 fputc('\n', stdout);
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400303 ext2fs_badblocks_list_free(bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000304}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000305
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500306static void print_inline_journal_information(ext2_filsys fs)
307{
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400308 journal_superblock_t *jsb;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500309 struct ext2_inode inode;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400310 ext2_file_t journal_file;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500311 errcode_t retval;
312 ino_t ino = fs->super->s_journal_inum;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400313 char buf[1024];
314 __u32 *mask_ptr, mask, m;
315 int i, j, size, printed = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400316
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500317 retval = ext2fs_read_inode(fs, ino, &inode);
318 if (retval) {
319 com_err(program_name, retval,
320 _("while reading journal inode"));
321 exit(1);
322 }
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400323 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
324 if (retval) {
325 com_err(program_name, retval,
326 _("while opening journal inode"));
327 exit(1);
328 }
329 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
330 if (retval) {
331 com_err(program_name, retval,
332 _("while reading journal super block"));
333 exit(1);
334 }
335 ext2fs_file_close(journal_file);
336 jsb = (journal_superblock_t *) buf;
337 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
338 fprintf(stderr,
339 "Journal superblock magic number invalid!\n");
340 exit(1);
341 }
342 printf(_("Journal features: "));
343 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
344 mask = be32_to_cpu(*mask_ptr);
345 for (j=0,m=1; j < 32; j++, m<<=1) {
346 if (mask & m) {
347 printf(" %s", e2p_jrnl_feature2string(i, m));
348 printed++;
349 }
350 }
351 }
352 if (printed == 0)
353 printf(" (none)");
354 printf("\n");
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500355 fputs(_("Journal size: "), stdout);
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400356 if ((fs->super->s_feature_ro_compat &
357 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
358 (inode.i_flags & EXT4_HUGE_FILE_FL))
359 size = inode.i_blocks / (fs->blocksize / 1024);
360 else
361 size = inode.i_blocks >> 1;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500362 if (size < 8192)
363 printf("%uk\n", size);
364 else
365 printf("%uM\n", size >> 10);
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400366 printf(_("Journal length: %u\n"
367 "Journal sequence: 0x%08x\n"
368 "Journal start: %u\n"),
369 (unsigned int)ntohl(jsb->s_maxlen),
370 (unsigned int)ntohl(jsb->s_sequence),
371 (unsigned int)ntohl(jsb->s_start));
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500372}
373
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000374static void print_journal_information(ext2_filsys fs)
375{
376 errcode_t retval;
377 char buf[1024];
378 char str[80];
Theodore Ts'o54434922003-12-07 01:28:50 -0500379 unsigned int i;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000380 journal_superblock_t *jsb;
381
382 /* Get the journal superblock */
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400383 if ((retval = io_channel_read_blk64(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000384 com_err(program_name, retval,
385 _("while reading journal superblock"));
386 exit(1);
387 }
388 jsb = (journal_superblock_t *) buf;
389 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
390 (jsb->s_header.h_blocktype !=
391 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
392 com_err(program_name, 0,
393 _("Couldn't find journal superblock magic numbers"));
394 exit(1);
395 }
396
Takashi Sato8deb80a2006-03-18 21:43:46 -0500397 printf(_("\nJournal block size: %u\n"
398 "Journal length: %u\n"
399 "Journal first block: %u\n"
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100400 "Journal sequence: 0x%08x\n"
Takashi Sato8deb80a2006-03-18 21:43:46 -0500401 "Journal start: %u\n"
Matthias Andreef6567a82006-05-30 16:26:29 +0200402 "Journal number of users: %u\n"),
403 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
404 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
405 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100406
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000407 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000408 uuid_unparse(&jsb->s_users[i*16], str);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100409 printf(i ? " %s\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400410 : _("Journal users: %s\n"),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100411 str);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000412 }
413}
414
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400415static void parse_extended_opts(const char *opts, blk64_t *superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500416 int *blocksize)
417{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400418 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500419 int len;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400420 int do_usage = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500421
422 len = strlen(opts);
423 buf = malloc(len+1);
424 if (!buf) {
425 fprintf(stderr,
426 _("Couldn't allocate memory to parse options!\n"));
427 exit(1);
428 }
429 strcpy(buf, opts);
430 for (token = buf; token && *token; token = next) {
431 p = strchr(token, ',');
432 next = 0;
433 if (p) {
434 *p = 0;
435 next = p+1;
436 }
437 arg = strchr(token, '=');
438 if (arg) {
439 *arg = 0;
440 arg++;
441 }
442 if (strcmp(token, "superblock") == 0 ||
443 strcmp(token, "sb") == 0) {
444 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400445 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500446 badopt = token;
447 continue;
448 }
449 *superblock = strtoul(arg, &p, 0);
450 if (*p) {
451 fprintf(stderr,
452 _("Invalid superblock parameter: %s\n"),
453 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400454 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500455 continue;
456 }
457 } else if (strcmp(token, "blocksize") == 0 ||
458 strcmp(token, "bs") == 0) {
459 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400460 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500461 badopt = token;
462 continue;
463 }
464 *blocksize = strtoul(arg, &p, 0);
465 if (*p) {
466 fprintf(stderr,
467 _("Invalid blocksize parameter: %s\n"),
468 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400469 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500470 continue;
471 }
472 } else {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400473 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500474 badopt = token;
475 }
476 }
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400477 if (do_usage) {
Theodore Ts'odb197a82008-02-26 19:05:33 -0500478 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
479 "Extended options are separated by commas, "
480 "and may take an argument which\n"
481 "\tis set off by an equals ('=') sign.\n\n"
482 "Valid extended options are:\n"
483 "\tsuperblock=<superblock number>\n"
484 "\tblocksize=<blocksize>\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400485 badopt ? badopt : "");
Theodore Ts'odb197a82008-02-26 19:05:33 -0500486 free(buf);
487 exit(1);
488 }
489 free(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400490}
Theodore Ts'odb197a82008-02-26 19:05:33 -0500491
Theodore Ts'o00e54331997-09-16 02:13:52 +0000492int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000493{
494 errcode_t retval;
495 ext2_filsys fs;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000496 int print_badblocks = 0;
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400497 blk64_t use_superblock = 0;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000498 int use_blocksize = 0;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000499 int image_dump = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000500 int force = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000501 int flags;
Theodore Ts'o27401561999-09-14 20:11:19 +0000502 int header_only = 0;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000503 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000505#ifdef ENABLE_NLS
506 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500507 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000508 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
509 textdomain(NLS_CAT_NAME);
510#endif
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500511 add_error_table(&et_ext2_error_table);
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400512 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
513 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000514 if (argc && *argv)
515 program_name = *argv;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400516
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000517 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000518 switch (c) {
519 case 'b':
520 print_badblocks++;
521 break;
Theodore Ts'o27401561999-09-14 20:11:19 +0000522 case 'f':
523 force++;
524 break;
525 case 'h':
526 header_only++;
527 break;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000528 case 'i':
529 image_dump++;
530 break;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000531 case 'o':
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400532 parse_extended_opts(optarg, &use_superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500533 &use_blocksize);
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000534 break;
Theodore Ts'o5c576471997-04-29 15:29:49 +0000535 case 'V':
536 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000537 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o5c576471997-04-29 15:29:49 +0000538 error_message(EXT2_ET_BASE));
539 exit(0);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000540 case 'x':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500541 hex_format++;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000542 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000543 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000544 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000545 }
546 }
547 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000548 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000549 device_name = argv[optind++];
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400550 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000551 if (force)
552 flags |= EXT2_FLAG_FORCE;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000553 if (image_dump)
554 flags |= EXT2_FLAG_IMAGE_FILE;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400555
Theodore Ts'odb197a82008-02-26 19:05:33 -0500556 if (use_superblock && !use_blocksize) {
557 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
558 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
559 use_blocksize *= 2) {
560 retval = ext2fs_open (device_name, flags,
561 use_superblock,
562 use_blocksize, unix_io_manager,
563 &fs);
564 if (!retval)
565 break;
566 }
567 } else
568 retval = ext2fs_open (device_name, flags, use_superblock,
569 use_blocksize, unix_io_manager, &fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000570 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000571 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000572 device_name);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000573 printf (_("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000574 exit (1);
575 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000576 if (print_badblocks) {
Theodore Ts'o0655b102001-12-21 23:59:46 -0500577 list_bad_blocks(fs, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000578 } else {
Theodore Ts'o27401561999-09-14 20:11:19 +0000579 list_super (fs->super);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000580 if (fs->super->s_feature_incompat &
581 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
582 print_journal_information(fs);
583 ext2fs_close(fs);
584 exit(0);
585 }
Theodore Ts'oa11d0742008-11-15 15:05:51 -0500586 if ((fs->super->s_feature_compat &
587 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
588 (fs->super->s_journal_inum != 0))
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500589 print_inline_journal_information(fs);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500590 list_bad_blocks(fs, 0);
Theodore Ts'o27401561999-09-14 20:11:19 +0000591 if (header_only) {
592 ext2fs_close (fs);
593 exit (0);
594 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000595 retval = ext2fs_read_bitmaps (fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000596 list_desc (fs);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400597 if (retval) {
598 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
599 program_name, device_name,
600 error_message(retval));
601 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000602 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603 ext2fs_close (fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500604 remove_error_table(&et_ext2_error_table);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000605 exit (0);
606}