forked from gkostka/lwext4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ext4_journal.c
2292 lines (2000 loc) · 60 KB
/
ext4_journal.c
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
/*
* Copyright (c) 2015 Grzegorz Kostka ([email protected])
* Copyright (c) 2015 Kaho Ng ([email protected])
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup lwext4
* @{
*/
/**
* @file ext4_journal.c
* @brief Journal handle functions
*/
#include <ext4_config.h>
#include <ext4_types.h>
#include <ext4_misc.h>
#include <ext4_errno.h>
#include <ext4_debug.h>
#include <ext4_fs.h>
#include <ext4_super.h>
#include <ext4_journal.h>
#include <ext4_blockdev.h>
#include <ext4_crc32.h>
#include <ext4_journal.h>
#include <string.h>
#include <stdlib.h>
/**@brief Revoke entry during journal replay.*/
struct revoke_entry {
/**@brief Block number not to be replayed.*/
ext4_fsblk_t block;
/**@brief For any transaction id smaller
* than trans_id, records of @block
* in those transactions should not
* be replayed.*/
uint32_t trans_id;
/**@brief Revoke tree node.*/
RB_ENTRY(revoke_entry) revoke_node;
};
/**@brief Valid journal replay information.*/
struct recover_info {
/**@brief Starting transaction id.*/
uint32_t start_trans_id;
/**@brief Ending transaction id.*/
uint32_t last_trans_id;
/**@brief Used as internal argument.*/
uint32_t this_trans_id;
/**@brief No of transactions went through.*/
uint32_t trans_cnt;
/**@brief RB-Tree storing revoke entries.*/
RB_HEAD(jbd_revoke, revoke_entry) revoke_root;
};
/**@brief Journal replay internal arguments.*/
struct replay_arg {
/**@brief Journal replay information.*/
struct recover_info *info;
/**@brief Current block we are on.*/
uint32_t *this_block;
/**@brief Current trans_id we are on.*/
uint32_t this_trans_id;
};
/* Make sure we wrap around the log correctly! */
#define wrap(sb, var) \
do { \
if (var >= jbd_get32((sb), maxlen)) \
var -= (jbd_get32((sb), maxlen) - jbd_get32((sb), first)); \
} while (0)
static inline int32_t
trans_id_diff(uint32_t x, uint32_t y)
{
int32_t diff = x - y;
return diff;
}
static int
jbd_revoke_entry_cmp(struct revoke_entry *a, struct revoke_entry *b)
{
if (a->block > b->block)
return 1;
else if (a->block < b->block)
return -1;
return 0;
}
static int
jbd_block_rec_cmp(struct jbd_block_rec *a, struct jbd_block_rec *b)
{
if (a->lba > b->lba)
return 1;
else if (a->lba < b->lba)
return -1;
return 0;
}
static int
jbd_revoke_rec_cmp(struct jbd_revoke_rec *a, struct jbd_revoke_rec *b)
{
if (a->lba > b->lba)
return 1;
else if (a->lba < b->lba)
return -1;
return 0;
}
RB_GENERATE_INTERNAL(jbd_revoke, revoke_entry, revoke_node,
jbd_revoke_entry_cmp, static inline)
RB_GENERATE_INTERNAL(jbd_block, jbd_block_rec, block_rec_node,
jbd_block_rec_cmp, static inline)
RB_GENERATE_INTERNAL(jbd_revoke_tree, jbd_revoke_rec, revoke_node,
jbd_revoke_rec_cmp, static inline)
#define jbd_alloc_revoke_entry() ext4_calloc(1, sizeof(struct revoke_entry))
#define jbd_free_revoke_entry(addr) ext4_free(addr)
static int jbd_has_csum(struct jbd_sb *jbd_sb)
{
if (JBD_HAS_INCOMPAT_FEATURE(jbd_sb, JBD_FEATURE_INCOMPAT_CSUM_V2))
return 2;
if (JBD_HAS_INCOMPAT_FEATURE(jbd_sb, JBD_FEATURE_INCOMPAT_CSUM_V3))
return 3;
return 0;
}
#if CONFIG_META_CSUM_ENABLE
static uint32_t jbd_sb_csum(struct jbd_sb *jbd_sb)
{
uint32_t checksum = 0;
if (jbd_has_csum(jbd_sb)) {
uint32_t orig_checksum = jbd_sb->checksum;
jbd_set32(jbd_sb, checksum, 0);
/* Calculate crc32c checksum against tho whole superblock */
checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_sb,
JBD_SUPERBLOCK_SIZE);
jbd_sb->checksum = orig_checksum;
}
return checksum;
}
#else
#define jbd_sb_csum(...) 0
#endif
static void jbd_sb_csum_set(struct jbd_sb *jbd_sb)
{
if (!jbd_has_csum(jbd_sb))
return;
jbd_set32(jbd_sb, checksum, jbd_sb_csum(jbd_sb));
}
#if CONFIG_META_CSUM_ENABLE
static bool
jbd_verify_sb_csum(struct jbd_sb *jbd_sb)
{
if (!jbd_has_csum(jbd_sb))
return true;
return jbd_sb_csum(jbd_sb) == jbd_get32(jbd_sb, checksum);
}
#else
#define jbd_verify_sb_csum(...) true
#endif
#if CONFIG_META_CSUM_ENABLE
static uint32_t jbd_meta_csum(struct jbd_fs *jbd_fs,
struct jbd_bhdr *bhdr)
{
uint32_t checksum = 0;
if (jbd_has_csum(&jbd_fs->sb)) {
uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
struct jbd_block_tail *tail =
(struct jbd_block_tail *)((char *)bhdr + block_size -
sizeof(struct jbd_block_tail));
uint32_t orig_checksum = tail->checksum;
tail->checksum = 0;
/* First calculate crc32c checksum against fs uuid */
checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
sizeof(jbd_fs->sb.uuid));
/* Calculate crc32c checksum against tho whole block */
checksum = ext4_crc32c(checksum, bhdr,
block_size);
tail->checksum = orig_checksum;
}
return checksum;
}
#else
#define jbd_meta_csum(...) 0
#endif
static void jbd_meta_csum_set(struct jbd_fs *jbd_fs,
struct jbd_bhdr *bhdr)
{
uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
struct jbd_block_tail *tail = (struct jbd_block_tail *)
((char *)bhdr + block_size -
sizeof(struct jbd_block_tail));
if (!jbd_has_csum(&jbd_fs->sb))
return;
tail->checksum = to_be32(jbd_meta_csum(jbd_fs, bhdr));
}
#if CONFIG_META_CSUM_ENABLE
static bool
jbd_verify_meta_csum(struct jbd_fs *jbd_fs,
struct jbd_bhdr *bhdr)
{
uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
struct jbd_block_tail *tail = (struct jbd_block_tail *)
((char *)bhdr + block_size -
sizeof(struct jbd_block_tail));
if (!jbd_has_csum(&jbd_fs->sb))
return true;
return jbd_meta_csum(jbd_fs, bhdr) == to_be32(tail->checksum);
}
#else
#define jbd_verify_meta_csum(...) true
#endif
#if CONFIG_META_CSUM_ENABLE
static uint32_t jbd_commit_csum(struct jbd_fs *jbd_fs,
struct jbd_commit_header *header)
{
uint32_t checksum = 0;
if (jbd_has_csum(&jbd_fs->sb)) {
uint8_t orig_checksum_type = header->chksum_type,
orig_checksum_size = header->chksum_size;
uint32_t orig_checksum = header->chksum[0];
uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
header->chksum_type = 0;
header->chksum_size = 0;
header->chksum[0] = 0;
/* First calculate crc32c checksum against fs uuid */
checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
sizeof(jbd_fs->sb.uuid));
/* Calculate crc32c checksum against tho whole block */
checksum = ext4_crc32c(checksum, header,
block_size);
header->chksum_type = orig_checksum_type;
header->chksum_size = orig_checksum_size;
header->chksum[0] = orig_checksum;
}
return checksum;
}
#else
#define jbd_commit_csum(...) 0
#endif
static void jbd_commit_csum_set(struct jbd_fs *jbd_fs,
struct jbd_commit_header *header)
{
if (!jbd_has_csum(&jbd_fs->sb))
return;
header->chksum_type = 0;
header->chksum_size = 0;
header->chksum[0] = jbd_commit_csum(jbd_fs, header);
}
#if CONFIG_META_CSUM_ENABLE
static bool jbd_verify_commit_csum(struct jbd_fs *jbd_fs,
struct jbd_commit_header *header)
{
if (!jbd_has_csum(&jbd_fs->sb))
return true;
return header->chksum[0] == to_be32(jbd_commit_csum(jbd_fs,
header));
}
#else
#define jbd_verify_commit_csum(...) true
#endif
#if CONFIG_META_CSUM_ENABLE
/*
* NOTE: We only make use of @csum parameter when
* JBD_FEATURE_COMPAT_CHECKSUM is enabled.
*/
static uint32_t jbd_block_csum(struct jbd_fs *jbd_fs, const void *buf,
uint32_t csum,
uint32_t sequence)
{
uint32_t checksum = 0;
if (jbd_has_csum(&jbd_fs->sb)) {
uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
/* First calculate crc32c checksum against fs uuid */
checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
sizeof(jbd_fs->sb.uuid));
/* Then calculate crc32c checksum against sequence no. */
checksum = ext4_crc32c(checksum, &sequence,
sizeof(uint32_t));
/* Calculate crc32c checksum against tho whole block */
checksum = ext4_crc32c(checksum, buf,
block_size);
} else if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_COMPAT_CHECKSUM)) {
uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
/* Calculate crc32c checksum against tho whole block */
checksum = ext4_crc32(csum, buf,
block_size);
}
return checksum;
}
#else
#define jbd_block_csum(...) 0
#endif
static void jbd_block_tag_csum_set(struct jbd_fs *jbd_fs, void *__tag,
uint32_t checksum)
{
int ver = jbd_has_csum(&jbd_fs->sb);
if (!ver)
return;
if (ver == 2) {
struct jbd_block_tag *tag = __tag;
tag->checksum = (uint16_t)to_be32(checksum);
} else {
struct jbd_block_tag3 *tag = __tag;
tag->checksum = to_be32(checksum);
}
}
/**@brief Write jbd superblock to disk.
* @param jbd_fs jbd filesystem
* @param s jbd superblock
* @return standard error code*/
static int jbd_sb_write(struct jbd_fs *jbd_fs, struct jbd_sb *s)
{
int rc;
struct ext4_fs *fs = jbd_fs->inode_ref.fs;
uint64_t offset;
ext4_fsblk_t fblock;
rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
if (rc != EOK)
return rc;
jbd_sb_csum_set(s);
offset = fblock * ext4_sb_get_block_size(&fs->sb);
return ext4_block_writebytes(fs->bdev, offset, s,
EXT4_SUPERBLOCK_SIZE);
}
/**@brief Read jbd superblock from disk.
* @param jbd_fs jbd filesystem
* @param s jbd superblock
* @return standard error code*/
static int jbd_sb_read(struct jbd_fs *jbd_fs, struct jbd_sb *s)
{
int rc;
struct ext4_fs *fs = jbd_fs->inode_ref.fs;
uint64_t offset;
ext4_fsblk_t fblock;
rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
if (rc != EOK)
return rc;
offset = fblock * ext4_sb_get_block_size(&fs->sb);
return ext4_block_readbytes(fs->bdev, offset, s,
EXT4_SUPERBLOCK_SIZE);
}
/**@brief Verify jbd superblock.
* @param sb jbd superblock
* @return true if jbd superblock is valid */
static bool jbd_verify_sb(struct jbd_sb *sb)
{
struct jbd_bhdr *header = &sb->header;
if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER)
return false;
if (jbd_get32(header, blocktype) != JBD_SUPERBLOCK &&
jbd_get32(header, blocktype) != JBD_SUPERBLOCK_V2)
return false;
return jbd_verify_sb_csum(sb);
}
/**@brief Write back dirty jbd superblock to disk.
* @param jbd_fs jbd filesystem
* @return standard error code*/
static int jbd_write_sb(struct jbd_fs *jbd_fs)
{
int rc = EOK;
if (jbd_fs->dirty) {
rc = jbd_sb_write(jbd_fs, &jbd_fs->sb);
if (rc != EOK)
return rc;
jbd_fs->dirty = false;
}
return rc;
}
/**@brief Get reference to jbd filesystem.
* @param fs Filesystem to load journal of
* @param jbd_fs jbd filesystem
* @return standard error code*/
int jbd_get_fs(struct ext4_fs *fs,
struct jbd_fs *jbd_fs)
{
int rc;
uint32_t journal_ino;
memset(jbd_fs, 0, sizeof(struct jbd_fs));
/* See if there is journal inode on this filesystem.*/
/* FIXME: detection on existance ofbkejournal bdev is
* missing.*/
journal_ino = ext4_get32(&fs->sb, journal_inode_number);
rc = ext4_fs_get_inode_ref(fs,
journal_ino,
&jbd_fs->inode_ref);
if (rc != EOK)
return rc;
rc = jbd_sb_read(jbd_fs, &jbd_fs->sb);
if (rc != EOK)
goto Error;
if (!jbd_verify_sb(&jbd_fs->sb)) {
rc = EIO;
goto Error;
}
if (rc == EOK)
jbd_fs->bdev = fs->bdev;
return rc;
Error:
ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
memset(jbd_fs, 0, sizeof(struct jbd_fs));
return rc;
}
/**@brief Put reference of jbd filesystem.
* @param jbd_fs jbd filesystem
* @return standard error code*/
int jbd_put_fs(struct jbd_fs *jbd_fs)
{
int rc = EOK;
rc = jbd_write_sb(jbd_fs);
ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
return rc;
}
/**@brief Data block lookup helper.
* @param jbd_fs jbd filesystem
* @param iblock block index
* @param fblock logical block address
* @return standard error code*/
int jbd_inode_bmap(struct jbd_fs *jbd_fs,
ext4_lblk_t iblock,
ext4_fsblk_t *fblock)
{
int rc = ext4_fs_get_inode_dblk_idx(
&jbd_fs->inode_ref,
iblock,
fblock,
false);
return rc;
}
/**@brief jbd block get function (through cache).
* @param jbd_fs jbd filesystem
* @param block block descriptor
* @param fblock jbd logical block address
* @return standard error code*/
static int jbd_block_get(struct jbd_fs *jbd_fs,
struct ext4_block *block,
ext4_fsblk_t fblock)
{
/* TODO: journal device. */
int rc;
struct ext4_blockdev *bdev = jbd_fs->bdev;
ext4_lblk_t iblock = (ext4_lblk_t)fblock;
/* Lookup the logical block address of
* fblock.*/
rc = jbd_inode_bmap(jbd_fs, iblock,
&fblock);
if (rc != EOK)
return rc;
rc = ext4_block_get(bdev, block, fblock);
/* If succeeded, mark buffer as BC_FLUSH to indicate
* that data should be written to disk immediately.*/
if (rc == EOK) {
ext4_bcache_set_flag(block->buf, BC_FLUSH);
/* As we don't want to occupy too much space
* in block cache, we set this buffer BC_TMP.*/
ext4_bcache_set_flag(block->buf, BC_TMP);
}
return rc;
}
/**@brief jbd block get function (through cache, don't read).
* @param jbd_fs jbd filesystem
* @param block block descriptor
* @param fblock jbd logical block address
* @return standard error code*/
static int jbd_block_get_noread(struct jbd_fs *jbd_fs,
struct ext4_block *block,
ext4_fsblk_t fblock)
{
/* TODO: journal device. */
int rc;
struct ext4_blockdev *bdev = jbd_fs->bdev;
ext4_lblk_t iblock = (ext4_lblk_t)fblock;
rc = jbd_inode_bmap(jbd_fs, iblock,
&fblock);
if (rc != EOK)
return rc;
rc = ext4_block_get_noread(bdev, block, fblock);
if (rc == EOK)
ext4_bcache_set_flag(block->buf, BC_FLUSH);
return rc;
}
/**@brief jbd block set procedure (through cache).
* @param jbd_fs jbd filesystem
* @param block block descriptor
* @return standard error code*/
static int jbd_block_set(struct jbd_fs *jbd_fs,
struct ext4_block *block)
{
struct ext4_blockdev *bdev = jbd_fs->bdev;
return ext4_block_set(bdev, block);
}
/**@brief helper functions to calculate
* block tag size, not including UUID part.
* @param jbd_fs jbd filesystem
* @return tag size in bytes*/
static int jbd_tag_bytes(struct jbd_fs *jbd_fs)
{
int size;
/* It is very easy to deal with the case which
* JBD_FEATURE_INCOMPAT_CSUM_V3 is enabled.*/
if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_INCOMPAT_CSUM_V3))
return sizeof(struct jbd_block_tag3);
size = sizeof(struct jbd_block_tag);
/* If JBD_FEATURE_INCOMPAT_CSUM_V2 is enabled,
* add 2 bytes to size.*/
if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_INCOMPAT_CSUM_V2))
size += sizeof(uint16_t);
if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_INCOMPAT_64BIT))
return size;
/* If block number is 4 bytes in size,
* minus 4 bytes from size */
return size - sizeof(uint32_t);
}
/**@brief Tag information. */
struct tag_info {
/**@brief Tag size in bytes, including UUID part.*/
int tag_bytes;
/**@brief block number stored in this tag.*/
ext4_fsblk_t block;
/**@brief Is the first 4 bytes of block equals to
* JBD_MAGIC_NUMBER? */
bool is_escape;
/**@brief whether UUID part exists or not.*/
bool uuid_exist;
/**@brief UUID content if UUID part exists.*/
uint8_t uuid[UUID_SIZE];
/**@brief Is this the last tag? */
bool last_tag;
/**@brief crc32c checksum. */
uint32_t checksum;
};
/**@brief Extract information from a block tag.
* @param __tag pointer to the block tag
* @param tag_bytes block tag size of this jbd filesystem
* @param remaining size in buffer containing the block tag
* @param tag_info information of this tag.
* @return EOK when succeed, otherwise return EINVAL.*/
static int
jbd_extract_block_tag(struct jbd_fs *jbd_fs,
void *__tag,
int tag_bytes,
int32_t remain_buf_size,
struct tag_info *tag_info)
{
char *uuid_start;
tag_info->tag_bytes = tag_bytes;
tag_info->uuid_exist = false;
tag_info->last_tag = false;
tag_info->is_escape = false;
/* See whether it is possible to hold a valid block tag.*/
if (remain_buf_size - tag_bytes < 0)
return EINVAL;
if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_INCOMPAT_CSUM_V3)) {
struct jbd_block_tag3 *tag = __tag;
tag_info->block = jbd_get32(tag, blocknr);
if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_INCOMPAT_64BIT))
tag_info->block |=
(uint64_t)jbd_get32(tag, blocknr_high) << 32;
if (jbd_get32(tag, flags) & JBD_FLAG_ESCAPE)
tag_info->is_escape = true;
if (!(jbd_get32(tag, flags) & JBD_FLAG_SAME_UUID)) {
/* See whether it is possible to hold UUID part.*/
if (remain_buf_size - tag_bytes < UUID_SIZE)
return EINVAL;
uuid_start = (char *)tag + tag_bytes;
tag_info->uuid_exist = true;
tag_info->tag_bytes += UUID_SIZE;
memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
}
if (jbd_get32(tag, flags) & JBD_FLAG_LAST_TAG)
tag_info->last_tag = true;
} else {
struct jbd_block_tag *tag = __tag;
tag_info->block = jbd_get32(tag, blocknr);
if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_INCOMPAT_64BIT))
tag_info->block |=
(uint64_t)jbd_get32(tag, blocknr_high) << 32;
if (jbd_get16(tag, flags) & JBD_FLAG_ESCAPE)
tag_info->is_escape = true;
if (!(jbd_get16(tag, flags) & JBD_FLAG_SAME_UUID)) {
/* See whether it is possible to hold UUID part.*/
if (remain_buf_size - tag_bytes < UUID_SIZE)
return EINVAL;
uuid_start = (char *)tag + tag_bytes;
tag_info->uuid_exist = true;
tag_info->tag_bytes += UUID_SIZE;
memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
}
if (jbd_get16(tag, flags) & JBD_FLAG_LAST_TAG)
tag_info->last_tag = true;
}
return EOK;
}
/**@brief Write information to a block tag.
* @param __tag pointer to the block tag
* @param remaining size in buffer containing the block tag
* @param tag_info information of this tag.
* @return EOK when succeed, otherwise return EINVAL.*/
static int
jbd_write_block_tag(struct jbd_fs *jbd_fs,
void *__tag,
int32_t remain_buf_size,
struct tag_info *tag_info)
{
char *uuid_start;
int tag_bytes = jbd_tag_bytes(jbd_fs);
tag_info->tag_bytes = tag_bytes;
/* See whether it is possible to hold a valid block tag.*/
if (remain_buf_size - tag_bytes < 0)
return EINVAL;
if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_INCOMPAT_CSUM_V3)) {
struct jbd_block_tag3 *tag = __tag;
memset(tag, 0, sizeof(struct jbd_block_tag3));
jbd_set32(tag, blocknr, (uint32_t)tag_info->block);
if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_INCOMPAT_64BIT))
jbd_set32(tag, blocknr_high, tag_info->block >> 32);
if (tag_info->uuid_exist) {
/* See whether it is possible to hold UUID part.*/
if (remain_buf_size - tag_bytes < UUID_SIZE)
return EINVAL;
uuid_start = (char *)tag + tag_bytes;
tag_info->tag_bytes += UUID_SIZE;
memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
} else
jbd_set32(tag, flags,
jbd_get32(tag, flags) | JBD_FLAG_SAME_UUID);
jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum);
if (tag_info->last_tag)
jbd_set32(tag, flags,
jbd_get32(tag, flags) | JBD_FLAG_LAST_TAG);
if (tag_info->is_escape)
jbd_set32(tag, flags,
jbd_get32(tag, flags) | JBD_FLAG_ESCAPE);
} else {
struct jbd_block_tag *tag = __tag;
memset(tag, 0, sizeof(struct jbd_block_tag));
jbd_set32(tag, blocknr, (uint32_t)tag_info->block);
if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_INCOMPAT_64BIT))
jbd_set32(tag, blocknr_high, tag_info->block >> 32);
if (tag_info->uuid_exist) {
/* See whether it is possible to hold UUID part.*/
if (remain_buf_size - tag_bytes < UUID_SIZE)
return EINVAL;
uuid_start = (char *)tag + tag_bytes;
tag_info->tag_bytes += UUID_SIZE;
memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
} else
jbd_set16(tag, flags,
jbd_get16(tag, flags) | JBD_FLAG_SAME_UUID);
jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum);
if (tag_info->last_tag)
jbd_set16(tag, flags,
jbd_get16(tag, flags) | JBD_FLAG_LAST_TAG);
if (tag_info->is_escape)
jbd_set16(tag, flags,
jbd_get16(tag, flags) | JBD_FLAG_ESCAPE);
}
return EOK;
}
/**@brief Iterate all block tags in a block.
* @param jbd_fs jbd filesystem
* @param __tag_start pointer to the block
* @param tag_tbl_size size of the block
* @param func callback routine to indicate that
* a block tag is found
* @param arg additional argument to be passed to func */
static void
jbd_iterate_block_table(struct jbd_fs *jbd_fs,
void *__tag_start,
int32_t tag_tbl_size,
void (*func)(struct jbd_fs * jbd_fs,
struct tag_info *tag_info,
void *arg),
void *arg)
{
char *tag_start, *tag_ptr;
int tag_bytes = jbd_tag_bytes(jbd_fs);
tag_start = __tag_start;
tag_ptr = tag_start;
/* Cut off the size of block tail storing checksum. */
if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_INCOMPAT_CSUM_V2) ||
JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
JBD_FEATURE_INCOMPAT_CSUM_V3))
tag_tbl_size -= sizeof(struct jbd_block_tail);
while (tag_tbl_size) {
struct tag_info tag_info;
int rc = jbd_extract_block_tag(jbd_fs,
tag_ptr,
tag_bytes,
tag_tbl_size,
&tag_info);
if (rc != EOK)
break;
if (func)
func(jbd_fs, &tag_info, arg);
/* Stop the iteration when we reach the last tag. */
if (tag_info.last_tag)
break;
tag_ptr += tag_info.tag_bytes;
tag_tbl_size -= tag_info.tag_bytes;
}
}
static void jbd_display_block_tags(struct jbd_fs *jbd_fs,
struct tag_info *tag_info,
void *arg)
{
uint32_t *iblock = arg;
ext4_dbg(DEBUG_JBD, "Block in block_tag: %" PRIu64 "\n", tag_info->block);
(*iblock)++;
wrap(&jbd_fs->sb, *iblock);
(void)jbd_fs;
return;
}
static struct revoke_entry *
jbd_revoke_entry_lookup(struct recover_info *info, ext4_fsblk_t block)
{
struct revoke_entry tmp = {
.block = block
};
return RB_FIND(jbd_revoke, &info->revoke_root, &tmp);
}
/**@brief Replay a block in a transaction.
* @param jbd_fs jbd filesystem
* @param tag_info tag_info of the logged block.*/
static void jbd_replay_block_tags(struct jbd_fs *jbd_fs,
struct tag_info *tag_info,
void *__arg)
{
int r;
struct replay_arg *arg = __arg;
struct recover_info *info = arg->info;
uint32_t *this_block = arg->this_block;
struct revoke_entry *revoke_entry;
struct ext4_block journal_block, ext4_block;
struct ext4_fs *fs = jbd_fs->inode_ref.fs;
(*this_block)++;
wrap(&jbd_fs->sb, *this_block);
/* We replay this block only if the current transaction id
* is equal or greater than that in revoke entry.*/
revoke_entry = jbd_revoke_entry_lookup(info, tag_info->block);
if (revoke_entry &&
trans_id_diff(arg->this_trans_id, revoke_entry->trans_id) <= 0)
return;
ext4_dbg(DEBUG_JBD,
"Replaying block in block_tag: %" PRIu64 "\n",
tag_info->block);
r = jbd_block_get(jbd_fs, &journal_block, *this_block);
if (r != EOK)
return;
/* We need special treatment for ext4 superblock. */
if (tag_info->block) {
r = ext4_block_get_noread(fs->bdev, &ext4_block, tag_info->block);
if (r != EOK) {
jbd_block_set(jbd_fs, &journal_block);
return;
}
memcpy(ext4_block.data,
journal_block.data,
jbd_get32(&jbd_fs->sb, blocksize));
if (tag_info->is_escape)
((struct jbd_bhdr *)ext4_block.data)->magic =
to_be32(JBD_MAGIC_NUMBER);
ext4_bcache_set_dirty(ext4_block.buf);
ext4_block_set(fs->bdev, &ext4_block);
} else {
uint16_t mount_count, state;
mount_count = ext4_get16(&fs->sb, mount_count);
state = ext4_get16(&fs->sb, state);
memcpy(&fs->sb,
journal_block.data + EXT4_SUPERBLOCK_OFFSET,
EXT4_SUPERBLOCK_SIZE);
/* Mark system as mounted */
ext4_set16(&fs->sb, state, state);
r = ext4_sb_write(fs->bdev, &fs->sb);
if (r != EOK)
return;
/*Update mount count*/
ext4_set16(&fs->sb, mount_count, mount_count);
}
jbd_block_set(jbd_fs, &journal_block);
return;
}
/**@brief Add block address to revoke tree, along with
* its transaction id.
* @param info journal replay info
* @param block block address to be replayed.*/
static void jbd_add_revoke_block_tags(struct recover_info *info,
ext4_fsblk_t block)
{
struct revoke_entry *revoke_entry;
ext4_dbg(DEBUG_JBD, "Add block %" PRIu64 " to revoke tree\n", block);
/* If the revoke entry with respect to the block address
* exists already, update its transaction id.*/
revoke_entry = jbd_revoke_entry_lookup(info, block);
if (revoke_entry) {
revoke_entry->trans_id = info->this_trans_id;
return;
}
revoke_entry = jbd_alloc_revoke_entry();
ext4_assert(revoke_entry);
revoke_entry->block = block;
revoke_entry->trans_id = info->this_trans_id;
RB_INSERT(jbd_revoke, &info->revoke_root, revoke_entry);
return;
}
static void jbd_destroy_revoke_tree(struct recover_info *info)
{
while (!RB_EMPTY(&info->revoke_root)) {
struct revoke_entry *revoke_entry =
RB_MIN(jbd_revoke, &info->revoke_root);
ext4_assert(revoke_entry);
RB_REMOVE(jbd_revoke, &info->revoke_root, revoke_entry);
jbd_free_revoke_entry(revoke_entry);
}
}
#define ACTION_SCAN 0
#define ACTION_REVOKE 1
#define ACTION_RECOVER 2
/**@brief Add entries in a revoke block to revoke tree.
* @param jbd_fs jbd filesystem
* @param header revoke block header
* @param recover_info journal replay info*/
static void jbd_build_revoke_tree(struct jbd_fs *jbd_fs,
struct jbd_bhdr *header,
struct recover_info *info)