dumpe2fs.c revision 1ca1059fd0126fd2c065f272a566c18f14bab16d
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#include <uuid/uuid.h>
41
42#include "../version.h"
43#include "nls-enable.h"
44
45#define in_use(m, x)	(ext2fs_test_bit ((x), (m)))
46
47const char * program_name = "dumpe2fs";
48char * device_name = NULL;
49int hex_format = 0;
50
51static void usage(void)
52{
53	fprintf (stderr, _("Usage: %s [-bfhixV] [-ob superblock] "
54		 "[-oB blocksize] device\n"), program_name);
55	exit (1);
56}
57
58static void print_number(unsigned long num)
59{
60	if (hex_format)
61		printf("0x%04lx", num);
62	else
63		printf("%lu", num);
64}
65
66static void print_range(unsigned long a, unsigned long b)
67{
68	if (hex_format)
69		printf("0x%04lx-0x%04lx", a, b);
70	else
71		printf("%lu-%lu", a, b);
72}
73
74static 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
81	offset += group * nbytes;
82	for (i = 0; i < nbytes; i++)
83		if (!in_use (bitmap, i))
84		{
85			if (p)
86				printf (", ");
87			print_number(i + offset);
88			for (j = i; j < nbytes && !in_use (bitmap, j); j++)
89				;
90			if (--j != i) {
91				fputc('-', stdout);
92				print_number(j + offset);
93				i = j;
94			}
95			p = 1;
96		}
97}
98
99static 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{
113	int first = 1, bg_flags;
114
115	if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_LAZY_BG ||
116	    fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
117		bg_flags = fs->group_desc[i].bg_flags;
118	else
119		bg_flags = 0;
120
121	print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
122 		     &first);
123	print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
124 		     &first);
125	print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
126 		     &first);
127	if (!first)
128		fputc(']', stdout);
129	fputc('\n', stdout);
130}
131
132static void list_desc (ext2_filsys fs)
133{
134	unsigned long i;
135	long diff;
136	blk_t	first_block, last_block;
137	blk_t	super_blk, old_desc_blk, new_desc_blk;
138	char *block_bitmap=NULL, *inode_bitmap=NULL;
139	int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
140	int		block_nbytes, inode_nbytes;
141	int has_super;
142	blk_t		blk_itr = fs->super->s_first_data_block;
143	ext2_ino_t	ino_itr = 1;
144
145	block_nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
146	inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
147
148	if (fs->block_map)
149		block_bitmap = malloc(block_nbytes);
150	if (fs->inode_map)
151		inode_bitmap = malloc(inode_nbytes);
152
153	inode_blocks_per_group = ((fs->super->s_inodes_per_group *
154				   EXT2_INODE_SIZE(fs->super)) +
155				  EXT2_BLOCK_SIZE(fs->super) - 1) /
156				 EXT2_BLOCK_SIZE(fs->super);
157	reserved_gdt = fs->super->s_reserved_gdt_blocks;
158	fputc('\n', stdout);
159	first_block = fs->super->s_first_data_block;
160	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
161		old_desc_blocks = fs->super->s_first_meta_bg;
162	else
163		old_desc_blocks = fs->desc_blocks;
164	for (i = 0; i < fs->group_desc_count; i++) {
165		first_block = ext2fs_group_first_block(fs, i);
166		last_block = ext2fs_group_last_block(fs, i);
167
168		ext2fs_super_and_bgd_loc(fs, i, &super_blk,
169					 &old_desc_blk, &new_desc_blk, 0);
170
171		printf (_("Group %lu: (Blocks "), i);
172		print_range(first_block, last_block);
173		fputs(")", stdout);
174		print_bg_opts(fs, i);
175		if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
176			printf(_("  Checksum 0x%04x, unused inodes %d\n"),
177			       fs->group_desc[i].bg_checksum,
178			       fs->group_desc[i].bg_itable_unused);
179		has_super = ((i==0) || super_blk);
180		if (has_super) {
181			printf (_("  %s superblock at "),
182				i == 0 ? _("Primary") : _("Backup"));
183			print_number(super_blk);
184		}
185		if (old_desc_blk) {
186			printf(_(", Group descriptors at "));
187			print_range(old_desc_blk,
188				    old_desc_blk + old_desc_blocks - 1);
189			if (reserved_gdt) {
190				printf(_("\n  Reserved GDT blocks at "));
191				print_range(old_desc_blk + old_desc_blocks,
192					    old_desc_blk + old_desc_blocks +
193					    reserved_gdt - 1);
194			}
195		} else if (new_desc_blk) {
196			fputc(has_super ? ',' : ' ', stdout);
197			printf(_(" Group descriptor at "));
198			print_number(new_desc_blk);
199			has_super++;
200		}
201		if (has_super)
202			fputc('\n', stdout);
203		fputs(_("  Block bitmap at "), stdout);
204		print_number(fs->group_desc[i].bg_block_bitmap);
205		diff = fs->group_desc[i].bg_block_bitmap - first_block;
206		if (diff >= 0)
207			printf(" (+%ld)", diff);
208		fputs(_(", Inode bitmap at "), stdout);
209		print_number(fs->group_desc[i].bg_inode_bitmap);
210		diff = fs->group_desc[i].bg_inode_bitmap - first_block;
211		if (diff >= 0)
212			printf(" (+%ld)", diff);
213		fputs(_("\n  Inode table at "), stdout);
214		print_range(fs->group_desc[i].bg_inode_table,
215			    fs->group_desc[i].bg_inode_table +
216			    inode_blocks_per_group - 1);
217		diff = fs->group_desc[i].bg_inode_table - first_block;
218		if (diff > 0)
219			printf(" (+%ld)", diff);
220		printf (_("\n  %u free blocks, %u free inodes, "
221			  "%u directories%s"),
222			fs->group_desc[i].bg_free_blocks_count,
223			fs->group_desc[i].bg_free_inodes_count,
224			fs->group_desc[i].bg_used_dirs_count,
225			fs->group_desc[i].bg_itable_unused ? "" : "\n");
226		if (fs->group_desc[i].bg_itable_unused)
227			printf (_(", %u unused inodes\n"),
228				fs->group_desc[i].bg_itable_unused);
229		if (block_bitmap) {
230			fputs(_("  Free blocks: "), stdout);
231			ext2fs_get_block_bitmap_range(fs->block_map,
232				 blk_itr, block_nbytes << 3, block_bitmap);
233			print_free (i, block_bitmap,
234				    fs->super->s_blocks_per_group,
235				    fs->super->s_first_data_block);
236			fputc('\n', stdout);
237			blk_itr += fs->super->s_blocks_per_group;
238		}
239		if (inode_bitmap) {
240			fputs(_("  Free inodes: "), stdout);
241			ext2fs_get_inode_bitmap_range(fs->inode_map,
242				 ino_itr, inode_nbytes << 3, inode_bitmap);
243			print_free (i, inode_bitmap,
244				    fs->super->s_inodes_per_group, 1);
245			fputc('\n', stdout);
246			ino_itr += fs->super->s_inodes_per_group;
247		}
248	}
249}
250
251static void list_bad_blocks(ext2_filsys fs, int dump)
252{
253	badblocks_list		bb_list = 0;
254	badblocks_iterate	bb_iter;
255	blk_t			blk;
256	errcode_t		retval;
257	const char		*header, *fmt;
258
259	retval = ext2fs_read_bb_inode(fs, &bb_list);
260	if (retval) {
261		com_err("ext2fs_read_bb_inode", retval, 0);
262		return;
263	}
264	retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
265	if (retval) {
266		com_err("ext2fs_badblocks_list_iterate_begin", retval,
267			_("while printing bad block list"));
268		return;
269	}
270	if (dump) {
271		header = fmt = "%u\n";
272	} else {
273		header =  _("Bad blocks: %u");
274		fmt = ", %u";
275	}
276	while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
277		printf(header ? header : fmt, blk);
278		header = 0;
279	}
280	ext2fs_badblocks_list_iterate_end(bb_iter);
281	if (!dump)
282		fputc('\n', stdout);
283}
284
285static void print_inline_journal_information(ext2_filsys fs)
286{
287	struct ext2_inode	inode;
288	errcode_t		retval;
289	ino_t			ino = fs->super->s_journal_inum;
290	int			size;
291
292	retval = ext2fs_read_inode(fs, ino,  &inode);
293	if (retval) {
294		com_err(program_name, retval,
295			_("while reading journal inode"));
296		exit(1);
297	}
298	fputs(_("Journal size:             "), stdout);
299	if ((fs->super->s_feature_ro_compat &
300	     EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
301	    (inode.i_flags & EXT4_HUGE_FILE_FL))
302		size = inode.i_blocks / (fs->blocksize / 1024);
303	else
304		size = inode.i_blocks >> 1;
305	if (size < 8192)
306		printf("%uk\n", size);
307	else
308		printf("%uM\n", size >> 10);
309}
310
311static void print_journal_information(ext2_filsys fs)
312{
313	errcode_t	retval;
314	char		buf[1024];
315	char		str[80];
316	unsigned int	i;
317	journal_superblock_t	*jsb;
318
319	/* Get the journal superblock */
320	if ((retval = io_channel_read_blk(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
321		com_err(program_name, retval,
322			_("while reading journal superblock"));
323		exit(1);
324	}
325	jsb = (journal_superblock_t *) buf;
326	if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
327	    (jsb->s_header.h_blocktype !=
328	     (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
329		com_err(program_name, 0,
330			_("Couldn't find journal superblock magic numbers"));
331		exit(1);
332	}
333
334	printf(_("\nJournal block size:       %u\n"
335		 "Journal length:           %u\n"
336		 "Journal first block:      %u\n"
337		 "Journal sequence:         0x%08x\n"
338		 "Journal start:            %u\n"
339		 "Journal number of users:  %u\n"),
340	       (unsigned int)ntohl(jsb->s_blocksize),  (unsigned int)ntohl(jsb->s_maxlen),
341	       (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
342	       (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
343
344	for (i=0; i < ntohl(jsb->s_nr_users); i++) {
345		uuid_unparse(&jsb->s_users[i*16], str);
346		printf(i ? "                          %s\n"
347		       : _("Journal users:            %s\n"),
348		       str);
349	}
350}
351
352static void parse_extended_opts(const char *opts, blk_t *superblock,
353				int *blocksize)
354{
355	char	*buf, *token, *next, *p, *arg, *badopt = 0;
356	int	len;
357	int	do_usage = 0;
358
359	len = strlen(opts);
360	buf = malloc(len+1);
361	if (!buf) {
362		fprintf(stderr,
363			_("Couldn't allocate memory to parse options!\n"));
364		exit(1);
365	}
366	strcpy(buf, opts);
367	for (token = buf; token && *token; token = next) {
368		p = strchr(token, ',');
369		next = 0;
370		if (p) {
371			*p = 0;
372			next = p+1;
373		}
374		arg = strchr(token, '=');
375		if (arg) {
376			*arg = 0;
377			arg++;
378		}
379		if (strcmp(token, "superblock") == 0 ||
380		    strcmp(token, "sb") == 0) {
381			if (!arg) {
382				do_usage++;
383				badopt = token;
384				continue;
385			}
386			*superblock = strtoul(arg, &p, 0);
387			if (*p) {
388				fprintf(stderr,
389					_("Invalid superblock parameter: %s\n"),
390					arg);
391				do_usage++;
392				continue;
393			}
394		} else if (strcmp(token, "blocksize") == 0 ||
395			   strcmp(token, "bs") == 0) {
396			if (!arg) {
397				do_usage++;
398				badopt = token;
399				continue;
400			}
401			*blocksize = strtoul(arg, &p, 0);
402			if (*p) {
403				fprintf(stderr,
404					_("Invalid blocksize parameter: %s\n"),
405					arg);
406				do_usage++;
407				continue;
408			}
409		} else {
410			do_usage++;
411			badopt = token;
412		}
413	}
414	if (do_usage) {
415		fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
416			"Extended options are separated by commas, "
417			"and may take an argument which\n"
418			"\tis set off by an equals ('=') sign.\n\n"
419			"Valid extended options are:\n"
420			"\tsuperblock=<superblock number>\n"
421			"\tblocksize=<blocksize>\n"),
422			badopt ? badopt : "");
423		free(buf);
424		exit(1);
425	}
426	free(buf);
427}
428
429int main (int argc, char ** argv)
430{
431	errcode_t	retval;
432	ext2_filsys	fs;
433	int		print_badblocks = 0;
434	blk_t		use_superblock = 0;
435	int		use_blocksize = 0;
436	int		image_dump = 0;
437	int		force = 0;
438	int		flags;
439	int		header_only = 0;
440	int		c;
441
442#ifdef ENABLE_NLS
443	setlocale(LC_MESSAGES, "");
444	setlocale(LC_CTYPE, "");
445	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
446	textdomain(NLS_CAT_NAME);
447#endif
448	add_error_table(&et_ext2_error_table);
449	fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
450		 E2FSPROGS_DATE);
451	if (argc && *argv)
452		program_name = *argv;
453
454	while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
455		switch (c) {
456		case 'b':
457			print_badblocks++;
458			break;
459		case 'f':
460			force++;
461			break;
462		case 'h':
463			header_only++;
464			break;
465		case 'i':
466			image_dump++;
467			break;
468		case 'o':
469			parse_extended_opts(optarg, &use_superblock,
470					    &use_blocksize);
471			break;
472		case 'V':
473			/* Print version number and exit */
474			fprintf(stderr, _("\tUsing %s\n"),
475				error_message(EXT2_ET_BASE));
476			exit(0);
477		case 'x':
478			hex_format++;
479			break;
480		default:
481			usage();
482		}
483	}
484	if (optind > argc - 1)
485		usage();
486	device_name = argv[optind++];
487	flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES;
488	if (force)
489		flags |= EXT2_FLAG_FORCE;
490	if (image_dump)
491		flags |= EXT2_FLAG_IMAGE_FILE;
492
493	if (use_superblock && !use_blocksize) {
494		for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
495		     use_blocksize <= EXT2_MAX_BLOCK_SIZE;
496		     use_blocksize *= 2) {
497			retval = ext2fs_open (device_name, flags,
498					      use_superblock,
499					      use_blocksize, unix_io_manager,
500					      &fs);
501			if (!retval)
502				break;
503		}
504	} else
505		retval = ext2fs_open (device_name, flags, use_superblock,
506				      use_blocksize, unix_io_manager, &fs);
507	if (retval) {
508		com_err (program_name, retval, _("while trying to open %s"),
509			 device_name);
510		printf (_("Couldn't find valid filesystem superblock.\n"));
511		exit (1);
512	}
513	if (print_badblocks) {
514		list_bad_blocks(fs, 1);
515	} else {
516		list_super (fs->super);
517		if (fs->super->s_feature_incompat &
518		      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
519			print_journal_information(fs);
520			ext2fs_close(fs);
521			exit(0);
522		}
523		if (fs->super->s_feature_compat &
524		      EXT3_FEATURE_COMPAT_HAS_JOURNAL)
525			print_inline_journal_information(fs);
526		list_bad_blocks(fs, 0);
527		if (header_only) {
528			ext2fs_close (fs);
529			exit (0);
530		}
531		retval = ext2fs_read_bitmaps (fs);
532		list_desc (fs);
533		if (retval) {
534			printf(_("\n%s: %s: error reading bitmaps: %s\n"),
535			       program_name, device_name,
536			       error_message(retval));
537		}
538	}
539	ext2fs_close (fs);
540	remove_error_table(&et_ext2_error_table);
541	exit (0);
542}
543