Whamcloud - gitweb
fc0c45b757eb8f52b3864bc0f4eac3134c5c8688
[tools/e2fsprogs.git] / misc / dumpe2fs.c
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  * This file can be redistributed under the terms of the GNU General
10  * Public License
11  */
12
13 /*
14  * History:
15  * 94/01/09     - Creation
16  * 94/02/27     - Ported to use the ext2fs library
17  */
18
19 #include <getopt.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <linux/ext2_fs.h>
27
28 #include "ext2fs/ext2fs.h"
29 #include "e2p/e2p.h"
30
31 #include "../version.h"
32
33 #define in_use(m, x)    (test_bit ((x), (m)))
34
35 const char * program_name = "dumpe2fs";
36 char * device_name = NULL;
37
38 static volatile void usage (void)
39 {
40         fprintf (stderr, "usage: %s device\n", program_name);
41         exit (1);
42 }
43
44 static void print_free (unsigned long group, char * bitmap,
45                         unsigned long nbytes, unsigned long offset)
46 {
47         int p = 0;
48         unsigned long i;
49         unsigned long j;
50
51         for (i = 0; i < nbytes; i++)
52                 if (!in_use (bitmap, i))
53                 {
54                         if (p)
55                                 printf (", ");
56                         if (i == nbytes - 1 || in_use (bitmap, i + 1))
57                                 printf ("%lu", group * nbytes + i + offset);
58                         else
59                         {
60                                 for (j = i; j < nbytes && !in_use (bitmap, j);
61                                      j++)
62                                         ;
63                                 printf ("%lu-%lu", group * nbytes + i + offset,
64                                         group * nbytes + (j - 1) + offset);
65                                 i = j - 1;
66                         }
67                         p = 1;
68                 }
69 }
70
71 static void list_desc (ext2_filsys fs)
72 {
73         unsigned long i;
74         char * block_bitmap = fs->block_map;
75         char * inode_bitmap = fs->inode_map;
76
77         printf ("\n");
78         for (i = 0; i < fs->group_desc_count; i++)
79         {
80                 printf ("Group %lu:\n", i);
81                 printf ("  Block bitmap at %lu, Inode bitmap at %lu, "
82                         "Inode table at %lu\n",
83                         fs->group_desc[i].bg_block_bitmap,
84                         fs->group_desc[i].bg_inode_bitmap,
85                         fs->group_desc[i].bg_inode_table);
86                 printf ("  %d free blocks, %d free inodes, %d directories\n",
87                         fs->group_desc[i].bg_free_blocks_count,
88                         fs->group_desc[i].bg_free_inodes_count,
89                         fs->group_desc[i].bg_used_dirs_count);
90                 printf ("  Free blocks: ");
91                 print_free (i, block_bitmap, fs->super->s_blocks_per_group,
92                             fs->super->s_first_data_block);
93                 block_bitmap += fs->super->s_blocks_per_group / 8;
94                 printf ("\n");
95                 printf ("  Free inodes: ");
96                 print_free (i, inode_bitmap, fs->super->s_inodes_per_group, 1);
97                 inode_bitmap += fs->super->s_inodes_per_group / 8;
98                 printf ("\n");
99         }
100 }
101
102 static void list_bad_blocks(ext2_filsys fs)
103 {
104         badblocks_list          bb_list = 0;
105         badblocks_iterate       bb_iter;
106         blk_t                   blk;
107         errcode_t               retval;
108
109         retval = ext2fs_read_bb_inode(fs, &bb_list);
110         if (retval) {
111                 com_err("ext2fs_read_bb_inode", retval, "");
112                 exit(1);
113         }
114         retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
115         if (retval) {
116                 com_err("badblocks_list_iterate_begin", retval,
117                         "while printing bad block list");
118                 exit(1);
119         }
120         if (badblocks_list_iterate(bb_iter, &blk))
121                 printf("Bad blocks: %ld", blk);
122         while (badblocks_list_iterate(bb_iter, &blk))
123                 printf(", %ld", blk);
124         badblocks_list_iterate_end(bb_iter);
125         printf("\n");
126 }
127
128 void main (int argc, char ** argv)
129 {
130         errcode_t       retval;
131         ext2_filsys     fs;
132
133         fprintf (stderr, "dumpe2fs %s, %s for EXT2 FS %s, %s\n",
134                  E2FSPROGS_VERSION, E2FSPROGS_DATE,
135                  EXT2FS_VERSION, EXT2FS_DATE);
136         if (argc && *argv)
137                 program_name = *argv;
138         if (argc != 2)
139                 usage ();
140         device_name = argv[1];
141         retval = ext2fs_open (device_name, 0, 0, 0, unix_io_manager, &fs);
142         if (retval)
143         {
144                 com_err (program_name, retval, "while trying to open %s",
145                          device_name);
146                 printf ("Couldn't find valid filesystem superblock.\n");
147                 exit (1);
148         }
149         retval = ext2fs_read_bitmaps (fs);
150         if (retval)
151         {
152                 com_err (program_name, retval, "while trying to read the bitmaps",
153                          device_name);
154                 ext2fs_close (fs);
155                 exit (1);
156         }
157         list_super (fs->super);
158         list_bad_blocks (fs);
159         list_desc (fs);
160         ext2fs_close (fs);
161         exit (0);
162 }