blob: cb59f4129e79fa26e8909bba1c9a42bc93430884 [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
Theodore Ts'of4041672013-12-16 18:56:36 -050048static const char * program_name = "dumpe2fs";
49static char * device_name = NULL;
50static int hex_format = 0;
51static int blocks64 = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000052
Theodore Ts'o818180c1998-06-27 05:11:14 +000053static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000054{
Theodore Ts'o77f464f2009-10-24 14:15:55 -040055 fprintf (stderr, _("Usage: %s [-bfhixV] [-o superblock=<num>] "
56 "[-o blocksize=<num>] device\n"), program_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +000057 exit (1);
58}
59
Theodore Ts'oce10c312012-05-14 01:59:32 -040060static void print_number(unsigned long long num)
Theodore Ts'o54434922003-12-07 01:28:50 -050061{
Theodore Ts'oce10c312012-05-14 01:59:32 -040062 if (hex_format) {
63 if (blocks64)
64 printf("0x%08llx", num);
65 else
66 printf("0x%04llx", num);
67 } else
68 printf("%llu", num);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050069}
70
Theodore Ts'o295c3e02010-06-13 15:00:00 -040071static void print_range(unsigned long long a, unsigned long long b)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050072{
Theodore Ts'oce10c312012-05-14 01:59:32 -040073 if (hex_format) {
74 if (blocks64)
75 printf("0x%08llx-0x%08llx", a, b);
76 else
77 printf("0x%04llx-0x%04llx", a, b);
78 } else
Theodore Ts'o295c3e02010-06-13 15:00:00 -040079 printf("%llu-%llu", a, b);
Theodore Ts'o54434922003-12-07 01:28:50 -050080}
81
Theodore Ts'obcb942c2011-06-04 20:06:32 -040082static void print_free(unsigned long group, char * bitmap,
83 unsigned long num, unsigned long offset, int ratio)
Theodore Ts'o3839e651997-04-26 13:21:57 +000084{
85 int p = 0;
86 unsigned long i;
87 unsigned long j;
88
Theodore Ts'obcb942c2011-06-04 20:06:32 -040089 offset /= ratio;
90 offset += group * num;
91 for (i = 0; i < num; i++)
Theodore Ts'o3839e651997-04-26 13:21:57 +000092 if (!in_use (bitmap, i))
93 {
94 if (p)
95 printf (", ");
Theodore Ts'obcb942c2011-06-04 20:06:32 -040096 print_number((i + offset) * ratio);
97 for (j = i; j < num && !in_use (bitmap, j); j++)
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010098 ;
99 if (--j != i) {
100 fputc('-', stdout);
Theodore Ts'obcb942c2011-06-04 20:06:32 -0400101 print_number((j + offset) * ratio);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100102 i = j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 }
104 p = 1;
105 }
106}
107
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400108static void print_bg_opt(int bg_flags, int mask,
109 const char *str, int *first)
110{
111 if (bg_flags & mask) {
112 if (*first) {
113 fputs(" [", stdout);
114 *first = 0;
115 } else
116 fputs(", ", stdout);
117 fputs(str, stdout);
118 }
119}
120static void print_bg_opts(ext2_filsys fs, dgrp_t i)
121{
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400122 int first = 1, bg_flags = 0;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400123
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400124 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
Theodore Ts'o732c8cd2009-09-07 21:15:12 -0400125 bg_flags = ext2fs_bg_flags(fs, i);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400126
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400127 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400128 &first);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400129 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
130 &first);
131 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400132 &first);
133 if (!first)
134 fputc(']', stdout);
135 fputc('\n', stdout);
136}
137
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500138static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
139 blk64_t first_block, blk64_t last_block)
140{
141 if ((block >= first_block) && (block <= last_block)) {
142 if (itable && block == first_block)
143 return;
144 printf(" (+%u)", (unsigned)(block - first_block));
145 } else if (fs->super->s_feature_incompat &
146 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
Darrick J. Wong4dbfd792013-10-07 09:51:48 -0400147 dgrp_t flex_grp = ext2fs_group_of_blk2(fs, block);
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500148 printf(" (bg #%u + %u)", flex_grp,
Theodore Ts'o26da6612013-12-02 21:51:24 -0500149 (unsigned)(block-ext2fs_group_first_block2(fs,flex_grp)));
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500150 }
151}
152
Theodore Ts'o3839e651997-04-26 13:21:57 +0000153static void list_desc (ext2_filsys fs)
154{
155 unsigned long i;
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400156 blk64_t first_block, last_block;
157 blk64_t super_blk, old_desc_blk, new_desc_blk;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400158 char *block_bitmap=NULL, *inode_bitmap=NULL;
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400159 const char *units = _("blocks");
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500160 int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400161 int block_nbytes, inode_nbytes;
Theodore Ts'oef344e12003-11-21 09:02:21 -0500162 int has_super;
Theodore Ts'o6a6337c2011-06-04 20:24:36 -0400163 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400164 ext2_ino_t ino_itr = 1;
Darrick J. Wong6908c0c2013-12-12 13:10:29 -0500165 errcode_t retval;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400166
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400167 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
168 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
169 units = _("clusters");
170
Theodore Ts'obcb942c2011-06-04 20:06:32 -0400171 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400172 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000173
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400174 if (fs->block_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400175 block_bitmap = malloc(block_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400176 if (fs->inode_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400177 inode_bitmap = malloc(inode_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400178
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000179 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
180 EXT2_INODE_SIZE(fs->super)) +
181 EXT2_BLOCK_SIZE(fs->super) - 1) /
182 EXT2_BLOCK_SIZE(fs->super);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500183 reserved_gdt = fs->super->s_reserved_gdt_blocks;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100184 fputc('\n', stdout);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400185 first_block = fs->super->s_first_data_block;
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500186 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
187 old_desc_blocks = fs->super->s_first_meta_bg;
188 else
189 old_desc_blocks = fs->desc_blocks;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000190 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'ob49f78f2009-10-25 21:24:06 -0400191 first_block = ext2fs_group_first_block2(fs, i);
192 last_block = ext2fs_group_last_block2(fs, i);
Eric Sandeenabf23432006-09-12 14:56:16 -0400193
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400194 ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
195 &old_desc_blk, &new_desc_blk, 0);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400196
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100197 printf (_("Group %lu: (Blocks "), i);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400198 print_range(first_block, last_block);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400199 fputs(")", stdout);
200 print_bg_opts(fs, i);
Theodore Ts'o87141782012-03-15 12:13:25 -0400201 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
202 unsigned csum = ext2fs_bg_checksum(fs, i);
203 unsigned exp_csum = ext2fs_group_desc_csum(fs, i);
204
205 printf(_(" Checksum 0x%04x"), csum);
206 if (csum != exp_csum)
207 printf(_(" (EXPECTED 0x%04x)"), exp_csum);
208 printf(_(", unused inodes %u\n"),
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400209 ext2fs_bg_itable_unused(fs, i));
Theodore Ts'o87141782012-03-15 12:13:25 -0400210 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500211 has_super = ((i==0) || super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400212 if (has_super) {
213 printf (_(" %s superblock at "),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100214 i == 0 ? _("Primary") : _("Backup"));
Theodore Ts'o54434922003-12-07 01:28:50 -0500215 print_number(super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400216 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500217 if (old_desc_blk) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500218 printf("%s", _(", Group descriptors at "));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400219 print_range(old_desc_blk,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500220 old_desc_blk + old_desc_blocks - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500221 if (reserved_gdt) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500222 printf("%s", _("\n Reserved GDT blocks at "));
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500223 print_range(old_desc_blk + old_desc_blocks,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400224 old_desc_blk + old_desc_blocks +
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500225 reserved_gdt - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500226 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500227 } else if (new_desc_blk) {
228 fputc(has_super ? ',' : ' ', stdout);
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500229 printf("%s", _(" Group descriptor at "));
Theodore Ts'o54434922003-12-07 01:28:50 -0500230 print_number(new_desc_blk);
Theodore Ts'oef344e12003-11-21 09:02:21 -0500231 has_super++;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100232 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500233 if (has_super)
234 fputc('\n', stdout);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100235 fputs(_(" Block bitmap at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400236 print_number(ext2fs_block_bitmap_loc(fs, i));
Theodore Ts'o0358c9f2010-12-13 09:16:09 -0500237 print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500238 first_block, last_block);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100239 fputs(_(", Inode bitmap at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400240 print_number(ext2fs_inode_bitmap_loc(fs, i));
Theodore Ts'o0358c9f2010-12-13 09:16:09 -0500241 print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500242 first_block, last_block);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100243 fputs(_("\n Inode table at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400244 print_range(ext2fs_inode_table_loc(fs, i),
245 ext2fs_inode_table_loc(fs, i) +
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500246 inode_blocks_per_group - 1);
Theodore Ts'o0358c9f2010-12-13 09:16:09 -0500247 print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500248 first_block, last_block);
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400249 printf (_("\n %u free %s, %u free inodes, "
Jose R. Santos777a8c12007-10-21 21:03:46 -0500250 "%u directories%s"),
Theodore Ts'o2418dfd2011-09-16 10:13:41 -0400251 ext2fs_bg_free_blocks_count(fs, i), units,
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400252 ext2fs_bg_free_inodes_count(fs, i),
253 ext2fs_bg_used_dirs_count(fs, i),
254 ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
255 if (ext2fs_bg_itable_unused(fs, i))
Jose R. Santos777a8c12007-10-21 21:03:46 -0500256 printf (_(", %u unused inodes\n"),
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400257 ext2fs_bg_itable_unused(fs, i));
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400258 if (block_bitmap) {
259 fputs(_(" Free blocks: "), stdout);
Darrick J. Wong6908c0c2013-12-12 13:10:29 -0500260 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400261 blk_itr, block_nbytes << 3, block_bitmap);
Darrick J. Wong6908c0c2013-12-12 13:10:29 -0500262 if (retval)
263 com_err("list_desc", retval,
264 "while reading block bitmap");
265 else
266 print_free(i, block_bitmap,
267 fs->super->s_clusters_per_group,
268 fs->super->s_first_data_block,
269 EXT2FS_CLUSTER_RATIO(fs));
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400270 fputc('\n', stdout);
Theodore Ts'obcb942c2011-06-04 20:06:32 -0400271 blk_itr += fs->super->s_clusters_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400272 }
273 if (inode_bitmap) {
274 fputs(_(" Free inodes: "), stdout);
Darrick J. Wong6908c0c2013-12-12 13:10:29 -0500275 retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400276 ino_itr, inode_nbytes << 3, inode_bitmap);
Darrick J. Wong6908c0c2013-12-12 13:10:29 -0500277 if (retval)
278 com_err("list_desc", retval,
279 "while reading inode bitmap");
280 else
281 print_free(i, inode_bitmap,
282 fs->super->s_inodes_per_group,
283 1, 1);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400284 fputc('\n', stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400285 ino_itr += fs->super->s_inodes_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400286 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000287 }
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400288 if (block_bitmap)
289 free(block_bitmap);
290 if (inode_bitmap)
291 free(inode_bitmap);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000292}
293
Theodore Ts'o0655b102001-12-21 23:59:46 -0500294static void list_bad_blocks(ext2_filsys fs, int dump)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000295{
296 badblocks_list bb_list = 0;
297 badblocks_iterate bb_iter;
298 blk_t blk;
299 errcode_t retval;
Theodore Ts'o0655b102001-12-21 23:59:46 -0500300 const char *header, *fmt;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000301
302 retval = ext2fs_read_bb_inode(fs, &bb_list);
303 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500304 com_err("ext2fs_read_bb_inode", retval, 0);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500305 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000306 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000307 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000309 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500310 "%s", _("while printing bad block list"));
Theodore Ts'o0655b102001-12-21 23:59:46 -0500311 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000312 }
Theodore Ts'o0655b102001-12-21 23:59:46 -0500313 if (dump) {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400314 header = fmt = "%u\n";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500315 } else {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400316 header = _("Bad blocks: %u");
317 fmt = ", %u";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500318 }
319 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
320 printf(header ? header : fmt, blk);
321 header = 0;
322 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000323 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500324 if (!dump)
325 fputc('\n', stdout);
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400326 ext2fs_badblocks_list_free(bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000327}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000328
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500329static void print_inline_journal_information(ext2_filsys fs)
330{
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400331 journal_superblock_t *jsb;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500332 struct ext2_inode inode;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400333 ext2_file_t journal_file;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500334 errcode_t retval;
335 ino_t ino = fs->super->s_journal_inum;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400336 char buf[1024];
337 __u32 *mask_ptr, mask, m;
338 int i, j, size, printed = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400339
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500340 retval = ext2fs_read_inode(fs, ino, &inode);
341 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500342 com_err(program_name, retval, "%s",
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500343 _("while reading journal inode"));
344 exit(1);
345 }
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400346 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
347 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500348 com_err(program_name, retval, "%s",
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400349 _("while opening journal inode"));
350 exit(1);
351 }
352 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
353 if (retval) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500354 com_err(program_name, retval, "%s",
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400355 _("while reading journal super block"));
356 exit(1);
357 }
358 ext2fs_file_close(journal_file);
359 jsb = (journal_superblock_t *) buf;
360 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500361 fprintf(stderr, "%s",
362 _("Journal superblock magic number invalid!\n"));
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400363 exit(1);
364 }
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500365 printf("%s", _("Journal features: "));
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400366 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
367 mask = be32_to_cpu(*mask_ptr);
368 for (j=0,m=1; j < 32; j++, m<<=1) {
369 if (mask & m) {
370 printf(" %s", e2p_jrnl_feature2string(i, m));
371 printed++;
372 }
373 }
374 }
375 if (printed == 0)
376 printf(" (none)");
377 printf("\n");
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500378 fputs(_("Journal size: "), stdout);
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400379 if ((fs->super->s_feature_ro_compat &
380 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
381 (inode.i_flags & EXT4_HUGE_FILE_FL))
382 size = inode.i_blocks / (fs->blocksize / 1024);
383 else
384 size = inode.i_blocks >> 1;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500385 if (size < 8192)
386 printf("%uk\n", size);
387 else
388 printf("%uM\n", size >> 10);
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400389 printf(_("Journal length: %u\n"
390 "Journal sequence: 0x%08x\n"
391 "Journal start: %u\n"),
392 (unsigned int)ntohl(jsb->s_maxlen),
393 (unsigned int)ntohl(jsb->s_sequence),
394 (unsigned int)ntohl(jsb->s_start));
Theodore Ts'o99064092012-05-31 19:18:18 -0400395 if (jsb->s_errno != 0)
396 printf(_("Journal errno: %d\n"),
397 (int) ntohl(jsb->s_errno));
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500398}
399
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000400static void print_journal_information(ext2_filsys fs)
401{
402 errcode_t retval;
403 char buf[1024];
404 char str[80];
Theodore Ts'o54434922003-12-07 01:28:50 -0500405 unsigned int i;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000406 journal_superblock_t *jsb;
407
408 /* Get the journal superblock */
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500409 if ((retval = io_channel_read_blk64(fs->io,
410 fs->super->s_first_data_block + 1,
411 -1024, buf))) {
412 com_err(program_name, retval, "%s",
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000413 _("while reading journal superblock"));
414 exit(1);
415 }
416 jsb = (journal_superblock_t *) buf;
417 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
418 (jsb->s_header.h_blocktype !=
419 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500420 com_err(program_name, 0, "%s",
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000421 _("Couldn't find journal superblock magic numbers"));
422 exit(1);
423 }
424
Takashi Sato8deb80a2006-03-18 21:43:46 -0500425 printf(_("\nJournal block size: %u\n"
426 "Journal length: %u\n"
427 "Journal first block: %u\n"
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100428 "Journal sequence: 0x%08x\n"
Takashi Sato8deb80a2006-03-18 21:43:46 -0500429 "Journal start: %u\n"
Matthias Andreef6567a82006-05-30 16:26:29 +0200430 "Journal number of users: %u\n"),
431 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
432 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
433 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100434
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000435 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000436 uuid_unparse(&jsb->s_users[i*16], str);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100437 printf(i ? " %s\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400438 : _("Journal users: %s\n"),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100439 str);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000440 }
441}
442
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400443static void parse_extended_opts(const char *opts, blk64_t *superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500444 int *blocksize)
445{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400446 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500447 int len;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400448 int do_usage = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500449
450 len = strlen(opts);
451 buf = malloc(len+1);
452 if (!buf) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500453 fprintf(stderr, "%s",
Theodore Ts'odb197a82008-02-26 19:05:33 -0500454 _("Couldn't allocate memory to parse options!\n"));
455 exit(1);
456 }
457 strcpy(buf, opts);
458 for (token = buf; token && *token; token = next) {
459 p = strchr(token, ',');
460 next = 0;
461 if (p) {
462 *p = 0;
463 next = p+1;
464 }
465 arg = strchr(token, '=');
466 if (arg) {
467 *arg = 0;
468 arg++;
469 }
470 if (strcmp(token, "superblock") == 0 ||
471 strcmp(token, "sb") == 0) {
472 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400473 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500474 badopt = token;
475 continue;
476 }
477 *superblock = strtoul(arg, &p, 0);
478 if (*p) {
479 fprintf(stderr,
480 _("Invalid superblock parameter: %s\n"),
481 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400482 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500483 continue;
484 }
485 } else if (strcmp(token, "blocksize") == 0 ||
486 strcmp(token, "bs") == 0) {
487 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400488 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500489 badopt = token;
490 continue;
491 }
492 *blocksize = strtoul(arg, &p, 0);
493 if (*p) {
494 fprintf(stderr,
495 _("Invalid blocksize parameter: %s\n"),
496 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400497 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500498 continue;
499 }
500 } else {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400501 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500502 badopt = token;
503 }
504 }
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400505 if (do_usage) {
Theodore Ts'odb197a82008-02-26 19:05:33 -0500506 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
507 "Extended options are separated by commas, "
508 "and may take an argument which\n"
509 "\tis set off by an equals ('=') sign.\n\n"
510 "Valid extended options are:\n"
511 "\tsuperblock=<superblock number>\n"
512 "\tblocksize=<blocksize>\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400513 badopt ? badopt : "");
Theodore Ts'odb197a82008-02-26 19:05:33 -0500514 free(buf);
515 exit(1);
516 }
517 free(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400518}
Theodore Ts'odb197a82008-02-26 19:05:33 -0500519
Theodore Ts'o00e54331997-09-16 02:13:52 +0000520int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521{
522 errcode_t retval;
523 ext2_filsys fs;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000524 int print_badblocks = 0;
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400525 blk64_t use_superblock = 0;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000526 int use_blocksize = 0;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000527 int image_dump = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000528 int force = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000529 int flags;
Theodore Ts'o27401561999-09-14 20:11:19 +0000530 int header_only = 0;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000531 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000533#ifdef ENABLE_NLS
534 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500535 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000536 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
537 textdomain(NLS_CAT_NAME);
Theodore Ts'o9d4507c2011-10-05 01:00:30 -0400538 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000539#endif
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500540 add_error_table(&et_ext2_error_table);
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400541 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
542 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543 if (argc && *argv)
544 program_name = *argv;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400545
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000546 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000547 switch (c) {
548 case 'b':
549 print_badblocks++;
550 break;
Theodore Ts'o27401561999-09-14 20:11:19 +0000551 case 'f':
552 force++;
553 break;
554 case 'h':
555 header_only++;
556 break;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000557 case 'i':
558 image_dump++;
559 break;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000560 case 'o':
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400561 parse_extended_opts(optarg, &use_superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500562 &use_blocksize);
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000563 break;
Theodore Ts'o5c576471997-04-29 15:29:49 +0000564 case 'V':
565 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000566 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o5c576471997-04-29 15:29:49 +0000567 error_message(EXT2_ET_BASE));
568 exit(0);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000569 case 'x':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500570 hex_format++;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000571 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000572 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000573 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000574 }
575 }
576 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000577 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000578 device_name = argv[optind++];
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400579 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000580 if (force)
581 flags |= EXT2_FLAG_FORCE;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000582 if (image_dump)
583 flags |= EXT2_FLAG_IMAGE_FILE;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400584
Theodore Ts'odb197a82008-02-26 19:05:33 -0500585 if (use_superblock && !use_blocksize) {
586 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
587 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
588 use_blocksize *= 2) {
589 retval = ext2fs_open (device_name, flags,
590 use_superblock,
591 use_blocksize, unix_io_manager,
592 &fs);
593 if (!retval)
594 break;
595 }
596 } else
597 retval = ext2fs_open (device_name, flags, use_superblock,
598 use_blocksize, unix_io_manager, &fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000599 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000600 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000601 device_name);
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500602 printf("%s", _("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603 exit (1);
604 }
Theodore Ts'o24dea552012-03-15 16:39:56 -0400605 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
Theodore Ts'oce10c312012-05-14 01:59:32 -0400606 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
607 blocks64 = 1;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000608 if (print_badblocks) {
Theodore Ts'o0655b102001-12-21 23:59:46 -0500609 list_bad_blocks(fs, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000610 } else {
Theodore Ts'o27401561999-09-14 20:11:19 +0000611 list_super (fs->super);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000612 if (fs->super->s_feature_incompat &
613 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
614 print_journal_information(fs);
615 ext2fs_close(fs);
616 exit(0);
617 }
Theodore Ts'oa11d0742008-11-15 15:05:51 -0500618 if ((fs->super->s_feature_compat &
619 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
620 (fs->super->s_journal_inum != 0))
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500621 print_inline_journal_information(fs);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500622 list_bad_blocks(fs, 0);
Theodore Ts'o27401561999-09-14 20:11:19 +0000623 if (header_only) {
624 ext2fs_close (fs);
625 exit (0);
626 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000627 retval = ext2fs_read_bitmaps (fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000628 list_desc (fs);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400629 if (retval) {
630 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
631 program_name, device_name,
632 error_message(retval));
633 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000634 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000635 ext2fs_close (fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500636 remove_error_table(&et_ext2_error_table);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637 exit (0);
638}