btt.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_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. #define ent_lba(ent) (ent & MAP_LBA_MASK)
  38. #define ent_e_flag(ent) (!!(ent & MAP_ERR_MASK))
  39. #define ent_z_flag(ent) (!!(ent & MAP_TRIM_MASK))
  40. #define set_e_flag(ent) (ent |= MAP_ERR_MASK)
  41. enum btt_init_state {
  42. INIT_UNCHECKED = 0,
  43. INIT_NOTFOUND,
  44. INIT_READY
  45. };
  46. struct log_entry {
  47. __le32 lba;
  48. __le32 old_map;
  49. __le32 new_map;
  50. __le32 seq;
  51. __le64 padding[2];
  52. };
  53. struct btt_sb {
  54. u8 signature[BTT_SIG_LEN];
  55. u8 uuid[16];
  56. u8 parent_uuid[16];
  57. __le32 flags;
  58. __le16 version_major;
  59. __le16 version_minor;
  60. __le32 external_lbasize;
  61. __le32 external_nlba;
  62. __le32 internal_lbasize;
  63. __le32 internal_nlba;
  64. __le32 nfree;
  65. __le32 infosize;
  66. __le64 nextoff;
  67. __le64 dataoff;
  68. __le64 mapoff;
  69. __le64 logoff;
  70. __le64 info2off;
  71. u8 padding[3968];
  72. __le64 checksum;
  73. };
  74. struct free_entry {
  75. u32 block;
  76. u8 sub;
  77. u8 seq;
  78. u8 has_err;
  79. };
  80. struct aligned_lock {
  81. union {
  82. spinlock_t lock;
  83. u8 cacheline_padding[L1_CACHE_BYTES];
  84. };
  85. };
  86. /**
  87. * struct arena_info - handle for an arena
  88. * @size: Size in bytes this arena occupies on the raw device.
  89. * This includes arena metadata.
  90. * @external_lba_start: The first external LBA in this arena.
  91. * @internal_nlba: Number of internal blocks available in the arena
  92. * including nfree reserved blocks
  93. * @internal_lbasize: Internal and external lba sizes may be different as
  94. * we can round up 'odd' external lbasizes such as 520B
  95. * to be aligned.
  96. * @external_nlba: Number of blocks contributed by the arena to the number
  97. * reported to upper layers. (internal_nlba - nfree)
  98. * @external_lbasize: LBA size as exposed to upper layers.
  99. * @nfree: A reserve number of 'free' blocks that is used to
  100. * handle incoming writes.
  101. * @version_major: Metadata layout version major.
  102. * @version_minor: Metadata layout version minor.
  103. * @sector_size: The Linux sector size - 512 or 4096
  104. * @nextoff: Offset in bytes to the start of the next arena.
  105. * @infooff: Offset in bytes to the info block of this arena.
  106. * @dataoff: Offset in bytes to the data area of this arena.
  107. * @mapoff: Offset in bytes to the map area of this arena.
  108. * @logoff: Offset in bytes to the log area of this arena.
  109. * @info2off: Offset in bytes to the backup info block of this arena.
  110. * @freelist: Pointer to in-memory list of free blocks
  111. * @rtt: Pointer to in-memory "Read Tracking Table"
  112. * @map_locks: Spinlocks protecting concurrent map writes
  113. * @nd_btt: Pointer to parent nd_btt structure.
  114. * @list: List head for list of arenas
  115. * @debugfs_dir: Debugfs dentry
  116. * @flags: Arena flags - may signify error states.
  117. *
  118. * arena_info is a per-arena handle. Once an arena is narrowed down for an
  119. * IO, this struct is passed around for the duration of the IO.
  120. */
  121. struct arena_info {
  122. u64 size; /* Total bytes for this arena */
  123. u64 external_lba_start;
  124. u32 internal_nlba;
  125. u32 internal_lbasize;
  126. u32 external_nlba;
  127. u32 external_lbasize;
  128. u32 nfree;
  129. u16 version_major;
  130. u16 version_minor;
  131. u32 sector_size;
  132. /* Byte offsets to the different on-media structures */
  133. u64 nextoff;
  134. u64 infooff;
  135. u64 dataoff;
  136. u64 mapoff;
  137. u64 logoff;
  138. u64 info2off;
  139. /* Pointers to other in-memory structures for this arena */
  140. struct free_entry *freelist;
  141. u32 *rtt;
  142. struct aligned_lock *map_locks;
  143. struct nd_btt *nd_btt;
  144. struct list_head list;
  145. struct dentry *debugfs_dir;
  146. /* Arena flags */
  147. u32 flags;
  148. struct mutex err_lock;
  149. };
  150. /**
  151. * struct btt - handle for a BTT instance
  152. * @btt_disk: Pointer to the gendisk for BTT device
  153. * @btt_queue: Pointer to the request queue for the BTT device
  154. * @arena_list: Head of the list of arenas
  155. * @debugfs_dir: Debugfs dentry
  156. * @nd_btt: Parent nd_btt struct
  157. * @nlba: Number of logical blocks exposed to the upper layers
  158. * after removing the amount of space needed by metadata
  159. * @rawsize: Total size in bytes of the available backing device
  160. * @lbasize: LBA size as requested and presented to upper layers.
  161. * This is sector_size + size of any metadata.
  162. * @sector_size: The Linux sector size - 512 or 4096
  163. * @lanes: Per-lane spinlocks
  164. * @init_lock: Mutex used for the BTT initialization
  165. * @init_state: Flag describing the initialization state for the BTT
  166. * @num_arenas: Number of arenas in the BTT instance
  167. */
  168. struct btt {
  169. struct gendisk *btt_disk;
  170. struct request_queue *btt_queue;
  171. struct list_head arena_list;
  172. struct dentry *debugfs_dir;
  173. struct nd_btt *nd_btt;
  174. u64 nlba;
  175. unsigned long long rawsize;
  176. u32 lbasize;
  177. u32 sector_size;
  178. struct nd_region *nd_region;
  179. struct mutex init_lock;
  180. int init_state;
  181. int num_arenas;
  182. struct badblocks *phys_bb;
  183. };
  184. bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super);
  185. int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
  186. struct btt_sb *btt_sb);
  187. #endif