blob: 4d2d5c7d2100155b2b13c23b08239e1def78bb9c [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
Theodore Ts'o295c3e02010-06-13 15:00:00 -040066static void print_range(unsigned long long a, unsigned long long b)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050067{
Theodore Ts'oefc6f622008-08-27 23:07:54 -040068 if (hex_format)
Theodore Ts'o295c3e02010-06-13 15:00:00 -040069 printf("0x%llx-0x%llx", a, b);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050070 else
Theodore Ts'o295c3e02010-06-13 15:00:00 -040071 printf("%llu-%llu", a, b);
Theodore Ts'o54434922003-12-07 01:28:50 -050072}
73
Theodore Ts'obcb942c2011-06-04 20:06:32 -040074static void print_free(unsigned long group, char * bitmap,
75 unsigned long num, unsigned long offset, int ratio)
Theodore Ts'o3839e651997-04-26 13:21:57 +000076{
77 int p = 0;
78 unsigned long i;
79 unsigned long j;
80
Theodore Ts'obcb942c2011-06-04 20:06:32 -040081 offset /= ratio;
82 offset += group * num;
83 for (i = 0; i < num; i++)
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 if (!in_use (bitmap, i))
85 {
86 if (p)
87 printf (", ");
Theodore Ts'obcb942c2011-06-04 20:06:32 -040088 print_number((i + offset) * ratio);
89 for (j = i; j < num && !in_use (bitmap, j); j++)
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010090 ;
91 if (--j != i) {
92 fputc('-', stdout);
Theodore Ts'obcb942c2011-06-04 20:06:32 -040093 print_number((j + offset) * ratio);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010094 i = j;
Theodore Ts'o3839e651997-04-26 13:21:57 +000095 }
96 p = 1;
97 }
98}
99
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400100static void print_bg_opt(int bg_flags, int mask,
101 const char *str, int *first)
102{
103 if (bg_flags & mask) {
104 if (*first) {
105 fputs(" [", stdout);
106 *first = 0;
107 } else
108 fputs(", ", stdout);
109 fputs(str, stdout);
110 }
111}
112static void print_bg_opts(ext2_filsys fs, dgrp_t i)
113{
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400114 int first = 1, bg_flags = 0;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400115
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400116 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
Theodore Ts'o732c8cd2009-09-07 21:15:12 -0400117 bg_flags = ext2fs_bg_flags(fs, i);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400118
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400119 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400120 &first);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400121 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
122 &first);
123 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400124 &first);
125 if (!first)
126 fputc(']', stdout);
127 fputc('\n', stdout);
128}
129
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500130static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
131 blk64_t first_block, blk64_t last_block)
132{
133 if ((block >= first_block) && (block <= last_block)) {
134 if (itable && block == first_block)
135 return;
136 printf(" (+%u)", (unsigned)(block - first_block));
137 } else if (fs->super->s_feature_incompat &
138 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
139 dgrp_t flex_grp = ext2fs_group_of_blk(fs, block);
140 printf(" (bg #%u + %u)", flex_grp,
141 (unsigned)(block-ext2fs_group_first_block(fs,flex_grp)));
142 }
143}
144
Theodore Ts'o3839e651997-04-26 13:21:57 +0000145static void list_desc (ext2_filsys fs)
146{
147 unsigned long i;
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400148 blk64_t first_block, last_block;
149 blk64_t super_blk, old_desc_blk, new_desc_blk;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400150 char *block_bitmap=NULL, *inode_bitmap=NULL;
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400151 const char *units = _("blocks");
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500152 int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400153 int block_nbytes, inode_nbytes;
Theodore Ts'oef344e12003-11-21 09:02:21 -0500154 int has_super;
Theodore Ts'o6a6337c2011-06-04 20:24:36 -0400155 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400156 ext2_ino_t ino_itr = 1;
157
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400158 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
159 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
160 units = _("clusters");
161
Theodore Ts'obcb942c2011-06-04 20:06:32 -0400162 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400163 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000164
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400165 if (fs->block_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400166 block_bitmap = malloc(block_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400167 if (fs->inode_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400168 inode_bitmap = malloc(inode_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400169
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000170 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
171 EXT2_INODE_SIZE(fs->super)) +
172 EXT2_BLOCK_SIZE(fs->super) - 1) /
173 EXT2_BLOCK_SIZE(fs->super);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500174 reserved_gdt = fs->super->s_reserved_gdt_blocks;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100175 fputc('\n', stdout);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400176 first_block = fs->super->s_first_data_block;
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500177 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
178 old_desc_blocks = fs->super->s_first_meta_bg;
179 else
180 old_desc_blocks = fs->desc_blocks;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000181 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'ob49f78f2009-10-25 21:24:06 -0400182 first_block = ext2fs_group_first_block2(fs, i);
183 last_block = ext2fs_group_last_block2(fs, i);
Eric Sandeenabf23432006-09-12 14:56:16 -0400184
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400185 ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
186 &old_desc_blk, &new_desc_blk, 0);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400187
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100188 printf (_("Group %lu: (Blocks "), i);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400189 print_range(first_block, last_block);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400190 fputs(")", stdout);
191 print_bg_opts(fs, i);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400192 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400193 printf(_(" Checksum 0x%04x, unused inodes %u\n"),
194 ext2fs_bg_checksum(fs, i),
195 ext2fs_bg_itable_unused(fs, i));
Theodore Ts'oef344e12003-11-21 09:02:21 -0500196 has_super = ((i==0) || super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400197 if (has_super) {
198 printf (_(" %s superblock at "),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100199 i == 0 ? _("Primary") : _("Backup"));
Theodore Ts'o54434922003-12-07 01:28:50 -0500200 print_number(super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400201 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500202 if (old_desc_blk) {
203 printf(_(", Group descriptors at "));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400204 print_range(old_desc_blk,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500205 old_desc_blk + old_desc_blocks - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500206 if (reserved_gdt) {
207 printf(_("\n Reserved GDT blocks at "));
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500208 print_range(old_desc_blk + old_desc_blocks,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400209 old_desc_blk + old_desc_blocks +
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500210 reserved_gdt - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500211 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500212 } else if (new_desc_blk) {
213 fputc(has_super ? ',' : ' ', stdout);
214 printf(_(" Group descriptor at "));
Theodore Ts'o54434922003-12-07 01:28:50 -0500215 print_number(new_desc_blk);
Theodore Ts'oef344e12003-11-21 09:02:21 -0500216 has_super++;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100217 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500218 if (has_super)
219 fputc('\n', stdout);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100220 fputs(_(" Block bitmap at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400221 print_number(ext2fs_block_bitmap_loc(fs, i));
Theodore Ts'o0358c9f2010-12-13 09:16:09 -0500222 print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500223 first_block, last_block);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100224 fputs(_(", Inode bitmap at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400225 print_number(ext2fs_inode_bitmap_loc(fs, i));
Theodore Ts'o0358c9f2010-12-13 09:16:09 -0500226 print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500227 first_block, last_block);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100228 fputs(_("\n Inode table at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400229 print_range(ext2fs_inode_table_loc(fs, i),
230 ext2fs_inode_table_loc(fs, i) +
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500231 inode_blocks_per_group - 1);
Theodore Ts'o0358c9f2010-12-13 09:16:09 -0500232 print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500233 first_block, last_block);
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400234 printf (_("\n %u free %s, %u free inodes, "
Jose R. Santos777a8c12007-10-21 21:03:46 -0500235 "%u directories%s"),
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400236 ext2fs_bg_free_blocks_count(fs, i), units,
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400237 ext2fs_bg_free_inodes_count(fs, i),
238 ext2fs_bg_used_dirs_count(fs, i),
239 ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
240 if (ext2fs_bg_itable_unused(fs, i))
Jose R. Santos777a8c12007-10-21 21:03:46 -0500241 printf (_(", %u unused inodes\n"),
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400242 ext2fs_bg_itable_unused(fs, i));
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400243 if (block_bitmap) {
244 fputs(_(" Free blocks: "), stdout);
Valerie Aurora Henson3c041a52009-08-22 21:15:30 -0400245 ext2fs_get_block_bitmap_range2(fs->block_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400246 blk_itr, block_nbytes << 3, block_bitmap);
Theodore Ts'obcb942c2011-06-04 20:06:32 -0400247 print_free(i, block_bitmap,
248 fs->super->s_clusters_per_group,
249 fs->super->s_first_data_block,
250 EXT2FS_CLUSTER_RATIO(fs));
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400251 fputc('\n', stdout);
Theodore Ts'obcb942c2011-06-04 20:06:32 -0400252 blk_itr += fs->super->s_clusters_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400253 }
254 if (inode_bitmap) {
255 fputs(_(" Free inodes: "), stdout);
Valerie Aurora Henson3c041a52009-08-22 21:15:30 -0400256 ext2fs_get_inode_bitmap_range2(fs->inode_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400257 ino_itr, inode_nbytes << 3, inode_bitmap);
Theodore Ts'obcb942c2011-06-04 20:06:32 -0400258 print_free(i, inode_bitmap,
259 fs->super->s_inodes_per_group, 1, 1);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400260 fputc('\n', stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400261 ino_itr += fs->super->s_inodes_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400262 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000263 }
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400264 if (block_bitmap)
265 free(block_bitmap);
266 if (inode_bitmap)
267 free(inode_bitmap);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000268}
269
Theodore Ts'o0655b102001-12-21 23:59:46 -0500270static void list_bad_blocks(ext2_filsys fs, int dump)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000271{
272 badblocks_list bb_list = 0;
273 badblocks_iterate bb_iter;
274 blk_t blk;
275 errcode_t retval;
Theodore Ts'o0655b102001-12-21 23:59:46 -0500276 const char *header, *fmt;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277
278 retval = ext2fs_read_bb_inode(fs, &bb_list);
279 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500280 com_err("ext2fs_read_bb_inode", retval, 0);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500281 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000282 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000283 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000284 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000285 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000286 _("while printing bad block list"));
Theodore Ts'o0655b102001-12-21 23:59:46 -0500287 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000288 }
Theodore Ts'o0655b102001-12-21 23:59:46 -0500289 if (dump) {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400290 header = fmt = "%u\n";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500291 } else {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400292 header = _("Bad blocks: %u");
293 fmt = ", %u";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500294 }
295 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
296 printf(header ? header : fmt, blk);
297 header = 0;
298 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000299 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500300 if (!dump)
301 fputc('\n', stdout);
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400302 ext2fs_badblocks_list_free(bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000303}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000304
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500305static void print_inline_journal_information(ext2_filsys fs)
306{
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400307 journal_superblock_t *jsb;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500308 struct ext2_inode inode;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400309 ext2_file_t journal_file;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500310 errcode_t retval;
311 ino_t ino = fs->super->s_journal_inum;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400312 char buf[1024];
313 __u32 *mask_ptr, mask, m;
314 int i, j, size, printed = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400315
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500316 retval = ext2fs_read_inode(fs, ino, &inode);
317 if (retval) {
318 com_err(program_name, retval,
319 _("while reading journal inode"));
320 exit(1);
321 }
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400322 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
323 if (retval) {
324 com_err(program_name, retval,
325 _("while opening journal inode"));
326 exit(1);
327 }
328 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
329 if (retval) {
330 com_err(program_name, retval,
331 _("while reading journal super block"));
332 exit(1);
333 }
334 ext2fs_file_close(journal_file);
335 jsb = (journal_superblock_t *) buf;
336 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
337 fprintf(stderr,
338 "Journal superblock magic number invalid!\n");
339 exit(1);
340 }
341 printf(_("Journal features: "));
342 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
343 mask = be32_to_cpu(*mask_ptr);
344 for (j=0,m=1; j < 32; j++, m<<=1) {
345 if (mask & m) {
346 printf(" %s", e2p_jrnl_feature2string(i, m));
347 printed++;
348 }
349 }
350 }
351 if (printed == 0)
352 printf(" (none)");
353 printf("\n");
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500354 fputs(_("Journal size: "), stdout);
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400355 if ((fs->super->s_feature_ro_compat &
356 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
357 (inode.i_flags & EXT4_HUGE_FILE_FL))
358 size = inode.i_blocks / (fs->blocksize / 1024);
359 else
360 size = inode.i_blocks >> 1;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500361 if (size < 8192)
362 printf("%uk\n", size);
363 else
364 printf("%uM\n", size >> 10);
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400365 printf(_("Journal length: %u\n"
366 "Journal sequence: 0x%08x\n"
367 "Journal start: %u\n"),
368 (unsigned int)ntohl(jsb->s_maxlen),
369 (unsigned int)ntohl(jsb->s_sequence),
370 (unsigned int)ntohl(jsb->s_start));
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500371}
372
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000373static void print_journal_information(ext2_filsys fs)
374{
375 errcode_t retval;
376 char buf[1024];
377 char str[80];
Theodore Ts'o54434922003-12-07 01:28:50 -0500378 unsigned int i;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000379 journal_superblock_t *jsb;
380
381 /* Get the journal superblock */
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400382 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 +0000383 com_err(program_name, retval,
384 _("while reading journal superblock"));
385 exit(1);
386 }
387 jsb = (journal_superblock_t *) buf;
388 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
389 (jsb->s_header.h_blocktype !=
390 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
391 com_err(program_name, 0,
392 _("Couldn't find journal superblock magic numbers"));
393 exit(1);
394 }
395
Takashi Sato8deb80a2006-03-18 21:43:46 -0500396 printf(_("\nJournal block size: %u\n"
397 "Journal length: %u\n"
398 "Journal first block: %u\n"
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100399 "Journal sequence: 0x%08x\n"
Takashi Sato8deb80a2006-03-18 21:43:46 -0500400 "Journal start: %u\n"
Matthias Andreef6567a82006-05-30 16:26:29 +0200401 "Journal number of users: %u\n"),
402 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
403 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
404 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100405
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000406 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000407 uuid_unparse(&jsb->s_users[i*16], str);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100408 printf(i ? " %s\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400409 : _("Journal users: %s\n"),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100410 str);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000411 }
412}
413
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400414static void parse_extended_opts(const char *opts, blk64_t *superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500415 int *blocksize)
416{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400417 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500418 int len;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400419 int do_usage = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500420
421 len = strlen(opts);
422 buf = malloc(len+1);
423 if (!buf) {
424 fprintf(stderr,
425 _("Couldn't allocate memory to parse options!\n"));
426 exit(1);
427 }
428 strcpy(buf, opts);
429 for (token = buf; token && *token; token = next) {
430 p = strchr(token, ',');
431 next = 0;
432 if (p) {
433 *p = 0;
434 next = p+1;
435 }
436 arg = strchr(token, '=');
437 if (arg) {
438 *arg = 0;
439 arg++;
440 }
441 if (strcmp(token, "superblock") == 0 ||
442 strcmp(token, "sb") == 0) {
443 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400444 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500445 badopt = token;
446 continue;
447 }
448 *superblock = strtoul(arg, &p, 0);
449 if (*p) {
450 fprintf(stderr,
451 _("Invalid superblock parameter: %s\n"),
452 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400453 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500454 continue;
455 }
456 } else if (strcmp(token, "blocksize") == 0 ||
457 strcmp(token, "bs") == 0) {
458 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400459 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500460 badopt = token;
461 continue;
462 }
463 *blocksize = strtoul(arg, &p, 0);
464 if (*p) {
465 fprintf(stderr,
466 _("Invalid blocksize parameter: %s\n"),
467 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400468 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500469 continue;
470 }
471 } else {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400472 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500473 badopt = token;
474 }
475 }
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400476 if (do_usage) {
Theodore Ts'odb197a82008-02-26 19:05:33 -0500477 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
478 "Extended options are separated by commas, "
479 "and may take an argument which\n"
480 "\tis set off by an equals ('=') sign.\n\n"
481 "Valid extended options are:\n"
482 "\tsuperblock=<superblock number>\n"
483 "\tblocksize=<blocksize>\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400484 badopt ? badopt : "");
Theodore Ts'odb197a82008-02-26 19:05:33 -0500485 free(buf);
486 exit(1);
487 }
488 free(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400489}
Theodore Ts'odb197a82008-02-26 19:05:33 -0500490
Theodore Ts'o00e54331997-09-16 02:13:52 +0000491int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000492{
493 errcode_t retval;
494 ext2_filsys fs;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000495 int print_badblocks = 0;
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400496 blk64_t use_superblock = 0;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000497 int use_blocksize = 0;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000498 int image_dump = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000499 int force = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000500 int flags;
Theodore Ts'o27401561999-09-14 20:11:19 +0000501 int header_only = 0;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000502 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000504#ifdef ENABLE_NLS
505 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500506 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000507 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
508 textdomain(NLS_CAT_NAME);
509#endif
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500510 add_error_table(&et_ext2_error_table);
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400511 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
512 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513 if (argc && *argv)
514 program_name = *argv;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400515
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000516 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000517 switch (c) {
518 case 'b':
519 print_badblocks++;
520 break;
Theodore Ts'o27401561999-09-14 20:11:19 +0000521 case 'f':
522 force++;
523 break;
524 case 'h':
525 header_only++;
526 break;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000527 case 'i':
528 image_dump++;
529 break;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000530 case 'o':
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400531 parse_extended_opts(optarg, &use_superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500532 &use_blocksize);
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000533 break;
Theodore Ts'o5c576471997-04-29 15:29:49 +0000534 case 'V':
535 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000536 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o5c576471997-04-29 15:29:49 +0000537 error_message(EXT2_ET_BASE));
538 exit(0);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000539 case 'x':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500540 hex_format++;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000541 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000542 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000543 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000544 }
545 }
546 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000547 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000548 device_name = argv[optind++];
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400549 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000550 if (force)
551 flags |= EXT2_FLAG_FORCE;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000552 if (image_dump)
553 flags |= EXT2_FLAG_IMAGE_FILE;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400554
Theodore Ts'odb197a82008-02-26 19:05:33 -0500555 if (use_superblock && !use_blocksize) {
556 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
557 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
558 use_blocksize *= 2) {
559 retval = ext2fs_open (device_name, flags,
560 use_superblock,
561 use_blocksize, unix_io_manager,
562 &fs);
563 if (!retval)
564 break;
565 }
566 } else
567 retval = ext2fs_open (device_name, flags, use_superblock,
568 use_blocksize, unix_io_manager, &fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000569 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000570 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571 device_name);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000572 printf (_("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000573 exit (1);
574 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000575 if (print_badblocks) {
Theodore Ts'o0655b102001-12-21 23:59:46 -0500576 list_bad_blocks(fs, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000577 } else {
Theodore Ts'o27401561999-09-14 20:11:19 +0000578 list_super (fs->super);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000579 if (fs->super->s_feature_incompat &
580 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
581 print_journal_information(fs);
582 ext2fs_close(fs);
583 exit(0);
584 }
Theodore Ts'oa11d0742008-11-15 15:05:51 -0500585 if ((fs->super->s_feature_compat &
586 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
587 (fs->super->s_journal_inum != 0))
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500588 print_inline_journal_information(fs);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500589 list_bad_blocks(fs, 0);
Theodore Ts'o27401561999-09-14 20:11:19 +0000590 if (header_only) {
591 ext2fs_close (fs);
592 exit (0);
593 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000594 retval = ext2fs_read_bitmaps (fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000595 list_desc (fs);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400596 if (retval) {
597 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
598 program_name, device_name,
599 error_message(retval));
600 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000601 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000602 ext2fs_close (fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500603 remove_error_table(&et_ext2_error_table);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000604 exit (0);
605}