Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blk: update checksum calculation algorithm #57

Merged
merged 1 commit into from
Nov 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
blk: update checksum calculation algorithm
- Update the checksum handling to handle BE systems.
- Improve the way the chk-sums are calculated.
- Adjust the checksum unittest to be endianess aware.

Initial version by @mdalecki
  • Loading branch information
tomaszkapela committed Nov 12, 2014
commit 9ab0c1919521a27b5e5039f34cf5dbdab65e8126
1 change: 0 additions & 1 deletion src/blk.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ pmemblk_pool_open_common(const char *path, size_t bsize, int rdonly)
uuid_generate(hdrp->uuid);
hdrp->crtime = htole64((uint64_t)time(NULL));
util_checksum(hdrp, sizeof (*hdrp), &hdrp->checksum, 1);
hdrp->checksum = htole64(hdrp->checksum);

/* store pool's header */
pmem_msync(hdrp, sizeof (*hdrp));
Expand Down
1 change: 0 additions & 1 deletion src/btt.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ read_info(struct btt_info *infop)
infop->mapoff = le64toh(infop->mapoff);
infop->flogoff = le64toh(infop->flogoff);
infop->infooff = le64toh(infop->infooff);
infop->checksum = le64toh(infop->checksum);

/* and to be valid, the fields must checksum correctly */
if (!util_checksum(infop, sizeof (*infop), &infop->checksum, 0)) {
Expand Down
1 change: 0 additions & 1 deletion src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ pmemlog_pool_open_common(const char *path, int rdonly)
uuid_generate(hdrp->uuid);
hdrp->crtime = htole64((uint64_t)time(NULL));
util_checksum(hdrp, sizeof (*hdrp), &hdrp->checksum, 1);
hdrp->checksum = htole64(hdrp->checksum);

/* store pool's header */
pmem_msync(hdrp, sizeof (*hdrp));
Expand Down
8 changes: 5 additions & 3 deletions src/test/checksum/checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* usage: checksum files...
*/

#include <endian.h>
#include "unittest.h"

#include "util.h"
Expand All @@ -55,11 +56,12 @@ fletcher64(void *addr, size_t len)
uint32_t hi32 = 0;

while (p32 < p32end) {
lo32 += *p32++;
lo32 += le32toh(*p32);
p32++;
hi32 += lo32;
}

return (uint64_t)hi32 << 32 | lo32;
return htole64((uint64_t)hi32 << 32 | lo32);
}

int
Expand Down Expand Up @@ -96,7 +98,7 @@ main(int argc, char *argv[])
uint64_t oldval = *ptr;

/* mess with it */
*ptr = 0x123;
*ptr = htole64(0x123);

/*
* calc a checksum and have it installed
Expand Down
12 changes: 7 additions & 5 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ util_unmap(void *addr, size_t len)
* util_checksum -- compute Fletcher64 checksum
*
* csump points to where the checksum lives, so that location
* is treated as zeros while calculating the checksum. If
* insert is true, the calculated checksum is inserted into
* is treated as zeros while calculating the checksum. The
* checksummed data is assumed to be in little endian order.
* If insert is true, the calculated checksum is inserted into
* the range at *csump. Otherwise the calculated checksum is
* checked against *csump and the result returned (true means
* the range checksummed correctly).
Expand All @@ -242,18 +243,19 @@ util_checksum(void *addr, size_t len, uint64_t *csump, int insert)
p32++;
hi32 += lo32;
} else {
lo32 += *p32++;
lo32 += le32toh(*p32);
++p32;
hi32 += lo32;
}

csum = (uint64_t)hi32 << 32 | lo32;

if (insert) {
*csump = csum;
*csump = htole64(csum);
return 1;
}

return *csump == csum;
return *csump == htole64(csum);
}

/*
Expand Down