btt.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. BTT - Block Translation Table
  2. =============================
  3. 1. Introduction
  4. ---------------
  5. Persistent memory based storage is able to perform IO at byte (or more
  6. accurately, cache line) granularity. However, we often want to expose such
  7. storage as traditional block devices. The block drivers for persistent memory
  8. will do exactly this. However, they do not provide any atomicity guarantees.
  9. Traditional SSDs typically provide protection against torn sectors in hardware,
  10. using stored energy in capacitors to complete in-flight block writes, or perhaps
  11. in firmware. We don't have this luxury with persistent memory - if a write is in
  12. progress, and we experience a power failure, the block will contain a mix of old
  13. and new data. Applications may not be prepared to handle such a scenario.
  14. The Block Translation Table (BTT) provides atomic sector update semantics for
  15. persistent memory devices, so that applications that rely on sector writes not
  16. being torn can continue to do so. The BTT manifests itself as a stacked block
  17. device, and reserves a portion of the underlying storage for its metadata. At
  18. the heart of it, is an indirection table that re-maps all the blocks on the
  19. volume. It can be thought of as an extremely simple file system that only
  20. provides atomic sector updates.
  21. 2. Static Layout
  22. ----------------
  23. The underlying storage on which a BTT can be laid out is not limited in any way.
  24. The BTT, however, splits the available space into chunks of up to 512 GiB,
  25. called "Arenas".
  26. Each arena follows the same layout for its metadata, and all references in an
  27. arena are internal to it (with the exception of one field that points to the
  28. next arena). The following depicts the "On-disk" metadata layout:
  29. Backing Store +-------> Arena
  30. +---------------+ | +------------------+
  31. | | | | Arena info block |
  32. | Arena 0 +---+ | 4K |
  33. | 512G | +------------------+
  34. | | | |
  35. +---------------+ | |
  36. | | | |
  37. | Arena 1 | | Data Blocks |
  38. | 512G | | |
  39. | | | |
  40. +---------------+ | |
  41. | . | | |
  42. | . | | |
  43. | . | | |
  44. | | | |
  45. | | | |
  46. +---------------+ +------------------+
  47. | |
  48. | BTT Map |
  49. | |
  50. | |
  51. +------------------+
  52. | |
  53. | BTT Flog |
  54. | |
  55. +------------------+
  56. | Info block copy |
  57. | 4K |
  58. +------------------+
  59. 3. Theory of Operation
  60. ----------------------
  61. a. The BTT Map
  62. --------------
  63. The map is a simple lookup/indirection table that maps an LBA to an internal
  64. block. Each map entry is 32 bits. The two most significant bits are special
  65. flags, and the remaining form the internal block number.
  66. Bit Description
  67. 31 - 30 : Error and Zero flags - Used in the following way:
  68. Bit Description
  69. 31 30
  70. -----------------------------------------------------------------------
  71. 00 Initial state. Reads return zeroes; Premap = Postmap
  72. 01 Zero state: Reads return zeroes
  73. 10 Error state: Reads fail; Writes clear 'E' bit
  74. 11 Normal Block – has valid postmap
  75. 29 - 0 : Mappings to internal 'postmap' blocks
  76. Some of the terminology that will be subsequently used:
  77. External LBA : LBA as made visible to upper layers.
  78. ABA : Arena Block Address - Block offset/number within an arena
  79. Premap ABA : The block offset into an arena, which was decided upon by range
  80. checking the External LBA
  81. Postmap ABA : The block number in the "Data Blocks" area obtained after
  82. indirection from the map
  83. nfree : The number of free blocks that are maintained at any given time.
  84. This is the number of concurrent writes that can happen to the
  85. arena.
  86. For example, after adding a BTT, we surface a disk of 1024G. We get a read for
  87. the external LBA at 768G. This falls into the second arena, and of the 512G
  88. worth of blocks that this arena contributes, this block is at 256G. Thus, the
  89. premap ABA is 256G. We now refer to the map, and find out the mapping for block
  90. 'X' (256G) points to block 'Y', say '64'. Thus the postmap ABA is 64.
  91. b. The BTT Flog
  92. ---------------
  93. The BTT provides sector atomicity by making every write an "allocating write",
  94. i.e. Every write goes to a "free" block. A running list of free blocks is
  95. maintained in the form of the BTT flog. 'Flog' is a combination of the words
  96. "free list" and "log". The flog contains 'nfree' entries, and an entry contains:
  97. lba : The premap ABA that is being written to
  98. old_map : The old postmap ABA - after 'this' write completes, this will be a
  99. free block.
  100. new_map : The new postmap ABA. The map will up updated to reflect this
  101. lba->postmap_aba mapping, but we log it here in case we have to
  102. recover.
  103. seq : Sequence number to mark which of the 2 sections of this flog entry is
  104. valid/newest. It cycles between 01->10->11->01 (binary) under normal
  105. operation, with 00 indicating an uninitialized state.
  106. lba' : alternate lba entry
  107. old_map': alternate old postmap entry
  108. new_map': alternate new postmap entry
  109. seq' : alternate sequence number.
  110. Each of the above fields is 32-bit, making one entry 32 bytes. Entries are also
  111. padded to 64 bytes to avoid cache line sharing or aliasing. Flog updates are
  112. done such that for any entry being written, it:
  113. a. overwrites the 'old' section in the entry based on sequence numbers
  114. b. writes the 'new' section such that the sequence number is written last.
  115. c. The concept of lanes
  116. -----------------------
  117. While 'nfree' describes the number of concurrent IOs an arena can process
  118. concurrently, 'nlanes' is the number of IOs the BTT device as a whole can
  119. process.
  120. nlanes = min(nfree, num_cpus)
  121. A lane number is obtained at the start of any IO, and is used for indexing into
  122. all the on-disk and in-memory data structures for the duration of the IO. If
  123. there are more CPUs than the max number of available lanes, than lanes are
  124. protected by spinlocks.
  125. d. In-memory data structure: Read Tracking Table (RTT)
  126. ------------------------------------------------------
  127. Consider a case where we have two threads, one doing reads and the other,
  128. writes. We can hit a condition where the writer thread grabs a free block to do
  129. a new IO, but the (slow) reader thread is still reading from it. In other words,
  130. the reader consulted a map entry, and started reading the corresponding block. A
  131. writer started writing to the same external LBA, and finished the write updating
  132. the map for that external LBA to point to its new postmap ABA. At this point the
  133. internal, postmap block that the reader is (still) reading has been inserted
  134. into the list of free blocks. If another write comes in for the same LBA, it can
  135. grab this free block, and start writing to it, causing the reader to read
  136. incorrect data. To prevent this, we introduce the RTT.
  137. The RTT is a simple, per arena table with 'nfree' entries. Every reader inserts
  138. into rtt[lane_number], the postmap ABA it is reading, and clears it after the
  139. read is complete. Every writer thread, after grabbing a free block, checks the
  140. RTT for its presence. If the postmap free block is in the RTT, it waits till the
  141. reader clears the RTT entry, and only then starts writing to it.
  142. e. In-memory data structure: map locks
  143. --------------------------------------
  144. Consider a case where two writer threads are writing to the same LBA. There can
  145. be a race in the following sequence of steps:
  146. free[lane] = map[premap_aba]
  147. map[premap_aba] = postmap_aba
  148. Both threads can update their respective free[lane] with the same old, freed
  149. postmap_aba. This has made the layout inconsistent by losing a free entry, and
  150. at the same time, duplicating another free entry for two lanes.
  151. To solve this, we could have a single map lock (per arena) that has to be taken
  152. before performing the above sequence, but we feel that could be too contentious.
  153. Instead we use an array of (nfree) map_locks that is indexed by
  154. (premap_aba modulo nfree).
  155. f. Reconstruction from the Flog
  156. -------------------------------
  157. On startup, we analyze the BTT flog to create our list of free blocks. We walk
  158. through all the entries, and for each lane, of the set of two possible
  159. 'sections', we always look at the most recent one only (based on the sequence
  160. number). The reconstruction rules/steps are simple:
  161. - Read map[log_entry.lba].
  162. - If log_entry.new matches the map entry, then log_entry.old is free.
  163. - If log_entry.new does not match the map entry, then log_entry.new is free.
  164. (This case can only be caused by power-fails/unsafe shutdowns)
  165. g. Summarizing - Read and Write flows
  166. -------------------------------------
  167. Read:
  168. 1. Convert external LBA to arena number + pre-map ABA
  169. 2. Get a lane (and take lane_lock)
  170. 3. Read map to get the entry for this pre-map ABA
  171. 4. Enter post-map ABA into RTT[lane]
  172. 5. If TRIM flag set in map, return zeroes, and end IO (go to step 8)
  173. 6. If ERROR flag set in map, end IO with EIO (go to step 8)
  174. 7. Read data from this block
  175. 8. Remove post-map ABA entry from RTT[lane]
  176. 9. Release lane (and lane_lock)
  177. Write:
  178. 1. Convert external LBA to Arena number + pre-map ABA
  179. 2. Get a lane (and take lane_lock)
  180. 3. Use lane to index into in-memory free list and obtain a new block, next flog
  181. index, next sequence number
  182. 4. Scan the RTT to check if free block is present, and spin/wait if it is.
  183. 5. Write data to this free block
  184. 6. Read map to get the existing post-map ABA entry for this pre-map ABA
  185. 7. Write flog entry: [premap_aba / old postmap_aba / new postmap_aba / seq_num]
  186. 8. Write new post-map ABA into map.
  187. 9. Write old post-map entry into the free list
  188. 10. Calculate next sequence number and write into the free list entry
  189. 11. Release lane (and lane_lock)
  190. 4. Error Handling
  191. =================
  192. An arena would be in an error state if any of the metadata is corrupted
  193. irrecoverably, either due to a bug or a media error. The following conditions
  194. indicate an error:
  195. - Info block checksum does not match (and recovering from the copy also fails)
  196. - All internal available blocks are not uniquely and entirely addressed by the
  197. sum of mapped blocks and free blocks (from the BTT flog).
  198. - Rebuilding free list from the flog reveals missing/duplicate/impossible
  199. entries
  200. - A map entry is out of bounds
  201. If any of these error conditions are encountered, the arena is put into a read
  202. only state using a flag in the info block.
  203. 5. In-kernel usage
  204. ==================
  205. Any block driver that supports byte granularity IO to the storage may register
  206. with the BTT. It will have to provide the rw_bytes interface in its
  207. block_device_operations struct:
  208. int (*rw_bytes)(struct gendisk *, void *, size_t, off_t, int rw);
  209. It may register with the BTT after it adds its own gendisk, using btt_init:
  210. struct btt *btt_init(struct gendisk *disk, unsigned long long rawsize,
  211. u32 lbasize, u8 uuid[], int maxlane);
  212. note that maxlane is the maximum amount of concurrency the driver wishes to
  213. allow the BTT to use.
  214. The BTT 'disk' appears as a stacked block device that grabs the underlying block
  215. device in the O_EXCL mode.
  216. When the driver wishes to remove the backing disk, it should similarly call
  217. btt_fini using the same struct btt* handle that was provided to it by btt_init.
  218. void btt_fini(struct btt *btt);