blob: 6ec0858eb78babb44ef62509b06e766d3cda9a63 [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'o3839e651997-04-26 13:21:57 +000074static void print_free (unsigned long group, char * bitmap,
75 unsigned long nbytes, unsigned long offset)
76{
77 int p = 0;
78 unsigned long i;
79 unsigned long j;
80
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010081 offset += group * nbytes;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 for (i = 0; i < nbytes; i++)
83 if (!in_use (bitmap, i))
84 {
85 if (p)
86 printf (", ");
Theodore Ts'o54434922003-12-07 01:28:50 -050087 print_number(i + offset);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010088 for (j = i; j < nbytes && !in_use (bitmap, j); j++)
89 ;
90 if (--j != i) {
91 fputc('-', stdout);
Theodore Ts'o54434922003-12-07 01:28:50 -050092 print_number(j + offset);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010093 i = j;
Theodore Ts'o3839e651997-04-26 13:21:57 +000094 }
95 p = 1;
96 }
97}
98
Theodore Ts'of5fa2002006-05-08 20:17:26 -040099static void print_bg_opt(int bg_flags, int mask,
100 const char *str, int *first)
101{
102 if (bg_flags & mask) {
103 if (*first) {
104 fputs(" [", stdout);
105 *first = 0;
106 } else
107 fputs(", ", stdout);
108 fputs(str, stdout);
109 }
110}
111static void print_bg_opts(ext2_filsys fs, dgrp_t i)
112{
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400113 int first = 1, bg_flags = 0;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400114
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400115 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
Theodore Ts'o732c8cd2009-09-07 21:15:12 -0400116 bg_flags = ext2fs_bg_flags(fs, i);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400117
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400118 print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400119 &first);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400120 print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
121 &first);
122 print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400123 &first);
124 if (!first)
125 fputc(']', stdout);
126 fputc('\n', stdout);
127}
128
Theodore Ts'o3839e651997-04-26 13:21:57 +0000129static void list_desc (ext2_filsys fs)
130{
131 unsigned long i;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100132 long diff;
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400133 blk64_t first_block, last_block;
134 blk64_t super_blk, old_desc_blk, new_desc_blk;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400135 char *block_bitmap=NULL, *inode_bitmap=NULL;
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500136 int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400137 int block_nbytes, inode_nbytes;
Theodore Ts'oef344e12003-11-21 09:02:21 -0500138 int has_super;
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400139 blk64_t blk_itr = fs->super->s_first_data_block;
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400140 ext2_ino_t ino_itr = 1;
141
142 block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
143 inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000144
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400145 if (fs->block_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400146 block_bitmap = malloc(block_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400147 if (fs->inode_map)
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400148 inode_bitmap = malloc(inode_nbytes);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400149
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000150 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
151 EXT2_INODE_SIZE(fs->super)) +
152 EXT2_BLOCK_SIZE(fs->super) - 1) /
153 EXT2_BLOCK_SIZE(fs->super);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500154 reserved_gdt = fs->super->s_reserved_gdt_blocks;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100155 fputc('\n', stdout);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400156 first_block = fs->super->s_first_data_block;
Theodore Ts'o76dd5e52002-10-30 23:07:21 -0500157 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
158 old_desc_blocks = fs->super->s_first_meta_bg;
159 else
160 old_desc_blocks = fs->desc_blocks;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000161 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'ob49f78f2009-10-25 21:24:06 -0400162 first_block = ext2fs_group_first_block2(fs, i);
163 last_block = ext2fs_group_last_block2(fs, i);
Eric Sandeenabf23432006-09-12 14:56:16 -0400164
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400165 ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
166 &old_desc_blk, &new_desc_blk, 0);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400167
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100168 printf (_("Group %lu: (Blocks "), i);
Eric Sandeenbb1a46a2006-09-12 14:55:22 -0400169 print_range(first_block, last_block);
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400170 fputs(")", stdout);
171 print_bg_opts(fs, i);
Theodore Ts'ob89fc302008-04-01 14:59:47 -0400172 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400173 printf(_(" Checksum 0x%04x, unused inodes %u\n"),
174 ext2fs_bg_checksum(fs, i),
175 ext2fs_bg_itable_unused(fs, i));
Theodore Ts'oef344e12003-11-21 09:02:21 -0500176 has_super = ((i==0) || super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400177 if (has_super) {
178 printf (_(" %s superblock at "),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100179 i == 0 ? _("Primary") : _("Backup"));
Theodore Ts'o54434922003-12-07 01:28:50 -0500180 print_number(super_blk);
Theodore Ts'oc046ac72002-10-20 00:38:57 -0400181 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500182 if (old_desc_blk) {
183 printf(_(", Group descriptors at "));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400184 print_range(old_desc_blk,
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500185 old_desc_blk + old_desc_blocks - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500186 if (reserved_gdt) {
187 printf(_("\n Reserved GDT blocks at "));
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500188 print_range(old_desc_blk + old_desc_blocks,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400189 old_desc_blk + old_desc_blocks +
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500190 reserved_gdt - 1);
Theodore Ts'o35238dd2004-12-23 13:54:28 -0500191 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500192 } else if (new_desc_blk) {
193 fputc(has_super ? ',' : ' ', stdout);
194 printf(_(" Group descriptor at "));
Theodore Ts'o54434922003-12-07 01:28:50 -0500195 print_number(new_desc_blk);
Theodore Ts'oef344e12003-11-21 09:02:21 -0500196 has_super++;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100197 }
Theodore Ts'oef344e12003-11-21 09:02:21 -0500198 if (has_super)
199 fputc('\n', stdout);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100200 fputs(_(" Block bitmap at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400201 print_number(ext2fs_block_bitmap_loc(fs, i));
202 diff = ext2fs_block_bitmap_loc(fs, i) - first_block;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100203 if (diff >= 0)
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500204 printf(" (+%ld)", diff);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100205 fputs(_(", Inode bitmap at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400206 print_number(ext2fs_inode_bitmap_loc(fs, i));
207 diff = ext2fs_inode_bitmap_loc(fs, i) - first_block;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100208 if (diff >= 0)
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500209 printf(" (+%ld)", diff);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100210 fputs(_("\n Inode table at "), stdout);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400211 print_range(ext2fs_inode_table_loc(fs, i),
212 ext2fs_inode_table_loc(fs, i) +
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500213 inode_blocks_per_group - 1);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400214 diff = ext2fs_inode_table_loc(fs, i) - first_block;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100215 if (diff > 0)
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500216 printf(" (+%ld)", diff);
Jose R. Santos777a8c12007-10-21 21:03:46 -0500217 printf (_("\n %u free blocks, %u free inodes, "
218 "%u directories%s"),
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400219 ext2fs_bg_free_blocks_count(fs, i),
220 ext2fs_bg_free_inodes_count(fs, i),
221 ext2fs_bg_used_dirs_count(fs, i),
222 ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
223 if (ext2fs_bg_itable_unused(fs, i))
Jose R. Santos777a8c12007-10-21 21:03:46 -0500224 printf (_(", %u unused inodes\n"),
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400225 ext2fs_bg_itable_unused(fs, i));
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400226 if (block_bitmap) {
227 fputs(_(" Free blocks: "), stdout);
Valerie Aurora Henson3c041a52009-08-22 21:15:30 -0400228 ext2fs_get_block_bitmap_range2(fs->block_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400229 blk_itr, block_nbytes << 3, block_bitmap);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400230 print_free (i, block_bitmap,
231 fs->super->s_blocks_per_group,
232 fs->super->s_first_data_block);
233 fputc('\n', stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400234 blk_itr += fs->super->s_blocks_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400235 }
236 if (inode_bitmap) {
237 fputs(_(" Free inodes: "), stdout);
Valerie Aurora Henson3c041a52009-08-22 21:15:30 -0400238 ext2fs_get_inode_bitmap_range2(fs->inode_map,
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400239 ino_itr, inode_nbytes << 3, inode_bitmap);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400240 print_free (i, inode_bitmap,
241 fs->super->s_inodes_per_group, 1);
242 fputc('\n', stdout);
Theodore Ts'of1f115a2007-07-23 04:32:48 -0400243 ino_itr += fs->super->s_inodes_per_group;
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400244 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000245 }
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400246 if (block_bitmap)
247 free(block_bitmap);
248 if (inode_bitmap)
249 free(inode_bitmap);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250}
251
Theodore Ts'o0655b102001-12-21 23:59:46 -0500252static void list_bad_blocks(ext2_filsys fs, int dump)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000253{
254 badblocks_list bb_list = 0;
255 badblocks_iterate bb_iter;
256 blk_t blk;
257 errcode_t retval;
Theodore Ts'o0655b102001-12-21 23:59:46 -0500258 const char *header, *fmt;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000259
260 retval = ext2fs_read_bb_inode(fs, &bb_list);
261 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500262 com_err("ext2fs_read_bb_inode", retval, 0);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500263 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000264 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000265 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000267 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000268 _("while printing bad block list"));
Theodore Ts'o0655b102001-12-21 23:59:46 -0500269 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 }
Theodore Ts'o0655b102001-12-21 23:59:46 -0500271 if (dump) {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400272 header = fmt = "%u\n";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500273 } else {
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400274 header = _("Bad blocks: %u");
275 fmt = ", %u";
Theodore Ts'o0655b102001-12-21 23:59:46 -0500276 }
277 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
278 printf(header ? header : fmt, blk);
279 header = 0;
280 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000281 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500282 if (!dump)
283 fputc('\n', stdout);
Theodore Ts'o1acde2b2009-06-15 03:53:04 -0400284 ext2fs_badblocks_list_free(bb_list);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000285}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000286
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500287static void print_inline_journal_information(ext2_filsys fs)
288{
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400289 journal_superblock_t *jsb;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500290 struct ext2_inode inode;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400291 ext2_file_t journal_file;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500292 errcode_t retval;
293 ino_t ino = fs->super->s_journal_inum;
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400294 char buf[1024];
295 __u32 *mask_ptr, mask, m;
296 int i, j, size, printed = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400297
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500298 retval = ext2fs_read_inode(fs, ino, &inode);
299 if (retval) {
300 com_err(program_name, retval,
301 _("while reading journal inode"));
302 exit(1);
303 }
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400304 retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
305 if (retval) {
306 com_err(program_name, retval,
307 _("while opening journal inode"));
308 exit(1);
309 }
310 retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
311 if (retval) {
312 com_err(program_name, retval,
313 _("while reading journal super block"));
314 exit(1);
315 }
316 ext2fs_file_close(journal_file);
317 jsb = (journal_superblock_t *) buf;
318 if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
319 fprintf(stderr,
320 "Journal superblock magic number invalid!\n");
321 exit(1);
322 }
323 printf(_("Journal features: "));
324 for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
325 mask = be32_to_cpu(*mask_ptr);
326 for (j=0,m=1; j < 32; j++, m<<=1) {
327 if (mask & m) {
328 printf(" %s", e2p_jrnl_feature2string(i, m));
329 printed++;
330 }
331 }
332 }
333 if (printed == 0)
334 printf(" (none)");
335 printf("\n");
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500336 fputs(_("Journal size: "), stdout);
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400337 if ((fs->super->s_feature_ro_compat &
338 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
339 (inode.i_flags & EXT4_HUGE_FILE_FL))
340 size = inode.i_blocks / (fs->blocksize / 1024);
341 else
342 size = inode.i_blocks >> 1;
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500343 if (size < 8192)
344 printf("%uk\n", size);
345 else
346 printf("%uM\n", size >> 10);
Theodore Ts'o1d9b8182009-09-06 18:55:09 -0400347 printf(_("Journal length: %u\n"
348 "Journal sequence: 0x%08x\n"
349 "Journal start: %u\n"),
350 (unsigned int)ntohl(jsb->s_maxlen),
351 (unsigned int)ntohl(jsb->s_sequence),
352 (unsigned int)ntohl(jsb->s_start));
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500353}
354
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000355static void print_journal_information(ext2_filsys fs)
356{
357 errcode_t retval;
358 char buf[1024];
359 char str[80];
Theodore Ts'o54434922003-12-07 01:28:50 -0500360 unsigned int i;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000361 journal_superblock_t *jsb;
362
363 /* Get the journal superblock */
Valerie Aurora Henson24a117a2009-09-07 21:14:24 -0400364 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 +0000365 com_err(program_name, retval,
366 _("while reading journal superblock"));
367 exit(1);
368 }
369 jsb = (journal_superblock_t *) buf;
370 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
371 (jsb->s_header.h_blocktype !=
372 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
373 com_err(program_name, 0,
374 _("Couldn't find journal superblock magic numbers"));
375 exit(1);
376 }
377
Takashi Sato8deb80a2006-03-18 21:43:46 -0500378 printf(_("\nJournal block size: %u\n"
379 "Journal length: %u\n"
380 "Journal first block: %u\n"
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100381 "Journal sequence: 0x%08x\n"
Takashi Sato8deb80a2006-03-18 21:43:46 -0500382 "Journal start: %u\n"
Matthias Andreef6567a82006-05-30 16:26:29 +0200383 "Journal number of users: %u\n"),
384 (unsigned int)ntohl(jsb->s_blocksize), (unsigned int)ntohl(jsb->s_maxlen),
385 (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
386 (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100387
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000388 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000389 uuid_unparse(&jsb->s_users[i*16], str);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100390 printf(i ? " %s\n"
Theodore Ts'obb145b02005-06-20 08:35:27 -0400391 : _("Journal users: %s\n"),
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100392 str);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000393 }
394}
395
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400396static void parse_extended_opts(const char *opts, blk64_t *superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500397 int *blocksize)
398{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400399 char *buf, *token, *next, *p, *arg, *badopt = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500400 int len;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400401 int do_usage = 0;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500402
403 len = strlen(opts);
404 buf = malloc(len+1);
405 if (!buf) {
406 fprintf(stderr,
407 _("Couldn't allocate memory to parse options!\n"));
408 exit(1);
409 }
410 strcpy(buf, opts);
411 for (token = buf; token && *token; token = next) {
412 p = strchr(token, ',');
413 next = 0;
414 if (p) {
415 *p = 0;
416 next = p+1;
417 }
418 arg = strchr(token, '=');
419 if (arg) {
420 *arg = 0;
421 arg++;
422 }
423 if (strcmp(token, "superblock") == 0 ||
424 strcmp(token, "sb") == 0) {
425 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400426 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500427 badopt = token;
428 continue;
429 }
430 *superblock = strtoul(arg, &p, 0);
431 if (*p) {
432 fprintf(stderr,
433 _("Invalid superblock parameter: %s\n"),
434 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400435 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500436 continue;
437 }
438 } else if (strcmp(token, "blocksize") == 0 ||
439 strcmp(token, "bs") == 0) {
440 if (!arg) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400441 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500442 badopt = token;
443 continue;
444 }
445 *blocksize = strtoul(arg, &p, 0);
446 if (*p) {
447 fprintf(stderr,
448 _("Invalid blocksize parameter: %s\n"),
449 arg);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400450 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500451 continue;
452 }
453 } else {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400454 do_usage++;
Theodore Ts'odb197a82008-02-26 19:05:33 -0500455 badopt = token;
456 }
457 }
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400458 if (do_usage) {
Theodore Ts'odb197a82008-02-26 19:05:33 -0500459 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
460 "Extended options are separated by commas, "
461 "and may take an argument which\n"
462 "\tis set off by an equals ('=') sign.\n\n"
463 "Valid extended options are:\n"
464 "\tsuperblock=<superblock number>\n"
465 "\tblocksize=<blocksize>\n"),
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400466 badopt ? badopt : "");
Theodore Ts'odb197a82008-02-26 19:05:33 -0500467 free(buf);
468 exit(1);
469 }
470 free(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400471}
Theodore Ts'odb197a82008-02-26 19:05:33 -0500472
Theodore Ts'o00e54331997-09-16 02:13:52 +0000473int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000474{
475 errcode_t retval;
476 ext2_filsys fs;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000477 int print_badblocks = 0;
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400478 blk64_t use_superblock = 0;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000479 int use_blocksize = 0;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000480 int image_dump = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000481 int force = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000482 int flags;
Theodore Ts'o27401561999-09-14 20:11:19 +0000483 int header_only = 0;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000484 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000486#ifdef ENABLE_NLS
487 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500488 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000489 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
490 textdomain(NLS_CAT_NAME);
491#endif
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500492 add_error_table(&et_ext2_error_table);
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400493 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
494 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495 if (argc && *argv)
496 program_name = *argv;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400497
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000498 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000499 switch (c) {
500 case 'b':
501 print_badblocks++;
502 break;
Theodore Ts'o27401561999-09-14 20:11:19 +0000503 case 'f':
504 force++;
505 break;
506 case 'h':
507 header_only++;
508 break;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000509 case 'i':
510 image_dump++;
511 break;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000512 case 'o':
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400513 parse_extended_opts(optarg, &use_superblock,
Theodore Ts'odb197a82008-02-26 19:05:33 -0500514 &use_blocksize);
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000515 break;
Theodore Ts'o5c576471997-04-29 15:29:49 +0000516 case 'V':
517 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000518 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o5c576471997-04-29 15:29:49 +0000519 error_message(EXT2_ET_BASE));
520 exit(0);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000521 case 'x':
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500522 hex_format++;
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000523 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000524 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000525 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000526 }
527 }
528 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000529 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000530 device_name = argv[optind++];
Theodore Ts'o295c3e02010-06-13 15:00:00 -0400531 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000532 if (force)
533 flags |= EXT2_FLAG_FORCE;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000534 if (image_dump)
535 flags |= EXT2_FLAG_IMAGE_FILE;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400536
Theodore Ts'odb197a82008-02-26 19:05:33 -0500537 if (use_superblock && !use_blocksize) {
538 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
539 use_blocksize <= EXT2_MAX_BLOCK_SIZE;
540 use_blocksize *= 2) {
541 retval = ext2fs_open (device_name, flags,
542 use_superblock,
543 use_blocksize, unix_io_manager,
544 &fs);
545 if (!retval)
546 break;
547 }
548 } else
549 retval = ext2fs_open (device_name, flags, use_superblock,
550 use_blocksize, unix_io_manager, &fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000551 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000552 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 device_name);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000554 printf (_("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 exit (1);
556 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000557 if (print_badblocks) {
Theodore Ts'o0655b102001-12-21 23:59:46 -0500558 list_bad_blocks(fs, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000559 } else {
Theodore Ts'o27401561999-09-14 20:11:19 +0000560 list_super (fs->super);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000561 if (fs->super->s_feature_incompat &
562 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
563 print_journal_information(fs);
564 ext2fs_close(fs);
565 exit(0);
566 }
Theodore Ts'oa11d0742008-11-15 15:05:51 -0500567 if ((fs->super->s_feature_compat &
568 EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
569 (fs->super->s_journal_inum != 0))
Theodore Ts'o6515a6f2006-03-29 14:03:07 -0500570 print_inline_journal_information(fs);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500571 list_bad_blocks(fs, 0);
Theodore Ts'o27401561999-09-14 20:11:19 +0000572 if (header_only) {
573 ext2fs_close (fs);
574 exit (0);
575 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000576 retval = ext2fs_read_bitmaps (fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000577 list_desc (fs);
Theodore Ts'od90a23e2002-10-25 17:07:11 -0400578 if (retval) {
579 printf(_("\n%s: %s: error reading bitmaps: %s\n"),
580 program_name, device_name,
581 error_message(retval));
582 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000583 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584 ext2fs_close (fs);
Theodore Ts'oa6d83022006-12-26 03:38:07 -0500585 remove_error_table(&et_ext2_error_table);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000586 exit (0);
587}