btrfs: update superblock checksum on write

Note there are 32bytes on disk for the checksum, but only the first 4
are used. This is because btrfs can (or could, at some point?) use
sha256 instead of crc32 when higher reliability is needed (but high
performance isn't).

Change-Id: I8a2bcf8f462440568d9b3e2d9fbdb7208723bfb9
Reviewed-on: https://review.haiku-os.org/c/haiku/+/1596
Reviewed-by: Chế Vũ Gia Hy <[email protected]>
Reviewed-by: Adrien Destugues <[email protected]>
This commit is contained in:
brjhaiku
2019-10-27 08:54:16 +01:00
committed by Adrien Destugues
parent 5fb44dff84
commit 8f40380d6a
2 changed files with 10 additions and 2 deletions
@@ -66,7 +66,7 @@ static uint32 kCrcTable[256] = {
\param data Pointer to the byte stream. \param data Pointer to the byte stream.
\param length Length of the byte stream in bytes. \param length Length of the byte stream in bytes.
\return The crc checksum, or 0 if an error occurred. \return The crc checksum.
*/ */
uint32 uint32
calculate_crc(uint32 crc, uint8* data, uint16 length) calculate_crc(uint32 crc, uint8* data, uint16 length)
@@ -15,6 +15,7 @@
#include "BTree.h" #include "BTree.h"
#include "CachedBlock.h" #include "CachedBlock.h"
#include "Chunk.h" #include "Chunk.h"
#include "CRCTable.h"
#include "DebugSupport.h" #include "DebugSupport.h"
#include "ExtentAllocator.h" #include "ExtentAllocator.h"
#include "Inode.h" #include "Inode.h"
@@ -656,7 +657,14 @@ Volume::FindBlock(off_t logical, off_t& physical)
status_t status_t
Volume::WriteSuperBlock() Volume::WriteSuperBlock()
{ {
// TODO(lesderid): Calculate checksum uint32 checksum = calculate_crc((uint32)~1,
(uint8 *)(&fSuperBlock + sizeof(fSuperBlock.checksum)),
sizeof(fSuperBlock) - sizeof(fSuperBlock.checksum));
fSuperBlock.checksum[0] = (checksum >> 0) & 0xFF;
fSuperBlock.checksum[1] = (checksum >> 8) & 0xFF;
fSuperBlock.checksum[2] = (checksum >> 16) & 0xFF;
fSuperBlock.checksum[3] = (checksum >> 24) & 0xFF;
if (write_pos(fDevice, BTRFS_SUPER_BLOCK_OFFSET, &fSuperBlock, if (write_pos(fDevice, BTRFS_SUPER_BLOCK_OFFSET, &fSuperBlock,
sizeof(btrfs_super_block)) sizeof(btrfs_super_block))