dumpe2fs.c revision a5f0bb9d1b295ad907b2d8a58ce9121f4105e1a8
1/*
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 *
9 * 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%
15 */
16
17/*
18 * History:
19 * 94/01/09	- Creation
20 * 94/02/27	- Ported to use the ext2fs library
21 */
22
23#ifdef HAVE_GETOPT_H
24#include <getopt.h>
25#else
26extern char *optarg;
27extern int optind;
28#endif
29#include <fcntl.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
35#include "ext2fs/ext2_fs.h"
36
37#include "ext2fs/ext2fs.h"
38#include "e2p/e2p.h"
39#include "jfs_user.h"
40
41#include "../version.h"
42#include "nls-enable.h"
43
44#define in_use(m, x)	(ext2fs_test_bit ((x), (m)))
45
46const char * program_name = "dumpe2fs";
47char * device_name = NULL;
48char *num_format = "%lu";
49
50static void usage(void)
51{
52	fprintf (stderr, _("Usage: %s [-bfhixV] [-ob superblock] "
53		 "[-oB blocksize] device\n"), program_name);
54	exit (1);
55}
56
57static void print_free (unsigned long group, char * bitmap,
58			unsigned long nbytes, unsigned long offset)
59{
60	int p = 0;
61	unsigned long i;
62	unsigned long j;
63
64	offset += group * nbytes;
65	for (i = 0; i < nbytes; i++)
66		if (!in_use (bitmap, i))
67		{
68			if (p)
69				printf (", ");
70			printf (num_format, i + offset);
71			for (j = i; j < nbytes && !in_use (bitmap, j); j++)
72				;
73			if (--j != i) {
74				fputc('-', stdout);
75				printf(num_format, j + offset);
76				i = j;
77			}
78			p = 1;
79		}
80}
81
82static void list_desc (ext2_filsys fs)
83{
84	unsigned long i;
85	long diff;
86	blk_t	group_blk, next_blk;
87	char * block_bitmap = fs->block_map->bitmap;
88	char * inode_bitmap = fs->inode_map->bitmap;
89	int inode_blocks_per_group;
90	int group_desc_blocks;
91
92	inode_blocks_per_group = ((fs->super->s_inodes_per_group *
93				   EXT2_INODE_SIZE(fs->super)) +
94				  EXT2_BLOCK_SIZE(fs->super) - 1) /
95				 EXT2_BLOCK_SIZE(fs->super);
96	group_desc_blocks = ((fs->super->s_blocks_count -
97			      fs->super->s_first_data_block +
98			      EXT2_BLOCKS_PER_GROUP(fs->super) - 1) /
99			     EXT2_BLOCKS_PER_GROUP(fs->super) +
100			     EXT2_DESC_PER_BLOCK(fs->super) - 1) /
101			    EXT2_DESC_PER_BLOCK(fs->super);
102
103	fputc('\n', stdout);
104	group_blk = fs->super->s_first_data_block;
105	for (i = 0; i < fs->group_desc_count; i++) {
106		next_blk = group_blk + fs->super->s_blocks_per_group;
107		if (next_blk > fs->super->s_blocks_count)
108			next_blk = fs->super->s_blocks_count;
109		printf (_("Group %lu: (Blocks "), i);
110		printf(num_format, group_blk);
111		fputc('-', stdout);
112		printf(num_format, next_blk - 1);
113		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 "));
119			printf(num_format, group_blk+1);
120			fputc('-', stdout);
121			printf(num_format, group_blk + group_desc_blocks);
122			fputc('\n', stdout);
123		}
124		fputs(_("  Block bitmap at "), stdout);
125		printf(num_format, fs->group_desc[i].bg_block_bitmap);
126		diff = fs->group_desc[i].bg_block_bitmap - group_blk;
127		if (diff >= 0)
128			printf(" (+%d)", diff);
129		fputs(_(", Inode bitmap at "), stdout);
130		printf(num_format, fs->group_desc[i].bg_inode_bitmap);
131		diff = fs->group_desc[i].bg_inode_bitmap - group_blk;
132		if (diff >= 0)
133			printf(" (+%d)", diff);
134		fputs(_("\n  Inode table at "), stdout);
135		printf(num_format, fs->group_desc[i].bg_inode_table);
136		fputc('-', stdout);
137		printf(num_format, fs->group_desc[i].bg_inode_table +
138		       inode_blocks_per_group - 1);
139		diff = fs->group_desc[i].bg_inode_table - group_blk;
140		if (diff > 0)
141			printf(" (+%d)", diff);
142		printf (_("\n  %d free blocks, %d free inodes, "
143			  "%d directories\n  Free blocks: "),
144			fs->group_desc[i].bg_free_blocks_count,
145			fs->group_desc[i].bg_free_inodes_count,
146			fs->group_desc[i].bg_used_dirs_count);
147		print_free (i, block_bitmap, fs->super->s_blocks_per_group,
148			    fs->super->s_first_data_block);
149		fputs(_("\n  Free inodes: "), stdout);
150		print_free (i, inode_bitmap, fs->super->s_inodes_per_group, 1);
151		fputc('\n', stdout);
152		block_bitmap += fs->super->s_blocks_per_group / 8;
153		inode_bitmap += fs->super->s_inodes_per_group / 8;
154		group_blk = next_blk;
155	}
156}
157
158static void list_bad_blocks(ext2_filsys fs)
159{
160	badblocks_list		bb_list = 0;
161	badblocks_iterate	bb_iter;
162	blk_t			blk;
163	errcode_t		retval;
164
165	retval = ext2fs_read_bb_inode(fs, &bb_list);
166	if (retval) {
167		com_err("ext2fs_read_bb_inode", retval, "");
168		exit(1);
169	}
170	retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
171	if (retval) {
172		com_err("ext2fs_badblocks_list_iterate_begin", retval,
173			_("while printing bad block list"));
174		exit(1);
175	}
176	if (ext2fs_badblocks_list_iterate(bb_iter, &blk))
177		printf(_("Bad blocks: %d"), blk);
178	while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
179		printf(", %d", blk);
180	ext2fs_badblocks_list_iterate_end(bb_iter);
181	fputc('\n', stdout);
182}
183
184static void dump_bad_blocks(ext2_filsys fs)
185{
186	badblocks_list		bb_list = 0;
187	badblocks_iterate	bb_iter;
188	blk_t			blk;
189	errcode_t		retval;
190
191	retval = ext2fs_read_bb_inode(fs, &bb_list);
192	if (retval) {
193		com_err("ext2fs_read_bb_inode", retval, "");
194		exit(1);
195	}
196	retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
197	if (retval) {
198		com_err("ext2fs_badblocks_list_iterate_begin", retval,
199			_("while printing bad block list"));
200		exit(1);
201	}
202	while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
203		printf("%d\n", blk);
204	ext2fs_badblocks_list_iterate_end(bb_iter);
205}
206
207static void print_journal_information(ext2_filsys fs)
208{
209	errcode_t	retval;
210	char		buf[1024];
211	char		str[80];
212	int		i;
213	journal_superblock_t	*jsb;
214
215	/* Get the journal superblock */
216	if ((retval = io_channel_read_blk(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
217		com_err(program_name, retval,
218			_("while reading journal superblock"));
219		exit(1);
220	}
221	jsb = (journal_superblock_t *) buf;
222	if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
223	    (jsb->s_header.h_blocktype !=
224	     (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
225		com_err(program_name, 0,
226			_("Couldn't find journal superblock magic numbers"));
227		exit(1);
228	}
229
230	printf(_("\nJournal block size:       %d\n"
231		 "Journal length:           %d\n"
232		 "Journal first block:      %d\n"
233		 "Journal sequence:         0x%08x\n"
234		 "Journal start:            %d\n"
235		 "Journal number of users:  %d\n"),
236	       ntohl(jsb->s_blocksize),  ntohl(jsb->s_maxlen),
237	       ntohl(jsb->s_first), ntohl(jsb->s_sequence),
238	       ntohl(jsb->s_start), ntohl(jsb->s_nr_users));
239
240	for (i=0; i < ntohl(jsb->s_nr_users); i++) {
241		uuid_unparse(&jsb->s_users[i*16], str);
242		printf(i ? "                          %s\n"
243		       : "Journal users:            %s\n",
244		       str);
245	}
246}
247
248int main (int argc, char ** argv)
249{
250	errcode_t	retval;
251	ext2_filsys	fs;
252	int		print_badblocks = 0;
253	int		use_superblock = 0;
254	int		use_blocksize = 0;
255	int		image_dump = 0;
256	int		force = 0;
257	int		flags;
258	int		header_only = 0;
259	int		big_endian;
260	int		c;
261
262#ifdef ENABLE_NLS
263	setlocale(LC_MESSAGES, "");
264	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
265	textdomain(NLS_CAT_NAME);
266#endif
267	initialize_ext2_error_table();
268	fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
269		 E2FSPROGS_DATE);
270	if (argc && *argv)
271		program_name = *argv;
272
273	while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
274		switch (c) {
275		case 'b':
276			print_badblocks++;
277			break;
278		case 'f':
279			force++;
280			break;
281		case 'h':
282			header_only++;
283			break;
284		case 'i':
285			image_dump++;
286			break;
287		case 'o':
288			if (optarg[0] == 'b')
289				use_superblock = atoi(optarg+1);
290			else if (optarg[0] == 'B')
291				use_blocksize = atoi(optarg+1);
292			else
293				usage();
294			break;
295		case 'V':
296			/* Print version number and exit */
297			fprintf(stderr, _("\tUsing %s\n"),
298				error_message(EXT2_ET_BASE));
299			exit(0);
300		case 'x':
301			num_format = "0x%04x";
302			break;
303		default:
304			usage();
305		}
306	}
307	if (optind > argc - 1)
308		usage();
309	device_name = argv[optind++];
310	if (use_superblock && !use_blocksize)
311		use_blocksize = 1024;
312	flags = EXT2_FLAG_JOURNAL_DEV_OK;
313	if (force)
314		flags |= EXT2_FLAG_FORCE;
315	if (image_dump)
316		flags |= EXT2_FLAG_IMAGE_FILE;
317
318	retval = ext2fs_open (device_name, flags, use_superblock,
319			      use_blocksize, unix_io_manager, &fs);
320	if (retval) {
321		com_err (program_name, retval, _("while trying to open %s"),
322			 device_name);
323		printf (_("Couldn't find valid filesystem superblock.\n"));
324		exit (1);
325	}
326	if (print_badblocks) {
327		dump_bad_blocks(fs);
328	} else {
329		big_endian = ((fs->flags & EXT2_FLAG_SWAP_BYTES) != 0);
330#ifdef WORDS_BIGENDIAN
331		big_endian = !big_endian;
332#endif
333		if (big_endian)
334			printf(_("Note: This is a byte-swapped filesystem\n"));
335		list_super (fs->super);
336		if (fs->super->s_feature_incompat &
337		      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
338			print_journal_information(fs);
339			ext2fs_close(fs);
340			exit(0);
341		}
342		list_bad_blocks (fs);
343		if (header_only) {
344			ext2fs_close (fs);
345			exit (0);
346		}
347		retval = ext2fs_read_bitmaps (fs);
348		if (retval) {
349			com_err (program_name, retval,
350				 _("while trying to read the bitmaps"),
351				 device_name);
352			ext2fs_close (fs);
353			exit (1);
354		}
355		list_desc (fs);
356	}
357	ext2fs_close (fs);
358	exit (0);
359}
360