1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
|
/***************************************************************************
* Copyright (C) 2010 by Roberto Maar *
* robi@users.berlios.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <utime.h>
#include <blkid/blkid.h>
#include <ext2fs/ext2fs.h>
#include "jfs_user.h"
#include <uuid/uuid.h>
#include "ext4magic.h"
#include "util.h"
#include "journal.h"
enum journal_location {JOURNAL_IS_INTERNAL, JOURNAL_IS_EXTERNAL, JOURNAL_IS_DUMMY};
//flags for journaldescriptor control
#define ANY_BLOCK ((blk_t) -1)
#define WRAP_UNDEF 0
#define WRAP_ON 1
#define WRAP_OFF 2
#define WRAP_READY 3
struct journal_source
{
enum journal_location where;
int fd;
ext2_file_t file;
char* j_dummy;
};
//static variable for work with journal
struct journal_source journal_source;
extern ext2_filsys current_fs ;
extern time_t now_time ;
struct ext2_super_block *jsb_pointer; //pointer for find & open journal
char jsb_buffer[1024]; //buffer for journal-superblock (be_to_CPU)
void* pt_buff; /*pointer of the privat descriptor Table */
journal_descriptor_tag_t *pt; /*pointer for descriptor Table*/
__u32 *ptl; /*reverse pointer for lost inodeblocks*/
__u32 pt_count; /*counter for privat descriptor Table */
__u32 ptl_count; /*counter for lost inodeblocks*/
struct j_bitmap_list_t jbbm ; /* for Block Bitmaps of Journal */
//print journal superblock
void dump_journal_superblock( void)
{
int i;
__u32 nr_users;
char buffer[40];
uuid_t *uu;
journal_superblock_t *jsb = (journal_superblock_t*)jsb_buffer;
fprintf(stdout,"\nJournal Super Block:\n");
fprintf(stdout," Signature: 0x%08x \n",jsb->s_header.h_magic);
fprintf(stdout," Blocktype : %s \n",type_to_name(jsb->s_header.h_blocktype));
fprintf(stdout," Journal block size: %lu \n",jsb->s_blocksize);
fprintf(stdout," Number of journal blocks: %lu \n",jsb->s_maxlen);
fprintf(stdout," Journal block where the journal actually starts: %lu\n",jsb->s_first);
fprintf(stdout," Sequence number of first transaction: %lu\n", jsb->s_sequence);
fprintf(stdout," Journal block of first transaction: %lu\n", jsb->s_start);
fprintf(stdout," Error number: %ld\n",jsb->s_errno);
if ((jsb->s_header.h_blocktype) != JFS_SUPERBLOCK_V2)
return ;
// Remaining fields are only valid in a version-2 superblock
//FIXME: Strings of Features
fprintf(stdout," Compatible Features: %lu\n",jsb->s_feature_compat);
fprintf(stdout," Incompatible features: %lu\n",jsb->s_feature_incompat);
fprintf(stdout," Read only compatible features: %lu\n",jsb->s_feature_ro_compat);
uuid_unparse(jsb->s_uuid, buffer);
fprintf(stdout," Journal UUID: %s \n",buffer);
nr_users = jsb->s_nr_users;
fprintf(stdout," Number of file systems using journal: %lu\n", jsb->s_nr_users);
fprintf(stdout," Location of superblock copy: %lu\n",jsb->s_dynsuper);
fprintf(stdout," Max journal blocks per transaction: %lu\n",jsb->s_max_transaction);
fprintf(stdout," Max file system blocks per transaction: %lu\n",jsb->s_max_trans_data);
if (nr_users && (nr_users < 48)) {
fprintf(stdout," IDs of all file systems using the journal:\n");
for (i = 0; i < nr_users; i++){
uu=(void*) &jsb->s_users[i*16];
uuid_unparse(*uu, buffer);
fprintf(stdout,"%02d. : %s\n",i+1,buffer);
}
}
fprintf(stdout," \n\n");
return;
}
// local subfunction
static void journal_superblock_to_cpu ( __u32 *jsb )
{
int i;
for (i=0;i<12;i++)
*(jsb+i) = ext2fs_be32_to_cpu(*(jsb+i));
// UUIDs are endian-independent, so don't swap those bytes
for (i=16;i<20;i++)
*(jsb+i) = ext2fs_be32_to_cpu(*(jsb+i));
// User IDs are endian-independent
}
// open an extract the blocklist from journal
extern int journal_open( char *journal_file_name, int journal_backup_flag )
{
char *journal_dev_name = NULL;
char *dummy_journal = NULL;
int journal_fd = 0;
ext2_ino_t journal_inum;
struct ext2_inode journal_inode;
int retval;
ext2_file_t journal_file;
pt_buff = NULL;
ptl = NULL;
if (current_fs) jsb_pointer = current_fs->super;
else {
fprintf(stderr,"No filesystem open, this must be a bug\n");
return(JOURNAL_ERROR);
}
if (journal_file_name) {
/* Set up to read journal from a regular file somewhere */
journal_fd = open(journal_file_name, O_RDONLY, 0);
if (journal_fd < 0) {
fprintf(stderr,"Error %d while opening %s for read external journal\n",errno,journal_file_name);
goto errout;
}
journal_source.where = JOURNAL_IS_EXTERNAL;
journal_source.fd = journal_fd;
journal_source.file = NULL;
journal_source.j_dummy = NULL;
printf("Using external Journal at File %s \n",journal_file_name);
} else if ((journal_inum = jsb_pointer->s_journal_inum)) {
if (journal_backup_flag) {
if (jsb_pointer->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) {
fprintf(stderr,"no journal backup in super block\n");
goto errout;
}
memset(&journal_inode, 0, sizeof(struct ext2_inode));
memcpy(&journal_inode.i_block[0], jsb_pointer->s_jnl_blocks, EXT2_N_BLOCKS*4);
journal_inode.i_size = jsb_pointer->s_jnl_blocks[16];
journal_inode.i_links_count = 1;
journal_inode.i_mode = LINUX_S_IFREG | 0600;
if (jsb_pointer->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
journal_inode.i_flags |= EXT4_EXTENTS_FL;
} else {
if (intern_read_inode(journal_inum, &journal_inode))
goto errout;
}
retval = ext2fs_file_open2(current_fs, journal_inum,
&journal_inode, 0, &journal_file);
#ifdef EXPERT_MODE
if (journal_backup_flag){
//check the Journal Magic Number
char* jsb_buffer = NULL;
int got;
journal_superblock_t* jsb;
jsb_buffer = malloc(1024);
journal_source.where = JOURNAL_IS_INTERNAL;
journal_source.file = journal_file;
if ((jsb_buffer) && (! read_journal_block(0,jsb_buffer, 1024, &got))){
jsb = (journal_superblock_t *) jsb_buffer;
if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) ||
(current_fs->blocksize != be32_to_cpu(jsb->s_blocksize))) {
retval = 1;
}
free(jsb_buffer);
}
else return JOURNAL_ERROR ;
}
#endif
if (retval) {
fprintf(stderr," Error %d while opening journal\n",retval);
#ifdef EXPERT_MODE
if (journal_backup_flag) {
journal_superblock_t *jsb;
//the journal is defect, create a insubstantial blank journal
dummy_journal = (char*) malloc(current_fs->blocksize * 2);
if (dummy_journal){
memset (dummy_journal,0,current_fs->blocksize *2);
jsb = (journal_superblock_t*) dummy_journal;
jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
jsb->s_blocksize = htonl(current_fs->blocksize);
jsb->s_maxlen = htonl(2);
jsb->s_nr_users = htonl(1);
jsb->s_first = htonl(1);
jsb->s_sequence = htonl(1);
memcpy(jsb->s_uuid, current_fs->super->s_uuid, sizeof(current_fs->super->s_uuid));
journal_source.where = JOURNAL_IS_DUMMY;
journal_source.file = NULL;
journal_source.fd = -1 ;
journal_source.j_dummy = dummy_journal;
printf("Journal defect, using a dummy Journal\n");
}else
goto errout;
}
else
#endif
goto errout;
}
else {
journal_source.where = JOURNAL_IS_INTERNAL;
journal_source.file = journal_file;
journal_source.fd = -1 ;
journal_source.j_dummy = NULL;
printf("Using %s internal Journal at Inode %d\n",(journal_backup_flag)?"a recovered":"",journal_inum);
}
} else {
char uuid[37];
uuid_unparse(jsb_pointer->s_journal_uuid, uuid);
journal_dev_name = blkid_get_devname(NULL, "UUID", uuid);
if (!journal_dev_name)
journal_dev_name = blkid_devno_to_devname(jsb_pointer->s_journal_dev);
if (!journal_dev_name) {
fprintf(stderr,"filesystem has no journal\n");
goto errout;
}
journal_fd = open(journal_dev_name, O_RDONLY, 0);
if (journal_fd < 0) {
fprintf(stderr,"%d while opening %s for external journal\n",errno,journal_dev_name);
free(journal_dev_name);
goto errout;
}
printf("Using external journal found at %s\n", journal_dev_name);
free(journal_dev_name);
journal_source.where = JOURNAL_IS_EXTERNAL;
journal_source.fd = journal_fd;
journal_source.file = NULL;
}
return init_journal();
errout:
fprintf(stderr,"Error: while open journal\n" );
jsb_pointer = NULL;
return (JOURNAL_CLOSE);
}
// close the journal (last function in main() if the journal open)
extern int journal_close(void)
{
jsb_pointer = NULL;
pt_count = 0;
if ( pt_buff ) {
free(pt_buff);
pt_buff = NULL;
}
switch (journal_source.where){
case JOURNAL_IS_DUMMY :
if (journal_source.j_dummy){
free(journal_source.j_dummy);
journal_source.j_dummy = NULL;
}
break;
case JOURNAL_IS_INTERNAL :
if (journal_source.file){
ext2fs_file_close(journal_source.file);
journal_source.file = NULL;
}
break;
case JOURNAL_IS_EXTERNAL :
if (journal_source.fd > 0) {
close(journal_source.fd);
journal_source.fd = -1;
}
}
return (JOURNAL_CLOSE);
}
//print hexdump of a journalblock
int dump_journal_block( __u32 block_nr , int flag){
char *buf = NULL;
int got,retval = 0;
int blocksize = current_fs->blocksize;
buf = (char*) malloc(blocksize);
if(! buf) return 1 ;
retval = read_journal_block(block_nr * blocksize ,buf,blocksize,&got);
if (retval || got != blocksize)
return retval;
blockhex (stdout, buf, flag, blocksize);
if (buf)
free(buf);
return 0;
}
//read a journal block
int read_journal_block(off_t offset, char *buf, int size, unsigned int *got)
{
int retval;
switch (journal_source.where) {
case JOURNAL_IS_EXTERNAL :
if (lseek(journal_source.fd, offset, SEEK_SET) < 0) {
retval = errno;
fprintf(stderr,"Error %d while seeking in reading journal",retval);
return retval;
}
retval = read(journal_source.fd, buf, size);
if (retval >= 0) {
*got = retval;
retval = 0;
} else retval = errno;
break;
case JOURNAL_IS_INTERNAL :
retval = ext2fs_file_lseek(journal_source.file, offset, EXT2_SEEK_SET, NULL);
if (retval) {
fprintf(stderr,"Error %d while seeking in reading journal",retval);
return retval;
}
retval = ext2fs_file_read(journal_source.file, buf, size, got);
break;
case JOURNAL_IS_DUMMY :
if((offset + size) > (2 *current_fs->blocksize)) retval = -1 ;
memcpy(buf,journal_source.j_dummy + offset,size);
*got = (unsigned int) size;
retval = 0;
break;
}
if (retval)
fprintf(stderr,"Error %d while reading journal",retval);
else if (*got != (unsigned int) size) {
fprintf(stderr,"short read (read %d, expected %d) while reading journal", *got, size);
retval = -1;
}
return retval;
}
static const char *type_to_name(int btype)
{
switch (btype) {
case JFS_DESCRIPTOR_BLOCK:
return "descriptor block";
case JFS_COMMIT_BLOCK:
return "commit block";
case JFS_SUPERBLOCK_V1:
return "V1 superblock";
case JFS_SUPERBLOCK_V2:
return "V2 superblock";
case JFS_REVOKE_BLOCK:
return "revoke table";
}
return "unrecognised type";
}
//check if journal block is a lost inodeblock local function
int jb_is_inodetable(unsigned char *buf){
struct ext2_inode *inode;
__u16 mode;
__u32 atime;
__u32 ctime;
__u32 mtime;
__u32 dtime;
int i;
__u32 min = 315601200; // 315601200 = "1980-01-01 20:00:00"
__u32 max = (__u32) now_time;
int ret = 0;
int i_size = EXT2_INODE_SIZE(current_fs->super);
int inodes_per_block = (current_fs->blocksize / i_size );
int flag = 0;
for (i=0; i<inodes_per_block;i++){
inode = (struct ext2_inode*) (buf + (i_size * i));
mode = ext2fs_le16_to_cpu(inode->i_mode);
atime = ext2fs_le32_to_cpu(inode->i_atime);
ctime = ext2fs_le32_to_cpu(inode->i_ctime);
mtime = ext2fs_le32_to_cpu(inode->i_mtime);
dtime = ext2fs_le32_to_cpu(inode->i_dtime);
if ((!mode) && (!atime) && (!ctime) && (!mtime) && (!dtime) && (!i))
return 0;
if ((!dtime) && (ctime > min) && (ctime < max) && (atime > min) && (atime < max)
&& (mtime > min) && (mtime < max) && inode->i_size && inode->i_blocks
&& LINUX_S_ISREG(mode) && inode->i_block[0] )
flag++;
if (ctime > max)
return 0;
}
return flag;
}
//check if block is a inodeblock local function
static int block_is_inodetable(blk64_t block){
int group;
struct ext2_group_desc *gdp;
blk64_t last;
last = current_fs->super->s_inodes_count / (current_fs->blocksize / current_fs->super->s_inode_size ) ;
last -= ((current_fs->group_desc_count - 1) * current_fs->inode_blocks_per_group );
//FIXME for struct ext4_group_desc 48/64BIT
for (group = 0; group < current_fs->group_desc_count; group++){
#ifdef EXT2_FLAG_64BITS
gdp = ext2fs_group_desc(current_fs, current_fs->group_desc, group);
#else
gdp = ¤t_fs->group_desc[group];
#endif
if (block >= (gdp->bg_inode_table + current_fs->inode_blocks_per_group))
continue;
if (block >= gdp->bg_inode_table){
if ( group == (current_fs->group_desc_count - 1 )){
if (block >= gdp->bg_inode_table + last)
break;
}
return 1;
}
else
break;
}
return 0;
}
// get last timestamp of all inode in this journalblock
static __u32 get_transaction_time( __u32 j_block){
char *buf;
struct ext2_inode *inode;
__u32 t_time = 0;
int ino, got ,retval = 0;
int inode_size = EXT2_INODE_SIZE(current_fs->super);
int blocksize = current_fs->blocksize;
buf = (char*) malloc(blocksize);
if(! buf) return 0 ;
retval = read_journal_block(j_block * blocksize ,buf,blocksize,&got);
if (retval || got != blocksize){
fprintf(stderr,"Error while read journal block %ld\n",j_block);
t_time = 0;
goto errout;
}
for (ino = 0; ino < EXT2_INODES_PER_BLOCK(current_fs->super);ino++){
inode = (struct ext2_inode*) (buf + (inode_size * ino));
if (t_time < ext2fs_le32_to_cpu(inode->i_ctime))
t_time = ext2fs_le32_to_cpu(inode->i_ctime);
if (t_time < ext2fs_le32_to_cpu(inode->i_atime))
t_time = ext2fs_le32_to_cpu(inode->i_atime);
}
errout:
if (buf) free(buf);
return t_time;
}
//get the transactiontime of a transactionnumber
__u32 get_trans_time( __u32 transaction){
journal_descriptor_tag_t *pointer;
__u32 counter;
pointer = pt_buff;
for (counter=0 ; counter<pt_count ; counter++, pointer++) {
if (pointer->transaction != transaction) continue;
if (block_is_inodetable(pointer->f_blocknr)) {
return get_transaction_time(pointer->j_blocknr);
}
else continue;
}
return 0;
}
//print all usable copy of filesystem blocks are found in journal
void print_block_list(int flag){
journal_descriptor_tag_t *pointer;
__u32 counter;
__u32 transaction_time = 0;
pointer = pt_buff;
printf("\nFound %d copy of Filesystemblock in Journal\n",pt_count);
printf("FS-Block Journal Transact %s\n",(flag) ? "Time in sec Time of Transaction" :"" );
for (counter=0 ; counter<pt_count ; counter++) {
if (flag){
if (block_is_inodetable (pointer->f_blocknr)){
transaction_time = get_transaction_time(pointer->j_blocknr);
}
else transaction_time = 0;
}
if (transaction_time)
fprintf(stdout,"%12llu %8lu %8lu %8lu %s",(__u64) pointer->f_blocknr , pointer->j_blocknr ,
pointer->transaction, transaction_time, time_to_string(transaction_time) );
else
fprintf(stdout,"%12llu %8lu %8lu\n",(__u64) pointer->f_blocknr , pointer->j_blocknr ,
pointer->transaction);
pointer++ ;
}
}
//print all transactions for a fs-block
void print_block_transaction(blk64_t block_nr, int flag){
journal_descriptor_tag_t *pointer;
__u32 counter;
int is_inode_block = 0;
__u32 transaction_time = 0;
if (flag) is_inode_block = block_is_inodetable(block_nr);
pointer = pt_buff;
printf("\nTransactions of Filesystemblock %llu in Journal\n",block_nr);
printf("FS-Block Journal Transact %s\n",(is_inode_block) ? "Time in sec Time of Transaction" :"");
for (counter=0 ; counter<pt_count ; counter++) {
if (pointer->f_blocknr == block_nr){
if (is_inode_block){
transaction_time = get_transaction_time(pointer->j_blocknr);
fprintf(stdout,"%12llu %8lu %8lu %8lu %s",(__u64) pointer->f_blocknr , pointer->j_blocknr ,
pointer->transaction, transaction_time, time_to_string(transaction_time) );
}else
fprintf(stdout,"%12llu %8lu %8lu\n",(__u64) pointer->f_blocknr , pointer->j_blocknr , pointer->transaction);
}
pointer++ ;
}
}
//get the journalblocknumber for a transactionnumber
__u32 get_journal_blocknr(blk64_t block_nr, __u32 trans_nr){
__u32 journal_nr = 0;
journal_descriptor_tag_t *pointer;
int i;
pointer = pt_buff;
for (i=0; i<pt_count;i++ , pointer++){
if ( pointer->transaction != trans_nr) continue;
if ( pointer->f_blocknr == block_nr ) journal_nr = pointer->j_blocknr ;
}
return journal_nr;
}
// get the last dir-block for transaction from journal or if not, found the real block
//FIXME: blk64_t ????
int get_last_block(char *buf, blk64_t *block, __u32 t_start, __u32 t_end){
int retval = 0;
int i , count , got;
char *journal_tag_buf = NULL;
journal_descriptor_tag_t *block_list;
blk_t j_block = 0;
int blksize = current_fs->blocksize;
if ((!t_start) && (!t_end)){
//there is no transaction, it is not from a journal Inode
#ifdef DEBUG
printf("DIR_BLOCK (F) %lld\n", (blk64_t)*block);
#endif
return io_channel_read_blk(current_fs->io, *block, 1, buf);
}
count = get_block_list_count((blk64_t)*block) ;
journal_tag_buf = (void *)malloc((sizeof(journal_descriptor_tag_t) * count));
if (!journal_tag_buf) {
// fprintf(stderr,"Error: while allocate Memory for blocklist\n");
retval = BLOCK_ABORT;
goto errout;
}
block_list = (journal_descriptor_tag_t *) journal_tag_buf;
count = get_block_list(block_list, *block, count);
for (i = 0 ; i < count ; i++ , block_list++ ) {
if (block_list->transaction < t_start )
continue;
if (block_list->transaction <= t_end){
j_block = block_list->j_blocknr;
if (i != (count -1))
continue;
}
retval = read_journal_block((j_block) ? (j_block * blksize) : (block_list->j_blocknr * blksize),
buf , current_fs->blocksize , &got);
#ifdef DEBUG
printf("DIR_BLOCK (J) %lld (%ld <-> %ld) JB=%ld : %d\n", (blk64_t)*block,t_start,t_end, j_block, retval );
#endif
if (retval) {
retval = BLOCK_ERROR ;
}
goto errout;
}
// if not in journal found, use real block
#ifdef DEBUG
printf("DIR_BLOCK (N) %lld\n", (blk64_t)*block);
#endif
retval = io_channel_read_blk(current_fs->io, *block, 1, buf);
errout:
if (journal_tag_buf) free(journal_tag_buf);
return retval;
}
//get count of journal blocklist
int get_block_list_count(blk64_t block){
int counter = 0;
int i;
journal_descriptor_tag_t *list_pointer = pt_buff ;
for (i=0;i<pt_count;i++,list_pointer++)
if (list_pointer->f_blocknr == block) counter++ ;
return counter;
}
//local sortfunction for journalblocks
static int sort_block_list(journal_descriptor_tag_t *pointer, int counter ){
journal_descriptor_tag_t p1;
int c1,c2,flag=1;
c1 = 0;
//FIXME : for faster sort
while (flag) {
flag = 0;
for (c2 = 0;c2 < (counter -c1 -1);c2++){
if ((pointer+c2)->transaction > (pointer+c2+1)->transaction ){
p1=*(pointer+c2);
*(pointer+c2)=*(pointer+c2+1);
*(pointer+c2+1)=p1;
flag = 1 ;
}
}
c1++ ;
}
return counter;
}
//get a sorted list of all copys of a filesystemblock
int get_block_list(journal_descriptor_tag_t *pointer, blk64_t block , int counter ){
int i,j = 0;
journal_descriptor_tag_t *list_pointer = pt_buff ;
journal_descriptor_tag_t *fill_pointer = pointer ;
if (counter) {
for (i=0,j=0;(i<pt_count && j<counter);i++,list_pointer++)
if (list_pointer->f_blocknr == block) {
fill_pointer->f_blocknr = block;
fill_pointer->j_blocknr = list_pointer->j_blocknr;
fill_pointer->transaction = list_pointer->transaction;
fill_pointer++ ;
j++;
}
sort_block_list(pointer,counter);
}
return j;
}
//extract the journal in the local intern blocklist
static void extract_descriptor_block(char *buf, journal_superblock_t *jsb,
unsigned int *blockp, int blocksize,
tid_t transaction, unsigned int *wrapflag )
{
int offset, tag_size = JBD_TAG_SIZE32;
char *tagp;
journal_block_tag_t *tag;
unsigned int blocknr;
__u32 tag_block;
__u32 tag_flags;
if (jsb->s_feature_incompat & JFS_FEATURE_INCOMPAT_64BIT)
tag_size = JBD_TAG_SIZE64;
offset = sizeof(journal_header_t);
blocknr = *blockp;
#ifdef DEBUG
fprintf(stdout, "D-Block %u & %u: ", transaction, blocknr);
#endif
++blocknr;
if (blocknr >= jsb->s_maxlen) {
if (*wrapflag == WRAP_ON) {
blocknr -= (jsb->s_maxlen - jsb->s_first);
*wrapflag = WRAP_READY;
}
else {
*blockp = blocknr;
return;
}
}
do {
/* Work out the location of the current tag, and skip to
* the next one... */
tagp = &buf[offset];
tag = (journal_block_tag_t *) tagp;
offset += tag_size;
/* ... and if we have gone too far, then we've reached the
end of this block. */
if (offset > blocksize) break;
tag_block = be32_to_cpu(tag->t_blocknr) ;
tag_flags = be32_to_cpu(tag->t_flags);
if (!(tag_flags & JFS_FLAG_SAME_UUID))
offset += 16;
#ifdef DEBUG
// fprintf(stderr, " FS block %12lu logged at ", tag_block);
// fprintf(stderr, "sequence %u, ", transaction);
// fprintf(stderr, "journal block %u (flags 0x%x)\n", blocknr,tag_flags);
fprintf(stdout,"*");
#endif
pt->f_blocknr = tag_block ;
if (tag_size > JBD_TAG_SIZE32) pt->f_blocknr |= (__u64)be32_to_cpu(tag->t_blocknr_high) << 32;
pt->j_blocknr = blocknr;
pt->transaction = transaction;
pt++;
pt_count++;
++blocknr;
if (blocknr >= jsb->s_maxlen) {
if (*wrapflag == WRAP_ON) {
blocknr -= (jsb->s_maxlen - jsb->s_first);
*wrapflag = WRAP_READY;
}
else {
*blockp = blocknr;
return;
}
}
} while (!(tag_flags & JFS_FLAG_LAST_TAG));
*blockp = (*wrapflag == WRAP_READY) ? jsb->s_maxlen : blocknr ;
}
// init and extract the journal in the local private data
static int init_journal(void)
{
struct ext2_super_block *sb;
char buf[8192];
journal_superblock_t *jsb;
unsigned int blocksize = 1024;
unsigned int got;
int retval;
__u32 magic, sequence, blocktype;
journal_header_t *header;
tid_t transaction;
unsigned int blocknr = 0;
unsigned int maxlen = 0;
unsigned int wrapflag;
/* First, check to see if there's an ext2 superblock header */
retval = read_journal_block( 0, buf, 2048, &got);
if (retval) goto errout;
jsb = (journal_superblock_t *) buf;
sb = (struct ext2_super_block *) (buf+1024);
#ifdef WORDS_BIGENDIAN
if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ext2fs_swap_super(sb);
#endif
if ((be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) &&
(sb->s_magic == EXT2_SUPER_MAGIC) &&
(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
blocksize = EXT2_BLOCK_SIZE(sb);
blocknr = (blocksize == 1024) ? 2 : 1;
uuid_unparse(sb->s_uuid, jsb_buffer);
#ifdef DEBUG
fprintf(stdout, "Ext2 superblock header found.");
fprintf(stdout, "\tuuid=%s", jsb_buffer);
fprintf(stdout, "\tblocksize=%d", blocksize);
fprintf(stdout, "\tjournal data size %lu\n",(unsigned long) sb->s_blocks_count);
#endif
}
/* Next, read the journal superblock */
retval = read_journal_block(blocknr*blocksize,jsb_buffer, 1024, &got);
if (retval) goto errout;
jsb = (journal_superblock_t *) jsb_buffer;
if (be32_to_cpu(jsb->s_header.h_magic) != JFS_MAGIC_NUMBER) {
fprintf(stderr, "Journal superblock magic number invalid!\n");
return JOURNAL_ERROR ;
}
journal_superblock_to_cpu((__u32*)jsb_buffer);
blocksize = jsb->s_blocksize;
transaction = jsb->s_sequence;
maxlen = jsb->s_maxlen;
blocknr = jsb->s_first;
wrapflag = WRAP_UNDEF ;
pt_buff = malloc(maxlen * sizeof(journal_descriptor_tag_t));
pt = pt_buff ;
pt_count = 0;
if (pt_buff == NULL) {
fprintf(stderr,"Error: can't allocate %d Memory\n",maxlen * sizeof(journal_descriptor_tag_t));
goto errout;
}
ptl = (__u32*)(pt_buff + (maxlen * sizeof(journal_descriptor_tag_t)));
ptl_count = 0;
#ifdef DEBUG
fprintf(stdout, "Journal starts at block %u, transaction %u\n", blocknr, transaction);
#endif
while ( blocknr < maxlen ){
retval = read_journal_block(blocknr*blocksize, buf, blocksize, &got);
if (retval || got != blocksize)
//return JOURNAL_OPEN;
goto errout;
header = (journal_header_t *) buf;
magic = be32_to_cpu(header->h_magic);
sequence = be32_to_cpu(header->h_sequence);
blocktype = be32_to_cpu(header->h_blocktype);
if (magic != JFS_MAGIC_NUMBER) {
#ifdef DEBUG
// fprintf (stdout, "No magic number at block %u: skip this block\n", blocknr);
fprintf(stdout,"-");
#endif
if (jb_is_inodetable(buf)){
// printf("lost %d\n", blocknr);
ptl--;
*ptl = blocknr;
ptl_count ++;
}
if ( ! wrapflag ) wrapflag = WRAP_ON ;
blocknr++ ;
continue;
}
#ifdef DEBUG
fprintf (stdout, "\n%u (%s) T=%u JB=%u\n", blocktype, type_to_name(blocktype), sequence, blocknr);
#endif
if ( ! wrapflag ) wrapflag = WRAP_OFF ;
switch (blocktype) {
case JFS_DESCRIPTOR_BLOCK:
extract_descriptor_block(buf, jsb, &blocknr, blocksize, sequence, &wrapflag);
continue;
case JFS_COMMIT_BLOCK:
blocknr++;
continue;
case JFS_REVOKE_BLOCK:
blocknr++;
continue;
default:
fprintf (stderr, "Unexpected block type %u at block %u.\n", blocktype, blocknr);
return JOURNAL_OPEN;
}
}
#ifdef DEBUG
fprintf(stdout,"\nJournal init complete: %ld block copy\n",pt_count);
#endif
return JOURNAL_OPEN ;
errout:
fprintf(stderr,"Error: can't read journal\n");
if ( pt_buff ) {
free(pt_buff);
pt_buff = NULL;
}
return JOURNAL_ERROR ;
}
//get a list of all copy of blockbitmap from journal
int get_block_bitmap_list( journal_bitmap_tag_t **bitmap_list){
int count, sum, i, j;
char* tag_buf = NULL;
journal_bitmap_tag_t *fill_pointer ;
journal_descriptor_tag_t *list_pointer = pt_buff ;
count = sum = 0;
for (i = 0; i < pt_count ; i++, list_pointer++ )
for (j = 0 ; j < current_fs->group_desc_count; j++ )
#ifdef EXT2_FLAG_64BITS
if( list_pointer->f_blocknr == (blk64_t) ext2fs_block_bitmap_loc(current_fs, j))
#else
if (list_pointer->f_blocknr == (blk64_t) current_fs->group_desc[j].bg_block_bitmap)
#endif
count++ ;
if (count && (tag_buf = malloc((sizeof(journal_bitmap_tag_t) * count)))){
fill_pointer = (journal_bitmap_tag_t*)tag_buf ;
list_pointer = pt_buff ;
for ( i = 0 ; (i < pt_count && sum < count); i++ , list_pointer++ )
for ( j = 0; j < current_fs->group_desc_count; j++ )
#ifdef EXT2_FLAG_64BITS
if( list_pointer->f_blocknr == (blk64_t) ext2fs_block_bitmap_loc(current_fs, j)){
#else
if (list_pointer->f_blocknr == (blk64_t) current_fs->group_desc[j].bg_block_bitmap){
#endif
fill_pointer->blockgroup = (blk64_t) j;
fill_pointer->j_blocknr = list_pointer->j_blocknr;
fill_pointer->transaction = list_pointer->transaction;
fill_pointer++ ;
sum++;
}
list_pointer = (journal_descriptor_tag_t*) tag_buf;
sort_block_list( list_pointer,sum );
*bitmap_list = (journal_bitmap_tag_t*) tag_buf;
}
return sum;
}
//create and init the journal block bitmap
//return the count of all blockbitmap copies or 0
int init_block_bitmap_list(ext2fs_block_bitmap *d_bmap, __u32 t_after){
__u32 t;
jbbm.list = NULL;
jbbm.block_buf = NULL;
if (ext2fs_copy_bitmap(current_fs->block_map, d_bmap)){
fprintf(stderr,"Error: while duplicate bitmap\n");
return 0;
}
ext2fs_clear_block_bitmap(*d_bmap);
jbbm.count = get_block_bitmap_list( &jbbm.list);
jbbm.block_buf = malloc(current_fs->blocksize * 2);
if (! jbbm.block_buf){
fprintf(stderr,"Error: can't allocate Memory\n");
return 0;
}
if (jbbm.count){
jbbm.pointer = jbbm.list + jbbm.count - 1;
// jbbm.last_trans = jbbm.pointer->transaction;
jbbm.last_trans = 0;
jbbm.groups = current_fs->group_desc_count;
while ((jbbm.pointer > jbbm.list) && (get_trans_time(jbbm.pointer->transaction) >= t_after)) {
t = jbbm.pointer->transaction;
while (jbbm.pointer->transaction == t)
jbbm.pointer-- ;
}
jbbm.pointer++;
jbbm.first_trans = jbbm.pointer->transaction ;
jbbm.pointer = jbbm.list + jbbm.count - 1;
jbbm.blocksize = current_fs->blocksize;
jbbm.blocklen = current_fs->super->s_blocks_per_group >> 3 ;
jbbm.last_blocklen = (current_fs->super->s_blocks_count >> 3) % jbbm.blocklen;
if (!jbbm.last_blocklen)
jbbm.last_blocklen = jbbm.blocklen;
else
jbbm.last_blocklen += (current_fs->super->s_blocks_count % 8) ? 1 : 0 ;
}
return jbbm.count;
}
//destroy the journal block bitmap
void clear_block_bitmap_list(ext2fs_block_bitmap d_bmap){
if (jbbm.list)
free (jbbm.list);
if (jbbm.block_buf)
free (jbbm.block_buf);
jbbm.list = NULL;
jbbm.block_buf = NULL;
ext2fs_free_block_bitmap(d_bmap);
}
//produces a differential block bitmap for a transaction from the Journal
//return 1 if bitmap is differenzial ; an 2 if bitmap is empty
int next_block_bitmap(ext2fs_block_bitmap d_bmap){
struct ext2fs_struct_loc_generic_bitmap *fs_bitmap, *df_bitmap;
journal_bitmap_tag_t *p1;
journal_bitmap_tag_t *p2;
__u32 blockg, skip,i,len;
int got,retval;
char *diff_buf;
if (jbbm.pointer->transaction < jbbm.first_trans)
return 0;
fs_bitmap = (struct ext2fs_struct_loc_generic_bitmap*) current_fs->block_map;
df_bitmap = (struct ext2fs_struct_loc_generic_bitmap*) d_bmap;
ext2fs_clear_block_bitmap(d_bmap);
if (jbbm.last_trans == 0 ){
// last transaction compared with fs blockbitmap
jbbm.last_trans = jbbm.pointer->transaction;
p2 = jbbm.pointer;
while ((p2 >= jbbm.list) && (p2->transaction == jbbm.pointer->transaction)){
blockg = (__u32)p2->blockgroup;
skip = (jbbm.blocklen * blockg);
len = ((blockg + 1) == jbbm.groups) ? jbbm.last_blocklen : jbbm.blocklen;
retval = read_journal_block((__u32)p2->j_blocknr * jbbm.blocksize, jbbm.block_buf, jbbm.blocksize, &got);
if (retval || got != jbbm.blocksize){
fprintf(stderr,"Error: while reading journal\n");
goto errout;
}
for (i = 0 ; i < len ; i++){
*((df_bitmap->bitmap)+skip+i) = *(jbbm.block_buf + i) ^ *((fs_bitmap->bitmap)+skip+i) ;
}
p2--;
}
}
else{
//all other transactions compared with previous block copy
p1 = jbbm.pointer;
diff_buf = jbbm.block_buf + jbbm.blocksize ;
while ((p1->transaction >= jbbm.first_trans) && (p1->transaction == jbbm.pointer->transaction)){
p2 = p1 -1;
blockg = (__u32)p1->blockgroup;
skip = (jbbm.blocklen * blockg);
len = ((blockg + 1) == jbbm.groups) ? jbbm.last_blocklen : jbbm.blocklen;
while ((p2 > jbbm.list) && ((__u32)p2->blockgroup != blockg))
p2-- ;
retval = read_journal_block((__u32)p1->j_blocknr * jbbm.blocksize, jbbm.block_buf, jbbm.blocksize, &got);
if (retval || got != jbbm.blocksize){
fprintf(stderr,"Error: while reading journal\n");
goto errout;
}
if ((p2 == jbbm.list) && ((__u32)p2->blockgroup != blockg)){
//no previous block copy found, create difference to the entire block group
for (i = 0 ; i < len ; i++){
*((df_bitmap->bitmap)+skip+i) = *(jbbm.block_buf + i) ^ 0xFF ;
}
}
else{
retval = read_journal_block((__u32)p2->j_blocknr * jbbm.blocksize, diff_buf, jbbm.blocksize, &got);
if (retval || got != jbbm.blocksize){
fprintf(stderr,"Error: while reading journal\n");
goto errout;
}
//previous block copy is found, create difference to this copy
for (i = 0 ; i < len ; i++){
*((df_bitmap->bitmap)+skip+i) = *(jbbm.block_buf + i) ^ *(diff_buf + i ) ;
}
}
p1--;
}
jbbm.pointer = p1;
}
i = 0;
len = (fs_bitmap->end +1) >> 3;
while ((i < len) && (!(*(df_bitmap->bitmap +i))))
i++;
return ( i == len) ? 2 : 1 ;
errout:
return 0;
}
//read the next journal-lost inode block
int get_pool_block(unsigned char *buf){
int retval = 0;
int ret = 0;
int got,i ;
int blocksize = current_fs->blocksize;
if (ptl_count){
retval = read_journal_block(*ptl * blocksize ,buf,blocksize,&got);
if ((! retval) && (got == blocksize)){
ret = 1;
}
ptl_count--;
ptl++;
}
return ret;
}
|