blob: 9e5d2df4788fe9dfe134d130a6e4a875146390ba [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
JP Abgralle0ed7402014-03-19 19:08:39 -070047static const char * program_name = "dumpe2fs";
48static char * device_name = NULL;
49static int hex_format = 0;
50static int blocks64 = 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
JP Abgralle0ed7402014-03-19 19:08:39 -070059static void print_number(unsigned long long num)
Theodore Ts'o54434922003-12-07 01:28:50 -050060{
JP Abgralle0ed7402014-03-19 19:08:39 -070061 if (hex_format) {
62 if (blocks64)
63 printf("0x%08llx", num);
64 else
65 printf("0x%04llx", num);
66 } else
67 printf("%llu", num);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050068}
69
JP Abgralle0ed7402014-03-19 19:08:39 -070070static void print_range(unsigned long long a, unsigned long long b)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -050071{
JP Abgralle0ed7402014-03-19 19:08:39 -070072 if (hex_format) {
73 if (blocks64)
74 printf("0x%08llx-0x%08llx", a, b);
75 else
76 printf("0x%04llx-0x%04llx", a, b);
77 } else
78 printf("%llu-%llu", a, b);
Theodore Ts'o54434922003-12-07 01:28:50 -050079}
80
JP Abgralle0ed7402014-03-19 19:08:39 -070081static void print_free(unsigned long group, char * bitmap,
82 unsigned long num, unsigned long offset, int ratio)
Theodore Ts'o3839e651997-04-26 13:21:57 +000083{
84 int p = 0;
85 unsigned long i;
86 unsigned long j;
87
JP Abgralle0ed7402014-03-19 19:08:39 -070088 offset /= ratio;
89 offset += group * num;
90 for (i = 0; i < num; i++)
Theodore Ts'o3839e651997-04-26 13:21:57 +000091 if (!in_use (bitmap, i))
92 {
93 if (p)
94 printf (", ");
JP Abgralle0ed7402014-03-19 19:08:39 -070095 print_number((i + offset) * ratio);
96 for (j = i; j < num && !in_use (bitmap, j); j++)
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010097 ;
98 if (--j != i) {
99 fputc('-', stdout);
JP Abgralle0ed7402014-03-19 19:08:39 -0700100 print_number((j + offset) * ratio);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100101 i = j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102 }
103 p = 1;
104 }
105}
106
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400107static void print_bg_opt(int bg_flags, int mask,
108 const char *str, int *first)
109{
110 if (bg_flags & mask) {
111 if (*first) {
112 fputs(" [", stdout);
113 *first = 0;
114 } else
115 fputs(", ", stdout);
116 fputs(str, stdout);
117 }
118}
119static void print_bg_opts(ext2_filsys fs, dgrp_t i)
120{
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400121 int first = 1, bg_flags = 0;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400122
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400123 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
JP Abgralle0ed7402014-03-19 19:08:39 -0700124 bg_flags = ext2fs_bg_flags(fs, i);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400125
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400126 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400127 &first);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400128 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
129 &first);
130 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400131 &first);
132 if (!first)
133 fputc(']', stdout);
134 fputc('\n', stdout);
135}
136
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500137static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
138 blk64_t first_block, blk64_t last_block)
139{
140 if ((block >= first_block) && (block <= last_block)) {
141 if (itable && block == first_block)
142 return;
143 printf(" (+%u)", (unsigned)(block - first_block));
144 } else if (fs->super->s_feature_incompat &
145 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700146 dgrp_t flex_grp = ext2fs_group_of_blk2(fs, block);
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500147 printf(" (bg #%u + %u)", flex_grp,
JP Abgralle0ed7402014-03-19 19:08:39 -0700148 (unsigned)(block-ext2fs_group_first_block2(fs,flex_grp)));
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500149 }
150}
151
Theodore Ts'o3839e651997-04-26 13:21:57 +0000152static void list_desc (ext2_filsys fs)
153{
154 unsigned long i;
JP Abgralle0ed7402014-03-19 19:08:39 -0700155 blk64_t first_block, last_block;
156 blk64_t super_blk, old_desc_blk, new_desc_blk;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400157 char *block_bitmap=NULL, *inode_bitmap=NULL;
JP Abgralle0ed7402014-03-19 19:08:39 -0700158 const char *units = _("blocks");
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500159 int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400160 int block_nbytes, inode_nbytes;
Theodore Ts'oef344e12003-11-21 09:02:21 -0500161 int has_super;
JP Abgralle0ed7402014-03-19 19:08:39 -0700162 blk64_t blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400163 ext2_ino_t ino_itr = 1;
JP Abgralle0ed7402014-03-19 19:08:39 -0700164 errcode_t retval;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400165
JP Abgralle0ed7402014-03-19 19:08:39 -0700166 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
167 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
168 units = _("clusters");
169
170 block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400171 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000172
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400173 if (fs->block_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400174 block_bitmap = malloc(block_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400175 if (fs->inode_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400176 inode_bitmap = malloc(inode_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400177
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000178 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
179 EXT2_INODE_SIZE(fs->super)) +
180 EXT2_BLOCK_SIZE(fs->super) - 1) /
181 EXT2_BLOCK_SIZE(fs->super);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500182 reserved_gdt = fs->super->s_reserved_gdt_blocks;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100183 fputc('\n', stdout);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400184 first_block = fs->super->s_first_data_block;
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500185 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
186 old_desc_blocks = fs->super->s_first_meta_bg;
187 else
188 old_desc_blocks = fs->desc_blocks;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000189 for (i = 0; i < fs->group_desc_count; i++) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700190 first_block = ext2fs_group_first_block2(fs, i);
191 last_block = ext2fs_group_last_block2(fs, i);
Eric Sandeenabf23432006-09-12 14:56:16 -0400192
JP Abgralle0ed7402014-03-19 19:08:39 -0700193 ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
194 &old_desc_blk, &new_desc_blk, 0);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400195
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100196 printf (_("Group %lu: (Blocks "), i);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400197 print_range(first_block, last_block);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400198 fputs(")", stdout);
199 print_bg_opts(fs, i);
JP Abgralle0ed7402014-03-19 19:08:39 -0700200 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
201 unsigned csum = ext2fs_bg_checksum(fs, i);
202 unsigned exp_csum = ext2fs_group_desc_csum(fs, i);
203
204 printf(_(" Checksum 0x%04x"), csum);
205 if (csum != exp_csum)
206 printf(_(" (EXPECTED 0x%04x)"), exp_csum);
207 printf(_(", unused inodes %u\n"),
208 ext2fs_bg_itable_unused(fs, i));
209 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500210 has_super = ((i==0) || super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400211 if (has_super) {
212 printf (_(" %s superblock at "),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100213 i == 0 ? _("Primary") : _("Backup"));
Theodore Ts'o54434922003-12-07 01:28:50 -0500214 print_number(super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400215 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500216 if (old_desc_blk) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700217 printf("%s", _(", Group descriptors at "));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400218 print_range(old_desc_blk,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500219 old_desc_blk + old_desc_blocks - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500220 if (reserved_gdt) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700221 printf("%s", _("\n Reserved GDT blocks at "));
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500222 print_range(old_desc_blk + old_desc_blocks,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400223 old_desc_blk + old_desc_blocks +
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500224 reserved_gdt - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500225 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500226 } else if (new_desc_blk) {
227 fputc(has_super ? ',' : ' ', stdout);
JP Abgralle0ed7402014-03-19 19:08:39 -0700228 printf("%s", _(" Group descriptor at "));
Theodore Ts'o54434922003-12-07 01:28:50 -0500229 print_number(new_desc_blk);
Theodore Ts'oef344e12003-11-21 09:02:21 -0500230 has_super++;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100231 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500232 if (has_super)
233 fputc('\n', stdout);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100234 fputs(_(" Block bitmap at "), stdout);
JP Abgralle0ed7402014-03-19 19:08:39 -0700235 print_number(ext2fs_block_bitmap_loc(fs, i));
236 print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500237 first_block, last_block);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100238 fputs(_(", Inode bitmap at "), stdout);
JP Abgralle0ed7402014-03-19 19:08:39 -0700239 print_number(ext2fs_inode_bitmap_loc(fs, i));
240 print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500241 first_block, last_block);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100242 fputs(_("\n Inode table at "), stdout);
JP Abgralle0ed7402014-03-19 19:08:39 -0700243 print_range(ext2fs_inode_table_loc(fs, i),
244 ext2fs_inode_table_loc(fs, i) +
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500245 inode_blocks_per_group - 1);
JP Abgralle0ed7402014-03-19 19:08:39 -0700246 print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
Andreas Dilger0e2afdb2010-12-05 22:20:19 -0500247 first_block, last_block);
JP Abgralle0ed7402014-03-19 19:08:39 -0700248 printf (_("\n %u free %s, %u free inodes, "
Jose R. Santos777a8c12007-10-21 21:03:46 -0500249 "%u directories%s"),
JP Abgralle0ed7402014-03-19 19:08:39 -0700250 ext2fs_bg_free_blocks_count(fs, i), units,
251 ext2fs_bg_free_inodes_count(fs, i),
252 ext2fs_bg_used_dirs_count(fs, i),
253 ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
254 if (ext2fs_bg_itable_unused(fs, i))
Jose R. Santos777a8c12007-10-21 21:03:46 -0500255 printf (_(", %u unused inodes\n"),
JP Abgralle0ed7402014-03-19 19:08:39 -0700256 ext2fs_bg_itable_unused(fs, i));
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400257 if (block_bitmap) {
258 fputs(_(" Free blocks: "), stdout);
JP Abgralle0ed7402014-03-19 19:08:39 -0700259 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400260 blk_itr, block_nbytes << 3, block_bitmap);
JP Abgralle0ed7402014-03-19 19:08:39 -0700261 if (retval)
262 com_err("list_desc", retval,
263 "while reading block bitmap");
264 else
265 print_free(i, block_bitmap,
266 fs->super->s_clusters_per_group,
267 fs->super->s_first_data_block,
268 EXT2FS_CLUSTER_RATIO(fs));
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400269 fputc('\n', stdout);
JP Abgralle0ed7402014-03-19 19:08:39 -0700270 blk_itr += fs->super->s_clusters_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400271 }
272 if (inode_bitmap) {
273 fputs(_(" Free inodes: "), stdout);
JP Abgralle0ed7402014-03-19 19:08:39 -0700274 retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400275 ino_itr, inode_nbytes << 3, inode_bitmap);
JP Abgralle0ed7402014-03-19 19:08:39 -0700276 if (retval)
277 com_err("list_desc", retval,
278 "while reading inode bitmap");
279 else
280 print_free(i, inode_bitmap,
281 fs->super->s_inodes_per_group,
282 1, 1);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400283 fputc('\n', stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400284 ino_itr += fs->super->s_inodes_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400285 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286 }
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400287 if (block_bitmap)
288 free(block_bitmap);
289 if (inode_bitmap)
290 free(inode_bitmap);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291}
292
Theodore Ts'o0655b102001-12-21 23:59:46 -0500293static void list_bad_blocks(ext2_filsys fs, int dump)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000294{
295 badblocks_list bb_list = 0;
296 badblocks_iterate bb_iter;
297 blk_t blk;
298 errcode_t retval;
Theodore Ts'o0655b102001-12-21 23:59:46 -0500299 const char *header, *fmt;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300
301 retval = ext2fs_read_bb_inode(fs, &bb_list);
302 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500303 com_err("ext2fs_read_bb_inode", retval, 0);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500304 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000306 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000308 com_err("ext2fs_badblocks_list_iterate_begin", retval,
JP Abgralle0ed7402014-03-19 19:08:39 -0700309 "%s", _("while printing bad block list"));
Theodore Ts'o0655b102001-12-21 23:59:46 -0500310 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000311 }
Theodore Ts'o0655b102001-12-21 23:59:46 -0500312 if (dump) {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400313 header = fmt = "%u\n";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500314 } else {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400315 header = _("Bad blocks: %u");
316 fmt = ", %u";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500317 }
318 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
319 printf(header ? header : fmt, blk);
320 header = 0;
321 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000322 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500323 if (!dump)
324 fputc('\n', stdout);
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400325 ext2fs_badblocks_list_free(bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000326}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000327
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500328static void print_inline_journal_information(ext2_filsys fs)
329{
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400330 journal_superblock_t *jsb;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500331 struct ext2_inode inode;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400332 ext2_file_t journal_file;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500333 errcode_t retval;
334 ino_t ino = fs->super->s_journal_inum;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400335 char buf[1024];
336 __u32 *mask_ptr, mask, m;
337 int i, j, size, printed = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400338
JP Abgralle0ed7402014-03-19 19:08:39 -0700339 if (fs->flags & EXT2_FLAG_IMAGE_FILE)
340 return;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500341 retval = ext2fs_read_inode(fs, ino, &inode);
342 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700343 com_err(program_name, retval, "%s",
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500344 _("while reading journal inode"));
345 exit(1);
346 }
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400347 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
348 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700349 com_err(program_name, retval, "%s",
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400350 _("while opening journal inode"));
351 exit(1);
352 }
353 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
354 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700355 com_err(program_name, retval, "%s",
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400356 _("while reading journal super block"));
357 exit(1);
358 }
359 ext2fs_file_close(journal_file);
360 jsb = (journal_superblock_t *) buf;
361 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700362 fprintf(stderr, "%s",
363 _("Journal superblock magic number invalid!\n"));
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400364 exit(1);
365 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700366 printf("%s", _("Journal features: "));
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400367 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
368 mask = be32_to_cpu(*mask_ptr);
369 for (j=0,m=1; j < 32; j++, m<<=1) {
370 if (mask & m) {
371 printf(" %s", e2p_jrnl_feature2string(i, m));
372 printed++;
373 }
374 }
375 }
376 if (printed == 0)
377 printf(" (none)");
378 printf("\n");
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500379 fputs(_("Journal size: "), stdout);
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400380 if ((fs->super->s_feature_ro_compat &
381 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
382 (inode.i_flags & EXT4_HUGE_FILE_FL))
383 size = inode.i_blocks / (fs->blocksize / 1024);
384 else
385 size = inode.i_blocks >> 1;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500386 if (size < 8192)
387 printf("%uk\n", size);
388 else
389 printf("%uM\n", size >> 10);
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400390 printf(_("Journal length: %u\n"
391 "Journal sequence: 0x%08x\n"
392 "Journal start: %u\n"),
393 (unsigned int)ntohl(jsb->s_maxlen),
394 (unsigned int)ntohl(jsb->s_sequence),
395 (unsigned int)ntohl(jsb->s_start));
JP Abgralle0ed7402014-03-19 19:08:39 -0700396 if (jsb->s_errno != 0)
397 printf(_("Journal errno: %d\n"),
398 (int) ntohl(jsb->s_errno));
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500399}
400
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000401static void print_journal_information(ext2_filsys fs)
402{
403 errcode_t retval;
404 char buf[1024];
405 char str[80];
Theodore Ts'o54434922003-12-07 01:28:50 -0500406 unsigned int i;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000407 journal_superblock_t *jsb;
408
409 /* Get the journal superblock */
JP Abgralle0ed7402014-03-19 19:08:39 -0700410 if ((retval = io_channel_read_blk64(fs->io,
411 fs->super->s_first_data_block + 1,
412 -1024, buf))) {
413 com_err(program_name, retval, "%s",
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000414 _("while reading journal superblock"));
415 exit(1);
416 }
417 jsb = (journal_superblock_t *) buf;
418 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
419 (jsb->s_header.h_blocktype !=
420 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700421 com_err(program_name, 0, "%s",
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000422 _("Couldn't find journal superblock magic numbers"));
423 exit(1);
424 }
425
Takashi Sato8deb80a2006-03-18 21:43:46 -0500426 printf(_("\nJournal block size: %u\n"
427 "Journal length: %u\n"
428 "Journal first block: %u\n"
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100429 "Journal sequence: 0x%08x\n"
Takashi Sato8deb80a2006-03-18 21:43:46 -0500430 "Journal start: %u\n"
Matthias Andreef6567a82006-05-30 16:26:29 +0200431 "Journal number of users: %u\n"),
432 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
433 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
434 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100435
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000436 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000437 uuid_unparse(&jsb->s_users[i*16], str);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100438 printf(i ? " %s\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400439 : _("Journal users: %s\n"),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100440 str);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000441 }
442}
443
JP Abgralle0ed7402014-03-19 19:08:39 -0700444static void parse_extended_opts(const char *opts, blk64_t *superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500445 int *blocksize)
446{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400447 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500448 int len;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400449 int do_usage = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500450
451 len = strlen(opts);
452 buf = malloc(len+1);
453 if (!buf) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700454 fprintf(stderr, "%s",
Theodore Ts'odb197a82008-02-26 19:05:33 -0500455 _("Couldn't allocate memory to parse options!\n"));
456 exit(1);
457 }
458 strcpy(buf, opts);
459 for (token = buf; token && *token; token = next) {
460 p = strchr(token, ',');
461 next = 0;
462 if (p) {
463 *p = 0;
464 next = p+1;
465 }
466 arg = strchr(token, '=');
467 if (arg) {
468 *arg = 0;
469 arg++;
470 }
471 if (strcmp(token, "superblock") == 0 ||
472 strcmp(token, "sb") == 0) {
473 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400474 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500475 badopt = token;
476 continue;
477 }
478 *superblock = strtoul(arg, &p, 0);
479 if (*p) {
480 fprintf(stderr,
481 _("Invalid superblock parameter: %s\n"),
482 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400483 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500484 continue;
485 }
486 } else if (strcmp(token, "blocksize") == 0 ||
487 strcmp(token, "bs") == 0) {
488 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400489 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500490 badopt = token;
491 continue;
492 }
493 *blocksize = strtoul(arg, &p, 0);
494 if (*p) {
495 fprintf(stderr,
496 _("Invalid blocksize parameter: %s\n"),
497 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400498 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500499 continue;
500 }
501 } else {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400502 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500503 badopt = token;
504 }
505 }
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400506 if (do_usage) {
Theodore Ts'odb197a82008-02-26 19:05:33 -0500507 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
508 "Extended options are separated by commas, "
509 "and may take an argument which\n"
510 "\tis set off by an equals ('=') sign.\n\n"
511 "Valid extended options are:\n"
512 "\tsuperblock=<superblock number>\n"
513 "\tblocksize=<blocksize>\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400514 badopt ? badopt : "");
Theodore Ts'odb197a82008-02-26 19:05:33 -0500515 free(buf);
516 exit(1);
517 }
518 free(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400519}
Theodore Ts'odb197a82008-02-26 19:05:33 -0500520
Theodore Ts'o00e54331997-09-16 02:13:52 +0000521int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522{
523 errcode_t retval;
524 ext2_filsys fs;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000525 int print_badblocks = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700526 blk64_t use_superblock = 0;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000527 int use_blocksize = 0;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000528 int image_dump = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000529 int force = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000530 int flags;
Theodore Ts'o27401561999-09-14 20:11:19 +0000531 int header_only = 0;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000532 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000534#ifdef ENABLE_NLS
535 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500536 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000537 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
538 textdomain(NLS_CAT_NAME);
JP Abgralle0ed7402014-03-19 19:08:39 -0700539 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000540#endif
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500541 add_error_table(&et_ext2_error_table);
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400542 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
543 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 if (argc && *argv)
545 program_name = *argv;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400546
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000547 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000548 switch (c) {
549 case 'b':
550 print_badblocks++;
551 break;
Theodore Ts'o27401561999-09-14 20:11:19 +0000552 case 'f':
553 force++;
554 break;
555 case 'h':
556 header_only++;
557 break;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000558 case 'i':
559 image_dump++;
560 break;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000561 case 'o':
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400562 parse_extended_opts(optarg, &use_superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500563 &use_blocksize);
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000564 break;
Theodore Ts'o5c576471997-04-29 15:29:49 +0000565 case 'V':
566 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000567 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o5c576471997-04-29 15:29:49 +0000568 error_message(EXT2_ET_BASE));
569 exit(0);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000570 case 'x':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500571 hex_format++;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000572 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000573 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000574 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000575 }
576 }
577 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000578 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000579 device_name = argv[optind++];
JP Abgralle0ed7402014-03-19 19:08:39 -0700580 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000581 if (force)
582 flags |= EXT2_FLAG_FORCE;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000583 if (image_dump)
584 flags |= EXT2_FLAG_IMAGE_FILE;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400585
Theodore Ts'odb197a82008-02-26 19:05:33 -0500586 if (use_superblock && !use_blocksize) {
587 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
588 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
589 use_blocksize *= 2) {
590 retval = ext2fs_open (device_name, flags,
591 use_superblock,
592 use_blocksize, unix_io_manager,
593 &fs);
594 if (!retval)
595 break;
596 }
597 } else
598 retval = ext2fs_open (device_name, flags, use_superblock,
599 use_blocksize, unix_io_manager, &fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000600 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000601 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000602 device_name);
JP Abgralle0ed7402014-03-19 19:08:39 -0700603 printf("%s", _("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000604 exit (1);
605 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700606 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
607 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
608 blocks64 = 1;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000609 if (print_badblocks) {
Theodore Ts'o0655b102001-12-21 23:59:46 -0500610 list_bad_blocks(fs, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000611 } else {
Theodore Ts'o27401561999-09-14 20:11:19 +0000612 list_super (fs->super);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000613 if (fs->super->s_feature_incompat &
614 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
615 print_journal_information(fs);
616 ext2fs_close(fs);
617 exit(0);
618 }
Theodore Ts'oa11d0742008-11-15 15:05:51 -0500619 if ((fs->super->s_feature_compat &
620 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
621 (fs->super->s_journal_inum != 0))
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500622 print_inline_journal_information(fs);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500623 list_bad_blocks(fs, 0);
Theodore Ts'o27401561999-09-14 20:11:19 +0000624 if (header_only) {
625 ext2fs_close (fs);
626 exit (0);
627 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000628 retval = ext2fs_read_bitmaps (fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000629 list_desc (fs);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400630 if (retval) {
631 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
632 program_name, device_name,
633 error_message(retval));
634 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000635 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636 ext2fs_close (fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500637 remove_error_table(&et_ext2_error_table);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000638 exit (0);
639}