6 #include "ext4_config.h"
7 #include "ext4_types.h"
9 #include "ext4_super.h"
10 #include "ext4_errno.h"
11 #include "ext4_blockdev.h"
12 #include "ext4_crc32c.h"
13 #include "ext4_debug.h"
22 RB_ENTRY(revoke_entry) revoke_node;
26 uint32_t start_trans_id;
27 uint32_t last_trans_id;
28 uint32_t this_trans_id;
29 RB_HEAD(jbd_revoke, revoke_entry) revoke_root;
33 jbd_revoke_entry_cmp(struct revoke_entry *a, struct revoke_entry *b)
35 if (a->block > b->block)
37 else if (a->block < b->block)
42 RB_GENERATE_INTERNAL(jbd_revoke, revoke_entry, revoke_node,
43 jbd_revoke_entry_cmp, static inline)
45 #define jbd_alloc_revoke_entry() calloc(1, sizeof(struct revoke_entry))
46 #define jbd_free_revoke_entry(addr) free(addr)
48 int jbd_inode_bmap(struct jbd_fs *jbd_fs,
50 ext4_fsblk_t *fblock);
52 int jbd_sb_write(struct jbd_fs *jbd_fs, struct jbd_sb *s)
55 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
58 rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
62 offset = fblock * ext4_sb_get_block_size(&fs->sb);
63 return ext4_block_writebytes(fs->bdev, offset, s,
64 EXT4_SUPERBLOCK_SIZE);
67 int jbd_sb_read(struct jbd_fs *jbd_fs, struct jbd_sb *s)
70 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
73 rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
77 offset = fblock * ext4_sb_get_block_size(&fs->sb);
78 return ext4_block_readbytes(fs->bdev, offset, s,
79 EXT4_SUPERBLOCK_SIZE);
82 static bool jbd_verify_sb(struct jbd_sb *sb)
84 struct jbd_bhdr *header = &sb->header;
85 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER)
88 if (jbd_get32(header, blocktype) != JBD_SUPERBLOCK &&
89 jbd_get32(header, blocktype) != JBD_SUPERBLOCK_V2)
95 int jbd_get_fs(struct ext4_fs *fs,
96 struct jbd_fs *jbd_fs)
101 memset(jbd_fs, 0, sizeof(struct jbd_fs));
102 journal_ino = ext4_get32(&fs->sb, journal_inode_number);
104 rc = ext4_fs_get_inode_ref(fs,
108 memset(jbd_fs, 0, sizeof(struct jbd_fs));
111 rc = jbd_sb_read(jbd_fs, &jbd_fs->sb);
113 memset(jbd_fs, 0, sizeof(struct jbd_fs));
114 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
120 int jbd_put_fs(struct jbd_fs *jbd_fs)
123 rc = ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
127 int jbd_inode_bmap(struct jbd_fs *jbd_fs,
129 ext4_fsblk_t *fblock)
131 int rc = ext4_fs_get_inode_data_block_index(
139 int jbd_block_get(struct jbd_fs *jbd_fs,
140 struct ext4_block *block,
143 /* TODO: journal device. */
145 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
146 rc = jbd_inode_bmap(jbd_fs, iblock,
151 struct ext4_blockdev *bdev = jbd_fs->inode_ref.fs->bdev;
152 rc = ext4_block_get(bdev, block, fblock);
156 int jbd_block_get_noread(struct jbd_fs *jbd_fs,
157 struct ext4_block *block,
160 /* TODO: journal device. */
162 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
163 rc = jbd_inode_bmap(jbd_fs, iblock,
168 struct ext4_blockdev *bdev = jbd_fs->inode_ref.fs->bdev;
169 rc = ext4_block_get_noread(bdev, block, fblock);
173 int jbd_block_set(struct jbd_fs *jbd_fs,
174 struct ext4_block *block)
176 return ext4_block_set(jbd_fs->inode_ref.fs->bdev,
181 * helper functions to deal with 32 or 64bit block numbers.
183 int jbd_tag_bytes(struct jbd_fs *jbd_fs)
187 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
188 JBD_FEATURE_INCOMPAT_CSUM_V3))
189 return sizeof(struct jbd_block_tag3);
191 size = sizeof(struct jbd_block_tag);
193 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
194 JBD_FEATURE_INCOMPAT_CSUM_V2))
195 size += sizeof(uint16_t);
197 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
198 JBD_FEATURE_INCOMPAT_64BIT))
201 return size - sizeof(uint32_t);
205 jbd_extract_block_tag(struct jbd_fs *jbd_fs,
216 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
217 JBD_FEATURE_INCOMPAT_CSUM_V3)) {
218 struct jbd_block_tag3 *tag = __tag;
219 *block = jbd_get32(tag, blocknr);
220 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
221 JBD_FEATURE_INCOMPAT_64BIT))
222 *block |= (uint64_t)jbd_get32(tag, blocknr_high) << 32;
224 if (jbd_get32(tag, flags) & JBD_FLAG_ESCAPE)
227 if (!(jbd_get32(tag, flags) & JBD_FLAG_SAME_UUID)) {
228 uuid_start = (char *)tag + tag_bytes;
230 memcpy(uuid, uuid_start, UUID_SIZE);
233 if (jbd_get32(tag, flags) & JBD_FLAG_LAST_TAG)
237 struct jbd_block_tag *tag = __tag;
238 *block = jbd_get32(tag, blocknr);
239 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
240 JBD_FEATURE_INCOMPAT_64BIT))
241 *block |= (uint64_t)jbd_get32(tag, blocknr_high) << 32;
243 if (jbd_get16(tag, flags) & JBD_FLAG_ESCAPE)
246 if (!(jbd_get16(tag, flags) & JBD_FLAG_SAME_UUID)) {
247 uuid_start = (char *)tag + tag_bytes;
249 memcpy(uuid, uuid_start, UUID_SIZE);
252 if (jbd_get16(tag, flags) & JBD_FLAG_LAST_TAG)
259 jbd_iterate_block_table(struct jbd_fs *jbd_fs,
261 uint32_t tag_tbl_size,
262 void (*func)(struct jbd_fs * jbd_fs,
268 ext4_fsblk_t block = 0;
269 uint8_t uuid[UUID_SIZE];
270 char *tag_start, *tag_ptr;
271 uint32_t tag_bytes = jbd_tag_bytes(jbd_fs);
272 tag_start = __tag_start;
275 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
276 JBD_FEATURE_INCOMPAT_CSUM_V2) ||
277 JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
278 JBD_FEATURE_INCOMPAT_CSUM_V3))
279 tag_tbl_size -= sizeof(struct jbd_block_tail);
281 while (tag_ptr - tag_start + tag_bytes <= tag_tbl_size) {
284 jbd_extract_block_tag(jbd_fs,
292 func(jbd_fs, block, uuid, arg);
297 tag_ptr += tag_bytes;
299 tag_ptr += UUID_SIZE;
304 static void jbd_display_block_tags(struct jbd_fs *jbd_fs,
309 uint32_t *iblock = arg;
310 ext4_dbg(DEBUG_JBD, "Block in block_tag: %" PRIu64 "\n", block);
317 static struct revoke_entry *
318 jbd_revoke_entry_lookup(struct recover_info *info, ext4_fsblk_t block)
320 struct revoke_entry tmp = {
324 return RB_FIND(jbd_revoke, &info->revoke_root, &tmp);
327 static void jbd_add_revoke_block_tags(struct jbd_fs *jbd_fs __unused,
329 uint8_t *uuid __unused,
332 struct recover_info *info = arg;
333 struct revoke_entry *revoke_entry;
335 ext4_dbg(DEBUG_JBD, "Add block %" PRIu64 " to revoke tree\n", block);
336 revoke_entry = jbd_revoke_entry_lookup(info, block);
338 revoke_entry->trans_id = info->this_trans_id;
342 revoke_entry = jbd_alloc_revoke_entry();
343 ext4_assert(revoke_entry);
344 revoke_entry->block = block;
345 revoke_entry->trans_id = info->this_trans_id;
346 RB_INSERT(jbd_revoke, &info->revoke_root, revoke_entry);
351 static void jbd_destroy_revoke_tree(struct recover_info *info)
353 while (!RB_EMPTY(&info->revoke_root)) {
354 struct revoke_entry *revoke_entry =
355 RB_MIN(jbd_revoke, &info->revoke_root);
356 ext4_assert(revoke_entry);
357 RB_REMOVE(jbd_revoke, &info->revoke_root, revoke_entry);
358 jbd_free_revoke_entry(revoke_entry);
362 /* Make sure we wrap around the log correctly! */
363 #define wrap(sb, var) \
365 if (var >= jbd_get32((sb), maxlen)) \
366 var -= (jbd_get32((sb), maxlen) - jbd_get32((sb), first)); \
369 #define ACTION_SCAN 0
370 #define ACTION_REVOKE 1
371 #define ACTION_RECOVER 2
374 static void jbd_build_revoke_root(struct jbd_fs *jbd_fs,
375 struct jbd_bhdr *header,
376 struct recover_info *info)
378 struct jbd_revoke_header *revoke_hdr =
379 (struct jbd_revoke_header *)header;
381 jbd_iterate_block_table(jbd_fs,
383 jbd_get32(&jbd_fs->sb, blocksize) -
384 sizeof(struct jbd_revoke_header),
385 jbd_add_revoke_block_tags,
389 static void jbd_debug_descriptor_block(struct jbd_fs *jbd_fs,
390 struct jbd_bhdr *header,
393 jbd_iterate_block_table(jbd_fs,
395 jbd_get32(&jbd_fs->sb, blocksize) -
396 sizeof(struct jbd_bhdr),
397 jbd_display_block_tags,
401 int jbd_iterate_log(struct jbd_fs *jbd_fs,
402 struct recover_info *info,
406 bool log_end = false;
407 struct jbd_sb *sb = &jbd_fs->sb;
408 uint32_t start_trans_id, this_trans_id;
409 uint32_t start_block, this_block;
411 start_trans_id = this_trans_id = jbd_get32(sb, sequence);
412 start_block = this_block = jbd_get32(sb, start);
414 ext4_dbg(DEBUG_JBD, "Start of journal at trans id: %" PRIu32 "\n",
418 struct ext4_block block;
419 struct jbd_bhdr *header;
420 if (action != ACTION_SCAN)
421 if (this_trans_id > info->last_trans_id) {
426 r = jbd_block_get(jbd_fs, &block, this_block);
430 header = (struct jbd_bhdr *)block.data;
431 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER) {
432 jbd_block_set(jbd_fs, &block);
437 if (jbd_get32(header, sequence) != this_trans_id) {
438 if (action != ACTION_SCAN)
441 jbd_block_set(jbd_fs, &block);
446 switch (jbd_get32(header, blocktype)) {
447 case JBD_DESCRIPTOR_BLOCK:
448 ext4_dbg(DEBUG_JBD, "Descriptor block: %u, "
450 this_block, this_trans_id);
451 if (action == ACTION_SCAN)
452 jbd_debug_descriptor_block(jbd_fs,
453 header, &this_block);
455 case JBD_COMMIT_BLOCK:
456 ext4_dbg(DEBUG_JBD, "Commit block: %u, "
458 this_block, this_trans_id);
461 case JBD_REVOKE_BLOCK:
462 ext4_dbg(DEBUG_JBD, "Revoke block: %u, "
464 this_block, this_trans_id);
465 if (action == ACTION_REVOKE) {
466 info->this_trans_id = this_trans_id;
467 jbd_build_revoke_root(jbd_fs, header, info);
474 jbd_block_set(jbd_fs, &block);
476 wrap(sb, this_block);
477 if (this_block == start_block)
481 ext4_dbg(DEBUG_JBD, "End of journal.\n");
482 if (r == EOK && action == ACTION_SCAN) {
483 info->start_trans_id = start_trans_id;
484 if (this_trans_id > start_trans_id)
485 info->last_trans_id = this_trans_id - 1;
487 info->last_trans_id = this_trans_id;
493 int jbd_recover(struct jbd_fs *jbd_fs)
496 struct recover_info info;
497 struct jbd_sb *sb = &jbd_fs->sb;
501 RB_INIT(&info.revoke_root);
503 r = jbd_iterate_log(jbd_fs, &info, ACTION_SCAN);
507 r = jbd_iterate_log(jbd_fs, &info, ACTION_REVOKE);
508 jbd_destroy_revoke_tree(&info);