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