Whamcloud - gitweb
libext2fs: block group checksum should use metadata_csum algorithm
[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 const char * program_name = "dumpe2fs";
49 char * device_name = NULL;
50 int hex_format = 0;
51 int blocks64 = 0;
52
53 static void usage(void)
54 {
55         fprintf (stderr, _("Usage: %s [-bfhixV] [-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_blk(fs, block);
148                 printf(" (bg #%u + %u)", flex_grp,
149                        (unsigned)(block-ext2fs_group_first_block(fs,flex_grp)));
150         }
151 }
152
153 static void list_desc (ext2_filsys fs)
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
166         if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
167                                        EXT4_FEATURE_RO_COMPAT_BIGALLOC))
168                 units = _("clusters");
169
170         block_nbytes = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
171         inode_nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
172
173         if (fs->block_map)
174                 block_bitmap = malloc(block_nbytes);
175         if (fs->inode_map)
176                 inode_bitmap = malloc(inode_nbytes);
177
178         inode_blocks_per_group = ((fs->super->s_inodes_per_group *
179                                    EXT2_INODE_SIZE(fs->super)) +
180                                   EXT2_BLOCK_SIZE(fs->super) - 1) /
181                                  EXT2_BLOCK_SIZE(fs->super);
182         reserved_gdt = fs->super->s_reserved_gdt_blocks;
183         fputc('\n', stdout);
184         first_block = fs->super->s_first_data_block;
185         if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
186                 old_desc_blocks = fs->super->s_first_meta_bg;
187         else
188                 old_desc_blocks = fs->desc_blocks;
189         for (i = 0; i < fs->group_desc_count; i++) {
190                 first_block = ext2fs_group_first_block2(fs, i);
191                 last_block = ext2fs_group_last_block2(fs, i);
192
193                 ext2fs_super_and_bgd_loc2(fs, i, &super_blk,
194                                           &old_desc_blk, &new_desc_blk, 0);
195
196                 printf (_("Group %lu: (Blocks "), i);
197                 print_range(first_block, last_block);
198                 fputs(")", stdout);
199                 print_bg_opts(fs, i);
200                 if (ext2fs_has_group_desc_csum(fs)) {
201                         unsigned csum = ext2fs_bg_checksum(fs, i);
202                         unsigned exp_csum = ext2fs_group_desc_csum(fs, i);
203
204                         printf(_("  Checksum 0x%04x"), csum);
205                         if (csum != exp_csum)
206                                 printf(_(" (EXPECTED 0x%04x)"), exp_csum);
207                         printf(_(", unused inodes %u\n"),
208                                ext2fs_bg_itable_unused(fs, i));
209                 }
210                 has_super = ((i==0) || super_blk);
211                 if (has_super) {
212                         printf (_("  %s superblock at "),
213                                 i == 0 ? _("Primary") : _("Backup"));
214                         print_number(super_blk);
215                 }
216                 if (old_desc_blk) {
217                         printf(_(", Group descriptors at "));
218                         print_range(old_desc_blk,
219                                     old_desc_blk + old_desc_blocks - 1);
220                         if (reserved_gdt) {
221                                 printf(_("\n  Reserved GDT blocks at "));
222                                 print_range(old_desc_blk + old_desc_blocks,
223                                             old_desc_blk + old_desc_blocks +
224                                             reserved_gdt - 1);
225                         }
226                 } else if (new_desc_blk) {
227                         fputc(has_super ? ',' : ' ', stdout);
228                         printf(_(" Group descriptor at "));
229                         print_number(new_desc_blk);
230                         has_super++;
231                 }
232                 if (has_super)
233                         fputc('\n', stdout);
234                 fputs(_("  Block bitmap at "), stdout);
235                 print_number(ext2fs_block_bitmap_loc(fs, i));
236                 print_bg_rel_offset(fs, ext2fs_block_bitmap_loc(fs, i), 0,
237                                     first_block, last_block);
238                 if (fs->super->s_feature_ro_compat &
239                     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
240                         printf(_(", csum 0x%08x"),
241                                ext2fs_block_bitmap_checksum(fs, i));
242                 fputs(_(", Inode bitmap at "), stdout);
243                 print_number(ext2fs_inode_bitmap_loc(fs, i));
244                 print_bg_rel_offset(fs, ext2fs_inode_bitmap_loc(fs, i), 0,
245                                     first_block, last_block);
246                 if (fs->super->s_feature_ro_compat &
247                     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
248                         printf(_(", csum 0x%08x"),
249                                ext2fs_inode_bitmap_checksum(fs, i));
250                 fputs(_("\n  Inode table at "), stdout);
251                 print_range(ext2fs_inode_table_loc(fs, i),
252                             ext2fs_inode_table_loc(fs, i) +
253                             inode_blocks_per_group - 1);
254                 print_bg_rel_offset(fs, ext2fs_inode_table_loc(fs, i), 1,
255                                     first_block, last_block);
256                 printf (_("\n  %u free %s, %u free inodes, "
257                           "%u directories%s"),
258                         ext2fs_bg_free_blocks_count(fs, i), units,
259                         ext2fs_bg_free_inodes_count(fs, i),
260                         ext2fs_bg_used_dirs_count(fs, i),
261                         ext2fs_bg_itable_unused(fs, i) ? "" : "\n");
262                 if (ext2fs_bg_itable_unused(fs, i))
263                         printf (_(", %u unused inodes\n"),
264                                 ext2fs_bg_itable_unused(fs, i));
265                 if (block_bitmap) {
266                         fputs(_("  Free blocks: "), stdout);
267                         ext2fs_get_block_bitmap_range2(fs->block_map,
268                                  blk_itr, block_nbytes << 3, block_bitmap);
269                         print_free(i, block_bitmap,
270                                    fs->super->s_clusters_per_group,
271                                    fs->super->s_first_data_block,
272                                    EXT2FS_CLUSTER_RATIO(fs));
273                         fputc('\n', stdout);
274                         blk_itr += fs->super->s_clusters_per_group;
275                 }
276                 if (inode_bitmap) {
277                         fputs(_("  Free inodes: "), stdout);
278                         ext2fs_get_inode_bitmap_range2(fs->inode_map,
279                                  ino_itr, inode_nbytes << 3, inode_bitmap);
280                         print_free(i, inode_bitmap,
281                                    fs->super->s_inodes_per_group, 1, 1);
282                         fputc('\n', stdout);
283                         ino_itr += fs->super->s_inodes_per_group;
284                 }
285         }
286         if (block_bitmap)
287                 free(block_bitmap);
288         if (inode_bitmap)
289                 free(inode_bitmap);
290 }
291
292 static void list_bad_blocks(ext2_filsys fs, int dump)
293 {
294         badblocks_list          bb_list = 0;
295         badblocks_iterate       bb_iter;
296         blk_t                   blk;
297         errcode_t               retval;
298         const char              *header, *fmt;
299
300         retval = ext2fs_read_bb_inode(fs, &bb_list);
301         if (retval) {
302                 com_err("ext2fs_read_bb_inode", retval, 0);
303                 return;
304         }
305         retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
306         if (retval) {
307                 com_err("ext2fs_badblocks_list_iterate_begin", retval,
308                         _("while printing bad block list"));
309                 return;
310         }
311         if (dump) {
312                 header = fmt = "%u\n";
313         } else {
314                 header =  _("Bad blocks: %u");
315                 fmt = ", %u";
316         }
317         while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) {
318                 printf(header ? header : fmt, blk);
319                 header = 0;
320         }
321         ext2fs_badblocks_list_iterate_end(bb_iter);
322         if (!dump)
323                 fputc('\n', stdout);
324         ext2fs_badblocks_list_free(bb_list);
325 }
326
327 static void print_inline_journal_information(ext2_filsys fs)
328 {
329         journal_superblock_t    *jsb;
330         struct ext2_inode       inode;
331         ext2_file_t             journal_file;
332         errcode_t               retval;
333         ino_t                   ino = fs->super->s_journal_inum;
334         char                    buf[1024];
335         __u32                   *mask_ptr, mask, m;
336         int                     i, j, size, printed = 0;
337
338         retval = ext2fs_read_inode(fs, ino,  &inode);
339         if (retval) {
340                 com_err(program_name, retval,
341                         _("while reading journal inode"));
342                 exit(1);
343         }
344         retval = ext2fs_file_open2(fs, ino, &inode, 0, &journal_file);
345         if (retval) {
346                 com_err(program_name, retval,
347                         _("while opening journal inode"));
348                 exit(1);
349         }
350         retval = ext2fs_file_read(journal_file, buf, sizeof(buf), 0);
351         if (retval) {
352                 com_err(program_name, retval,
353                         _("while reading journal super block"));
354                 exit(1);
355         }
356         ext2fs_file_close(journal_file);
357         jsb = (journal_superblock_t *) buf;
358         if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
359                 fprintf(stderr,
360                         "Journal superblock magic number invalid!\n");
361                 exit(1);
362         }
363         printf(_("Journal features:        "));
364         for (i=0, mask_ptr=&jsb->s_feature_compat; i <3; i++,mask_ptr++) {
365                 mask = be32_to_cpu(*mask_ptr);
366                 for (j=0,m=1; j < 32; j++, m<<=1) {
367                         if (mask & m) {
368                                 printf(" %s", e2p_jrnl_feature2string(i, m));
369                                 printed++;
370                         }
371                 }
372         }
373         if (printed == 0)
374                 printf(" (none)");
375         printf("\n");
376         fputs(_("Journal size:             "), stdout);
377         if ((fs->super->s_feature_ro_compat &
378              EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
379             (inode.i_flags & EXT4_HUGE_FILE_FL))
380                 size = inode.i_blocks / (fs->blocksize / 1024);
381         else
382                 size = inode.i_blocks >> 1;
383         if (size < 8192)
384                 printf("%uk\n", size);
385         else
386                 printf("%uM\n", size >> 10);
387         printf(_("Journal length:           %u\n"
388                  "Journal sequence:         0x%08x\n"
389                  "Journal start:            %u\n"),
390                (unsigned int)ntohl(jsb->s_maxlen),
391                (unsigned int)ntohl(jsb->s_sequence),
392                (unsigned int)ntohl(jsb->s_start));
393         if (jsb->s_errno != 0)
394                 printf(_("Journal errno:            %d\n"),
395                        (int) ntohl(jsb->s_errno));
396 }
397
398 static void print_journal_information(ext2_filsys fs)
399 {
400         errcode_t       retval;
401         char            buf[1024];
402         char            str[80];
403         unsigned int    i;
404         journal_superblock_t    *jsb;
405
406         /* Get the journal superblock */
407         if ((retval = io_channel_read_blk64(fs->io, fs->super->s_first_data_block+1, -1024, buf))) {
408                 com_err(program_name, retval,
409                         _("while reading journal superblock"));
410                 exit(1);
411         }
412         jsb = (journal_superblock_t *) buf;
413         if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
414             (jsb->s_header.h_blocktype !=
415              (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
416                 com_err(program_name, 0,
417                         _("Couldn't find journal superblock magic numbers"));
418                 exit(1);
419         }
420
421         printf(_("\nJournal block size:       %u\n"
422                  "Journal length:           %u\n"
423                  "Journal first block:      %u\n"
424                  "Journal sequence:         0x%08x\n"
425                  "Journal start:            %u\n"
426                  "Journal number of users:  %u\n"),
427                (unsigned int)ntohl(jsb->s_blocksize),  (unsigned int)ntohl(jsb->s_maxlen),
428                (unsigned int)ntohl(jsb->s_first), (unsigned int)ntohl(jsb->s_sequence),
429                (unsigned int)ntohl(jsb->s_start), (unsigned int)ntohl(jsb->s_nr_users));
430
431         for (i=0; i < ntohl(jsb->s_nr_users); i++) {
432                 uuid_unparse(&jsb->s_users[i*16], str);
433                 printf(i ? "                          %s\n"
434                        : _("Journal users:            %s\n"),
435                        str);
436         }
437 }
438
439 static void parse_extended_opts(const char *opts, blk64_t *superblock,
440                                 int *blocksize)
441 {
442         char    *buf, *token, *next, *p, *arg, *badopt = 0;
443         int     len;
444         int     do_usage = 0;
445
446         len = strlen(opts);
447         buf = malloc(len+1);
448         if (!buf) {
449                 fprintf(stderr,
450                         _("Couldn't allocate memory to parse options!\n"));
451                 exit(1);
452         }
453         strcpy(buf, opts);
454         for (token = buf; token && *token; token = next) {
455                 p = strchr(token, ',');
456                 next = 0;
457                 if (p) {
458                         *p = 0;
459                         next = p+1;
460                 }
461                 arg = strchr(token, '=');
462                 if (arg) {
463                         *arg = 0;
464                         arg++;
465                 }
466                 if (strcmp(token, "superblock") == 0 ||
467                     strcmp(token, "sb") == 0) {
468                         if (!arg) {
469                                 do_usage++;
470                                 badopt = token;
471                                 continue;
472                         }
473                         *superblock = strtoul(arg, &p, 0);
474                         if (*p) {
475                                 fprintf(stderr,
476                                         _("Invalid superblock parameter: %s\n"),
477                                         arg);
478                                 do_usage++;
479                                 continue;
480                         }
481                 } else if (strcmp(token, "blocksize") == 0 ||
482                            strcmp(token, "bs") == 0) {
483                         if (!arg) {
484                                 do_usage++;
485                                 badopt = token;
486                                 continue;
487                         }
488                         *blocksize = strtoul(arg, &p, 0);
489                         if (*p) {
490                                 fprintf(stderr,
491                                         _("Invalid blocksize parameter: %s\n"),
492                                         arg);
493                                 do_usage++;
494                                 continue;
495                         }
496                 } else {
497                         do_usage++;
498                         badopt = token;
499                 }
500         }
501         if (do_usage) {
502                 fprintf(stderr, _("\nBad extended option(s) specified: %s\n\n"
503                         "Extended options are separated by commas, "
504                         "and may take an argument which\n"
505                         "\tis set off by an equals ('=') sign.\n\n"
506                         "Valid extended options are:\n"
507                         "\tsuperblock=<superblock number>\n"
508                         "\tblocksize=<blocksize>\n"),
509                         badopt ? badopt : "");
510                 free(buf);
511                 exit(1);
512         }
513         free(buf);
514 }
515
516 int main (int argc, char ** argv)
517 {
518         errcode_t       retval;
519         ext2_filsys     fs;
520         int             print_badblocks = 0;
521         blk64_t         use_superblock = 0;
522         int             use_blocksize = 0;
523         int             image_dump = 0;
524         int             force = 0;
525         int             flags;
526         int             header_only = 0;
527         int             c;
528
529 #ifdef ENABLE_NLS
530         setlocale(LC_MESSAGES, "");
531         setlocale(LC_CTYPE, "");
532         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
533         textdomain(NLS_CAT_NAME);
534         set_com_err_gettext(gettext);
535 #endif
536         add_error_table(&et_ext2_error_table);
537         fprintf (stderr, "dumpe2fs %s (%s)\n", E2FSPROGS_VERSION,
538                  E2FSPROGS_DATE);
539         if (argc && *argv)
540                 program_name = *argv;
541
542         while ((c = getopt (argc, argv, "bfhixVo:")) != EOF) {
543                 switch (c) {
544                 case 'b':
545                         print_badblocks++;
546                         break;
547                 case 'f':
548                         force++;
549                         break;
550                 case 'h':
551                         header_only++;
552                         break;
553                 case 'i':
554                         image_dump++;
555                         break;
556                 case 'o':
557                         parse_extended_opts(optarg, &use_superblock,
558                                             &use_blocksize);
559                         break;
560                 case 'V':
561                         /* Print version number and exit */
562                         fprintf(stderr, _("\tUsing %s\n"),
563                                 error_message(EXT2_ET_BASE));
564                         exit(0);
565                 case 'x':
566                         hex_format++;
567                         break;
568                 default:
569                         usage();
570                 }
571         }
572         if (optind > argc - 1)
573                 usage();
574         device_name = argv[optind++];
575         flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
576         if (force)
577                 flags |= EXT2_FLAG_FORCE;
578         if (image_dump)
579                 flags |= EXT2_FLAG_IMAGE_FILE;
580
581         if (use_superblock && !use_blocksize) {
582                 for (use_blocksize = EXT2_MIN_BLOCK_SIZE;
583                      use_blocksize <= EXT2_MAX_BLOCK_SIZE;
584                      use_blocksize *= 2) {
585                         retval = ext2fs_open (device_name, flags,
586                                               use_superblock,
587                                               use_blocksize, unix_io_manager,
588                                               &fs);
589                         if (!retval)
590                                 break;
591                 }
592         } else
593                 retval = ext2fs_open (device_name, flags, use_superblock,
594                                       use_blocksize, unix_io_manager, &fs);
595         if (retval) {
596                 com_err (program_name, retval, _("while trying to open %s"),
597                          device_name);
598                 printf (_("Couldn't find valid filesystem superblock.\n"));
599                 exit (1);
600         }
601         fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
602         if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
603                 blocks64 = 1;
604         if (print_badblocks) {
605                 list_bad_blocks(fs, 1);
606         } else {
607                 list_super (fs->super);
608                 if (fs->super->s_feature_incompat &
609                       EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
610                         print_journal_information(fs);
611                         ext2fs_close(fs);
612                         exit(0);
613                 }
614                 if ((fs->super->s_feature_compat &
615                      EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
616                     (fs->super->s_journal_inum != 0))
617                         print_inline_journal_information(fs);
618                 list_bad_blocks(fs, 0);
619                 if (header_only) {
620                         ext2fs_close (fs);
621                         exit (0);
622                 }
623                 retval = ext2fs_read_bitmaps (fs);
624                 list_desc (fs);
625                 if (retval) {
626                         printf(_("\n%s: %s: error reading bitmaps: %s\n"),
627                                program_name, device_name,
628                                error_message(retval));
629                 }
630         }
631         ext2fs_close (fs);
632         remove_error_table(&et_ext2_error_table);
633         exit (0);
634 }