Skip to contentSkip to conversation

reiser4progs: add metadata checksums support

PATCH New Edward Shishkin 1 participant 1 message Aug 16, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 reiser4progs: add metadata checksums support

---
 include/aux/crc32c.h        |   11 +
 libaux/Makefile.am          |    4 
 libaux/crc32c.c             |   90 +++++++++
 plugin/node/node41/node41.c |   31 +++
 6 files changed, 361 insertions(+), 214 deletions(-)
include/aux/crc32c.h new file +11 -0
@@ -0,0 +1,11 @@
+#ifndef CRC32C_H
+#define CRC32C_H
+
+#include <aal/libaal.h>
+
+uint32_t crc32c_le(uint32_t seed, unsigned char const *data, uint32_t length);
+
+#define crc32c(seed, data, length)			\
+  crc32c_le(seed, (unsigned char const *)data, length)
+#define reiser4_crc32c crc32c
+#endif
libaux/Makefile.am +2 -2
@@ -8,11 +8,11 @@ endif
 
 noinst_LTLIBRARIES		= libaux-static.la $(MINIMAL_LIBS)
 
-libaux_static_la_SOURCES	= aux.c gauge.c
+libaux_static_la_SOURCES	= aux.c gauge.c crc32c.c
 libaux_static_la_CFLAGS		= @GENERIC_CFLAGS@
 
 if ENABLE_MINIMAL
-libaux_minimal_la_SOURCES	= aux.c gauge.c
+libaux_minimal_la_SOURCES	= aux.c gauge.c crc32c.c
 libaux_minimal_la_CFLAGS	= @MINIMAL_CFLAGS@
 endif
 
libaux/crc32c.c new file +90 -0

90 lines not on this page · raw message

plugin/node/node41/node41.c +30 -1
@@ -6,12 +6,31 @@
 #include "../node40/node40_repair.h"
 #include "node41.h"
 #include "node41_repair.h"
+#include <aux/crc32c.h>
 
 uint32_t node41_get_csum(reiser4_node_t *entity) {
 	aal_assert("edward-3", entity != NULL);
 	return nh41_get_csum((reiser4_node_t *)entity);
 }
 
+uint32_t csum_node41(reiser4_node_t *entity, uint32_t check)
+{
+	uint32_t calc;
+
+	calc = reiser4_crc32c(~0,
+			      entity->block->data,
+			      sizeof(node40_header_t));
+	calc = reiser4_crc32c(calc,
+			      entity->block->data + sizeof(node41_header_t),
+			      entity->block->size - sizeof(node41_header_t));
+	if (check)
+		return calc == nh41_get_csum(entity);
+	else {
+		nh41_set_csum(entity, calc);
+		return 1;
+	}
+}
+
 reiser4_node_t *node41_prepare(aal_block_t *block, reiser4_key_plug_t *kplug) {
 	reiser4_node_t *entity;
 
@@ -41,6 +60,11 @@ reiser4_node_t *node41_open(aal_block_t
 	if (!(entity = node41_prepare(block, kplug)))
 		return NULL;
 
+	/* Verify checksum */
+	if (csum_node41(entity, 1 /* check */) == 0) {
+		aal_free(entity);
+		return NULL;
+	}
 	/* Check the magic. */
 	if (nh_get_magic(entity) != NODE41_MAGIC) {
 		aal_free(entity);
@@ -50,6 +74,11 @@ reiser4_node_t *node41_open(aal_block_t
 	return entity;
 }
 
+errno_t node41_sync(reiser4_node_t *entity) {
+	csum_node41(entity, 0 /* update */);
+	return node40_sync(entity);
+}
+
 #ifndef ENABLE_MINIMAL
 
 /* Returns maximal size of item possible for passed node instance */
@@ -99,7 +128,7 @@ reiser4_node_plug_t node41_plug = {
 
 #ifndef ENABLE_MINIMAL
 	.init		= node41_init,
-	.sync           = node40_sync,
+	.sync           = node41_sync,
 	.merge          = node40_merge,
 
 	.pack           = node41_pack,