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