btt.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/types.h>
  17. #define BTT_SIG_LEN 16
  18. #define BTT_SIG "BTT_ARENA_INFO\0"
  19. #define MAP_ENT_SIZE 4
  20. #define MAP_TRIM_SHIFT 31
  21. #define MAP_TRIM_MASK (1 << MAP_TRIM_SHIFT)
  22. #define MAP_ERR_SHIFT 30
  23. #define MAP_ERR_MASK (1 << MAP_ERR_SHIFT)
  24. #define MAP_LBA_MASK (~((1 << MAP_TRIM_SHIFT) | (1 << MAP_ERR_SHIFT)))
  25. #define MAP_ENT_NORMAL 0xC0000000
  26. #define LOG_GRP_SIZE sizeof(struct log_group)
  27. #define LOG_ENT_SIZE sizeof(struct log_entry)
  28. #define ARENA_MIN_SIZE (1UL << 24) /* 16 MB */
  29. #define ARENA_MAX_SIZE (1ULL << 39) /* 512 GB */
  30. #define RTT_VALID (1UL << 31)
  31. #define RTT_INVALID 0
  32. #define BTT_PG_SIZE 4096
  33. #define BTT_DEFAULT_NFREE ND_MAX_LANES
  34. #define LOG_SEQ_INIT 1
  35. #define IB_FLAG_ERROR 0x00000001
  36. #define IB_FLAG_ERROR_MASK 0x00000001
  37. enum btt_init_state {
  38. INIT_UNCHECKED = 0,
  39. INIT_NOTFOUND,
  40. INIT_READY
  41. };
  42. /*
  43. * A log group represents one log 'lane', and consists of four log entries.
  44. * Two of the four entries are valid entries, and the remaining two are
  45. * padding. Due to an old bug in the padding location, we need to perform a
  46. * test to determine the padding scheme being used, and use that scheme
  47. * thereafter.
  48. *
  49. * In kernels prior to 4.15, 'log group' would have actual log entries at
  50. * indices (0, 2) and padding at indices (1, 3), where as the correct/updated
  51. * format has log entries at indices (0, 1) and padding at indices (2, 3).
  52. *
  53. * Old (pre 4.15) format:
  54. * +-----------------+-----------------+
  55. * | ent[0] | ent[1] |
  56. * | 16B | 16B |
  57. * | lba/old/new/seq | pad |
  58. * +-----------------------------------+
  59. * | ent[2] | ent[3] |
  60. * | 16B | 16B |
  61. * | lba/old/new/seq | pad |
  62. * +-----------------+-----------------+
  63. *
  64. * New format:
  65. * +-----------------+-----------------+
  66. * | ent[0] | ent[1] |
  67. * | 16B | 16B |
  68. * | lba/old/new/seq | lba/old/new/seq |
  69. * +-----------------------------------+
  70. * | ent[2] | ent[3] |
  71. * | 16B | 16B |
  72. * | pad | pad |
  73. * +-----------------+-----------------+
  74. *
  75. * We detect during start-up which format is in use, and set
  76. * arena->log_index[(0, 1)] with the detected format.
  77. */
  78. struct log_entry {
  79. __le32 lba;
  80. __le32 old_map;
  81. __le32 new_map;
  82. __le32 seq;
  83. };
  84. struct log_group {
  85. struct log_entry ent[4];
  86. };
  87. struct btt_sb {
  88. u8 signature[BTT_SIG_LEN];
  89. u8 uuid[16];
  90. u8 parent_uuid[16];
  91. __le32 flags;
  92. __le16 version_major;
  93. __le16 version_minor;
  94. __le32 external_lbasize;
  95. __le32 external_nlba;
  96. __le32 internal_lbasize;
  97. __le32 internal_nlba;
  98. __le32 nfree;
  99. __le32 infosize;
  100. __le64 nextoff;
  101. __le64 dataoff;
  102. __le64 mapoff;
  103. __le64 logoff;
  104. __le64 info2off;
  105. u8 padding[3968];
  106. __le64 checksum;
  107. };
  108. struct free_entry {
  109. u32 block;
  110. u8 sub;
  111. u8 seq;
  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. * @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. * @log_index: Indices of the valid log entries in a log_group
  150. *
  151. * arena_info is a per-arena handle. Once an arena is narrowed down for an
  152. * IO, this struct is passed around for the duration of the IO.
  153. */
  154. struct arena_info {
  155. u64 size; /* Total bytes for this arena */
  156. u64 external_lba_start;
  157. u32 internal_nlba;
  158. u32 internal_lbasize;
  159. u32 external_nlba;
  160. u32 external_lbasize;
  161. u32 nfree;
  162. u16 version_major;
  163. u16 version_minor;
  164. /* Byte offsets to the different on-media structures */
  165. u64 nextoff;
  166. u64 infooff;
  167. u64 dataoff;
  168. u64 mapoff;
  169. u64 logoff;
  170. u64 info2off;
  171. /* Pointers to other in-memory structures for this arena */
  172. struct free_entry *freelist;
  173. u32 *rtt;
  174. struct aligned_lock *map_locks;
  175. struct nd_btt *nd_btt;
  176. struct list_head list;
  177. struct dentry *debugfs_dir;
  178. /* Arena flags */
  179. u32 flags;
  180. int log_index[2];
  181. };
  182. /**
  183. * struct btt - handle for a BTT instance
  184. * @btt_disk: Pointer to the gendisk for BTT device
  185. * @btt_queue: Pointer to the request queue for the BTT device
  186. * @arena_list: Head of the list of arenas
  187. * @debugfs_dir: Debugfs dentry
  188. * @nd_btt: Parent nd_btt struct
  189. * @nlba: Number of logical blocks exposed to the upper layers
  190. * after removing the amount of space needed by metadata
  191. * @rawsize: Total size in bytes of the available backing device
  192. * @lbasize: LBA size as requested and presented to upper layers.
  193. * This is sector_size + size of any metadata.
  194. * @sector_size: The Linux sector size - 512 or 4096
  195. * @lanes: Per-lane spinlocks
  196. * @init_lock: Mutex used for the BTT initialization
  197. * @init_state: Flag describing the initialization state for the BTT
  198. * @num_arenas: Number of arenas in the BTT instance
  199. */
  200. struct btt {
  201. struct gendisk *btt_disk;
  202. struct request_queue *btt_queue;
  203. struct list_head arena_list;
  204. struct dentry *debugfs_dir;
  205. struct nd_btt *nd_btt;
  206. u64 nlba;
  207. unsigned long long rawsize;
  208. u32 lbasize;
  209. u32 sector_size;
  210. struct nd_region *nd_region;
  211. struct mutex init_lock;
  212. int init_state;
  213. int num_arenas;
  214. };
  215. bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super);
  216. #endif