btt.h 7.2 KB

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