blob: 14931b0eec7bf6aefb705264a43502911a3fc17d [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'o4ea7bd02001-12-16 23:23:37 -050049const char *num_format = "%lu";
Theodore Ts'o0655b102001-12-21 23:59:46 -050050char range_format[16];
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'o348e43d2001-05-03 14:43:43 +000054 fprintf (stderr, _("Usage: %s [-bfhixV] [-ob superblock] "
Theodore Ts'od9c56d32000-02-08 00:47:55 +000055 "[-oB blocksize] device\n"), program_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +000056 exit (1);
57}
58
59static void print_free (unsigned long group, char * bitmap,
60 unsigned long nbytes, unsigned long offset)
61{
62 int p = 0;
63 unsigned long i;
64 unsigned long j;
65
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010066 offset += group * nbytes;
Theodore Ts'o3839e651997-04-26 13:21:57 +000067 for (i = 0; i < nbytes; i++)
68 if (!in_use (bitmap, i))
69 {
70 if (p)
71 printf (", ");
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010072 printf (num_format, i + offset);
73 for (j = i; j < nbytes && !in_use (bitmap, j); j++)
74 ;
75 if (--j != i) {
76 fputc('-', stdout);
77 printf(num_format, j + offset);
78 i = j;
Theodore Ts'o3839e651997-04-26 13:21:57 +000079 }
80 p = 1;
81 }
82}
83
84static void list_desc (ext2_filsys fs)
85{
86 unsigned long i;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +010087 long diff;
Theodore Ts'o521e3681997-04-29 17:48:10 +000088 blk_t group_blk, next_blk;
Theodore Ts'of3db3561997-04-26 13:34:30 +000089 char * block_bitmap = fs->block_map->bitmap;
90 char * inode_bitmap = fs->inode_map->bitmap;
Theodore Ts'o80c22c92000-08-14 15:32:11 +000091 int inode_blocks_per_group;
92 int group_desc_blocks;
93
94 inode_blocks_per_group = ((fs->super->s_inodes_per_group *
95 EXT2_INODE_SIZE(fs->super)) +
96 EXT2_BLOCK_SIZE(fs->super) - 1) /
97 EXT2_BLOCK_SIZE(fs->super);
98 group_desc_blocks = ((fs->super->s_blocks_count -
99 fs->super->s_first_data_block +
100 EXT2_BLOCKS_PER_GROUP(fs->super) - 1) /
101 EXT2_BLOCKS_PER_GROUP(fs->super) +
102 EXT2_DESC_PER_BLOCK(fs->super) - 1) /
103 EXT2_DESC_PER_BLOCK(fs->super);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000104
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100105 fputc('\n', stdout);
Theodore Ts'o521e3681997-04-29 17:48:10 +0000106 group_blk = fs->super->s_first_data_block;
107 for (i = 0; i < fs->group_desc_count; i++) {
108 next_blk = group_blk + fs->super->s_blocks_per_group;
109 if (next_blk > fs->super->s_blocks_count)
110 next_blk = fs->super->s_blocks_count;
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100111 printf (_("Group %lu: (Blocks "), i);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500112 printf(range_format, group_blk, next_blk - 1);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100113 fputs(")\n", stdout);
114 if (ext2fs_bg_has_super (fs, i)) {
115 printf (_(" %s Superblock at "),
116 i == 0 ? _("Primary") : _("Backup"));
117 printf(num_format, group_blk);
118 printf(_(", Group Descriptors at "));
Theodore Ts'o0655b102001-12-21 23:59:46 -0500119 printf(range_format, group_blk+1,
120 group_blk + group_desc_blocks);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100121 fputc('\n', stdout);
122 }
123 fputs(_(" Block bitmap at "), stdout);
124 printf(num_format, fs->group_desc[i].bg_block_bitmap);
125 diff = fs->group_desc[i].bg_block_bitmap - group_blk;
126 if (diff >= 0)
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500127 printf(" (+%ld)", diff);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100128 fputs(_(", Inode bitmap at "), stdout);
129 printf(num_format, fs->group_desc[i].bg_inode_bitmap);
130 diff = fs->group_desc[i].bg_inode_bitmap - group_blk;
131 if (diff >= 0)
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500132 printf(" (+%ld)", diff);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100133 fputs(_("\n Inode table at "), stdout);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500134 printf(range_format, fs->group_desc[i].bg_inode_table,
135 fs->group_desc[i].bg_inode_table +
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100136 inode_blocks_per_group - 1);
137 diff = fs->group_desc[i].bg_inode_table - group_blk;
138 if (diff > 0)
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500139 printf(" (+%ld)", diff);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100140 printf (_("\n %d free blocks, %d free inodes, "
141 "%d directories\n Free blocks: "),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000142 fs->group_desc[i].bg_free_blocks_count,
143 fs->group_desc[i].bg_free_inodes_count,
144 fs->group_desc[i].bg_used_dirs_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000145 print_free (i, block_bitmap, fs->super->s_blocks_per_group,
146 fs->super->s_first_data_block);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100147 fputs(_("\n Free inodes: "), stdout);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148 print_free (i, inode_bitmap, fs->super->s_inodes_per_group, 1);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100149 fputc('\n', stdout);
150 block_bitmap += fs->super->s_blocks_per_group / 8;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151 inode_bitmap += fs->super->s_inodes_per_group / 8;
Theodore Ts'o521e3681997-04-29 17:48:10 +0000152 group_blk = next_blk;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000153 }
154}
155
Theodore Ts'o0655b102001-12-21 23:59:46 -0500156static void list_bad_blocks(ext2_filsys fs, int dump)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157{
158 badblocks_list bb_list = 0;
159 badblocks_iterate bb_iter;
160 blk_t blk;
161 errcode_t retval;
Theodore Ts'o0655b102001-12-21 23:59:46 -0500162 const char *header, *fmt;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000163
164 retval = ext2fs_read_bb_inode(fs, &bb_list);
165 if (retval) {
166 com_err("ext2fs_read_bb_inode", retval, "");
Theodore Ts'o0655b102001-12-21 23:59:46 -0500167 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000168 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000169 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000170 if (retval) {
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000171 com_err("ext2fs_badblocks_list_iterate_begin", retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000172 _("while printing bad block list"));
Theodore Ts'o0655b102001-12-21 23:59:46 -0500173 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174 }
Theodore Ts'o0655b102001-12-21 23:59:46 -0500175 if (dump) {
176 header = fmt = "%d\n";
177 } else {
178 header = _("Bad blocks: %d");
179 fmt = ", %d";
180 }
181 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
182 printf(header ? header : fmt, blk);
183 header = 0;
184 }
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000185 ext2fs_badblocks_list_iterate_end(bb_iter);
Theodore Ts'o0655b102001-12-21 23:59:46 -0500186 if (!dump)
187 fputc('\n', stdout);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000188}
Theodore Ts'of3db3561997-04-26 13:34:30 +0000189
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000190static void print_journal_information(ext2_filsys fs)
191{
192 errcode_t retval;
193 char buf[1024];
194 char str[80];
195 int i;
196 journal_superblock_t *jsb;
197
198 /* Get the journal superblock */
Theodore Ts'o02088862001-01-18 01:44:19 +0000199 if ((retval = io_channel_read_blk(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000200 com_err(program_name, retval,
201 _("while reading journal superblock"));
202 exit(1);
203 }
204 jsb = (journal_superblock_t *) buf;
205 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
206 (jsb->s_header.h_blocktype !=
207 (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
208 com_err(program_name, 0,
209 _("Couldn't find journal superblock magic numbers"));
210 exit(1);
211 }
212
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100213 printf(_("\nJournal block size: %d\n"
214 "Journal length: %d\n"
215 "Journal first block: %d\n"
216 "Journal sequence: 0x%08x\n"
217 "Journal start: %d\n"
218 "Journal number of users: %d\n"),
219 ntohl(jsb->s_blocksize), ntohl(jsb->s_maxlen),
220 ntohl(jsb->s_first), ntohl(jsb->s_sequence),
221 ntohl(jsb->s_start), ntohl(jsb->s_nr_users));
222
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000223 for (i=0; i < ntohl(jsb->s_nr_users); i++) {
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000224 uuid_unparse(&jsb->s_users[i*16], str);
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100225 printf(i ? " %s\n"
226 : "Journal users: %s\n",
227 str);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000228 }
229}
230
Theodore Ts'o00e54331997-09-16 02:13:52 +0000231int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000232{
233 errcode_t retval;
234 ext2_filsys fs;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000235 int print_badblocks = 0;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000236 int use_superblock = 0;
237 int use_blocksize = 0;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000238 int image_dump = 0;
Theodore Ts'o27401561999-09-14 20:11:19 +0000239 int force = 0;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000240 int flags;
Theodore Ts'o27401561999-09-14 20:11:19 +0000241 int header_only = 0;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000242 int big_endian;
Theodore Ts'o519149f1997-10-25 03:49:49 +0000243 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000245#ifdef ENABLE_NLS
246 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500247 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000248 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
249 textdomain(NLS_CAT_NAME);
250#endif
Theodore Ts'o5c576471997-04-29 15:29:49 +0000251 initialize_ext2_error_table();
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400252 fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
253 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 if (argc && *argv)
255 program_name = *argv;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000256
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000257 while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000258 switch (c) {
259 case 'b':
260 print_badblocks++;
261 break;
Theodore Ts'o27401561999-09-14 20:11:19 +0000262 case 'f':
263 force++;
264 break;
265 case 'h':
266 header_only++;
267 break;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000268 case 'i':
269 image_dump++;
270 break;
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000271 case 'o':
272 if (optarg[0] == 'b')
273 use_superblock = atoi(optarg+1);
274 else if (optarg[0] == 'B')
275 use_blocksize = atoi(optarg+1);
276 else
277 usage();
278 break;
Theodore Ts'o5c576471997-04-29 15:29:49 +0000279 case 'V':
280 /* Print version number and exit */
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000281 fprintf(stderr, _("\tUsing %s\n"),
Theodore Ts'o5c576471997-04-29 15:29:49 +0000282 error_message(EXT2_ET_BASE));
283 exit(0);
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000284 case 'x':
Theodore Ts'oa5f0bb92001-12-02 19:29:35 +0100285 num_format = "0x%04x";
Theodore Ts'o80c22c92000-08-14 15:32:11 +0000286 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000287 default:
Theodore Ts'o818180c1998-06-27 05:11:14 +0000288 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000289 }
290 }
291 if (optind > argc - 1)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000292 usage();
Theodore Ts'o0655b102001-12-21 23:59:46 -0500293 sprintf(range_format, "%s-%s", num_format, num_format);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000294 device_name = argv[optind++];
Theodore Ts'o02e7dd91999-06-18 00:48:41 +0000295 if (use_superblock && !use_blocksize)
296 use_blocksize = 1024;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000297 flags = EXT2_FLAG_JOURNAL_DEV_OK;
298 if (force)
299 flags |= EXT2_FLAG_FORCE;
Theodore Ts'o348e43d2001-05-03 14:43:43 +0000300 if (image_dump)
301 flags |= EXT2_FLAG_IMAGE_FILE;
302
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000303 retval = ext2fs_open (device_name, flags, use_superblock,
304 use_blocksize, unix_io_manager, &fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000305 if (retval) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000306 com_err (program_name, retval, _("while trying to open %s"),
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 device_name);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000308 printf (_("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309 exit (1);
310 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000311 if (print_badblocks) {
Theodore Ts'o0655b102001-12-21 23:59:46 -0500312 list_bad_blocks(fs, 1);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000313 } else {
Theodore Ts'o27401561999-09-14 20:11:19 +0000314 big_endian = ((fs->flags & EXT2_FLAG_SWAP_BYTES) != 0);
Theodore Ts'ocbbf0312001-06-13 00:12:04 +0000315#ifdef WORDS_BIGENDIAN
316 big_endian = !big_endian;
317#endif
Theodore Ts'o27401561999-09-14 20:11:19 +0000318 if (big_endian)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000319 printf(_("Note: This is a byte-swapped filesystem\n"));
Theodore Ts'o27401561999-09-14 20:11:19 +0000320 list_super (fs->super);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000321 if (fs->super->s_feature_incompat &
322 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
323 print_journal_information(fs);
324 ext2fs_close(fs);
325 exit(0);
326 }
Theodore Ts'o0655b102001-12-21 23:59:46 -0500327 list_bad_blocks(fs, 0);
Theodore Ts'o27401561999-09-14 20:11:19 +0000328 if (header_only) {
329 ext2fs_close (fs);
330 exit (0);
331 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000332 retval = ext2fs_read_bitmaps (fs);
333 if (retval) {
334 com_err (program_name, retval,
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000335 _("while trying to read the bitmaps"),
Theodore Ts'of3db3561997-04-26 13:34:30 +0000336 device_name);
337 ext2fs_close (fs);
338 exit (1);
339 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000340 list_desc (fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000342 ext2fs_close (fs);
343 exit (0);
344}