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