]> git.proxmox.com Git - ceph.git/blob - ceph/src/os/bluestore/bluefs_types.h
import ceph 14.2.5
[ceph.git] / ceph / src / os / bluestore / bluefs_types.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 #ifndef CEPH_OS_BLUESTORE_BLUEFS_TYPES_H
4 #define CEPH_OS_BLUESTORE_BLUEFS_TYPES_H
5
6 #include "bluestore_types.h"
7 #include "include/utime.h"
8 #include "include/encoding.h"
9 #include "include/denc.h"
10
11 class bluefs_extent_t {
12 public:
13 uint64_t offset = 0;
14 uint32_t length = 0;
15 uint8_t bdev;
16
17 bluefs_extent_t(uint8_t b = 0, uint64_t o = 0, uint32_t l = 0)
18 : offset(o), length(l), bdev(b) {}
19
20 uint64_t end() const { return offset + length; }
21 DENC(bluefs_extent_t, v, p) {
22 DENC_START(1, 1, p);
23 denc_lba(v.offset, p);
24 denc_varint_lowz(v.length, p);
25 denc(v.bdev, p);
26 DENC_FINISH(p);
27 }
28
29 void dump(Formatter *f) const;
30 static void generate_test_instances(list<bluefs_extent_t*>&);
31 };
32 WRITE_CLASS_DENC(bluefs_extent_t)
33
34 ostream& operator<<(ostream& out, const bluefs_extent_t& e);
35
36
37 struct bluefs_fnode_t {
38 uint64_t ino;
39 uint64_t size;
40 utime_t mtime;
41 uint8_t prefer_bdev;
42 mempool::bluefs::vector<bluefs_extent_t> extents;
43
44 // precalculated logical offsets for extents vector entries
45 // allows fast lookup for extent index by the offset value via upper_bound()
46 mempool::bluefs::vector<uint64_t> extents_index;
47
48 uint64_t allocated;
49
50 bluefs_fnode_t() : ino(0), size(0), prefer_bdev(0), allocated(0) {}
51
52 uint64_t get_allocated() const {
53 return allocated;
54 }
55
56 void recalc_allocated() {
57 allocated = 0;
58 extents_index.reserve(extents.size());
59 for (auto& p : extents) {
60 extents_index.emplace_back(allocated);
61 allocated += p.length;
62 }
63 }
64
65 DENC_HELPERS
66 void bound_encode(size_t& p) const {
67 _denc_friend(*this, p);
68 }
69 void encode(bufferlist::contiguous_appender& p) const {
70 DENC_DUMP_PRE(bluefs_fnode_t);
71 _denc_friend(*this, p);
72 DENC_DUMP_POST(bluefs_fnode_t);
73 }
74 void decode(buffer::ptr::const_iterator& p) {
75 _denc_friend(*this, p);
76 recalc_allocated();
77 }
78 template<typename T, typename P>
79 friend std::enable_if_t<std::is_same_v<bluefs_fnode_t, std::remove_const_t<T>>>
80 _denc_friend(T& v, P& p) {
81 DENC_START(1, 1, p);
82 denc_varint(v.ino, p);
83 denc_varint(v.size, p);
84 denc(v.mtime, p);
85 denc(v.prefer_bdev, p);
86 denc(v.extents, p);
87 DENC_FINISH(p);
88 }
89
90 void append_extent(const bluefs_extent_t& ext) {
91 if (!extents.empty() &&
92 extents.back().end() == ext.offset &&
93 extents.back().bdev == ext.bdev &&
94 (uint64_t)extents.back().length + (uint64_t)ext.length < 0xffffffff) {
95 extents.back().length += ext.length;
96 } else {
97 extents_index.emplace_back(allocated);
98 extents.push_back(ext);
99 }
100 allocated += ext.length;
101 }
102
103 void pop_front_extent() {
104 auto it = extents.begin();
105 allocated -= it->length;
106 extents_index.erase(extents_index.begin());
107 for (auto& i: extents_index) {
108 i -= it->length;
109 }
110 extents.erase(it);
111 }
112
113 void swap_extents(bluefs_fnode_t& other) {
114 other.extents.swap(extents);
115 other.extents_index.swap(extents_index);
116 std::swap(allocated, other.allocated);
117 }
118 void clear_extents() {
119 extents_index.clear();
120 extents.clear();
121 allocated = 0;
122 }
123
124 mempool::bluefs::vector<bluefs_extent_t>::iterator seek(
125 uint64_t off, uint64_t *x_off);
126
127 void dump(Formatter *f) const;
128 static void generate_test_instances(list<bluefs_fnode_t*>& ls);
129
130 };
131 WRITE_CLASS_DENC(bluefs_fnode_t)
132
133 ostream& operator<<(ostream& out, const bluefs_fnode_t& file);
134
135
136 struct bluefs_super_t {
137 uuid_d uuid; ///< unique to this bluefs instance
138 uuid_d osd_uuid; ///< matches the osd that owns us
139 uint64_t version;
140 uint32_t block_size;
141
142 bluefs_fnode_t log_fnode;
143
144 bluefs_super_t()
145 : version(0),
146 block_size(4096) { }
147
148 uint64_t block_mask() const {
149 return ~((uint64_t)block_size - 1);
150 }
151
152 void encode(bufferlist& bl) const;
153 void decode(bufferlist::const_iterator& p);
154 void dump(Formatter *f) const;
155 static void generate_test_instances(list<bluefs_super_t*>& ls);
156 };
157 WRITE_CLASS_ENCODER(bluefs_super_t)
158
159 ostream& operator<<(ostream&, const bluefs_super_t& s);
160
161
162 struct bluefs_transaction_t {
163 typedef enum {
164 OP_NONE = 0,
165 OP_INIT, ///< initial (empty) file system marker
166 OP_ALLOC_ADD, ///< add extent to available block storage (extent)
167 OP_ALLOC_RM, ///< remove extent from available block storage (extent)
168 OP_DIR_LINK, ///< (re)set a dir entry (dirname, filename, ino)
169 OP_DIR_UNLINK, ///< remove a dir entry (dirname, filename)
170 OP_DIR_CREATE, ///< create a dir (dirname)
171 OP_DIR_REMOVE, ///< remove a dir (dirname)
172 OP_FILE_UPDATE, ///< set/update file metadata (file)
173 OP_FILE_REMOVE, ///< remove file (ino)
174 OP_JUMP, ///< jump the seq # and offset
175 OP_JUMP_SEQ, ///< jump the seq #
176 } op_t;
177
178 uuid_d uuid; ///< fs uuid
179 uint64_t seq; ///< sequence number
180 bufferlist op_bl; ///< encoded transaction ops
181
182 bluefs_transaction_t() : seq(0) {}
183
184 void clear() {
185 *this = bluefs_transaction_t();
186 }
187 bool empty() const {
188 return op_bl.length() == 0;
189 }
190
191 void op_init() {
192 using ceph::encode;
193 encode((__u8)OP_INIT, op_bl);
194 }
195 void op_alloc_add(uint8_t id, uint64_t offset, uint64_t length) {
196 using ceph::encode;
197 encode((__u8)OP_ALLOC_ADD, op_bl);
198 encode(id, op_bl);
199 encode(offset, op_bl);
200 encode(length, op_bl);
201 }
202 void op_alloc_rm(uint8_t id, uint64_t offset, uint64_t length) {
203 using ceph::encode;
204 encode((__u8)OP_ALLOC_RM, op_bl);
205 encode(id, op_bl);
206 encode(offset, op_bl);
207 encode(length, op_bl);
208 }
209 void op_dir_create(const string& dir) {
210 using ceph::encode;
211 encode((__u8)OP_DIR_CREATE, op_bl);
212 encode(dir, op_bl);
213 }
214 void op_dir_remove(const string& dir) {
215 using ceph::encode;
216 encode((__u8)OP_DIR_REMOVE, op_bl);
217 encode(dir, op_bl);
218 }
219 void op_dir_link(const string& dir, const string& file, uint64_t ino) {
220 using ceph::encode;
221 encode((__u8)OP_DIR_LINK, op_bl);
222 encode(dir, op_bl);
223 encode(file, op_bl);
224 encode(ino, op_bl);
225 }
226 void op_dir_unlink(const string& dir, const string& file) {
227 using ceph::encode;
228 encode((__u8)OP_DIR_UNLINK, op_bl);
229 encode(dir, op_bl);
230 encode(file, op_bl);
231 }
232 void op_file_update(const bluefs_fnode_t& file) {
233 using ceph::encode;
234 encode((__u8)OP_FILE_UPDATE, op_bl);
235 encode(file, op_bl);
236 }
237 void op_file_remove(uint64_t ino) {
238 using ceph::encode;
239 encode((__u8)OP_FILE_REMOVE, op_bl);
240 encode(ino, op_bl);
241 }
242 void op_jump(uint64_t next_seq, uint64_t offset) {
243 using ceph::encode;
244 encode((__u8)OP_JUMP, op_bl);
245 encode(next_seq, op_bl);
246 encode(offset, op_bl);
247 }
248 void op_jump_seq(uint64_t next_seq) {
249 using ceph::encode;
250 encode((__u8)OP_JUMP_SEQ, op_bl);
251 encode(next_seq, op_bl);
252 }
253 void claim_ops(bluefs_transaction_t& from) {
254 op_bl.claim_append(from.op_bl);
255 }
256
257 void encode(bufferlist& bl) const;
258 void decode(bufferlist::const_iterator& p);
259 void dump(Formatter *f) const;
260 static void generate_test_instances(list<bluefs_transaction_t*>& ls);
261 };
262 WRITE_CLASS_ENCODER(bluefs_transaction_t)
263
264 ostream& operator<<(ostream& out, const bluefs_transaction_t& t);
265
266 #endif