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