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