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