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