Whamcloud - gitweb
0102c2484ca29bca73876281c0a299f470dc44e2
[tools/e2fsprogs.git] / debugfs / logdump.c
1 /*
2  * logdump.c --- dump the contents of the journal out to a file
3  * 
4  * Authro: Stephen C. Tweedie, 2001  <sct@redhat.com>
5  * Copyright (C) 2001 Red Hat, Inc.
6  * Based on portions  Copyright (C) 1994 Theodore Ts'o.  
7  *
8  * This file may be redistributed under the terms of the GNU Public 
9  * License.
10  */
11
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include <time.h>
18 #ifdef HAVE_ERRNO_H
19 #include <errno.h>
20 #endif
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <utime.h>
25 #ifdef HAVE_GETOPT_H
26 #include <getopt.h>
27 #else 
28 extern int optind;
29 extern char *optarg;
30 #endif
31 #ifdef HAVE_OPTRESET
32 extern int optreset;            /* defined by BSD, but not others */
33 #endif
34
35 #include "debugfs.h"
36 #include "jfs_user.h"
37
38 enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL};
39
40 int             dump_all, dump_contents, dump_descriptors;
41 unsigned int    block_to_dump, group_to_dump, bitmap_to_dump;
42 unsigned int    inode_block_to_dump, inode_offset_to_dump, bitmap_to_dump;
43 ext2_ino_t      inode_to_dump;
44
45 struct journal_source 
46 {
47         enum journal_location where;
48         int fd;
49         ext2_file_t file;
50 };
51
52 static void dump_journal(char *, FILE *, struct journal_source *);
53
54 static void dump_descriptor_block(FILE *, struct journal_source *,
55                                   char *, journal_superblock_t *,
56                                   unsigned int *, int, tid_t);
57
58 static void dump_revoke_block(FILE *, char *, journal_superblock_t *,
59                                   unsigned int, int, tid_t);
60
61 static void dump_metadata_block(FILE *, struct journal_source *,
62                                 journal_superblock_t*, 
63                                 unsigned int, unsigned int, int, tid_t);
64
65 static void do_hexdump (FILE *, char *, int);
66
67 #define WRAP(jsb, blocknr)                                      \
68         if (blocknr >= be32_to_cpu((jsb)->s_maxlen))            \
69                 blocknr -= (be32_to_cpu((jsb)->s_maxlen) -      \
70                             be32_to_cpu((jsb)->s_first));
71
72
73 void do_logdump(int argc, char **argv)
74 {
75         int             c;
76         int             retval;
77         char            *out_fn;
78         FILE            *out_file;
79         
80         char            *inode_spec = NULL;
81         char            *journal_fn = NULL;
82         int             journal_fd = 0;
83         ext2_ino_t      journal_inum;
84         struct ext2_inode journal_inode;
85         ext2_file_t     journal_file;
86         
87         char            *tmp;
88         
89         const char      *logdump_usage = ("Usage: logdump "
90                                           "[-ac] [-b<block>] [-i<inode>] "
91                                           "[-f<journal_file>] [output_file]");
92         
93         struct journal_source journal_source;
94
95         optind = 0;
96 #ifdef HAVE_OPTRESET
97         optreset = 1;           /* Makes BSD getopt happy */
98 #endif
99         journal_source.where = 0;
100         journal_source.fd = 0;
101         journal_source.file = 0;
102         dump_all = 0;
103         dump_contents = 0;
104         dump_descriptors = 1;
105         block_to_dump = -1;
106         bitmap_to_dump = -1;
107         inode_block_to_dump = -1;
108         inode_to_dump = -1;
109         
110         while ((c = getopt (argc, argv, "ab:ci:f:")) != EOF) {
111                 switch (c) {
112                 case 'a':
113                         dump_all++;
114                         break;
115                 case 'b':
116                         block_to_dump = strtoul(optarg, &tmp, 0);
117                         if (*tmp) {
118                                 com_err(argv[0], 0,
119                                         "Bad block number - %s", optarg);
120                                 return;
121                         }
122                         dump_descriptors = 0;
123                         break;
124                 case 'c':
125                         dump_contents++;
126                         break;
127                 case 'f':
128                         journal_fn = optarg;
129                         break;
130                 case 'i':
131                         inode_spec = optarg;
132                         dump_descriptors = 0;
133                         break;
134                 default:
135                         com_err(argv[0], 0, logdump_usage);
136                         return;
137                 }
138         }
139         if (optind != argc && optind != argc-1) {
140                 com_err(argv[0], 0, logdump_usage);
141                 return;
142         }
143
144         if (inode_spec) {
145                 int inode_group, group_offset, inodes_per_block;
146                 
147                 if (check_fs_open(argv[0]))
148                         return;
149
150                 inode_to_dump = string_to_inode(inode_spec);
151                 if (!inode_to_dump)
152                         return;
153
154                 inode_group = ((inode_to_dump - 1)
155                                / current_fs->super->s_inodes_per_group);
156                 group_offset = ((inode_to_dump - 1)
157                                 % current_fs->super->s_inodes_per_group);
158                 inodes_per_block = (current_fs->blocksize 
159                                     / sizeof(struct ext2_inode));
160                 
161                 inode_block_to_dump = 
162                         current_fs->group_desc[inode_group].bg_inode_table + 
163                         (group_offset / inodes_per_block);
164                 inode_offset_to_dump = ((group_offset % inodes_per_block)
165                                         * sizeof(struct ext2_inode));
166                 printf("Inode %u is at group %u, block %u, offset %u\n",
167                        inode_to_dump, inode_group,
168                        inode_block_to_dump, inode_offset_to_dump);
169         }
170
171         if (optind == argc) {
172                 out_file = stdout;
173         } else {
174                 out_fn = argv[optind];
175                 out_file = fopen(out_fn, "w");
176                 if (!out_file < 0) {
177                         com_err(argv[0], errno, "while opening %s for logdump",
178                                 out_fn);
179                         return;
180                 }
181         }
182
183         if (block_to_dump != -1 && current_fs != NULL) {
184                 group_to_dump = ((block_to_dump - 
185                                   current_fs->super->s_first_data_block)
186                                  / current_fs->super->s_blocks_per_group);
187                 bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap;
188         }
189
190         if (!journal_fn && check_fs_open(argv[0]))
191                 return;
192
193         if (journal_fn) {
194                 /* Set up to read journal from a regular file somewhere */
195                 journal_fd = open(journal_fn, O_RDONLY, 0);
196                 if (journal_fd < 0) {
197                         com_err(argv[0], errno, "while opening %s for logdump",
198                                 journal_fn);
199                         return;
200                 }
201                 
202                 journal_source.where = JOURNAL_IS_EXTERNAL;
203                 journal_source.fd = journal_fd;
204         } else if ((journal_inum = current_fs->super->s_journal_inum)) {
205                 retval = ext2fs_read_inode(current_fs, journal_inum, 
206                                            &journal_inode);
207                 if (retval) {
208                         com_err(argv[0], retval,
209                                 "while reading inode %u", journal_inum);
210                         return;
211                 }
212
213                 retval = ext2fs_file_open(current_fs, journal_inum,
214                                           0, &journal_file);
215                 if (retval) {
216                         com_err(argv[0], retval, "while opening ext2 file");
217                         return;
218                 }
219                 journal_source.where = JOURNAL_IS_INTERNAL;
220                 journal_source.file = journal_file;
221         } else if ((journal_fn =
222             ext2fs_find_block_device(current_fs->super->s_journal_dev))) {
223                 journal_fd = open(journal_fn, O_RDONLY, 0);
224                 if (journal_fd < 0) {
225                         com_err(argv[0], errno, "while opening %s for logdump",
226                                 journal_fn);
227                         free(journal_fn);
228                         return;
229                 }
230                 fprintf(out_file, "Using external journal found at %s\n",
231                         journal_fn);
232                 free(journal_fn);
233                 journal_source.where = JOURNAL_IS_EXTERNAL;
234                 journal_source.fd = journal_fd;
235         } else {
236                 com_err(argv[0], 0, "filesystem has no journal");
237                 return;
238         }
239
240         dump_journal(argv[0], out_file, &journal_source);
241
242         if (journal_source.where == JOURNAL_IS_INTERNAL)
243                 ext2fs_file_close(journal_file);
244         else
245                 close(journal_fd);
246
247         if (out_file != stdout)
248                 fclose(out_file);
249
250         return;
251 }
252
253
254 static int read_journal_block(const char *cmd, struct journal_source *source, 
255                               off_t offset, char *buf, int size,
256                               unsigned int *got)
257 {
258         int retval;
259         
260         if (source->where == JOURNAL_IS_EXTERNAL) {
261                 if (lseek(source->fd, offset, SEEK_SET) < 0) {
262                         retval = errno;
263                         com_err(cmd, retval, "while seeking in reading journal");
264                         return retval;
265                 }
266                 retval = read(source->fd, buf, size);
267                 if (retval >= 0) {
268                         *got = retval;
269                         retval = 0;
270                 } else
271                         retval = errno;
272         } else {
273                 retval = ext2fs_file_lseek(source->file, offset, 
274                                            EXT2_SEEK_SET, NULL);
275                 if (retval) {
276                         com_err(cmd, retval, "while seeking in reading journal");
277                         return retval;
278                 }
279                 
280                 retval = ext2fs_file_read(source->file, buf, size, got);
281         }
282         
283         if (retval)
284                 com_err(cmd, retval, "while while reading journal");
285         else if (*got != size) {
286                 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
287                 retval = -1;
288         }
289         
290         return retval;
291 }
292
293 static const char *type_to_name(int btype)
294 {
295         switch (btype) {
296         case JFS_DESCRIPTOR_BLOCK:
297                 return "descriptor block";
298         case JFS_COMMIT_BLOCK:
299                 return "commit block";
300         case JFS_SUPERBLOCK_V1:
301                 return "V1 superblock";
302         case JFS_SUPERBLOCK_V2:
303                 return "V2 superblock";
304         case JFS_REVOKE_BLOCK:
305                 return "revoke table";
306         }
307         return "unrecognised type";
308 }
309
310
311 static void dump_journal(char *cmdname, FILE *out_file, 
312                          struct journal_source *source)
313 {
314         struct ext2_super_block *sb;
315         char                    jsb_buffer[1024];
316         char                    buf[8192];
317         journal_superblock_t    *jsb;
318         int                     blocksize = 1024;
319         unsigned int            got;
320         int                     retval;
321         __u32                   magic, sequence, blocktype;
322         journal_header_t        *header;
323         
324         tid_t                   transaction;
325         unsigned int            blocknr = 0;
326         
327         /* First, check to see if there's an ext2 superblock header */
328         retval = read_journal_block(cmdname, source, 0, 
329                                     buf, 2048, &got);
330         if (retval)
331                 return;
332
333         jsb = (journal_superblock_t *) buf;
334         sb = (struct ext2_super_block *) (buf+1024);
335 #ifdef ENABLE_SWAPFS
336         if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) 
337                 ext2fs_swap_super(sb);
338 #endif
339         
340         if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
341             (sb->s_magic == EXT2_SUPER_MAGIC) &&
342             (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
343                 blocksize = EXT2_BLOCK_SIZE(sb);
344                 blocknr = (blocksize == 1024) ? 2 : 1;
345                 uuid_unparse(&sb->s_uuid, jsb_buffer);
346                 fprintf(out_file, "Ext2 superblock header found.\n");
347                 if (dump_all) {
348                         fprintf(out_file, "\tuuid=%s\n", jsb_buffer);
349                         fprintf(out_file, "\tblocksize=%d\n", blocksize);
350                         fprintf(out_file, "\tjournal data size %ld\n",
351                                 sb->s_blocks_count);
352                 }
353         }
354         
355         /* Next, read the journal superblock */
356
357         retval = read_journal_block(cmdname, source, blocknr*blocksize, 
358                                     jsb_buffer, 1024, &got);
359         if (retval)
360                 return;
361
362         jsb = (journal_superblock_t *) jsb_buffer;
363         if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
364                 fprintf(out_file,
365                         "Journal superblock magic number invalid!\n");
366                 return;
367         }
368         blocksize = be32_to_cpu(jsb->s_blocksize);
369         transaction = be32_to_cpu(jsb->s_sequence);
370         blocknr = be32_to_cpu(jsb->s_start);
371
372         fprintf(out_file, "Journal starts at block %u, transaction %u\n",
373                 blocknr, transaction);
374
375         if (!blocknr)
376                 /* Empty journal, nothing to do. */
377                 return;
378                 
379         while (1) {
380                 retval = read_journal_block(cmdname, source, 
381                                             blocknr*blocksize, buf,
382                                             blocksize, &got);
383                 if (retval || got != blocksize)
384                         return;
385         
386                 header = (journal_header_t *) buf;
387
388                 magic = be32_to_cpu(header->h_magic);
389                 sequence = be32_to_cpu(header->h_sequence);
390                 blocktype = be32_to_cpu(header->h_blocktype);
391                 
392                 if (magic != JFS_MAGIC_NUMBER) {
393                         fprintf (out_file, "No magic number at block %u: "
394                                  "end of journal.\n", blocknr);
395                         return;
396                 }
397                 
398                 if (sequence != transaction) {
399                         fprintf (out_file, "Found sequence %u (not %u) at "
400                                  "block %u: end of journal.\n", 
401                                  sequence, transaction, blocknr);
402                         return;
403                 }
404
405                 if (dump_descriptors) {
406                         fprintf (out_file, "Found expected sequence %u, "
407                                  "type %u (%s) at block %u\n",
408                                  sequence, blocktype, 
409                                  type_to_name(blocktype), blocknr);
410                 }
411                 
412                 switch (blocktype) {
413                 case JFS_DESCRIPTOR_BLOCK:
414                         dump_descriptor_block(out_file, source, buf, jsb, 
415                                               &blocknr, blocksize,
416                                               transaction);
417                         continue;
418
419                 case JFS_COMMIT_BLOCK:
420                         transaction++;
421                         blocknr++;
422                         WRAP(jsb, blocknr);
423                         continue;
424                         
425                 case JFS_REVOKE_BLOCK:
426                         dump_revoke_block(out_file, buf, jsb,
427                                           blocknr, blocksize, 
428                                           transaction);
429                         blocknr++;
430                         WRAP(jsb, blocknr);
431                         continue;
432
433                 default:
434                         fprintf (out_file, "Unexpected block type %u at "
435                                  "block %u.\n", blocktype, blocknr);
436                         return;
437                 }
438         }
439 }
440
441
442 static void dump_descriptor_block(FILE *out_file, 
443                                   struct journal_source *source, 
444                                   char *buf,
445                                   journal_superblock_t *jsb, 
446                                   unsigned int *blockp, int blocksize,
447                                   tid_t transaction)
448 {
449         int                     offset;
450         char                    *tagp;
451         journal_block_tag_t     *tag;
452         unsigned int            blocknr;
453         __u32                   tag_block;
454         __u32                   tag_flags;
455                 
456
457         offset = sizeof(journal_header_t);
458         blocknr = *blockp;
459
460         if (dump_all) 
461                 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
462                         "block %u:\n", transaction, blocknr);
463         
464         ++blocknr;
465         WRAP(jsb, blocknr);
466         
467         do {
468                 /* Work out the location of the current tag, and skip to 
469                  * the next one... */
470                 tagp = &buf[offset];
471                 tag = (journal_block_tag_t *) tagp;
472                 offset += sizeof(journal_block_tag_t);
473
474                 /* ... and if we have gone too far, then we've reached the
475                    end of this block. */
476                 if (offset > blocksize)
477                         break;
478         
479                 tag_block = be32_to_cpu(tag->t_blocknr);
480                 tag_flags = be32_to_cpu(tag->t_flags);
481
482                 if (!(tag_flags & JFS_FLAG_SAME_UUID))
483                         offset += 16;
484
485                 dump_metadata_block(out_file, source, jsb, 
486                                     blocknr, tag_block, blocksize, 
487                                     transaction);
488
489                 ++blocknr;
490                 WRAP(jsb, blocknr);
491                 
492         } while (!(tag_flags & JFS_FLAG_LAST_TAG));
493         
494         *blockp = blocknr;
495 }
496
497
498 static void dump_revoke_block(FILE *out_file, char *buf,
499                                   journal_superblock_t *jsb, 
500                                   unsigned int blocknr, int blocksize,
501                                   tid_t transaction)
502 {
503         int                     offset, max;
504         journal_revoke_header_t *header;
505         unsigned int            *entry, rblock;
506         
507         if (dump_all) 
508                 fprintf(out_file, "Dumping revoke block, sequence %u, at "
509                         "block %u:\n", transaction, blocknr);
510         
511         header = (journal_revoke_header_t *) buf;
512         offset = sizeof(journal_revoke_header_t);
513         max = be32_to_cpu(header->r_count);
514
515         while (offset < max) {
516                 entry = (unsigned int *) (buf + offset);
517                 rblock = be32_to_cpu(*entry);
518                 if (dump_all || rblock == block_to_dump) {
519                         fprintf(out_file, "  Revoke FS block %u", rblock);
520                         if (dump_all)
521                                 fprintf(out_file, "\n");
522                         else
523                                 fprintf(out_file," at block %u, sequence %u\n",
524                                         blocknr, transaction);
525                 }
526                 offset += 4;
527         }
528 }
529
530
531 static void show_extent(FILE *out_file, int start_extent, int end_extent,
532                         __u32 first_block)
533 {
534         if (start_extent >= 0 && first_block != 0)
535                 fprintf(out_file, "(%d+%u): %u ", 
536                         start_extent, end_extent-start_extent, first_block);
537 }
538
539 static void show_indirect(FILE *out_file, const char *name, __u32 where)
540 {
541         if (where)
542                 fprintf(out_file, "(%s): %u ", name, where);
543 }
544
545
546 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
547                                 journal_superblock_t *jsb,
548                                 unsigned int log_blocknr, 
549                                 unsigned int fs_blocknr, 
550                                 int blocksize,
551                                 tid_t transaction)
552 {
553         unsigned int    got;
554         int             retval;
555         char            buf[8192];
556         
557         if (!(dump_all
558               || (fs_blocknr == block_to_dump)
559               || (fs_blocknr == inode_block_to_dump)
560               || (fs_blocknr == bitmap_to_dump)))
561                 return;
562         
563         fprintf(out_file, "  FS block %u logged at ", fs_blocknr);
564         if (!dump_all) 
565                 fprintf(out_file, "sequence %u, ", transaction);
566         fprintf(out_file, "journal block %u\n", log_blocknr);
567         
568         /* There are two major special cases to parse:
569          * 
570          * If this block is a block
571          * bitmap block, we need to give it special treatment so that we
572          * can log any allocates and deallocates which affect the
573          * block_to_dump query block. 
574          * 
575          * If the block is an inode block for the inode being searched
576          * for, then we need to dump the contents of that inode
577          * structure symbolically.  
578          */
579         
580         if (!(dump_contents && dump_all)
581             && fs_blocknr != block_to_dump
582             && fs_blocknr != bitmap_to_dump 
583             && fs_blocknr != inode_block_to_dump)
584                 return;
585         
586         retval = read_journal_block("logdump", source, 
587                                     blocksize * log_blocknr,
588                                     buf, blocksize, &got);
589         if (retval)
590                 return;
591         
592         if (fs_blocknr == bitmap_to_dump) {
593                 struct ext2_super_block *super;
594                 int offset;
595                 
596                 super = current_fs->super;
597                 offset = ((fs_blocknr - super->s_first_data_block) %
598                           super->s_blocks_per_group);
599         
600                 fprintf(out_file, "    (block bitmap for block %u: "
601                         "block is %s)\n", 
602                         block_to_dump,
603                         ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
604         }
605         
606         if (fs_blocknr == inode_block_to_dump) {
607                 struct ext2_inode *inode;
608                 int first, prev, this, start_extent, i;
609                 
610                 fprintf(out_file, "    (inode block for inode %u):\n",
611                         inode_to_dump);
612                 
613                 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
614                 internal_dump_inode(out_file, "    ", inode_to_dump, inode, 0);
615                 
616                 /* Dump out the direct/indirect blocks here:
617                  * internal_dump_inode can only dump them from the main
618                  * on-disk inode, not from the journaled copy of the
619                  * inode. */
620                 
621                 fprintf (out_file, "    Blocks:  ");
622                 first = prev = start_extent = -1;
623
624                 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
625                         this = inode->i_block[i];
626                         if (start_extent >= 0  && this == prev+1) {
627                                 prev = this;
628                                 continue;
629                         } else {
630                                 show_extent(out_file, start_extent, i, first);
631                                 start_extent = i;
632                                 first = prev = this;
633                         }
634                 }
635                 show_extent(out_file, start_extent, i, first);
636                 show_indirect(out_file, "IND", inode->i_block[i++]);
637                 show_indirect(out_file, "DIND", inode->i_block[i++]);
638                 show_indirect(out_file, "TIND", inode->i_block[i++]);
639                 
640                 fprintf(out_file, "\n");
641         }
642
643         if (dump_contents)
644                 do_hexdump(out_file, buf, blocksize);
645         
646 }
647
648 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
649 {
650         int i,j;
651         int *intp;
652         char *charp;
653         unsigned char c;
654         
655         intp = (int *) buf;
656         charp = (char *) buf;
657         
658         for (i=0; i<blocksize; i+=16) {
659                 fprintf(out_file, "    %04x:  ", i);
660                 for (j=0; j<16; j+=4)
661                         fprintf(out_file, "%08x ", *intp++);
662                 for (j=0; j<16; j++) {
663                         c = *charp++;
664                         if (c < ' ' || c >= 127)
665                                 c = '.';
666                         fprintf(out_file, "%c", c);
667                 }
668                 fprintf(out_file, "\n");
669         }
670 }
671