dumpe2fs.c revision 6908c0c391af248854b892737c92a7a678a24f66
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#include "config.h"
24#ifdef HAVE_GETOPT_H
25#include <getopt.h>
26#else
27extern char *optarg;
28extern int optind;
29#endif
30#include <fcntl.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36#include "ext2fs/ext2_fs.h"
37
38#include "ext2fs/ext2fs.h"
39#include "e2p/e2p.h"
40#include "jfs_user.h"
41#include <uuid/uuid.h>
42
43#include "../version.h"
44#include "nls-enable.h"
45
46#define in_use(m, x)	(ext2fs_test_bit ((x), (m)))
47
48const char * program_name = "dumpe2fs";
49char * device_name = NULL;
50int hex_format = 0;
51int blocks64 = 0;
52
53static void usage(void)
54{
55	fprintf (stderr, _("Usage: %s [-bfhixV] [-o superblock=<num>] "
56		 "[-o blocksize=<num>] device\n"), program_name);
57	exit (1);
58}
59
60static void print_number(unsigned long long num)
61{
62	if (hex_format) {
63		if (blocks64)
64			printf("0x%08llx", num);
65		else
66			printf("0x%04llx", num);
67	} else
68		printf("%llu", num);
69}
70
71static void print_range(unsigned long long a, unsigned long long b)
72{
73	if (hex_format) {
74		if (blocks64)
75			printf("0x%08llx-0x%08llx", a, b);
76		else
77			printf("0x%04llx-0x%04llx", a, b);
78	} else
79		printf("%llu-%llu", a, b);
80}
81
82static void print_free(unsigned long group, char * bitmap,
83		       unsigned long num, unsigned long offset, int ratio)
84{
85	int p = 0;
86	unsigned long i;
87	unsigned long j;
88
89	offset /= ratio;
90	offset += group * num;
91	for (i = 0; i < num; i++)
92		if (!in_use (bitmap, i))
93		{
94			if (p)
95				printf (", ");
96			print_number((i + offset) * ratio);
97			for (j = i; j < num && !in_use (bitmap, j); j++)
98				;
99			if (--j != i) {
100				fputc('-', stdout);
101				print_number((j + offset) * ratio);
102				i = j;
103			}
104			p = 1;
105		}
106}
107
108static void print_bg_opt(int bg_flags, int mask,
109			  const char *str, int *first)
110{
111	if (bg_flags & mask) {
112		if (*first) {
113			fputs(" [", stdout);
114			*first = 0;
115		} else
116			fputs(", ", stdout);
117		fputs(str, stdout);
118	}
119}
120static void print_bg_opts(ext2_filsys fs, dgrp_t i)
121{
122	int first = 1, bg_flags = 0;
123
124	if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
125		bg_flags = ext2fs_bg_flags(fs, i);
126
127	print_bg_opt(bg_flags, EXT2_BG_INODE_UNINIT, "INODE_UNINIT",
128 		     &first);
129	print_bg_opt(bg_flags, EXT2_BG_BLOCK_UNINIT, "BLOCK_UNINIT",
130 		     &first);
131	print_bg_opt(bg_flags, EXT2_BG_INODE_ZEROED, "ITABLE_ZEROED",
132 		     &first);
133	if (!first)
134		fputc(']', stdout);
135	fputc('\n', stdout);
136}
137
138static void print_bg_rel_offset(ext2_filsys fs, blk64_t block, int itable,
139				blk64_t first_block, blk64_t last_block)
140{
141	if ((block >= first_block) && (block <= last_block)) {
142		if (itable && block == first_block)
143			return;
144		printf(" (+%u)", (unsigned)(block - first_block));
145	} else if (fs->super->s_feature_incompat &
146		   EXT4_FEATURE_INCOMPAT_FLEX_BG) {
147		dgrp_t flex_grp = ext2fs_group_of_blk2(fs, block);
148		printf(" (bg #%u + %u)", flex_grp,
149		       (unsigned)(block-ext2fs_group_first_block2(fs,flex_grp)));
150	}
151}
152
153static void list_desc (ext2_filsys fs)
154{
155	unsigned long i;
156	blk64_t	first_block, last_block;
157	blk64_t	super_blk, old_desc_blk, new_desc_blk;
158	char *block_bitmap=NULL, *inode_bitmap=NULL;
159	const char *units = _("blocks");
160	int inode_blocks_per_group, old_desc_blocks, reserved_gdt;
161	int		block_nbytes, inode_nbytes;
162	int has_super;
163	blk64_t		blk_itr = EXT2FS_B2C(fs, fs->super->s_first_data_block);
164	ext2_ino_t	ino_itr = 1;
165	errcode_t	retval;
166
167	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
168				       EXT4_FEATURE_RO_COMPAT_BIGALLOC))
169		units = _("clusters");
170
171	block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
172	inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
173
174	if (fs->block_map)
175		block_bitmap = malloc(block_nbytes);
176	if (fs->inode_map)
177		inode_bitmap = malloc(inode_nbytes);
178
179	inode_blocks_per_group = ((fs->super->s_inodes_per_group *
180				   EXT2_INODE_SIZE(fs->super)) +
181				  EXT2_BLOCK_SIZE(fs->super) - 1) /
182				 EXT2_BLOCK_SIZE(fs->super);
183	reserved_gdt = fs->super->s_reserved_gdt_blocks;
184	fputc('\n', stdout);
185	first_block = fs->super->s_first_data_block;
186	if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
187		old_desc_blocks = fs->super->s_first_meta_bg;
188	else
189		old_desc_blocks = fs->desc_blocks;
190	for (i = 0; i < fs->group_desc_count; i++) {
191		first_block = ext2fs_group_first_block2(fs, i);
192		last_block = ext2fs_group_last_block2(fs, i);
193
194		ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
195					  &old_desc_blk, &new_desc_blk, 0);
196
197		printf (_("Group %lu: (Blocks "), i);
198		print_range(first_block, last_block);
199		fputs(")", stdout);
200		print_bg_opts(fs, i);
201		if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
202			unsigned csum = ext2fs_bg_checksum(fs, i);
203			unsigned exp_csum = ext2fs_group_desc_csum(fs, i);
204
205			printf(_("  Checksum 0x%04x"), csum);
206			if (csum != exp_csum)
207				printf(_(" (EXPECTED 0x%04x)"), exp_csum);
208			printf(_(", unused inodes %u\n"),
209			       ext2fs_bg_itable_unused(fs, i));
210		}
211		has_super = ((i==0) || super_blk);
212		if (has_super) {
213			printf (_("  %s superblock at "),
214				i == 0 ? _("Primary") : _("Backup"));
215			print_number(super_blk);
216		}
217		if (old_desc_blk) {
218			printf(_(", Group descriptors at "));
219			print_range(old_desc_blk,
220				    old_desc_blk + old_desc_blocks - 1);
221			if (reserved_gdt) {
222				printf(_("\n  Reserved GDT blocks at "));
223				print_range(old_desc_blk + old_desc_blocks,
224					    old_desc_blk + old_desc_blocks +
225					    reserved_gdt - 1);
226			}
227		} else if (new_desc_blk) {
228			fputc(has_super ? ',' : ' ', stdout);
229			printf(_(" Group descriptor at "));
230			print_number(new_desc_blk);
231			has_super++;
232		}
233		if (has_super)
234			fputc('\n', stdout);
235		fputs(_("  Block bitmap at "), stdout);
236		print_number(ext2fs_block_bitmap_loc(fs, i));
237		print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
238				    first_block, last_block);
239		fputs(_(", Inode bitmap at "), stdout);
240		print_number(ext2fs_inode_bitmap_loc(fs, i));
241		print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
242				    first_block, last_block);
243		fputs(_("\n  Inode table at "), stdout);
244		print_range(ext2fs_inode_table_loc(fs, i),
245			    ext2fs_inode_table_loc(fs, i) +
246			    inode_blocks_per_group - 1);
247		print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
248				    first_block, last_block);
249		printf (_("\n  %u free %s, %u free inodes, "
250			  "%u directories%s"),
251			ext2fs_bg_free_blocks_count(fs, i), units,
252			ext2fs_bg_free_inodes_count(fs, i),
253			ext2fs_bg_used_dirs_count(fs, i),
254			ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
255		if (ext2fs_bg_itable_unused(fs, i))
256			printf (_(", %u unused inodes\n"),
257				ext2fs_bg_itable_unused(fs, i));
258		if (block_bitmap) {
259			fputs(_("  Free blocks: "), stdout);
260			retval = ext2fs_get_block_bitmap_range2(fs->block_map,
261				 blk_itr, block_nbytes << 3, block_bitmap);
262			if (retval)
263				com_err("list_desc", retval,
264					"while reading block bitmap");
265			else
266				print_free(i, block_bitmap,
267					   fs->super->s_clusters_per_group,
268					   fs->super->s_first_data_block,
269					   EXT2FS_CLUSTER_RATIO(fs));
270			fputc('\n', stdout);
271			blk_itr += fs->super->s_clusters_per_group;
272		}
273		if (inode_bitmap) {
274			fputs(_("  Free inodes: "), stdout);
275			retval = ext2fs_get_inode_bitmap_range2(fs->inode_map,
276				 ino_itr, inode_nbytes << 3, inode_bitmap);
277			if (retval)
278				com_err("list_desc", retval,
279					"while reading inode bitmap");
280			else
281				print_free(i, inode_bitmap,
282					   fs->super->s_inodes_per_group,
283					   1, 1);
284			fputc('\n', stdout);
285			ino_itr += fs->super->s_inodes_per_group;
286		}
287	}
288	if (block_bitmap)
289		free(block_bitmap);
290	if (inode_bitmap)
291		free(inode_bitmap);
292}
293
294static void list_bad_blocks(ext2_filsys fs, int dump)
295{
296	badblocks_list		bb_list = 0;
297	badblocks_iterate	bb_iter;
298	blk_t			blk;
299	errcode_t		retval;
300	const char		*header, *fmt;
301
302	retval = ext2fs_read_bb_inode(fs, &bb_list);
303	if (retval) {
304		com_err("ext2fs_read_bb_inode", retval, 0);
305		return;
306	}
307	retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
308	if (retval) {
309		com_err("ext2fs_badblocks_list_iterate_begin", retval,
310			_("while printing bad block list"));
311		return;
312	}
313	if (dump) {
314		header = fmt = "%u\n";
315	} else {
316		header =  _("Bad blocks: %u");
317		fmt = ", %u";
318	}
319	while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
320		printf(header ? header : fmt, blk);
321		header = 0;
322	}
323	ext2fs_badblocks_list_iterate_end(bb_iter);
324	if (!dump)
325		fputc('\n', stdout);
326	ext2fs_badblocks_list_free(bb_list);
327}
328
329static void print_inline_journal_information(ext2_filsys fs)
330{
331	journal_superblock_t	*jsb;
332	struct ext2_inode	inode;
333	ext2_file_t		journal_file;
334	errcode_t		retval;
335	ino_t			ino = fs->super->s_journal_inum;
336	char			buf[1024];
337	__u32			*mask_ptr, mask, m;
338	int			i, j, size, printed = 0;
339
340	retval = ext2fs_read_inode(fs, ino,  &inode);
341	if (retval) {
342		com_err(program_name, retval,
343			_("while reading journal inode"));
344		exit(1);
345	}
346	retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
347	if (retval) {
348		com_err(program_name, retval,
349			_("while opening journal inode"));
350		exit(1);
351	}
352	retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
353	if (retval) {
354		com_err(program_name, retval,
355			_("while reading journal super block"));
356		exit(1);
357	}
358	ext2fs_file_close(journal_file);
359	jsb = (journal_superblock_t *) buf;
360	if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
361		fprintf(stderr,
362			"Journal superblock magic number invalid!\n");
363		exit(1);
364	}
365	printf(_("Journal features:        "));
366	for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
367		mask = be32_to_cpu(*mask_ptr);
368		for (j=0,m=1; j < 32; j++, m<<=1) {
369			if (mask & m) {
370				printf(" %s", e2p_jrnl_feature2string(i, m));
371				printed++;
372			}
373		}
374	}
375	if (printed == 0)
376		printf(" (none)");
377	printf("\n");
378	fputs(_("Journal size:             "), stdout);
379	if ((fs->super->s_feature_ro_compat &
380	     EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
381	    (inode.i_flags & EXT4_HUGE_FILE_FL))
382		size = inode.i_blocks / (fs->blocksize / 1024);
383	else
384		size = inode.i_blocks >> 1;
385	if (size < 8192)
386		printf("%uk\n", size);
387	else
388		printf("%uM\n", size >> 10);
389	printf(_("Journal length:           %u\n"
390		 "Journal sequence:         0x%08x\n"
391		 "Journal start:            %u\n"),
392	       (unsigned int)ntohl(jsb->s_maxlen),
393	       (unsigned int)ntohl(jsb->s_sequence),
394	       (unsigned int)ntohl(jsb->s_start));
395	if (jsb->s_errno != 0)
396		printf(_("Journal errno:            %d\n"),
397		       (int) ntohl(jsb->s_errno));
398}
399
400static void print_journal_information(ext2_filsys fs)
401{
402	errcode_t	retval;
403	char		buf[1024];
404	char		str[80];
405	unsigned int	i;
406	journal_superblock_t	*jsb;
407
408	/* Get the journal superblock */
409	if ((retval = io_channel_read_blk64(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
410		com_err(program_name, retval,
411			_("while reading journal superblock"));
412		exit(1);
413	}
414	jsb = (journal_superblock_t *) buf;
415	if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
416	    (jsb->s_header.h_blocktype !=
417	     (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
418		com_err(program_name, 0,
419			_("Couldn't find journal superblock magic numbers"));
420		exit(1);
421	}
422
423	printf(_("\nJournal block size:       %u\n"
424		 "Journal length:           %u\n"
425		 "Journal first block:      %u\n"
426		 "Journal sequence:         0x%08x\n"
427		 "Journal start:            %u\n"
428		 "Journal number of users:  %u\n"),
429	       (unsigned int)ntohl(jsb->s_blocksize),  (unsigned int)ntohl(jsb->s_maxlen),
430	       (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
431	       (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
432
433	for (i=0; i < ntohl(jsb->s_nr_users); i++) {
434		uuid_unparse(&jsb->s_users[i*16], str);
435		printf(i ? "                          %s\n"
436		       : _("Journal users:            %s\n"),
437		       str);
438	}
439}
440
441static void parse_extended_opts(const char *opts, blk64_t *superblock,
442				int *blocksize)
443{
444	char	*buf, *token, *next, *p, *arg, *badopt = 0;
445	int	len;
446	int	do_usage = 0;
447
448	len = strlen(opts);
449	buf = malloc(len+1);
450	if (!buf) {
451		fprintf(stderr,
452			_("Couldn't allocate memory to parse options!\n"));
453		exit(1);
454	}
455	strcpy(buf, opts);
456	for (token = buf; token && *token; token = next) {
457		p = strchr(token, ',');
458		next = 0;
459		if (p) {
460			*p = 0;
461			next = p+1;
462		}
463		arg = strchr(token, '=');
464		if (arg) {
465			*arg = 0;
466			arg++;
467		}
468		if (strcmp(token, "superblock") == 0 ||
469		    strcmp(token, "sb") == 0) {
470			if (!arg) {
471				do_usage++;
472				badopt = token;
473				continue;
474			}
475			*superblock = strtoul(arg, &p, 0);
476			if (*p) {
477				fprintf(stderr,
478					_("Invalid superblock parameter: %s\n"),
479					arg);
480				do_usage++;
481				continue;
482			}
483		} else if (strcmp(token, "blocksize") == 0 ||
484			   strcmp(token, "bs") == 0) {
485			if (!arg) {
486				do_usage++;
487				badopt = token;
488				continue;
489			}
490			*blocksize = strtoul(arg, &p, 0);
491			if (*p) {
492				fprintf(stderr,
493					_("Invalid blocksize parameter: %s\n"),
494					arg);
495				do_usage++;
496				continue;
497			}
498		} else {
499			do_usage++;
500			badopt = token;
501		}
502	}
503	if (do_usage) {
504		fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
505			"Extended options are separated by commas, "
506			"and may take an argument which\n"
507			"\tis set off by an equals ('=') sign.\n\n"
508			"Valid extended options are:\n"
509			"\tsuperblock=<superblock number>\n"
510			"\tblocksize=<blocksize>\n"),
511			badopt ? badopt : "");
512		free(buf);
513		exit(1);
514	}
515	free(buf);
516}
517
518int main (int argc, char ** argv)
519{
520	errcode_t	retval;
521	ext2_filsys	fs;
522	int		print_badblocks = 0;
523	blk64_t		use_superblock = 0;
524	int		use_blocksize = 0;
525	int		image_dump = 0;
526	int		force = 0;
527	int		flags;
528	int		header_only = 0;
529	int		c;
530
531#ifdef ENABLE_NLS
532	setlocale(LC_MESSAGES, "");
533	setlocale(LC_CTYPE, "");
534	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
535	textdomain(NLS_CAT_NAME);
536	set_com_err_gettext(gettext);
537#endif
538	add_error_table(&et_ext2_error_table);
539	fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
540		 E2FSPROGS_DATE);
541	if (argc && *argv)
542		program_name = *argv;
543
544	while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
545		switch (c) {
546		case 'b':
547			print_badblocks++;
548			break;
549		case 'f':
550			force++;
551			break;
552		case 'h':
553			header_only++;
554			break;
555		case 'i':
556			image_dump++;
557			break;
558		case 'o':
559			parse_extended_opts(optarg, &use_superblock,
560					    &use_blocksize);
561			break;
562		case 'V':
563			/* Print version number and exit */
564			fprintf(stderr, _("\tUsing %s\n"),
565				error_message(EXT2_ET_BASE));
566			exit(0);
567		case 'x':
568			hex_format++;
569			break;
570		default:
571			usage();
572		}
573	}
574	if (optind > argc - 1)
575		usage();
576	device_name = argv[optind++];
577	flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
578	if (force)
579		flags |= EXT2_FLAG_FORCE;
580	if (image_dump)
581		flags |= EXT2_FLAG_IMAGE_FILE;
582
583	if (use_superblock && !use_blocksize) {
584		for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
585		     use_blocksize <= EXT2_MAX_BLOCK_SIZE;
586		     use_blocksize *= 2) {
587			retval = ext2fs_open (device_name, flags,
588					      use_superblock,
589					      use_blocksize, unix_io_manager,
590					      &fs);
591			if (!retval)
592				break;
593		}
594	} else
595		retval = ext2fs_open (device_name, flags, use_superblock,
596				      use_blocksize, unix_io_manager, &fs);
597	if (retval) {
598		com_err (program_name, retval, _("while trying to open %s"),
599			 device_name);
600		printf (_("Couldn't find valid filesystem superblock.\n"));
601		exit (1);
602	}
603	fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
604	if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
605		blocks64 = 1;
606	if (print_badblocks) {
607		list_bad_blocks(fs, 1);
608	} else {
609		list_super (fs->super);
610		if (fs->super->s_feature_incompat &
611		      EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
612			print_journal_information(fs);
613			ext2fs_close(fs);
614			exit(0);
615		}
616		if ((fs->super->s_feature_compat &
617		     EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
618		    (fs->super->s_journal_inum != 0))
619			print_inline_journal_information(fs);
620		list_bad_blocks(fs, 0);
621		if (header_only) {
622			ext2fs_close (fs);
623			exit (0);
624		}
625		retval = ext2fs_read_bitmaps (fs);
626		list_desc (fs);
627		if (retval) {
628			printf(_("\n%s: %s: error reading bitmaps: %s\n"),
629			       program_name, device_name,
630			       error_message(retval));
631		}
632	}
633	ext2fs_close (fs);
634	remove_error_table(&et_ext2_error_table);
635	exit (0);
636}
637