1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "crimson/os/seastore/cached_extent.h"
#include "crimson/os/seastore/transaction.h"
#include "crimson/common/log.h"
#include "crimson/os/seastore/btree/fixed_kv_node.h"
#include "crimson/os/seastore/lba_mapping.h"
namespace {
[[maybe_unused]] seastar::logger& logger() {
return crimson::get_logger(ceph_subsys_seastore_tm);
}
}
namespace crimson::os::seastore {
#ifdef DEBUG_CACHED_EXTENT_REF
void intrusive_ptr_add_ref(CachedExtent *ptr)
{
intrusive_ptr_add_ref(
static_cast<boost::intrusive_ref_counter<
CachedExtent,
boost::thread_unsafe_counter>*>(ptr));
logger().debug("intrusive_ptr_add_ref: {}", *ptr);
}
void intrusive_ptr_release(CachedExtent *ptr)
{
logger().debug("intrusive_ptr_release: {}", *ptr);
intrusive_ptr_release(
static_cast<boost::intrusive_ref_counter<
CachedExtent,
boost::thread_unsafe_counter>*>(ptr));
}
#endif
std::ostream &operator<<(std::ostream &out, CachedExtent::extent_state_t state)
{
switch (state) {
case CachedExtent::extent_state_t::INITIAL_WRITE_PENDING:
return out << "INITIAL_WRITE_PENDING";
case CachedExtent::extent_state_t::MUTATION_PENDING:
return out << "MUTATION_PENDING";
case CachedExtent::extent_state_t::CLEAN_PENDING:
return out << "CLEAN_PENDING";
case CachedExtent::extent_state_t::CLEAN:
return out << "CLEAN";
case CachedExtent::extent_state_t::DIRTY:
return out << "DIRTY";
case CachedExtent::extent_state_t::EXIST_CLEAN:
return out << "EXIST_CLEAN";
case CachedExtent::extent_state_t::EXIST_MUTATION_PENDING:
return out << "EXIST_MUTATION_PENDING";
case CachedExtent::extent_state_t::INVALID:
return out << "INVALID";
default:
return out << "UNKNOWN";
}
}
std::ostream &operator<<(std::ostream &out, const CachedExtent &ext)
{
return ext.print(out);
}
CachedExtent::~CachedExtent()
{
if (parent_index) {
assert(is_linked());
parent_index->erase(*this);
}
}
CachedExtent* CachedExtent::get_transactional_view(Transaction &t) {
return get_transactional_view(t.get_trans_id());
}
CachedExtent* CachedExtent::get_transactional_view(transaction_id_t tid) {
auto it = mutation_pendings.find(tid, trans_spec_view_t::cmp_t());
if (it != mutation_pendings.end()) {
return (CachedExtent*)&(*it);
} else {
return this;
}
}
std::ostream &operator<<(std::ostream &out, const parent_tracker_t &tracker) {
return out << "tracker_ptr=" << (void*)&tracker
<< ", parent_ptr=" << (void*)tracker.get_parent().get();
}
std::ostream &ChildableCachedExtent::print_detail(std::ostream &out) const {
if (parent_tracker) {
out << ", parent_tracker(" << *parent_tracker << ")";
} else {
out << ", parent_tracker(nullptr)";
}
_print_detail(out);
return out;
}
std::ostream &LogicalCachedExtent::_print_detail(std::ostream &out) const
{
out << ", laddr=" << laddr;
return print_detail_l(out);
}
void child_pos_t::link_child(ChildableCachedExtent *c) {
get_parent<FixedKVNode<laddr_t>>()->link_child(c, pos);
}
void CachedExtent::set_invalid(Transaction &t) {
state = extent_state_t::INVALID;
if (trans_view_hook.is_linked()) {
trans_view_hook.unlink();
}
on_invalidated(t);
}
LogicalCachedExtent::~LogicalCachedExtent() {
if (has_parent_tracker() && is_valid() && !is_pending()) {
assert(get_parent_node());
auto parent = get_parent_node<FixedKVNode<laddr_t>>();
auto off = parent->lower_bound_offset(laddr);
assert(parent->get_key_from_idx(off) == laddr);
assert(parent->children[off] == this);
parent->children[off] = nullptr;
}
}
void LogicalCachedExtent::on_replace_prior() {
assert(is_mutation_pending());
take_prior_parent_tracker();
assert(get_parent_node());
auto parent = get_parent_node<FixedKVNode<laddr_t>>();
//TODO: can this search be avoided?
auto off = parent->lower_bound_offset(laddr);
assert(parent->get_key_from_idx(off) == laddr);
parent->children[off] = this;
}
void LogicalCachedExtent::maybe_set_intermediate_laddr(LBAMapping &mapping) {
laddr = mapping.is_indirect()
? mapping.get_intermediate_base()
: mapping.get_key();
}
parent_tracker_t::~parent_tracker_t() {
// this is parent's tracker, reset it
auto &p = (FixedKVNode<laddr_t>&)*parent;
if (p.my_tracker == this) {
p.my_tracker = nullptr;
}
}
bool BufferSpace::is_range_loaded(extent_len_t offset, extent_len_t length) const
{
assert(length > 0);
auto i = buffer_map.upper_bound(offset);
if (i == buffer_map.begin()) {
return false;
}
--i;
auto& [i_offset, i_bl] = *i;
assert(offset >= i_offset);
assert(i_bl.length() > 0);
if (offset + length > i_offset + i_bl.length()) {
return false;
} else {
return true;
}
}
ceph::bufferlist BufferSpace::get_buffer(extent_len_t offset, extent_len_t length) const
{
assert(length > 0);
auto i = buffer_map.upper_bound(offset);
assert(i != buffer_map.begin());
--i;
auto& [i_offset, i_bl] = *i;
assert(offset >= i_offset);
assert(i_bl.length() > 0);
assert(offset + length <= i_offset + i_bl.length());
ceph::bufferlist res;
res.substr_of(i_bl, offset - i_offset, length);
return res;
}
load_ranges_t BufferSpace::load_ranges(extent_len_t offset, extent_len_t length)
{
assert(length > 0);
load_ranges_t ret;
auto next = buffer_map.upper_bound(offset);
// must be assigned for the main-loop
map_t::iterator previous;
extent_len_t range_offset;
extent_len_t range_length;
// returns whether to proceed main-loop or not
auto f_merge_next_check_hole = [this, &next, &range_offset, &range_length](
ceph::bufferlist& previous_bl,
extent_len_t hole_length,
extent_len_t next_offset,
const ceph::bufferlist& next_bl) {
range_length -= hole_length;
previous_bl.append(next_bl);
if (range_length <= next_bl.length()) {
// "next" end includes or beyonds the range
buffer_map.erase(next);
return false;
} else {
range_offset = next_offset + next_bl.length();
range_length -= next_bl.length();
// erase next should destruct next_bl
next = buffer_map.erase(next);
return true;
}
};
// returns whether to proceed main-loop or not
auto f_prepare_without_merge_previous = [
this, offset, length,
&ret, &previous, &next, &range_length,
&f_merge_next_check_hole]() {
if (next == buffer_map.end()) {
// "next" reaches end,
// range has no "next" to merge
create_hole_insert_map(ret, offset, length, next);
return false;
}
// "next" is valid
auto& [n_offset, n_bl] = *next;
// next is from upper_bound()
assert(offset < n_offset);
extent_len_t hole_length = n_offset - offset;
if (length < hole_length) {
// "next" is beyond the range end,
// range has no "next" to merge
create_hole_insert_map(ret, offset, length, next);
return false;
}
// length >= hole_length
// insert hole as "previous"
previous = create_hole_insert_map(ret, offset, hole_length, next);
auto& p_bl = previous->second;
range_length = length;
return f_merge_next_check_hole(p_bl, hole_length, n_offset, n_bl);
};
/*
* prepare main-loop
*/
if (next == buffer_map.begin()) {
// "previous" is invalid
if (!f_prepare_without_merge_previous()) {
return ret;
}
} else {
// "previous" is valid
previous = std::prev(next);
auto& [p_offset, p_bl] = *previous;
assert(offset >= p_offset);
extent_len_t p_end = p_offset + p_bl.length();
if (offset <= p_end) {
// "previous" is adjacent or overlaps the range
range_offset = p_end;
assert(offset + length > p_end);
range_length = offset + length - p_end;
// start the main-loop (merge "previous")
} else {
// "previous" is not adjacent to the range
// range and buffer_map should not overlap
assert(offset > p_end);
if (!f_prepare_without_merge_previous()) {
return ret;
}
}
}
/*
* main-loop: merge the range with "previous" and look at "next"
*
* "previous": the previous buffer_map entry, must be valid, must be mergable
* "next": the next buffer_map entry, maybe end, maybe mergable
* range_offset/length: the current range right after "previous"
*/
assert(std::next(previous) == next);
auto& [p_offset, p_bl] = *previous;
assert(range_offset == p_offset + p_bl.length());
assert(range_length > 0);
while (next != buffer_map.end()) {
auto& [n_offset, n_bl] = *next;
assert(range_offset < n_offset);
extent_len_t hole_length = n_offset - range_offset;
if (range_length < hole_length) {
// "next" offset is beyond the range end
break;
}
// range_length >= hole_length
create_hole_append_bl(ret, p_bl, range_offset, hole_length);
if (!f_merge_next_check_hole(p_bl, hole_length, n_offset, n_bl)) {
return ret;
}
assert(std::next(previous) == next);
assert(range_offset == p_offset + p_bl.length());
assert(range_length > 0);
}
// range has no "next" to merge:
// 1. "next" reaches end
// 2. "next" offset is beyond the range end
create_hole_append_bl(ret, p_bl, range_offset, range_length);
return ret;
}
ceph::bufferptr BufferSpace::to_full_ptr(extent_len_t length)
{
assert(length > 0);
assert(buffer_map.size() == 1);
auto it = buffer_map.begin();
auto& [i_off, i_buf] = *it;
assert(i_off == 0);
if (!i_buf.is_contiguous()) {
// Allocate page aligned ptr, also see create_extent_ptr_*()
i_buf.rebuild();
}
assert(i_buf.get_num_buffers() == 1);
ceph::bufferptr ptr(i_buf.front());
assert(ptr.is_page_aligned());
assert(ptr.length() == length);
buffer_map.clear();
return ptr;
}
}
|