hfsplus: implement get and verify block list header functionality
Subsystems (2)
Vyacheslav Dubeyko
maintainerPATCH v4 9/15 hfsplus: implement get and verify block list header functionality
From: Vyacheslav Dubeyko <[email protected]>
Subject: [PATCH v4 09/15] hfsplus: implement get and verify block list header functionality
This patch implements functionality of reading of block list's header from journal log and veryfing of it.
Signed-off-by Vyacheslav Dubeyko <[email protected]>
CC Al Viro <[email protected]>
CC Christoph Hellwig <[email protected]>
Tested-by Hin-Tak Leung <[email protected]>
--- fs/hfsplus/journal.c | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+)
fs/hfsplus/journal.c +143 -0
@@ -58,6 +58,16 @@ (be64_to_cpu (HFSPLUS_JIB (jnl)->offset) >> \ HFSPLUS_SB(sb)->alloc_blksz_shift) +static inline sector_t JOURNAL_OFF_TO_SEC (struct super_block *sb) +{ +struct hfsplus_journal *jnl =HFSPLUS_JNL (sb); +sector_t blk_off =HFSPLUS_SB (sb)->blockoffset; +u64 jh_off =be64_to_cpu (HFSPLUS_JIB (jnl)->offset); +u64 start =le64_to_cpu (JNL_SWAP64 (jnl,HFSPLUS_JH (jnl)->start)); + +return blk_off + ((jh_off + start) >>HFSPLUS_SECTOR_SHIFT ); +} +#define BLOCK_TO_SEC (sb, blk) \ ((sector_t)(blk) << \ (HFSPLUS_SB(sb)->alloc_blksz_shift - HFSPLUS_SECTOR_SHIFT))
@@ -69,6 +79,23 @@#define JHDR_SIZE (jnl) \ (le32_to_cpu(JNL_SWAP32(jnl, HFSPLUS_JH(jnl)->jhdr_size))) +#define TR_MAX_BLOCKS (jnl, blhdr) \ + (le16_to_cpu(JNL_SWAP16(jnl, HFSPLUS_BLHDR_PTR(blhdr)->max_blocks))) +#define TR_BLOCKS (jnl, blhdr) \ + (le16_to_cpu(JNL_SWAP16(jnl, HFSPLUS_BLHDR_PTR(blhdr)->num_blocks))) +#define TR_BYTES (jnl, blhdr) \ + (le32_to_cpu(JNL_SWAP32(jnl, HFSPLUS_BLHDR_PTR(blhdr)->bytes_used))) + +static inline u32 TR_SEQ_NUM (struct hfsplus_journal *jnl, +struct hfsplus_blhdr *blhdr) +{ +return le32_to_cpu (JNL_SWAP32 (jnl, blhdr->binfo[0 ].block.seq_num)); +} + +#define NEED_CHECK_CSUM (jnl, blhdr) \ + (le32_to_cpu(JNL_SWAP32(jnl, HFSPLUS_BLHDR_PTR(blhdr)->flags)) & \ + HFSPLUS_BLHDR_CHECK_CHECKSUMS) +/* * struct hfsplus_blist_desc - descriptor of block list buffer * @start_sec: start sector of block list
@@ -349,15 +376,110 @@ failed_init:return err; } +static struct hfsplus_blhdr *hfsplus_get_blhdr (struct super_block *sb, +sector_t start_sector, +struct hfsplus_blist_desc *desc) +{ +struct hfsplus_blhdr *blhdr; +int err; + +hfs_dbg (JREPLAY ,"get block list header: start_sector %llu\n" , + (unsigned long long )start_sector); + +BUG_ON (!desc->blist_buf); + +if (desc->start_sec == start_sector && desc->cur_sec == start_sector) +return &desc->blhdr; + + err =hfsplus_submit_bio (sb, start_sector, + desc->blist_buf, (void **)&blhdr,READA , + &desc->available_bytes); +if (err) { +pr_err ("unable to read block list header\n" ); +return NULL ; + } + + desc->start_sec = start_sector; + desc->cur_sec = start_sector; +memcpy ((void *)&desc->blhdr, (const void *)blhdr,sizeof (*blhdr)); + desc->binfo = &blhdr->binfo[0 ]; + +return &desc->blhdr; +} +static inline bool hfsplus_journal_empty (struct hfsplus_journal *jnl) {return TR_START (jnl) ==LAST_TR_END (jnl); } +static inline +bool hfsplus_transaction_seq_num_valid (struct hfsplus_journal *jnl, +u32 prev_seq_num, +struct hfsplus_blhdr *blhdr) +{ +if (prev_seq_num !=0 ) { +u32 seq =TR_SEQ_NUM (jnl, blhdr); + +if ((seq !=0 ) && (seq != prev_seq_num) && + (seq != (prev_seq_num +1 ))) { +pr_err ("transaction seq number %u is invalid\n" , +TR_SEQ_NUM (jnl, blhdr)); +return false; + } + } + +return true; +} + +static int hfsplus_verify_blhdr (struct hfsplus_journal *jnl, +u32 prev_seq_num, +struct hfsplus_blhdr *blhdr) +{ +__le32 blhdr_checksum, cksum; + +if (!hfsplus_transaction_seq_num_valid (jnl, prev_seq_num, blhdr)) +return -EIO ; + +if (TR_BLOCKS (jnl, blhdr) ==0 ||TR_BYTES (jnl, blhdr) ==0 ) { +pr_err ("corrupted block list header\n" ); +hfs_dbg (JOURNAL ,"num_blocks %u, max_blocks %u\n" , +TR_BLOCKS (jnl, blhdr),TR_BYTES (jnl, blhdr)); +return -EIO ; + } + +if (TR_BLOCKS (jnl, blhdr) >TR_MAX_BLOCKS (jnl, blhdr)) { +pr_err ("corrupted block list header\n" ); +hfs_dbg (JOURNAL ,"num_blocks %u, max_blocks %u\n" , +TR_BLOCKS (jnl, blhdr),TR_MAX_BLOCKS (jnl, blhdr)); +return -EIO ; + } + +if (!NEED_CHECK_CSUM (jnl, blhdr)) +return 0 ;/* don't check checksums */ + + blhdr_checksum =JNL_SWAP32 (jnl, blhdr->checksum); + + blhdr->checksum =0 ; + cksum =HFSPLUS_CALC_CHECKSUM ((unsigned char *)blhdr,sizeof (*blhdr)); + +if (le32_to_cpu (cksum) !=le32_to_cpu (blhdr_checksum)) { + blhdr->checksum =JNL_SWAP32 (jnl, blhdr_checksum); +pr_err ("corrupted block list header\n" ); +hfs_dbg (JOURNAL ,"calculated CRC %#x, blhdr_checksum %#x\n" , +le32_to_cpu (cksum),le32_to_cpu (blhdr_checksum)); +return -EIO ; + } + + blhdr->checksum =JNL_SWAP32 (jnl, blhdr_checksum); +return 0 ; +} +static int hfsplus_replay_journal (struct super_block *sb) { +struct hfsplus_journal *jnl =HFSPLUS_SB (sb)->jnl;struct hfsplus_blist_desc desc; +u32 last_seq_num =0 ;int err; err =hfsplus_init_block_list_desc (sb, &desc);
@@ -366,9 +488,30 @@ static int hfsplus_replay_journal(struct super_block *sb)return err; } +/* Go through transactions */ +while (!hfsplus_journal_empty (jnl)) { +struct hfsplus_blhdr *blhdr; + + blhdr =hfsplus_get_blhdr (sb,JOURNAL_OFF_TO_SEC (sb), &desc); +if (!blhdr) { + err = -EIO ; + goto failed_journal_replay; + } + +hfs_dbg (JREPLAY ,"num_blocks: %u, bytes_used: %u\n" , +TR_BLOCKS (jnl, blhdr),TR_BYTES (jnl, blhdr)); + + err =hfsplus_verify_blhdr (jnl, last_seq_num, blhdr); +if (err) + goto failed_journal_replay; + + last_seq_num =TR_SEQ_NUM (jnl, blhdr); + } +/* TODO: implement */ return -EINVAL ; +failed_journal_replay:hfsplus_deinit_block_list_desc (&desc);return err; }
Signature · 1 line
1.7.9.5