btt.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Block Translation Table library
  4. * Copyright (c) 2014-2015, Intel Corporation.
  5. */
  6. #ifndef _LINUX_BTT_H
  7. #define _LINUX_BTT_H
  8. #include <linux/badblocks.h>
  9. #include <linux/types.h>
  10. #define BTT_SIG_LEN 16
  11. #define BTT_SIG "BTT_ARENA_INFO\0"
  12. #define MAP_ENT_SIZE 4
  13. #define MAP_TRIM_SHIFT 31
  14. #define MAP_TRIM_MASK (1 << MAP_TRIM_SHIFT)
  15. #define MAP_ERR_SHIFT 30
  16. #define MAP_ERR_MASK (1 << MAP_ERR_SHIFT)
  17. #define MAP_LBA_MASK (~((1 << MAP_TRIM_SHIFT) | (1 << MAP_ERR_SHIFT)))
  18. #define MAP_ENT_NORMAL 0xC0000000
  19. #define LOG_GRP_SIZE sizeof(struct log_group)
  20. #define LOG_ENT_SIZE sizeof(struct log_entry)
  21. #define ARENA_MIN_SIZE (1UL << 24) /* 16 MB */
  22. #define ARENA_MAX_SIZE (1ULL << 39) /* 512 GB */
  23. #define RTT_VALID (1UL << 31)
  24. #define RTT_INVALID 0
  25. #define BTT_PG_SIZE 4096
  26. #define BTT_DEFAULT_NFREE ND_MAX_LANES
  27. #define LOG_SEQ_INIT 1
  28. #define IB_FLAG_ERROR 0x00000001
  29. #define IB_FLAG_ERROR_MASK 0x00000001
  30. #define ent_lba(ent) (ent & MAP_LBA_MASK)
  31. #define ent_e_flag(ent) (!!(ent & MAP_ERR_MASK))
  32. #define ent_z_flag(ent) (!!(ent & MAP_TRIM_MASK))
  33. #define set_e_flag(ent) (ent |= MAP_ERR_MASK)
  34. /* 'normal' is both e and z flags set */
  35. #define ent_normal(ent) (ent_e_flag(ent) && ent_z_flag(ent))
  36. enum btt_init_state {
  37. INIT_UNCHECKED = 0,
  38. INIT_NOTFOUND,
  39. INIT_READY
  40. };
  41. /*
  42. * A log group represents one log 'lane', and consists of four log entries.
  43. * Two of the four entries are valid entries, and the remaining two are
  44. * padding. Due to an old bug in the padding location, we need to perform a
  45. * test to determine the padding scheme being used, and use that scheme
  46. * thereafter.
  47. *
  48. * In kernels prior to 4.15, 'log group' would have actual log entries at
  49. * indices (0, 2) and padding at indices (1, 3), where as the correct/updated
  50. * format has log entries at indices (0, 1) and padding at indices (2, 3).
  51. *
  52. * Old (pre 4.15) format:
  53. * +-----------------+-----------------+
  54. * | ent[0] | ent[1] |
  55. * | 16B | 16B |
  56. * | lba/old/new/seq | pad |
  57. * +-----------------------------------+
  58. * | ent[2] | ent[3] |
  59. * | 16B | 16B |
  60. * | lba/old/new/seq | pad |
  61. * +-----------------+-----------------+
  62. *
  63. * New format:
  64. * +-----------------+-----------------+
  65. * | ent[0] | ent[1] |
  66. * | 16B | 16B |
  67. * | lba/old/new/seq | lba/old/new/seq |
  68. * +-----------------------------------+
  69. * | ent[2] | ent[3] |
  70. * | 16B | 16B |
  71. * | pad | pad |
  72. * +-----------------+-----------------+
  73. *
  74. * We detect during start-up which format is in use, and set
  75. * arena->log_index[(0, 1)] with the detected format.
  76. */
  77. struct log_entry {
  78. __le32 lba;
  79. __le32 old_map;
  80. __le32 new_map;
  81. __le32 seq;
  82. };
  83. struct log_group {
  84. struct log_entry ent[4];
  85. };
  86. struct btt_sb {
  87. u8 signature[BTT_SIG_LEN];
  88. u8 uuid[16];
  89. u8 parent_uuid[16];
  90. __le32 flags;
  91. __le16 version_major;
  92. __le16 version_minor;
  93. __le32 external_lbasize;
  94. __le32 external_nlba;
  95. __le32 internal_lbasize;
  96. __le32 internal_nlba;
  97. __le32 nfree;
  98. __le32 infosize;
  99. __le64 nextoff;
  100. __le64 dataoff;
  101. __le64 mapoff;
  102. __le64 logoff;
  103. __le64 info2off;
  104. u8 padding[3968];
  105. __le64 checksum;
  106. };
  107. struct free_entry {
  108. u32 block;
  109. u8 sub;
  110. u8 seq;
  111. u8 has_err;
  112. };
  113. struct aligned_lock {
  114. union {
  115. spinlock_t lock;
  116. u8 cacheline_padding[L1_CACHE_BYTES];
  117. };
  118. };
  119. /**
  120. * struct arena_info - handle for an arena
  121. * @size: Size in bytes this arena occupies on the raw device.
  122. * This includes arena metadata.
  123. * @external_lba_start: The first external LBA in this arena.
  124. * @internal_nlba: Number of internal blocks available in the arena
  125. * including nfree reserved blocks
  126. * @internal_lbasize: Internal and external lba sizes may be different as
  127. * we can round up 'odd' external lbasizes such as 520B
  128. * to be aligned.
  129. * @external_nlba: Number of blocks contributed by the arena to the number
  130. * reported to upper layers. (internal_nlba - nfree)
  131. * @external_lbasize: LBA size as exposed to upper layers.
  132. * @nfree: A reserve number of 'free' blocks that is used to
  133. * handle incoming writes.
  134. * @version_major: Metadata layout version major.
  135. * @version_minor: Metadata layout version minor.
  136. * @sector_size: The Linux sector size - 512 or 4096
  137. * @nextoff: Offset in bytes to the start of the next arena.
  138. * @infooff: Offset in bytes to the info block of this arena.
  139. * @dataoff: Offset in bytes to the data area of this arena.
  140. * @mapoff: Offset in bytes to the map area of this arena.
  141. * @logoff: Offset in bytes to the log area of this arena.
  142. * @info2off: Offset in bytes to the backup info block of this arena.
  143. * @freelist: Pointer to in-memory list of free blocks
  144. * @rtt: Pointer to in-memory "Read Tracking Table"
  145. * @map_locks: Spinlocks protecting concurrent map writes
  146. * @nd_btt: Pointer to parent nd_btt structure.
  147. * @list: List head for list of arenas
  148. * @debugfs_dir: Debugfs dentry
  149. * @flags: Arena flags - may signify error states.
  150. * @err_lock: Mutex for synchronizing error clearing.
  151. * @log_index: Indices of the valid log entries in a log_group
  152. *
  153. * arena_info is a per-arena handle. Once an arena is narrowed down for an
  154. * IO, this struct is passed around for the duration of the IO.
  155. */
  156. struct arena_info {
  157. u64 size; /* Total bytes for this arena */
  158. u64 external_lba_start;
  159. u32 internal_nlba;
  160. u32 internal_lbasize;
  161. u32 external_nlba;
  162. u32 external_lbasize;
  163. u32 nfree;
  164. u16 version_major;
  165. u16 version_minor;
  166. u32 sector_size;
  167. /* Byte offsets to the different on-media structures */
  168. u64 nextoff;
  169. u64 infooff;
  170. u64 dataoff;
  171. u64 mapoff;
  172. u64 logoff;
  173. u64 info2off;
  174. /* Pointers to other in-memory structures for this arena */
  175. struct free_entry *freelist;
  176. u32 *rtt;
  177. struct aligned_lock *map_locks;
  178. struct nd_btt *nd_btt;
  179. struct list_head list;
  180. struct dentry *debugfs_dir;
  181. /* Arena flags */
  182. u32 flags;
  183. struct mutex err_lock;
  184. int log_index[2];
  185. };
  186. /**
  187. * struct btt - handle for a BTT instance
  188. * @btt_disk: Pointer to the gendisk for BTT device
  189. * @btt_queue: Pointer to the request queue for the BTT device
  190. * @arena_list: Head of the list of arenas
  191. * @debugfs_dir: Debugfs dentry
  192. * @nd_btt: Parent nd_btt struct
  193. * @nlba: Number of logical blocks exposed to the upper layers
  194. * after removing the amount of space needed by metadata
  195. * @rawsize: Total size in bytes of the available backing device
  196. * @lbasize: LBA size as requested and presented to upper layers.
  197. * This is sector_size + size of any metadata.
  198. * @sector_size: The Linux sector size - 512 or 4096
  199. * @lanes: Per-lane spinlocks
  200. * @init_lock: Mutex used for the BTT initialization
  201. * @init_state: Flag describing the initialization state for the BTT
  202. * @num_arenas: Number of arenas in the BTT instance
  203. * @phys_bb: Pointer to the namespace's badblocks structure
  204. */
  205. struct btt {
  206. struct gendisk *btt_disk;
  207. struct request_queue *btt_queue;
  208. struct list_head arena_list;
  209. struct dentry *debugfs_dir;
  210. struct nd_btt *nd_btt;
  211. u64 nlba;
  212. unsigned long long rawsize;
  213. u32 lbasize;
  214. u32 sector_size;
  215. struct nd_region *nd_region;
  216. struct mutex init_lock;
  217. int init_state;
  218. int num_arenas;
  219. struct badblocks *phys_bb;
  220. };
  221. bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super);
  222. int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
  223. struct btt_sb *btt_sb);
  224. #endif