]> git.proxmox.com Git - ceph.git/blob - ceph/src/os/bluestore/bluefs_types.h
import 14.2.4 nautilus point release
[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 (uint64_t)extents.back().length + (uint64_t)ext.length < 0xffffffff) {
94 extents.back().length += ext.length;
95 } else {
96 extents_index.emplace_back(allocated);
97 extents.push_back(ext);
98 }
99 allocated += ext.length;
100 }
101
102 void pop_front_extent() {
103 auto it = extents.begin();
104 allocated -= it->length;
105 extents_index.erase(extents_index.begin());
106 for (auto& i: extents_index) {
107 i -= it->length;
108 }
109 extents.erase(it);
110 }
111
112 void swap_extents(bluefs_fnode_t& other) {
113 other.extents.swap(extents);
114 other.extents_index.swap(extents_index);
115 std::swap(allocated, other.allocated);
116 }
117 void clear_extents() {
118 extents_index.clear();
119 extents.clear();
120 allocated = 0;
121 }
122
123 mempool::bluefs::vector<bluefs_extent_t>::iterator seek(
124 uint64_t off, uint64_t *x_off);
125
126 void dump(Formatter *f) const;
127 static void generate_test_instances(list<bluefs_fnode_t*>& ls);
128
129 };
130 WRITE_CLASS_DENC(bluefs_fnode_t)
131
132 ostream& operator<<(ostream& out, const bluefs_fnode_t& file);
133
134
135 struct bluefs_super_t {
136 uuid_d uuid; ///< unique to this bluefs instance
137 uuid_d osd_uuid; ///< matches the osd that owns us
138 uint64_t version;
139 uint32_t block_size;
140
141 bluefs_fnode_t log_fnode;
142
143 bluefs_super_t()
144 : version(0),
145 block_size(4096) { }
146
147 uint64_t block_mask() const {
148 return ~((uint64_t)block_size - 1);
149 }
150
151 void encode(bufferlist& bl) const;
152 void decode(bufferlist::const_iterator& p);
153 void dump(Formatter *f) const;
154 static void generate_test_instances(list<bluefs_super_t*>& ls);
155 };
156 WRITE_CLASS_ENCODER(bluefs_super_t)
157
158 ostream& operator<<(ostream&, const bluefs_super_t& s);
159
160
161 struct bluefs_transaction_t {
162 typedef enum {
163 OP_NONE = 0,
164 OP_INIT, ///< initial (empty) file system marker
165 OP_ALLOC_ADD, ///< add extent to available block storage (extent)
166 OP_ALLOC_RM, ///< remove extent from available block storage (extent)
167 OP_DIR_LINK, ///< (re)set a dir entry (dirname, filename, ino)
168 OP_DIR_UNLINK, ///< remove a dir entry (dirname, filename)
169 OP_DIR_CREATE, ///< create a dir (dirname)
170 OP_DIR_REMOVE, ///< remove a dir (dirname)
171 OP_FILE_UPDATE, ///< set/update file metadata (file)
172 OP_FILE_REMOVE, ///< remove file (ino)
173 OP_JUMP, ///< jump the seq # and offset
174 OP_JUMP_SEQ, ///< jump the seq #
175 } op_t;
176
177 uuid_d uuid; ///< fs uuid
178 uint64_t seq; ///< sequence number
179 bufferlist op_bl; ///< encoded transaction ops
180
181 bluefs_transaction_t() : seq(0) {}
182
183 void clear() {
184 *this = bluefs_transaction_t();
185 }
186 bool empty() const {
187 return op_bl.length() == 0;
188 }
189
190 void op_init() {
191 using ceph::encode;
192 encode((__u8)OP_INIT, op_bl);
193 }
194 void op_alloc_add(uint8_t id, uint64_t offset, uint64_t length) {
195 using ceph::encode;
196 encode((__u8)OP_ALLOC_ADD, op_bl);
197 encode(id, op_bl);
198 encode(offset, op_bl);
199 encode(length, op_bl);
200 }
201 void op_alloc_rm(uint8_t id, uint64_t offset, uint64_t length) {
202 using ceph::encode;
203 encode((__u8)OP_ALLOC_RM, op_bl);
204 encode(id, op_bl);
205 encode(offset, op_bl);
206 encode(length, op_bl);
207 }
208 void op_dir_create(const string& dir) {
209 using ceph::encode;
210 encode((__u8)OP_DIR_CREATE, op_bl);
211 encode(dir, op_bl);
212 }
213 void op_dir_remove(const string& dir) {
214 using ceph::encode;
215 encode((__u8)OP_DIR_REMOVE, op_bl);
216 encode(dir, op_bl);
217 }
218 void op_dir_link(const string& dir, const string& file, uint64_t ino) {
219 using ceph::encode;
220 encode((__u8)OP_DIR_LINK, op_bl);
221 encode(dir, op_bl);
222 encode(file, op_bl);
223 encode(ino, op_bl);
224 }
225 void op_dir_unlink(const string& dir, const string& file) {
226 using ceph::encode;
227 encode((__u8)OP_DIR_UNLINK, op_bl);
228 encode(dir, op_bl);
229 encode(file, op_bl);
230 }
231 void op_file_update(const bluefs_fnode_t& file) {
232 using ceph::encode;
233 encode((__u8)OP_FILE_UPDATE, op_bl);
234 encode(file, op_bl);
235 }
236 void op_file_remove(uint64_t ino) {
237 using ceph::encode;
238 encode((__u8)OP_FILE_REMOVE, op_bl);
239 encode(ino, op_bl);
240 }
241 void op_jump(uint64_t next_seq, uint64_t offset) {
242 using ceph::encode;
243 encode((__u8)OP_JUMP, op_bl);
244 encode(next_seq, op_bl);
245 encode(offset, op_bl);
246 }
247 void op_jump_seq(uint64_t next_seq) {
248 using ceph::encode;
249 encode((__u8)OP_JUMP_SEQ, op_bl);
250 encode(next_seq, op_bl);
251 }
252 void claim_ops(bluefs_transaction_t& from) {
253 op_bl.claim_append(from.op_bl);
254 }
255
256 void encode(bufferlist& bl) const;
257 void decode(bufferlist::const_iterator& p);
258 void dump(Formatter *f) const;
259 static void generate_test_instances(list<bluefs_transaction_t*>& ls);
260 };
261 WRITE_CLASS_ENCODER(bluefs_transaction_t)
262
263 ostream& operator<<(ostream& out, const bluefs_transaction_t& t);
264
265 #endif