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