btt.h 7.6 KB

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