blob: 2bc0d10b8438f0482bdd0241d697bb17c802d020 [file] [log] [blame]
Dan Williams8c2f7e82015-06-25 04:20:04 -04001/*
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
15#ifndef _LINUX_BTT_H
16#define _LINUX_BTT_H
17
18#include <linux/types.h>
19
20#define BTT_SIG_LEN 16
21#define BTT_SIG "BTT_ARENA_INFO\0"
Vishal Verma5212e112015-06-25 04:20:32 -040022#define MAP_ENT_SIZE 4
23#define MAP_TRIM_SHIFT 31
24#define MAP_TRIM_MASK (1 << MAP_TRIM_SHIFT)
25#define MAP_ERR_SHIFT 30
26#define MAP_ERR_MASK (1 << MAP_ERR_SHIFT)
27#define MAP_LBA_MASK (~((1 << MAP_TRIM_SHIFT) | (1 << MAP_ERR_SHIFT)))
28#define MAP_ENT_NORMAL 0xC0000000
29#define LOG_ENT_SIZE sizeof(struct log_entry)
30#define ARENA_MIN_SIZE (1UL << 24) /* 16 MB */
31#define ARENA_MAX_SIZE (1ULL << 39) /* 512 GB */
32#define RTT_VALID (1UL << 31)
33#define RTT_INVALID 0
Vishal Verma5212e112015-06-25 04:20:32 -040034#define BTT_PG_SIZE 4096
35#define BTT_DEFAULT_NFREE ND_MAX_LANES
36#define LOG_SEQ_INIT 1
37
38#define IB_FLAG_ERROR 0x00000001
39#define IB_FLAG_ERROR_MASK 0x00000001
40
Vishal Verma0595d532017-08-30 19:35:59 -060041#define ent_lba(ent) (ent & MAP_LBA_MASK)
42#define ent_e_flag(ent) (!!(ent & MAP_ERR_MASK))
43#define ent_z_flag(ent) (!!(ent & MAP_TRIM_MASK))
44
Vishal Verma5212e112015-06-25 04:20:32 -040045enum btt_init_state {
46 INIT_UNCHECKED = 0,
47 INIT_NOTFOUND,
48 INIT_READY
49};
50
51struct log_entry {
52 __le32 lba;
53 __le32 old_map;
54 __le32 new_map;
55 __le32 seq;
56 __le64 padding[2];
57};
Dan Williams8c2f7e82015-06-25 04:20:04 -040058
59struct btt_sb {
60 u8 signature[BTT_SIG_LEN];
61 u8 uuid[16];
62 u8 parent_uuid[16];
63 __le32 flags;
64 __le16 version_major;
65 __le16 version_minor;
66 __le32 external_lbasize;
67 __le32 external_nlba;
68 __le32 internal_lbasize;
69 __le32 internal_nlba;
70 __le32 nfree;
71 __le32 infosize;
72 __le64 nextoff;
73 __le64 dataoff;
74 __le64 mapoff;
75 __le64 logoff;
76 __le64 info2off;
77 u8 padding[3968];
78 __le64 checksum;
79};
80
Vishal Verma5212e112015-06-25 04:20:32 -040081struct free_entry {
82 u32 block;
83 u8 sub;
84 u8 seq;
85};
86
87struct aligned_lock {
88 union {
89 spinlock_t lock;
90 u8 cacheline_padding[L1_CACHE_BYTES];
91 };
92};
93
94/**
95 * struct arena_info - handle for an arena
96 * @size: Size in bytes this arena occupies on the raw device.
97 * This includes arena metadata.
98 * @external_lba_start: The first external LBA in this arena.
99 * @internal_nlba: Number of internal blocks available in the arena
100 * including nfree reserved blocks
101 * @internal_lbasize: Internal and external lba sizes may be different as
102 * we can round up 'odd' external lbasizes such as 520B
103 * to be aligned.
104 * @external_nlba: Number of blocks contributed by the arena to the number
105 * reported to upper layers. (internal_nlba - nfree)
106 * @external_lbasize: LBA size as exposed to upper layers.
107 * @nfree: A reserve number of 'free' blocks that is used to
108 * handle incoming writes.
109 * @version_major: Metadata layout version major.
110 * @version_minor: Metadata layout version minor.
Vishal Verma75892002017-08-30 19:36:01 -0600111 * @sector_size: The Linux sector size - 512 or 4096
Vishal Verma5212e112015-06-25 04:20:32 -0400112 * @nextoff: Offset in bytes to the start of the next arena.
113 * @infooff: Offset in bytes to the info block of this arena.
114 * @dataoff: Offset in bytes to the data area of this arena.
115 * @mapoff: Offset in bytes to the map area of this arena.
116 * @logoff: Offset in bytes to the log area of this arena.
117 * @info2off: Offset in bytes to the backup info block of this arena.
118 * @freelist: Pointer to in-memory list of free blocks
119 * @rtt: Pointer to in-memory "Read Tracking Table"
120 * @map_locks: Spinlocks protecting concurrent map writes
121 * @nd_btt: Pointer to parent nd_btt structure.
122 * @list: List head for list of arenas
123 * @debugfs_dir: Debugfs dentry
124 * @flags: Arena flags - may signify error states.
125 *
126 * arena_info is a per-arena handle. Once an arena is narrowed down for an
127 * IO, this struct is passed around for the duration of the IO.
128 */
129struct arena_info {
130 u64 size; /* Total bytes for this arena */
131 u64 external_lba_start;
132 u32 internal_nlba;
133 u32 internal_lbasize;
134 u32 external_nlba;
135 u32 external_lbasize;
136 u32 nfree;
137 u16 version_major;
138 u16 version_minor;
Vishal Verma75892002017-08-30 19:36:01 -0600139 u32 sector_size;
Vishal Verma5212e112015-06-25 04:20:32 -0400140 /* Byte offsets to the different on-media structures */
141 u64 nextoff;
142 u64 infooff;
143 u64 dataoff;
144 u64 mapoff;
145 u64 logoff;
146 u64 info2off;
147 /* Pointers to other in-memory structures for this arena */
148 struct free_entry *freelist;
149 u32 *rtt;
150 struct aligned_lock *map_locks;
151 struct nd_btt *nd_btt;
152 struct list_head list;
153 struct dentry *debugfs_dir;
154 /* Arena flags */
155 u32 flags;
156};
157
158/**
159 * struct btt - handle for a BTT instance
160 * @btt_disk: Pointer to the gendisk for BTT device
161 * @btt_queue: Pointer to the request queue for the BTT device
162 * @arena_list: Head of the list of arenas
163 * @debugfs_dir: Debugfs dentry
164 * @nd_btt: Parent nd_btt struct
165 * @nlba: Number of logical blocks exposed to the upper layers
166 * after removing the amount of space needed by metadata
167 * @rawsize: Total size in bytes of the available backing device
168 * @lbasize: LBA size as requested and presented to upper layers.
169 * This is sector_size + size of any metadata.
170 * @sector_size: The Linux sector size - 512 or 4096
171 * @lanes: Per-lane spinlocks
172 * @init_lock: Mutex used for the BTT initialization
173 * @init_state: Flag describing the initialization state for the BTT
174 * @num_arenas: Number of arenas in the BTT instance
175 */
176struct btt {
177 struct gendisk *btt_disk;
178 struct request_queue *btt_queue;
179 struct list_head arena_list;
180 struct dentry *debugfs_dir;
181 struct nd_btt *nd_btt;
182 u64 nlba;
183 unsigned long long rawsize;
184 u32 lbasize;
185 u32 sector_size;
186 struct nd_region *nd_region;
187 struct mutex init_lock;
188 int init_state;
189 int num_arenas;
190};
Vishal Vermaab45e762015-07-29 14:58:08 -0600191
192bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super);
Vishal Verma14e49452017-06-28 14:25:00 -0600193int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
194 struct btt_sb *btt_sb);
Vishal Vermaab45e762015-07-29 14:58:08 -0600195
Dan Williams8c2f7e82015-06-25 04:20:04 -0400196#endif