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 <ugen@cinnamon.is>
Reviewed-by: Adrien Destugues <pulkomandy@gmail.com>
This commit is contained in:
brjhaiku 2019-07-16 09:45:31 +05:30 committed by Adrien Destugues
parent 5fb44dff84
commit 8f40380d6a
2 changed files with 10 additions and 2 deletions

View File

@ -66,7 +66,7 @@ static uint32 kCrcTable[256] = {
\param data Pointer to the byte stream.
\param length Length of the byte stream in bytes.
\return The crc checksum, or 0 if an error occurred.
\return The crc checksum.
*/
uint32
calculate_crc(uint32 crc, uint8* data, uint16 length)

View File

@ -15,6 +15,7 @@
#include "BTree.h"
#include "CachedBlock.h"
#include "Chunk.h"
#include "CRCTable.h"
#include "DebugSupport.h"
#include "ExtentAllocator.h"
#include "Inode.h"
@ -656,7 +657,14 @@ Volume::FindBlock(off_t logical, off_t& physical)
status_t
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,
sizeof(btrfs_super_block))