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