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