18c2f7e86SDan Williams /* 28c2f7e86SDan Williams * Block Translation Table library 38c2f7e86SDan Williams * Copyright (c) 2014-2015, Intel Corporation. 48c2f7e86SDan Williams * 58c2f7e86SDan Williams * This program is free software; you can redistribute it and/or modify it 68c2f7e86SDan Williams * under the terms and conditions of the GNU General Public License, 78c2f7e86SDan Williams * version 2, as published by the Free Software Foundation. 88c2f7e86SDan Williams * 98c2f7e86SDan Williams * This program is distributed in the hope it will be useful, but WITHOUT 108c2f7e86SDan Williams * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 118c2f7e86SDan Williams * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 128c2f7e86SDan Williams * more details. 138c2f7e86SDan Williams */ 148c2f7e86SDan Williams 158c2f7e86SDan Williams #ifndef _LINUX_BTT_H 168c2f7e86SDan Williams #define _LINUX_BTT_H 178c2f7e86SDan Williams 188c2f7e86SDan Williams #include <linux/types.h> 198c2f7e86SDan Williams 208c2f7e86SDan Williams #define BTT_SIG_LEN 16 218c2f7e86SDan Williams #define BTT_SIG "BTT_ARENA_INFO\0" 225212e11fSVishal Verma #define MAP_ENT_SIZE 4 235212e11fSVishal Verma #define MAP_TRIM_SHIFT 31 245212e11fSVishal Verma #define MAP_TRIM_MASK (1 << MAP_TRIM_SHIFT) 255212e11fSVishal Verma #define MAP_ERR_SHIFT 30 265212e11fSVishal Verma #define MAP_ERR_MASK (1 << MAP_ERR_SHIFT) 275212e11fSVishal Verma #define MAP_LBA_MASK (~((1 << MAP_TRIM_SHIFT) | (1 << MAP_ERR_SHIFT))) 285212e11fSVishal Verma #define MAP_ENT_NORMAL 0xC0000000 295212e11fSVishal Verma #define LOG_ENT_SIZE sizeof(struct log_entry) 305212e11fSVishal Verma #define ARENA_MIN_SIZE (1UL << 24) /* 16 MB */ 315212e11fSVishal Verma #define ARENA_MAX_SIZE (1ULL << 39) /* 512 GB */ 325212e11fSVishal Verma #define RTT_VALID (1UL << 31) 335212e11fSVishal Verma #define RTT_INVALID 0 345212e11fSVishal Verma #define BTT_PG_SIZE 4096 355212e11fSVishal Verma #define BTT_DEFAULT_NFREE ND_MAX_LANES 365212e11fSVishal Verma #define LOG_SEQ_INIT 1 375212e11fSVishal Verma 385212e11fSVishal Verma #define IB_FLAG_ERROR 0x00000001 395212e11fSVishal Verma #define IB_FLAG_ERROR_MASK 0x00000001 405212e11fSVishal Verma 410595d539SVishal Verma #define ent_lba(ent) (ent & MAP_LBA_MASK) 420595d539SVishal Verma #define ent_e_flag(ent) (!!(ent & MAP_ERR_MASK)) 430595d539SVishal Verma #define ent_z_flag(ent) (!!(ent & MAP_TRIM_MASK)) 440595d539SVishal Verma 455212e11fSVishal Verma enum btt_init_state { 465212e11fSVishal Verma INIT_UNCHECKED = 0, 475212e11fSVishal Verma INIT_NOTFOUND, 485212e11fSVishal Verma INIT_READY 495212e11fSVishal Verma }; 505212e11fSVishal Verma 515212e11fSVishal Verma struct log_entry { 525212e11fSVishal Verma __le32 lba; 535212e11fSVishal Verma __le32 old_map; 545212e11fSVishal Verma __le32 new_map; 555212e11fSVishal Verma __le32 seq; 565212e11fSVishal Verma __le64 padding[2]; 575212e11fSVishal Verma }; 588c2f7e86SDan Williams 598c2f7e86SDan Williams struct btt_sb { 608c2f7e86SDan Williams u8 signature[BTT_SIG_LEN]; 618c2f7e86SDan Williams u8 uuid[16]; 628c2f7e86SDan Williams u8 parent_uuid[16]; 638c2f7e86SDan Williams __le32 flags; 648c2f7e86SDan Williams __le16 version_major; 658c2f7e86SDan Williams __le16 version_minor; 668c2f7e86SDan Williams __le32 external_lbasize; 678c2f7e86SDan Williams __le32 external_nlba; 688c2f7e86SDan Williams __le32 internal_lbasize; 698c2f7e86SDan Williams __le32 internal_nlba; 708c2f7e86SDan Williams __le32 nfree; 718c2f7e86SDan Williams __le32 infosize; 728c2f7e86SDan Williams __le64 nextoff; 738c2f7e86SDan Williams __le64 dataoff; 748c2f7e86SDan Williams __le64 mapoff; 758c2f7e86SDan Williams __le64 logoff; 768c2f7e86SDan Williams __le64 info2off; 778c2f7e86SDan Williams u8 padding[3968]; 788c2f7e86SDan Williams __le64 checksum; 798c2f7e86SDan Williams }; 808c2f7e86SDan Williams 815212e11fSVishal Verma struct free_entry { 825212e11fSVishal Verma u32 block; 835212e11fSVishal Verma u8 sub; 845212e11fSVishal Verma u8 seq; 855212e11fSVishal Verma }; 865212e11fSVishal Verma 875212e11fSVishal Verma struct aligned_lock { 885212e11fSVishal Verma union { 895212e11fSVishal Verma spinlock_t lock; 905212e11fSVishal Verma u8 cacheline_padding[L1_CACHE_BYTES]; 915212e11fSVishal Verma }; 925212e11fSVishal Verma }; 935212e11fSVishal Verma 945212e11fSVishal Verma /** 955212e11fSVishal Verma * struct arena_info - handle for an arena 965212e11fSVishal Verma * @size: Size in bytes this arena occupies on the raw device. 975212e11fSVishal Verma * This includes arena metadata. 985212e11fSVishal Verma * @external_lba_start: The first external LBA in this arena. 995212e11fSVishal Verma * @internal_nlba: Number of internal blocks available in the arena 1005212e11fSVishal Verma * including nfree reserved blocks 1015212e11fSVishal Verma * @internal_lbasize: Internal and external lba sizes may be different as 1025212e11fSVishal Verma * we can round up 'odd' external lbasizes such as 520B 1035212e11fSVishal Verma * to be aligned. 1045212e11fSVishal Verma * @external_nlba: Number of blocks contributed by the arena to the number 1055212e11fSVishal Verma * reported to upper layers. (internal_nlba - nfree) 1065212e11fSVishal Verma * @external_lbasize: LBA size as exposed to upper layers. 1075212e11fSVishal Verma * @nfree: A reserve number of 'free' blocks that is used to 1085212e11fSVishal Verma * handle incoming writes. 1095212e11fSVishal Verma * @version_major: Metadata layout version major. 1105212e11fSVishal Verma * @version_minor: Metadata layout version minor. 111*75892004SVishal Verma * @sector_size: The Linux sector size - 512 or 4096 1125212e11fSVishal Verma * @nextoff: Offset in bytes to the start of the next arena. 1135212e11fSVishal Verma * @infooff: Offset in bytes to the info block of this arena. 1145212e11fSVishal Verma * @dataoff: Offset in bytes to the data area of this arena. 1155212e11fSVishal Verma * @mapoff: Offset in bytes to the map area of this arena. 1165212e11fSVishal Verma * @logoff: Offset in bytes to the log area of this arena. 1175212e11fSVishal Verma * @info2off: Offset in bytes to the backup info block of this arena. 1185212e11fSVishal Verma * @freelist: Pointer to in-memory list of free blocks 1195212e11fSVishal Verma * @rtt: Pointer to in-memory "Read Tracking Table" 1205212e11fSVishal Verma * @map_locks: Spinlocks protecting concurrent map writes 1215212e11fSVishal Verma * @nd_btt: Pointer to parent nd_btt structure. 1225212e11fSVishal Verma * @list: List head for list of arenas 1235212e11fSVishal Verma * @debugfs_dir: Debugfs dentry 1245212e11fSVishal Verma * @flags: Arena flags - may signify error states. 1255212e11fSVishal Verma * 1265212e11fSVishal Verma * arena_info is a per-arena handle. Once an arena is narrowed down for an 1275212e11fSVishal Verma * IO, this struct is passed around for the duration of the IO. 1285212e11fSVishal Verma */ 1295212e11fSVishal Verma struct arena_info { 1305212e11fSVishal Verma u64 size; /* Total bytes for this arena */ 1315212e11fSVishal Verma u64 external_lba_start; 1325212e11fSVishal Verma u32 internal_nlba; 1335212e11fSVishal Verma u32 internal_lbasize; 1345212e11fSVishal Verma u32 external_nlba; 1355212e11fSVishal Verma u32 external_lbasize; 1365212e11fSVishal Verma u32 nfree; 1375212e11fSVishal Verma u16 version_major; 1385212e11fSVishal Verma u16 version_minor; 139*75892004SVishal Verma u32 sector_size; 1405212e11fSVishal Verma /* Byte offsets to the different on-media structures */ 1415212e11fSVishal Verma u64 nextoff; 1425212e11fSVishal Verma u64 infooff; 1435212e11fSVishal Verma u64 dataoff; 1445212e11fSVishal Verma u64 mapoff; 1455212e11fSVishal Verma u64 logoff; 1465212e11fSVishal Verma u64 info2off; 1475212e11fSVishal Verma /* Pointers to other in-memory structures for this arena */ 1485212e11fSVishal Verma struct free_entry *freelist; 1495212e11fSVishal Verma u32 *rtt; 1505212e11fSVishal Verma struct aligned_lock *map_locks; 1515212e11fSVishal Verma struct nd_btt *nd_btt; 1525212e11fSVishal Verma struct list_head list; 1535212e11fSVishal Verma struct dentry *debugfs_dir; 1545212e11fSVishal Verma /* Arena flags */ 1555212e11fSVishal Verma u32 flags; 1565212e11fSVishal Verma }; 1575212e11fSVishal Verma 1585212e11fSVishal Verma /** 1595212e11fSVishal Verma * struct btt - handle for a BTT instance 1605212e11fSVishal Verma * @btt_disk: Pointer to the gendisk for BTT device 1615212e11fSVishal Verma * @btt_queue: Pointer to the request queue for the BTT device 1625212e11fSVishal Verma * @arena_list: Head of the list of arenas 1635212e11fSVishal Verma * @debugfs_dir: Debugfs dentry 1645212e11fSVishal Verma * @nd_btt: Parent nd_btt struct 1655212e11fSVishal Verma * @nlba: Number of logical blocks exposed to the upper layers 1665212e11fSVishal Verma * after removing the amount of space needed by metadata 1675212e11fSVishal Verma * @rawsize: Total size in bytes of the available backing device 1685212e11fSVishal Verma * @lbasize: LBA size as requested and presented to upper layers. 1695212e11fSVishal Verma * This is sector_size + size of any metadata. 1705212e11fSVishal Verma * @sector_size: The Linux sector size - 512 or 4096 1715212e11fSVishal Verma * @lanes: Per-lane spinlocks 1725212e11fSVishal Verma * @init_lock: Mutex used for the BTT initialization 1735212e11fSVishal Verma * @init_state: Flag describing the initialization state for the BTT 1745212e11fSVishal Verma * @num_arenas: Number of arenas in the BTT instance 1755212e11fSVishal Verma */ 1765212e11fSVishal Verma struct btt { 1775212e11fSVishal Verma struct gendisk *btt_disk; 1785212e11fSVishal Verma struct request_queue *btt_queue; 1795212e11fSVishal Verma struct list_head arena_list; 1805212e11fSVishal Verma struct dentry *debugfs_dir; 1815212e11fSVishal Verma struct nd_btt *nd_btt; 1825212e11fSVishal Verma u64 nlba; 1835212e11fSVishal Verma unsigned long long rawsize; 1845212e11fSVishal Verma u32 lbasize; 1855212e11fSVishal Verma u32 sector_size; 1865212e11fSVishal Verma struct nd_region *nd_region; 1875212e11fSVishal Verma struct mutex init_lock; 1885212e11fSVishal Verma int init_state; 1895212e11fSVishal Verma int num_arenas; 1905212e11fSVishal Verma }; 191ab45e763SVishal Verma 192ab45e763SVishal Verma bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super); 19314e49454SVishal Verma int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns, 19414e49454SVishal Verma struct btt_sb *btt_sb); 195ab45e763SVishal Verma 1968c2f7e86SDan Williams #endif 197