2 * Author: Kent Overstreet <kmo@daterainc.com>
10 #define BITMASK(name, type, field, offset, size) \
11 static inline uint64_t name(const type *k) \
12 { return (k->field >> offset) & ~(((uint64_t) ~0) << size); } \
14 static inline void SET_##name(type *k, uint64_t v) \
16 k->field &= ~(~((uint64_t) ~0 << size) << offset); \
17 k->field |= v << offset; \
20 static const char bcache_magic[] = {
21 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
22 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 };
25 * Version 0: Cache device
26 * Version 1: Backing device
27 * Version 2: Seed pointer into btree node checksum
28 * Version 3: Cache device with new UUID format
29 * Version 4: Backing device with data offset
31 #define BCACHE_SB_VERSION_CDEV_V0 0
32 #define BCACHE_SB_VERSION_BDEV 1
33 #define BCACHE_SB_VERSION_CDEV_WITH_UUID 3
34 #define BCACHE_SB_VERSION_BDEV_WITH_OFFSET 4
35 #define BCACHE_SB_VERSION_CDEV 5
36 #define BCACHE_SB_MAX_VERSION 5
39 #define SB_LABEL_SIZE 32
40 #define SB_JOURNAL_BUCKETS 256U
41 #define BDEV_DATA_START_DEFAULT 16 /* sectors */
42 #define SB_START (SB_SECTOR * 512)
46 uint64_t offset; /* sector where this sb was written */
56 uint8_t label[SB_LABEL_SIZE];
65 uint64_t nbuckets; /* device size */
67 uint16_t block_size; /* sectors */
68 uint16_t bucket_size; /* sectors */
78 * block_size from the cache device section is still used by
79 * backing devices, so don't add anything here until we fix
80 * things to not need it for backing devices anymore
85 uint32_t last_mount; /* time_t */
87 uint16_t first_bucket;
89 uint16_t njournal_buckets;
92 uint64_t d[SB_JOURNAL_BUCKETS]; /* journal buckets */
95 static inline bool SB_IS_BDEV(const struct cache_sb *sb)
97 return sb->version == BCACHE_SB_VERSION_BDEV
98 || sb->version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET;
101 BITMASK(CACHE_SYNC, struct cache_sb, flags, 0, 1);
102 BITMASK(CACHE_DISCARD, struct cache_sb, flags, 1, 1);
103 BITMASK(CACHE_REPLACEMENT, struct cache_sb, flags, 2, 3);
104 #define CACHE_REPLACEMENT_LRU 0U
105 #define CACHE_REPLACEMENT_FIFO 1U
106 #define CACHE_REPLACEMENT_RANDOM 2U
108 BITMASK(CACHE_TIER, struct cache_sb, flags, 2, 5);
109 #define CACHE_TIERS 4U
111 BITMASK(BDEV_CACHE_MODE, struct cache_sb, flags, 0, 4);
112 #define CACHE_MODE_WRITETHROUGH 0U
113 #define CACHE_MODE_WRITEBACK 1U
114 #define CACHE_MODE_WRITEAROUND 2U
115 #define CACHE_MODE_NONE 3U
116 BITMASK(BDEV_STATE, struct cache_sb, flags, 61, 2);
117 #define BDEV_STATE_NONE 0U
118 #define BDEV_STATE_CLEAN 1U
119 #define BDEV_STATE_DIRTY 2U
120 #define BDEV_STATE_STALE 3U
122 uint64_t crc64(const void *_data, size_t len);
124 #define node(i, j) ((void *) ((i)->d + (j)))
125 #define end(i) node(i, (i)->keys)
127 #define csum_set(i) \
128 crc64(((void *) (i)) + 8, ((void *) end(i)) - (((void *) (i)) + 8))