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