linux/include/uapi/linux/bcache.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
   2#ifndef _LINUX_BCACHE_H
   3#define _LINUX_BCACHE_H
   4
   5/*
   6 * Bcache on disk data structures
   7 */
   8
   9#include <linux/types.h>
  10
  11#define BITMASK(name, type, field, offset, size)                \
  12static inline __u64 name(const type *k)                         \
  13{ return (k->field >> offset) & ~(~0ULL << size); }             \
  14                                                                \
  15static inline void SET_##name(type *k, __u64 v)                 \
  16{                                                               \
  17        k->field &= ~(~(~0ULL << size) << offset);              \
  18        k->field |= (v & ~(~0ULL << size)) << offset;           \
  19}
  20
  21/* Btree keys - all units are in sectors */
  22
  23struct bkey {
  24        __u64   high;
  25        __u64   low;
  26        __u64   ptr[];
  27};
  28
  29#define KEY_FIELD(name, field, offset, size)                            \
  30        BITMASK(name, struct bkey, field, offset, size)
  31
  32#define PTR_FIELD(name, offset, size)                                   \
  33static inline __u64 name(const struct bkey *k, unsigned int i)          \
  34{ return (k->ptr[i] >> offset) & ~(~0ULL << size); }                    \
  35                                                                        \
  36static inline void SET_##name(struct bkey *k, unsigned int i, __u64 v)  \
  37{                                                                       \
  38        k->ptr[i] &= ~(~(~0ULL << size) << offset);                     \
  39        k->ptr[i] |= (v & ~(~0ULL << size)) << offset;                  \
  40}
  41
  42#define KEY_SIZE_BITS           16
  43#define KEY_MAX_U64S            8
  44
  45KEY_FIELD(KEY_PTRS,     high, 60, 3)
  46KEY_FIELD(HEADER_SIZE,  high, 58, 2)
  47KEY_FIELD(KEY_CSUM,     high, 56, 2)
  48KEY_FIELD(KEY_PINNED,   high, 55, 1)
  49KEY_FIELD(KEY_DIRTY,    high, 36, 1)
  50
  51KEY_FIELD(KEY_SIZE,     high, 20, KEY_SIZE_BITS)
  52KEY_FIELD(KEY_INODE,    high, 0,  20)
  53
  54/* Next time I change the on disk format, KEY_OFFSET() won't be 64 bits */
  55
  56static inline __u64 KEY_OFFSET(const struct bkey *k)
  57{
  58        return k->low;
  59}
  60
  61static inline void SET_KEY_OFFSET(struct bkey *k, __u64 v)
  62{
  63        k->low = v;
  64}
  65
  66/*
  67 * The high bit being set is a relic from when we used it to do binary
  68 * searches - it told you where a key started. It's not used anymore,
  69 * and can probably be safely dropped.
  70 */
  71#define KEY(inode, offset, size)                                        \
  72((struct bkey) {                                                        \
  73        .high = (1ULL << 63) | ((__u64) (size) << 20) | (inode),        \
  74        .low = (offset)                                                 \
  75})
  76
  77#define ZERO_KEY                        KEY(0, 0, 0)
  78
  79#define MAX_KEY_INODE                   (~(~0 << 20))
  80#define MAX_KEY_OFFSET                  (~0ULL >> 1)
  81#define MAX_KEY                         KEY(MAX_KEY_INODE, MAX_KEY_OFFSET, 0)
  82
  83#define KEY_START(k)                    (KEY_OFFSET(k) - KEY_SIZE(k))
  84#define START_KEY(k)                    KEY(KEY_INODE(k), KEY_START(k), 0)
  85
  86#define PTR_DEV_BITS                    12
  87
  88PTR_FIELD(PTR_DEV,                      51, PTR_DEV_BITS)
  89PTR_FIELD(PTR_OFFSET,                   8,  43)
  90PTR_FIELD(PTR_GEN,                      0,  8)
  91
  92#define PTR_CHECK_DEV                   ((1 << PTR_DEV_BITS) - 1)
  93
  94#define MAKE_PTR(gen, offset, dev)                                      \
  95        ((((__u64) dev) << 51) | ((__u64) offset) << 8 | gen)
  96
  97/* Bkey utility code */
  98
  99static inline unsigned long bkey_u64s(const struct bkey *k)
 100{
 101        return (sizeof(struct bkey) / sizeof(__u64)) + KEY_PTRS(k);
 102}
 103
 104static inline unsigned long bkey_bytes(const struct bkey *k)
 105{
 106        return bkey_u64s(k) * sizeof(__u64);
 107}
 108
 109#define bkey_copy(_dest, _src)  memcpy(_dest, _src, bkey_bytes(_src))
 110
 111static inline void bkey_copy_key(struct bkey *dest, const struct bkey *src)
 112{
 113        SET_KEY_INODE(dest, KEY_INODE(src));
 114        SET_KEY_OFFSET(dest, KEY_OFFSET(src));
 115}
 116
 117static inline struct bkey *bkey_next(const struct bkey *k)
 118{
 119        __u64 *d = (void *) k;
 120
 121        return (struct bkey *) (d + bkey_u64s(k));
 122}
 123
 124static inline struct bkey *bkey_idx(const struct bkey *k, unsigned int nr_keys)
 125{
 126        __u64 *d = (void *) k;
 127
 128        return (struct bkey *) (d + nr_keys);
 129}
 130/* Enough for a key with 6 pointers */
 131#define BKEY_PAD                8
 132
 133#define BKEY_PADDED(key)                                        \
 134        union { struct bkey key; __u64 key ## _pad[BKEY_PAD]; }
 135
 136/* Superblock */
 137
 138/* Version 0: Cache device
 139 * Version 1: Backing device
 140 * Version 2: Seed pointer into btree node checksum
 141 * Version 3: Cache device with new UUID format
 142 * Version 4: Backing device with data offset
 143 */
 144#define BCACHE_SB_VERSION_CDEV          0
 145#define BCACHE_SB_VERSION_BDEV          1
 146#define BCACHE_SB_VERSION_CDEV_WITH_UUID 3
 147#define BCACHE_SB_VERSION_BDEV_WITH_OFFSET 4
 148#define BCACHE_SB_MAX_VERSION           4
 149
 150#define SB_SECTOR                       8
 151#define SB_SIZE                         4096
 152#define SB_LABEL_SIZE                   32
 153#define SB_JOURNAL_BUCKETS              256U
 154/* SB_JOURNAL_BUCKETS must be divisible by BITS_PER_LONG */
 155#define MAX_CACHES_PER_SET              8
 156
 157#define BDEV_DATA_START_DEFAULT         16      /* sectors */
 158
 159struct cache_sb {
 160        __u64                   csum;
 161        __u64                   offset; /* sector where this sb was written */
 162        __u64                   version;
 163
 164        __u8                    magic[16];
 165
 166        __u8                    uuid[16];
 167        union {
 168                __u8            set_uuid[16];
 169                __u64           set_magic;
 170        };
 171        __u8                    label[SB_LABEL_SIZE];
 172
 173        __u64                   flags;
 174        __u64                   seq;
 175        __u64                   pad[8];
 176
 177        union {
 178        struct {
 179                /* Cache devices */
 180                __u64           nbuckets;       /* device size */
 181
 182                __u16           block_size;     /* sectors */
 183                __u16           bucket_size;    /* sectors */
 184
 185                __u16           nr_in_set;
 186                __u16           nr_this_dev;
 187        };
 188        struct {
 189                /* Backing devices */
 190                __u64           data_offset;
 191
 192                /*
 193                 * block_size from the cache device section is still used by
 194                 * backing devices, so don't add anything here until we fix
 195                 * things to not need it for backing devices anymore
 196                 */
 197        };
 198        };
 199
 200        __u32                   last_mount;     /* time overflow in y2106 */
 201
 202        __u16                   first_bucket;
 203        union {
 204                __u16           njournal_buckets;
 205                __u16           keys;
 206        };
 207        __u64                   d[SB_JOURNAL_BUCKETS];  /* journal buckets */
 208};
 209
 210static inline _Bool SB_IS_BDEV(const struct cache_sb *sb)
 211{
 212        return sb->version == BCACHE_SB_VERSION_BDEV
 213                || sb->version == BCACHE_SB_VERSION_BDEV_WITH_OFFSET;
 214}
 215
 216BITMASK(CACHE_SYNC,                     struct cache_sb, flags, 0, 1);
 217BITMASK(CACHE_DISCARD,                  struct cache_sb, flags, 1, 1);
 218BITMASK(CACHE_REPLACEMENT,              struct cache_sb, flags, 2, 3);
 219#define CACHE_REPLACEMENT_LRU           0U
 220#define CACHE_REPLACEMENT_FIFO          1U
 221#define CACHE_REPLACEMENT_RANDOM        2U
 222
 223BITMASK(BDEV_CACHE_MODE,                struct cache_sb, flags, 0, 4);
 224#define CACHE_MODE_WRITETHROUGH         0U
 225#define CACHE_MODE_WRITEBACK            1U
 226#define CACHE_MODE_WRITEAROUND          2U
 227#define CACHE_MODE_NONE                 3U
 228BITMASK(BDEV_STATE,                     struct cache_sb, flags, 61, 2);
 229#define BDEV_STATE_NONE                 0U
 230#define BDEV_STATE_CLEAN                1U
 231#define BDEV_STATE_DIRTY                2U
 232#define BDEV_STATE_STALE                3U
 233
 234/*
 235 * Magic numbers
 236 *
 237 * The various other data structures have their own magic numbers, which are
 238 * xored with the first part of the cache set's UUID
 239 */
 240
 241#define JSET_MAGIC                      0x245235c1a3625032ULL
 242#define PSET_MAGIC                      0x6750e15f87337f91ULL
 243#define BSET_MAGIC                      0x90135c78b99e07f5ULL
 244
 245static inline __u64 jset_magic(struct cache_sb *sb)
 246{
 247        return sb->set_magic ^ JSET_MAGIC;
 248}
 249
 250static inline __u64 pset_magic(struct cache_sb *sb)
 251{
 252        return sb->set_magic ^ PSET_MAGIC;
 253}
 254
 255static inline __u64 bset_magic(struct cache_sb *sb)
 256{
 257        return sb->set_magic ^ BSET_MAGIC;
 258}
 259
 260/*
 261 * Journal
 262 *
 263 * On disk format for a journal entry:
 264 * seq is monotonically increasing; every journal entry has its own unique
 265 * sequence number.
 266 *
 267 * last_seq is the oldest journal entry that still has keys the btree hasn't
 268 * flushed to disk yet.
 269 *
 270 * version is for on disk format changes.
 271 */
 272
 273#define BCACHE_JSET_VERSION_UUIDv1      1
 274#define BCACHE_JSET_VERSION_UUID        1       /* Always latest UUID format */
 275#define BCACHE_JSET_VERSION             1
 276
 277struct jset {
 278        __u64                   csum;
 279        __u64                   magic;
 280        __u64                   seq;
 281        __u32                   version;
 282        __u32                   keys;
 283
 284        __u64                   last_seq;
 285
 286        BKEY_PADDED(uuid_bucket);
 287        BKEY_PADDED(btree_root);
 288        __u16                   btree_level;
 289        __u16                   pad[3];
 290
 291        __u64                   prio_bucket[MAX_CACHES_PER_SET];
 292
 293        union {
 294                struct bkey     start[0];
 295                __u64           d[0];
 296        };
 297};
 298
 299/* Bucket prios/gens */
 300
 301struct prio_set {
 302        __u64                   csum;
 303        __u64                   magic;
 304        __u64                   seq;
 305        __u32                   version;
 306        __u32                   pad;
 307
 308        __u64                   next_bucket;
 309
 310        struct bucket_disk {
 311                __u16           prio;
 312                __u8            gen;
 313        } __attribute((packed)) data[];
 314};
 315
 316/* UUIDS - per backing device/flash only volume metadata */
 317
 318struct uuid_entry {
 319        union {
 320                struct {
 321                        __u8    uuid[16];
 322                        __u8    label[32];
 323                        __u32   first_reg; /* time overflow in y2106 */
 324                        __u32   last_reg;
 325                        __u32   invalidated;
 326
 327                        __u32   flags;
 328                        /* Size of flash only volumes */
 329                        __u64   sectors;
 330                };
 331
 332                __u8            pad[128];
 333        };
 334};
 335
 336BITMASK(UUID_FLASH_ONLY,        struct uuid_entry, flags, 0, 1);
 337
 338/* Btree nodes */
 339
 340/* Version 1: Seed pointer into btree node checksum
 341 */
 342#define BCACHE_BSET_CSUM                1
 343#define BCACHE_BSET_VERSION             1
 344
 345/*
 346 * Btree nodes
 347 *
 348 * On disk a btree node is a list/log of these; within each set the keys are
 349 * sorted
 350 */
 351struct bset {
 352        __u64                   csum;
 353        __u64                   magic;
 354        __u64                   seq;
 355        __u32                   version;
 356        __u32                   keys;
 357
 358        union {
 359                struct bkey     start[0];
 360                __u64           d[0];
 361        };
 362};
 363
 364/* OBSOLETE */
 365
 366/* UUIDS - per backing device/flash only volume metadata */
 367
 368struct uuid_entry_v0 {
 369        __u8            uuid[16];
 370        __u8            label[32];
 371        __u32           first_reg;
 372        __u32           last_reg;
 373        __u32           invalidated;
 374        __u32           pad;
 375};
 376
 377#endif /* _LINUX_BCACHE_H */
 378