]> git.sesse.net Git - bcachefs-tools-debian/blob - bcache.h
make-bcache.c: Added support for storing "label" information for the
[bcachefs-tools-debian] / bcache.h
1 /*
2  * Author: Kent Overstreet <kmo@daterainc.com>
3  *
4  * GPLv2
5  */
6
7 #ifndef _BCACHE_H
8 #define _BCACHE_H
9
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); }   \
13                                                                 \
14 static inline void SET_##name(type *k, uint64_t v)              \
15 {                                                               \
16         k->field &= ~(~((uint64_t) ~0 << size) << offset);      \
17         k->field |= v << offset;                                \
18 }
19
20 static const char bcache_magic[] = {
21         0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
22         0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 };
23
24 /*
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
30  */
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
37
38 #define SB_SECTOR               8
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)
43
44 struct cache_sb {
45         uint64_t                csum;
46         uint64_t                offset; /* sector where this sb was written */
47         uint64_t                version;
48
49         uint8_t                 magic[16];
50
51         uint8_t                 uuid[16];
52         union {
53                 uint8_t         set_uuid[16];
54                 uint64_t        set_magic;
55         };
56         uint8_t                 label[SB_LABEL_SIZE];
57
58         uint64_t                flags;
59         uint64_t                seq;
60         uint64_t                pad[8];
61
62         union {
63         struct {
64                 /* Cache devices */
65                 uint64_t        nbuckets;       /* device size */
66
67                 uint16_t        block_size;     /* sectors */
68                 uint16_t        bucket_size;    /* sectors */
69
70                 uint16_t        nr_in_set;
71                 uint16_t        nr_this_dev;
72         };
73         struct {
74                 /* Backing devices */
75                 uint64_t        data_offset;
76
77                 /*
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
81                  */
82         };
83         };
84
85         uint32_t                last_mount;     /* time_t */
86
87         uint16_t                first_bucket;
88         union {
89                 uint16_t        njournal_buckets;
90                 uint16_t        keys;
91         };
92         uint64_t                d[SB_JOURNAL_BUCKETS];  /* journal buckets */
93 };
94
95 static inline bool SB_IS_BDEV(const struct cache_sb *sb)
96 {
97         return sb->version == BCACHE_SB_VERSION_BDEV
98                 || sb->version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET;
99 }
100
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
107
108 BITMASK(CACHE_TIER,             struct cache_sb, flags, 2, 5);
109 #define CACHE_TIERS             4U
110
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
121
122 uint64_t crc64(const void *_data, size_t len);
123
124 #define node(i, j)              ((void *) ((i)->d + (j)))
125 #define end(i)                  node(i, (i)->keys)
126
127 #define csum_set(i)                                                     \
128         crc64(((void *) (i)) + 8, ((void *) end(i)) - (((void *) (i)) + 8))
129
130 #endif