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