Whamcloud - gitweb
ChangeLog, debugfs.c, debugfs.h, logdump.c:
[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) {
191
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                         return;
198                 }
199                 
200                 journal_source.where = JOURNAL_IS_EXTERNAL;
201                 journal_source.fd = journal_fd;
202
203         } else {
204
205                 /* Set up to read journal from the open filesystem */
206                 if (check_fs_open(argv[0]))
207                         return;
208                 journal_inum = current_fs->super->s_journal_inum;
209                 if (!journal_inum) {
210                         com_err(argv[0], 0, "filesystem has no journal");
211                         return;
212                 }
213
214                 retval = ext2fs_read_inode(current_fs, journal_inum, 
215                                            &journal_inode);
216                 if (retval) {
217                         com_err(argv[0], retval,
218                                 "while reading inode %u", journal_inum);
219                         return;
220                 }
221                 
222                 retval = ext2fs_file_open(current_fs, journal_inum,
223                                           0, &journal_file);
224                 if (retval) {
225                         com_err(argv[0], retval, "while opening ext2 file");
226                         return;
227                 }
228                 
229                 journal_source.where = JOURNAL_IS_INTERNAL;
230                 journal_source.file = journal_file;
231         }
232
233         dump_journal(argv[0], out_file, &journal_source);
234
235         if (journal_source.where == JOURNAL_IS_INTERNAL)
236                 ext2fs_file_close(journal_file);
237         else
238                 close(journal_fd);
239
240         if (out_file != stdout)
241                 fclose(out_file);
242
243         return;
244 }
245
246
247 static int read_journal_block(const char *cmd, struct journal_source *source, 
248                               off_t offset, char *buf, int size,
249                               unsigned int *got)
250 {
251         int retval;
252         
253         if (source->where == JOURNAL_IS_EXTERNAL) {
254                 if (lseek(source->fd, offset, SEEK_SET) < 0) {
255                         retval = errno;
256                         com_err(cmd, retval, "while seeking in reading journal");
257                         return retval;
258                 }
259                 retval = read(source->fd, buf, size);
260                 if (retval >= 0) {
261                         *got = retval;
262                         retval = 0;
263                 }
264                 retval = errno;
265         } else {
266                 retval = ext2fs_file_lseek(source->file, offset, 
267                                            EXT2_SEEK_SET, NULL);
268                 if (retval) {
269                         com_err(cmd, retval, "while seeking in reading journal");
270                         return retval;
271                 }
272                 
273                 retval = ext2fs_file_read(source->file, buf, size, got);
274         }
275         
276         if (retval)
277                 com_err(cmd, retval, "while while reading journal");
278         else if (*got != size) {
279                 com_err(cmd, 0, "short read (read %d, expected %d) while while reading journal", *got, size);
280                 retval = -1;
281         }
282         
283         return retval;
284 }
285
286 static const char *type_to_name(int btype)
287 {
288         switch (btype) {
289         case JFS_DESCRIPTOR_BLOCK:
290                 return "descriptor block";
291         case JFS_COMMIT_BLOCK:
292                 return "commit block";
293         case JFS_SUPERBLOCK_V1:
294                 return "V1 superblock";
295         case JFS_SUPERBLOCK_V2:
296                 return "V2 superblock";
297         case JFS_REVOKE_BLOCK:
298                 return "revoke table";
299         }
300         return "unrecognised type";
301 }
302
303
304 static void dump_journal(char *cmdname, FILE *out_file, 
305                          struct journal_source *source)
306 {
307         char                    jsb_buffer[1024];
308         char                    buf[8192];
309         journal_superblock_t    *jsb;
310         int                     blocksize;
311         unsigned int            got;
312         int                     retval;
313         __u32                   magic, sequence, blocktype;
314         journal_header_t        *header;
315         
316         tid_t                   transaction;
317         unsigned int            blocknr;
318         
319         /* First: locate the journal superblock */
320
321         retval = read_journal_block(cmdname, source, 0, 
322                                     jsb_buffer, 1024, &got);
323         if (retval)
324                 return;
325         
326         jsb = (journal_superblock_t *) jsb_buffer;
327         blocksize = be32_to_cpu(jsb->s_blocksize);
328         transaction = be32_to_cpu(jsb->s_sequence);
329         blocknr = be32_to_cpu(jsb->s_start);
330
331         fprintf(out_file, "Journal starts at block %u, transaction %u\n",
332                 blocknr, transaction);
333
334         if (!blocknr)
335                 /* Empty journal, nothing to do. */
336                 return;
337                 
338         while (1) {
339                 retval = read_journal_block(cmdname, source, 
340                                             blocknr*blocksize, buf,
341                                             blocksize, &got);
342                 if (retval || got != blocksize)
343                         return;
344         
345                 header = (journal_header_t *) buf;
346
347                 magic = be32_to_cpu(header->h_magic);
348                 sequence = be32_to_cpu(header->h_sequence);
349                 blocktype = be32_to_cpu(header->h_blocktype);
350                 
351                 if (magic != JFS_MAGIC_NUMBER) {
352                         fprintf (out_file, "No magic number at block %u: "
353                                  "end of journal.\n", blocknr);
354                         return;
355                 }
356                 
357                 if (sequence != transaction) {
358                         fprintf (out_file, "Found sequence %u (not %u) at "
359                                  "block %u: end of journal.\n", 
360                                  sequence, transaction, blocknr);
361                         return;
362                 }
363
364                 if (dump_descriptors) {
365                         fprintf (out_file, "Found expected sequence %u, "
366                                  "type %u (%s) at block %u\n",
367                                  sequence, blocktype, 
368                                  type_to_name(blocktype), blocknr);
369                 }
370                 
371                 switch (blocktype) {
372                 case JFS_DESCRIPTOR_BLOCK:
373                         dump_descriptor_block(out_file, source, buf, jsb, 
374                                               &blocknr, blocksize,
375                                               transaction);
376                         continue;
377
378                 case JFS_COMMIT_BLOCK:
379                         transaction++;
380                         blocknr++;
381                         WRAP(jsb, blocknr);
382                         continue;
383                         
384                 case JFS_REVOKE_BLOCK:
385                         dump_revoke_block(out_file, buf, jsb,
386                                           blocknr, blocksize, 
387                                           transaction);
388                         blocknr++;
389                         WRAP(jsb, blocknr);
390                         continue;
391
392                 default:
393                         fprintf (out_file, "Unexpected block type %u at "
394                                  "block %u.\n", blocktype, blocknr);
395                         return;
396                 }
397         }
398 }
399
400
401 static void dump_descriptor_block(FILE *out_file, 
402                                   struct journal_source *source, 
403                                   char *buf,
404                                   journal_superblock_t *jsb, 
405                                   unsigned int *blockp, int blocksize,
406                                   tid_t transaction)
407 {
408         int                     offset;
409         char                    *tagp;
410         journal_block_tag_t     *tag;
411         unsigned int            blocknr;
412         __u32                   tag_block;
413         __u32                   tag_flags;
414                 
415
416         offset = sizeof(journal_header_t);
417         blocknr = *blockp;
418
419         if (dump_all) 
420                 fprintf(out_file, "Dumping descriptor block, sequence %u, at "
421                         "block %u:\n", transaction, blocknr);
422         
423         ++blocknr;
424         WRAP(jsb, blocknr);
425         
426         do {
427                 /* Work out the location of the current tag, and skip to 
428                  * the next one... */
429                 tagp = &buf[offset];
430                 tag = (journal_block_tag_t *) tagp;
431                 offset += sizeof(journal_block_tag_t);
432
433                 /* ... and if we have gone too far, then we've reached the
434                    end of this block. */
435                 if (offset > blocksize)
436                         break;
437         
438                 tag_block = be32_to_cpu(tag->t_blocknr);
439                 tag_flags = be32_to_cpu(tag->t_flags);
440
441                 if (!(tag_flags & JFS_FLAG_SAME_UUID))
442                         offset += 16;
443
444                 dump_metadata_block(out_file, source, jsb, 
445                                     blocknr, tag_block, blocksize, 
446                                     transaction);
447
448                 ++blocknr;
449                 WRAP(jsb, blocknr);
450                 
451         } while (!(tag_flags & JFS_FLAG_LAST_TAG));
452         
453         *blockp = blocknr;
454 }
455
456
457 static void dump_revoke_block(FILE *out_file, char *buf,
458                                   journal_superblock_t *jsb, 
459                                   unsigned int blocknr, int blocksize,
460                                   tid_t transaction)
461 {
462         int                     offset, max;
463         journal_revoke_header_t *header;
464         unsigned int            *entry, rblock;
465         
466         if (dump_all) 
467                 fprintf(out_file, "Dumping revoke block, sequence %u, at "
468                         "block %u:\n", transaction, blocknr);
469         
470         header = (journal_revoke_header_t *) buf;
471         offset = sizeof(journal_revoke_header_t);
472         max = be32_to_cpu(header->r_count);
473
474         while (offset < max) {
475                 entry = (unsigned int *) (buf + offset);
476                 rblock = be32_to_cpu(*entry);
477                 if (dump_all || rblock == block_to_dump) {
478                         fprintf(out_file, "  Revoke FS block %u", rblock);
479                         if (dump_all)
480                                 fprintf(out_file, "\n");
481                         else
482                                 fprintf(out_file," at block %u, sequence %u\n",
483                                         blocknr, transaction);
484                 }
485                 offset += 4;
486         }
487 }
488
489
490 static void show_extent(FILE *out_file, int start_extent, int end_extent,
491                         __u32 first_block)
492 {
493         if (start_extent >= 0 && first_block != 0)
494                 fprintf(out_file, "(%d+%u): %u ", 
495                         start_extent, end_extent-start_extent, first_block);
496 }
497
498 static void show_indirect(FILE *out_file, const char *name, __u32 where)
499 {
500         if (where)
501                 fprintf(out_file, "(%s): %u ", name, where);
502 }
503
504
505 static void dump_metadata_block(FILE *out_file, struct journal_source *source,
506                                 journal_superblock_t *jsb,
507                                 unsigned int log_blocknr, 
508                                 unsigned int fs_blocknr, 
509                                 int blocksize,
510                                 tid_t transaction)
511 {
512         unsigned int    got;
513         int             retval;
514         char            buf[8192];
515         
516         if (!(dump_all
517               || (fs_blocknr == block_to_dump)
518               || (fs_blocknr == inode_block_to_dump)
519               || (fs_blocknr == bitmap_to_dump)))
520                 return;
521         
522         fprintf(out_file, "  FS block %u logged at ", fs_blocknr);
523         if (!dump_all) 
524                 fprintf(out_file, "sequence %u, ", transaction);
525         fprintf(out_file, "journal block %u\n", log_blocknr);
526         
527         /* There are two major special cases to parse:
528          * 
529          * If this block is a block
530          * bitmap block, we need to give it special treatment so that we
531          * can log any allocates and deallocates which affect the
532          * block_to_dump query block. 
533          * 
534          * If the block is an inode block for the inode being searched
535          * for, then we need to dump the contents of that inode
536          * structure symbolically.  
537          */
538         
539         if (!(dump_contents && dump_all)
540             && fs_blocknr != block_to_dump
541             && fs_blocknr != bitmap_to_dump 
542             && fs_blocknr != inode_block_to_dump)
543                 return;
544         
545         retval = read_journal_block("logdump", source, 
546                                     blocksize * log_blocknr,
547                                     buf, blocksize, &got);
548         if (retval)
549                 return;
550         
551         if (fs_blocknr == bitmap_to_dump) {
552                 struct ext2_super_block *super;
553                 int offset;
554                 
555                 super = current_fs->super;
556                 offset = ((fs_blocknr - super->s_first_data_block) %
557                           super->s_blocks_per_group);
558         
559                 fprintf(out_file, "    (block bitmap for block %u: "
560                         "block is %s)\n", 
561                         block_to_dump,
562                         ext2fs_test_bit(offset, buf) ? "SET" : "CLEAR");
563         }
564         
565         if (fs_blocknr == inode_block_to_dump) {
566                 struct ext2_inode *inode;
567                 int first, prev, this, start_extent, i;
568                 
569                 fprintf(out_file, "    (inode block for inode %u):\n",
570                         inode_to_dump);
571                 
572                 inode = (struct ext2_inode *) (buf + inode_offset_to_dump);
573                 internal_dump_inode(out_file, "    ", inode_to_dump, inode, 0);
574                 
575                 /* Dump out the direct/indirect blocks here:
576                  * internal_dump_inode can only dump them from the main
577                  * on-disk inode, not from the journaled copy of the
578                  * inode. */
579                 
580                 fprintf (out_file, "    Blocks:  ");
581                 first = prev = start_extent = -1;
582
583                 for (i=0; i<EXT2_NDIR_BLOCKS; i++) {
584                         this = inode->i_block[i];
585                         if (start_extent >= 0  && this == prev+1) {
586                                 prev = this;
587                                 continue;
588                         } else {
589                                 show_extent(out_file, start_extent, i, first);
590                                 start_extent = i;
591                                 first = prev = this;
592                         }
593                 }
594                 show_extent(out_file, start_extent, i, first);
595                 show_indirect(out_file, "IND", inode->i_block[i++]);
596                 show_indirect(out_file, "DIND", inode->i_block[i++]);
597                 show_indirect(out_file, "TIND", inode->i_block[i++]);
598                 
599                 fprintf(out_file, "\n");
600         }
601
602         if (dump_contents)
603                 do_hexdump(out_file, buf, blocksize);
604         
605 }
606
607 static void do_hexdump (FILE *out_file, char *buf, int blocksize)
608 {
609         int i,j;
610         int *intp;
611         char *charp;
612         unsigned char c;
613         
614         intp = (int *) buf;
615         charp = (char *) buf;
616         
617         for (i=0; i<blocksize; i+=16) {
618                 fprintf(out_file, "    %04x:  ", i);
619                 for (j=0; j<16; j+=4)
620                         fprintf(out_file, "%08x ", *intp++);
621                 for (j=0; j<16; j++) {
622                         c = *charp++;
623                         if (c < ' ' || c >= 127)
624                                 c = '.';
625                         fprintf(out_file, "%c", c);
626                 }
627                 fprintf(out_file, "\n");
628         }
629 }
630