Bootcache now gets alignment from dm-bootcache

The alignement of various on disk data structures for
the bootcache was hard coded to be page size. Now, it
is communicated via the Header.

BUG=chromium-os:25441
TEST=boot the system with bootcache enabled

Change-Id: I51e526d4ce5b760a5597cc7812dd89a317bb2384
Reviewed-on: https://gerrit.chromium.org/gerrit/33096
Commit-Ready: Paul Taysom <taysom@chromium.org>
Reviewed-by: Paul Taysom <taysom@chromium.org>
Tested-by: Paul Taysom <taysom@chromium.org>
diff --git a/bootcache.c b/bootcache.c
index 5fa9a8f..5abcc95 100644
--- a/bootcache.c
+++ b/bootcache.c
@@ -13,7 +13,9 @@
  * the blocks used during boot.
  *
  * Sizes and offsets are measured in 512 byte sectors.
- * Space is allocated in CHUNK_SIZE chunks.
+ * Space is allocated in chunks. The size of chunks is
+ * derived from alignment restrictions obtained from
+ * the header.
  *
  * bootcache [-t] <device-name> <raw-partition>
  *
@@ -61,11 +63,9 @@
 typedef uint64_t u64;
 typedef uint32_t u32;
 
-#define CHUNK_SIZE 4096	/* todo(taysom) should get from bootcache_hdr */
 #define SECTOR_SHIFT 9
 #define MAX_CHUNKS 128
 #define MAX_FILE_NAME 256
-#define SECTORS_PER_CHUNK (CHUNK_SIZE >> SECTOR_SHIFT)
 #define MAX_MSG 1024
 
 static struct bootcache_hdr Header;
@@ -83,6 +83,8 @@
 
 static u64 Trace_start;
 static u64 Cache_start;
+static u64 Chunk_size;
+static u64 Sectors_per_chunk;
 
 #define fatal(fmt, ...)	pr_fatal(__FILE__, __FUNCTION__, __LINE__, \
 				fmt, ## __VA_ARGS__)
@@ -172,12 +174,12 @@
 	return rc;
 }
 
-static void *malloc_buf(size_t npages)
+static void *malloc_buf(size_t nchunks)
 {
 	void *buf;
 	int rc;
 
-	rc = posix_memalign(&buf, CHUNK_SIZE, npages * CHUNK_SIZE);
+	rc = posix_memalign(&buf, Chunk_size, nchunks * Chunk_size);
 	if (rc) {
 		fatal("posix_memalign rc=%d", rc);
 	}
@@ -200,7 +202,7 @@
 	u64 num_bytes = Trace.num * sizeof(*Trace.tr);
 
 	/* Align to page boundary then convert to sectors */
-	return ((num_bytes + CHUNK_SIZE - 1) / CHUNK_SIZE) * SECTORS_PER_CHUNK;
+	return ((num_bytes + Chunk_size - 1) / Chunk_size) * Sectors_per_chunk;
 }
 
 static void compute_sections(void)
@@ -208,7 +210,7 @@
 	Header.num_trace_recs = Trace.num;
 	Header.sectors_meta = num_meta_sectors();
 	Header.sectors_data = num_sectors_in_cache();
-	Trace_start = Header.sector + SECTORS_PER_CHUNK;
+	Trace_start = Header.sector + Sectors_per_chunk;
 	Cache_start = Trace_start + Header.sectors_meta;
 }
 
@@ -221,7 +223,7 @@
 
 	offset    = tr.sector << SECTOR_SHIFT;
 	remainder = tr.count << SECTOR_SHIFT;
-	n = MAX_CHUNKS * CHUNK_SIZE;
+	n = MAX_CHUNKS * Chunk_size;
 	while (remainder) {
 		if (n > remainder) {
 			n = remainder;
@@ -289,7 +291,7 @@
  */
 static u64 num_bytes(const char *file)
 {
-	char buf[CHUNK_SIZE];
+	char buf[Chunk_size];
 	ssize_t rc;
 	u64 sum = 0;
 
@@ -356,6 +358,8 @@
 	if (Header.version != BOOTCACHE_VERSION) {
 		fatal("Bad version %u != %u", Header.version, BOOTCACHE_VERSION);
 	}
+	Chunk_size = Header.alignment;
+	Sectors_per_chunk = Chunk_size >> SECTOR_SHIFT;
 }
 
 /*
diff --git a/dm-bootcache.h b/dm-bootcache.h
index 536911f..5bb2553 100644
--- a/dm-bootcache.h
+++ b/dm-bootcache.h
@@ -17,7 +17,7 @@
 #include <linux/types.h>
 
 enum {	BOOTCACHE_MAGIC = 1651470196,
-	BOOTCACHE_VERSION = 2,
+	BOOTCACHE_VERSION = 3,
 	MAX_SIGNATURE = 256
 };
 
@@ -37,6 +37,7 @@
 	__u32	sectors_data;	/* Size of the data area in sectors*/
 	__u32	max_sectors;	/* Max sectors that can to read */
 	__u32	max_hw_sectors;	/* Max hardware sectore that can be read */
+	__u32	alignment;	/* Alignement on disk */
 	char	date[12];	/* Date and time dm-bootcache was compiled */
 	char	time[12];
 	char	signature[MAX_SIGNATURE];