12025cf9eSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 28c2f7e86SDan Williams /* 38c2f7e86SDan Williams * Block Translation Table library 48c2f7e86SDan Williams * Copyright (c) 2014-2015, Intel Corporation. 58c2f7e86SDan Williams */ 68c2f7e86SDan Williams 78c2f7e86SDan Williams #ifndef _LINUX_BTT_H 88c2f7e86SDan Williams #define _LINUX_BTT_H 98c2f7e86SDan Williams 108c2f7e86SDan Williams #include <linux/types.h> 118c2f7e86SDan Williams 128c2f7e86SDan Williams #define BTT_SIG_LEN 16 138c2f7e86SDan Williams #define BTT_SIG "BTT_ARENA_INFO\0" 145212e11fSVishal Verma #define MAP_ENT_SIZE 4 155212e11fSVishal Verma #define MAP_TRIM_SHIFT 31 165212e11fSVishal Verma #define MAP_TRIM_MASK (1 << MAP_TRIM_SHIFT) 175212e11fSVishal Verma #define MAP_ERR_SHIFT 30 185212e11fSVishal Verma #define MAP_ERR_MASK (1 << MAP_ERR_SHIFT) 195212e11fSVishal Verma #define MAP_LBA_MASK (~((1 << MAP_TRIM_SHIFT) | (1 << MAP_ERR_SHIFT))) 205212e11fSVishal Verma #define MAP_ENT_NORMAL 0xC0000000 2124e3a7fbSVishal Verma #define LOG_GRP_SIZE sizeof(struct log_group) 225212e11fSVishal Verma #define LOG_ENT_SIZE sizeof(struct log_entry) 235212e11fSVishal Verma #define ARENA_MIN_SIZE (1UL << 24) /* 16 MB */ 245212e11fSVishal Verma #define ARENA_MAX_SIZE (1ULL << 39) /* 512 GB */ 255212e11fSVishal Verma #define RTT_VALID (1UL << 31) 265212e11fSVishal Verma #define RTT_INVALID 0 275212e11fSVishal Verma #define BTT_PG_SIZE 4096 285212e11fSVishal Verma #define BTT_DEFAULT_NFREE ND_MAX_LANES 295212e11fSVishal Verma #define LOG_SEQ_INIT 1 305212e11fSVishal Verma 315212e11fSVishal Verma #define IB_FLAG_ERROR 0x00000001 325212e11fSVishal Verma #define IB_FLAG_ERROR_MASK 0x00000001 335212e11fSVishal Verma 340595d539SVishal Verma #define ent_lba(ent) (ent & MAP_LBA_MASK) 350595d539SVishal Verma #define ent_e_flag(ent) (!!(ent & MAP_ERR_MASK)) 360595d539SVishal Verma #define ent_z_flag(ent) (!!(ent & MAP_TRIM_MASK)) 37d9b83c75SVishal Verma #define set_e_flag(ent) (ent |= MAP_ERR_MASK) 389dedc73aSVishal Verma /* 'normal' is both e and z flags set */ 399dedc73aSVishal Verma #define ent_normal(ent) (ent_e_flag(ent) && ent_z_flag(ent)) 400595d539SVishal Verma 415212e11fSVishal Verma enum btt_init_state { 425212e11fSVishal Verma INIT_UNCHECKED = 0, 435212e11fSVishal Verma INIT_NOTFOUND, 445212e11fSVishal Verma INIT_READY 455212e11fSVishal Verma }; 465212e11fSVishal Verma 4724e3a7fbSVishal Verma /* 4824e3a7fbSVishal Verma * A log group represents one log 'lane', and consists of four log entries. 4924e3a7fbSVishal Verma * Two of the four entries are valid entries, and the remaining two are 5024e3a7fbSVishal Verma * padding. Due to an old bug in the padding location, we need to perform a 5124e3a7fbSVishal Verma * test to determine the padding scheme being used, and use that scheme 5224e3a7fbSVishal Verma * thereafter. 5324e3a7fbSVishal Verma * 5424e3a7fbSVishal Verma * In kernels prior to 4.15, 'log group' would have actual log entries at 5524e3a7fbSVishal Verma * indices (0, 2) and padding at indices (1, 3), where as the correct/updated 5624e3a7fbSVishal Verma * format has log entries at indices (0, 1) and padding at indices (2, 3). 5724e3a7fbSVishal Verma * 5824e3a7fbSVishal Verma * Old (pre 4.15) format: 5924e3a7fbSVishal Verma * +-----------------+-----------------+ 6024e3a7fbSVishal Verma * | ent[0] | ent[1] | 6124e3a7fbSVishal Verma * | 16B | 16B | 6224e3a7fbSVishal Verma * | lba/old/new/seq | pad | 6324e3a7fbSVishal Verma * +-----------------------------------+ 6424e3a7fbSVishal Verma * | ent[2] | ent[3] | 6524e3a7fbSVishal Verma * | 16B | 16B | 6624e3a7fbSVishal Verma * | lba/old/new/seq | pad | 6724e3a7fbSVishal Verma * +-----------------+-----------------+ 6824e3a7fbSVishal Verma * 6924e3a7fbSVishal Verma * New format: 7024e3a7fbSVishal Verma * +-----------------+-----------------+ 7124e3a7fbSVishal Verma * | ent[0] | ent[1] | 7224e3a7fbSVishal Verma * | 16B | 16B | 7324e3a7fbSVishal Verma * | lba/old/new/seq | lba/old/new/seq | 7424e3a7fbSVishal Verma * +-----------------------------------+ 7524e3a7fbSVishal Verma * | ent[2] | ent[3] | 7624e3a7fbSVishal Verma * | 16B | 16B | 7724e3a7fbSVishal Verma * | pad | pad | 7824e3a7fbSVishal Verma * +-----------------+-----------------+ 7924e3a7fbSVishal Verma * 8024e3a7fbSVishal Verma * We detect during start-up which format is in use, and set 8124e3a7fbSVishal Verma * arena->log_index[(0, 1)] with the detected format. 8224e3a7fbSVishal Verma */ 8324e3a7fbSVishal Verma 845212e11fSVishal Verma struct log_entry { 855212e11fSVishal Verma __le32 lba; 865212e11fSVishal Verma __le32 old_map; 875212e11fSVishal Verma __le32 new_map; 885212e11fSVishal Verma __le32 seq; 8924e3a7fbSVishal Verma }; 9024e3a7fbSVishal Verma 9124e3a7fbSVishal Verma struct log_group { 9224e3a7fbSVishal Verma struct log_entry ent[4]; 935212e11fSVishal Verma }; 948c2f7e86SDan Williams 958c2f7e86SDan Williams struct btt_sb { 968c2f7e86SDan Williams u8 signature[BTT_SIG_LEN]; 978c2f7e86SDan Williams u8 uuid[16]; 988c2f7e86SDan Williams u8 parent_uuid[16]; 998c2f7e86SDan Williams __le32 flags; 1008c2f7e86SDan Williams __le16 version_major; 1018c2f7e86SDan Williams __le16 version_minor; 1028c2f7e86SDan Williams __le32 external_lbasize; 1038c2f7e86SDan Williams __le32 external_nlba; 1048c2f7e86SDan Williams __le32 internal_lbasize; 1058c2f7e86SDan Williams __le32 internal_nlba; 1068c2f7e86SDan Williams __le32 nfree; 1078c2f7e86SDan Williams __le32 infosize; 1088c2f7e86SDan Williams __le64 nextoff; 1098c2f7e86SDan Williams __le64 dataoff; 1108c2f7e86SDan Williams __le64 mapoff; 1118c2f7e86SDan Williams __le64 logoff; 1128c2f7e86SDan Williams __le64 info2off; 1138c2f7e86SDan Williams u8 padding[3968]; 1148c2f7e86SDan Williams __le64 checksum; 1158c2f7e86SDan Williams }; 1168c2f7e86SDan Williams 1175212e11fSVishal Verma struct free_entry { 1185212e11fSVishal Verma u32 block; 1195212e11fSVishal Verma u8 sub; 1205212e11fSVishal Verma u8 seq; 121d9b83c75SVishal Verma u8 has_err; 1225212e11fSVishal Verma }; 1235212e11fSVishal Verma 1245212e11fSVishal Verma struct aligned_lock { 1255212e11fSVishal Verma union { 1265212e11fSVishal Verma spinlock_t lock; 1275212e11fSVishal Verma u8 cacheline_padding[L1_CACHE_BYTES]; 1285212e11fSVishal Verma }; 1295212e11fSVishal Verma }; 1305212e11fSVishal Verma 1315212e11fSVishal Verma /** 1325212e11fSVishal Verma * struct arena_info - handle for an arena 1335212e11fSVishal Verma * @size: Size in bytes this arena occupies on the raw device. 1345212e11fSVishal Verma * This includes arena metadata. 1355212e11fSVishal Verma * @external_lba_start: The first external LBA in this arena. 1365212e11fSVishal Verma * @internal_nlba: Number of internal blocks available in the arena 1375212e11fSVishal Verma * including nfree reserved blocks 1385212e11fSVishal Verma * @internal_lbasize: Internal and external lba sizes may be different as 1395212e11fSVishal Verma * we can round up 'odd' external lbasizes such as 520B 1405212e11fSVishal Verma * to be aligned. 1415212e11fSVishal Verma * @external_nlba: Number of blocks contributed by the arena to the number 1425212e11fSVishal Verma * reported to upper layers. (internal_nlba - nfree) 1435212e11fSVishal Verma * @external_lbasize: LBA size as exposed to upper layers. 1445212e11fSVishal Verma * @nfree: A reserve number of 'free' blocks that is used to 1455212e11fSVishal Verma * handle incoming writes. 1465212e11fSVishal Verma * @version_major: Metadata layout version major. 1475212e11fSVishal Verma * @version_minor: Metadata layout version minor. 14875892004SVishal Verma * @sector_size: The Linux sector size - 512 or 4096 1495212e11fSVishal Verma * @nextoff: Offset in bytes to the start of the next arena. 1505212e11fSVishal Verma * @infooff: Offset in bytes to the info block of this arena. 1515212e11fSVishal Verma * @dataoff: Offset in bytes to the data area of this arena. 1525212e11fSVishal Verma * @mapoff: Offset in bytes to the map area of this arena. 1535212e11fSVishal Verma * @logoff: Offset in bytes to the log area of this arena. 1545212e11fSVishal Verma * @info2off: Offset in bytes to the backup info block of this arena. 1555212e11fSVishal Verma * @freelist: Pointer to in-memory list of free blocks 1565212e11fSVishal Verma * @rtt: Pointer to in-memory "Read Tracking Table" 1575212e11fSVishal Verma * @map_locks: Spinlocks protecting concurrent map writes 1585212e11fSVishal Verma * @nd_btt: Pointer to parent nd_btt structure. 1595212e11fSVishal Verma * @list: List head for list of arenas 1605212e11fSVishal Verma * @debugfs_dir: Debugfs dentry 1615212e11fSVishal Verma * @flags: Arena flags - may signify error states. 16213b7954cSVishal Verma * @err_lock: Mutex for synchronizing error clearing. 16324e3a7fbSVishal Verma * @log_index: Indices of the valid log entries in a log_group 1645212e11fSVishal Verma * 1655212e11fSVishal Verma * arena_info is a per-arena handle. Once an arena is narrowed down for an 1665212e11fSVishal Verma * IO, this struct is passed around for the duration of the IO. 1675212e11fSVishal Verma */ 1685212e11fSVishal Verma struct arena_info { 1695212e11fSVishal Verma u64 size; /* Total bytes for this arena */ 1705212e11fSVishal Verma u64 external_lba_start; 1715212e11fSVishal Verma u32 internal_nlba; 1725212e11fSVishal Verma u32 internal_lbasize; 1735212e11fSVishal Verma u32 external_nlba; 1745212e11fSVishal Verma u32 external_lbasize; 1755212e11fSVishal Verma u32 nfree; 1765212e11fSVishal Verma u16 version_major; 1775212e11fSVishal Verma u16 version_minor; 17875892004SVishal Verma u32 sector_size; 1795212e11fSVishal Verma /* Byte offsets to the different on-media structures */ 1805212e11fSVishal Verma u64 nextoff; 1815212e11fSVishal Verma u64 infooff; 1825212e11fSVishal Verma u64 dataoff; 1835212e11fSVishal Verma u64 mapoff; 1845212e11fSVishal Verma u64 logoff; 1855212e11fSVishal Verma u64 info2off; 1865212e11fSVishal Verma /* Pointers to other in-memory structures for this arena */ 1875212e11fSVishal Verma struct free_entry *freelist; 1885212e11fSVishal Verma u32 *rtt; 1895212e11fSVishal Verma struct aligned_lock *map_locks; 1905212e11fSVishal Verma struct nd_btt *nd_btt; 1915212e11fSVishal Verma struct list_head list; 1925212e11fSVishal Verma struct dentry *debugfs_dir; 1935212e11fSVishal Verma /* Arena flags */ 1945212e11fSVishal Verma u32 flags; 195d9b83c75SVishal Verma struct mutex err_lock; 19624e3a7fbSVishal Verma int log_index[2]; 1975212e11fSVishal Verma }; 1985212e11fSVishal Verma 199*5ae96d77SEnrico Weigelt struct badblocks; 200*5ae96d77SEnrico Weigelt 2015212e11fSVishal Verma /** 2025212e11fSVishal Verma * struct btt - handle for a BTT instance 2035212e11fSVishal Verma * @btt_disk: Pointer to the gendisk for BTT device 2045212e11fSVishal Verma * @arena_list: Head of the list of arenas 2055212e11fSVishal Verma * @debugfs_dir: Debugfs dentry 2065212e11fSVishal Verma * @nd_btt: Parent nd_btt struct 2075212e11fSVishal Verma * @nlba: Number of logical blocks exposed to the upper layers 2085212e11fSVishal Verma * after removing the amount of space needed by metadata 2095212e11fSVishal Verma * @rawsize: Total size in bytes of the available backing device 2105212e11fSVishal Verma * @lbasize: LBA size as requested and presented to upper layers. 2115212e11fSVishal Verma * This is sector_size + size of any metadata. 2125212e11fSVishal Verma * @sector_size: The Linux sector size - 512 or 4096 2135212e11fSVishal Verma * @lanes: Per-lane spinlocks 2145212e11fSVishal Verma * @init_lock: Mutex used for the BTT initialization 2155212e11fSVishal Verma * @init_state: Flag describing the initialization state for the BTT 2165212e11fSVishal Verma * @num_arenas: Number of arenas in the BTT instance 21713b7954cSVishal Verma * @phys_bb: Pointer to the namespace's badblocks structure 2185212e11fSVishal Verma */ 2195212e11fSVishal Verma struct btt { 2205212e11fSVishal Verma struct gendisk *btt_disk; 2215212e11fSVishal Verma struct list_head arena_list; 2225212e11fSVishal Verma struct dentry *debugfs_dir; 2235212e11fSVishal Verma struct nd_btt *nd_btt; 2245212e11fSVishal Verma u64 nlba; 2255212e11fSVishal Verma unsigned long long rawsize; 2265212e11fSVishal Verma u32 lbasize; 2275212e11fSVishal Verma u32 sector_size; 2285212e11fSVishal Verma struct nd_region *nd_region; 2295212e11fSVishal Verma struct mutex init_lock; 2305212e11fSVishal Verma int init_state; 2315212e11fSVishal Verma int num_arenas; 232d9b83c75SVishal Verma struct badblocks *phys_bb; 2335212e11fSVishal Verma }; 234ab45e763SVishal Verma 235ab45e763SVishal Verma bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super); 23614e49454SVishal Verma int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns, 23714e49454SVishal Verma struct btt_sb *btt_sb); 238ab45e763SVishal Verma 2398c2f7e86SDan Williams #endif 240