From e8c85bb806084037a48bedb0613be9c1bb43d853 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Sun, 31 Jul 2011 19:29:22 -0700 Subject: [PATCH] Add a --writeback switch to make-bcache --- bcache.h | 12 ++++++++++++ make-bcache.c | 19 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/bcache.h b/bcache.h index d91b3e18..e667cc01 100644 --- a/bcache.h +++ b/bcache.h @@ -1,6 +1,16 @@ #ifndef _BCACHE_H #define _BCACHE_H +#define BITMASK(name, type, field, offset, size) \ +static inline uint64_t name(const type *k) \ +{ return (k->field >> offset) & ~(((uint64_t) ~0) << size); } \ + \ +static inline void SET_##name(type *k, uint64_t v) \ +{ \ + k->field &= ~(~((uint64_t) ~0 << size) << offset); \ + k->field |= v << offset; \ +} + static const char bcache_magic[] = { 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca, 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 }; @@ -41,6 +51,8 @@ struct cache_sb { uint64_t d[]; /* journal buckets */ }; +BITMASK(BDEV_WRITEBACK, struct cache_sb, flags, 0, 1); + inline uint64_t crc64(const void *_data, size_t len); #define node(i, j) ((void *) ((i)->d + (j))) diff --git a/make-bcache.c b/make-bcache.c index 58fe6744..d442853a 100644 --- a/make-bcache.c +++ b/make-bcache.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -67,10 +68,16 @@ void usage() " -b bucket size\n" " -w block size (hard sector size of SSD, often 2k)\n" " -U UUID\n" - " -S Set UUID\n"); + " --writeback Enable writeback\n"); exit(EXIT_FAILURE); } +int writeback; + +struct option opts[2] = { + { "writeback", 0, &writeback, 1 } +}; + void write_sb(char *dev, struct cache_sb *sb) { int fd; @@ -97,6 +104,12 @@ void write_sb(char *dev, struct cache_sb *sb) exit(EXIT_FAILURE); } + if (sb->version == CACHE_BACKING_DEV && + writeback) + SET_BDEV_WRITEBACK(sb, 1); + else + SET_BDEV_WRITEBACK(sb, 0); + sb->offset = SB_SECTOR; memcpy(sb->magic, bcache_magic, 16); sb->nbuckets = getblocks(fd) / sb->bucket_size; @@ -153,7 +166,9 @@ int main(int argc, char **argv) uuid_generate(sb.uuid); uuid_generate(sb.set_uuid); - while ((c = getopt(argc, argv, "-CBU:w:b:")) != -1) + while ((c = getopt_long(argc, argv, + "-CBU:w:b:", + opts, NULL)) != -1) switch (c) { case 'C': sb.version = 0;