libnvdimm, btt: ensure that flags were also unchanged during a map_read
[linux-2.6-block.git] / drivers / nvdimm / btt.h
CommitLineData
8c2f7e86
DW
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
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"
5212e11f
VV
22#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
5212e11f
VV
34#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
0595d539
VV
41#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
5212e11f
VV
45enum 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};
8c2f7e86
DW
58
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
5212e11f
VV
81struct 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.
111 * @nextoff: Offset in bytes to the start of the next arena.
112 * @infooff: Offset in bytes to the info block of this arena.
113 * @dataoff: Offset in bytes to the data area of this arena.
114 * @mapoff: Offset in bytes to the map area of this arena.
115 * @logoff: Offset in bytes to the log area of this arena.
116 * @info2off: Offset in bytes to the backup info block of this arena.
117 * @freelist: Pointer to in-memory list of free blocks
118 * @rtt: Pointer to in-memory "Read Tracking Table"
119 * @map_locks: Spinlocks protecting concurrent map writes
120 * @nd_btt: Pointer to parent nd_btt structure.
121 * @list: List head for list of arenas
122 * @debugfs_dir: Debugfs dentry
123 * @flags: Arena flags - may signify error states.
124 *
125 * arena_info is a per-arena handle. Once an arena is narrowed down for an
126 * IO, this struct is passed around for the duration of the IO.
127 */
128struct arena_info {
129 u64 size; /* Total bytes for this arena */
130 u64 external_lba_start;
131 u32 internal_nlba;
132 u32 internal_lbasize;
133 u32 external_nlba;
134 u32 external_lbasize;
135 u32 nfree;
136 u16 version_major;
137 u16 version_minor;
138 /* Byte offsets to the different on-media structures */
139 u64 nextoff;
140 u64 infooff;
141 u64 dataoff;
142 u64 mapoff;
143 u64 logoff;
144 u64 info2off;
145 /* Pointers to other in-memory structures for this arena */
146 struct free_entry *freelist;
147 u32 *rtt;
148 struct aligned_lock *map_locks;
149 struct nd_btt *nd_btt;
150 struct list_head list;
151 struct dentry *debugfs_dir;
152 /* Arena flags */
153 u32 flags;
154};
155
156/**
157 * struct btt - handle for a BTT instance
158 * @btt_disk: Pointer to the gendisk for BTT device
159 * @btt_queue: Pointer to the request queue for the BTT device
160 * @arena_list: Head of the list of arenas
161 * @debugfs_dir: Debugfs dentry
162 * @nd_btt: Parent nd_btt struct
163 * @nlba: Number of logical blocks exposed to the upper layers
164 * after removing the amount of space needed by metadata
165 * @rawsize: Total size in bytes of the available backing device
166 * @lbasize: LBA size as requested and presented to upper layers.
167 * This is sector_size + size of any metadata.
168 * @sector_size: The Linux sector size - 512 or 4096
169 * @lanes: Per-lane spinlocks
170 * @init_lock: Mutex used for the BTT initialization
171 * @init_state: Flag describing the initialization state for the BTT
172 * @num_arenas: Number of arenas in the BTT instance
173 */
174struct btt {
175 struct gendisk *btt_disk;
176 struct request_queue *btt_queue;
177 struct list_head arena_list;
178 struct dentry *debugfs_dir;
179 struct nd_btt *nd_btt;
180 u64 nlba;
181 unsigned long long rawsize;
182 u32 lbasize;
183 u32 sector_size;
184 struct nd_region *nd_region;
185 struct mutex init_lock;
186 int init_state;
187 int num_arenas;
188};
ab45e763
VV
189
190bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super);
14e49454
VV
191int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
192 struct btt_sb *btt_sb);
ab45e763 193
8c2f7e86 194#endif