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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_LIBRBD_IO_IMAGE_DISPATCH_SPEC_H
#define CEPH_LIBRBD_IO_IMAGE_DISPATCH_SPEC_H
#include "include/int_types.h"
#include "include/buffer.h"
#include "include/Context.h"
#include "common/zipkin_trace.h"
#include "librbd/io/AioCompletion.h"
#include "librbd/io/Types.h"
#include "librbd/io/ReadResult.h"
#include <boost/variant/variant.hpp>
#include <atomic>
namespace librbd {
class ImageCtx;
namespace io {
struct ImageDispatcherInterface;
class ImageDispatchSpec {
private:
// helper to avoid extra heap allocation per object IO
struct C_Dispatcher : public Context {
ImageDispatchSpec* image_dispatch_spec;
C_Dispatcher(ImageDispatchSpec* image_dispatch_spec)
: image_dispatch_spec(image_dispatch_spec) {
}
void complete(int r) override;
void finish(int r) override;
};
public:
struct Read {
ReadResult read_result;
int read_flags;
Read(ReadResult &&read_result, int read_flags)
: read_result(std::move(read_result)), read_flags(read_flags) {
}
};
struct Discard {
uint32_t discard_granularity_bytes;
Discard(uint32_t discard_granularity_bytes)
: discard_granularity_bytes(discard_granularity_bytes) {
}
};
struct Write {
bufferlist bl;
Write(bufferlist&& bl) : bl(std::move(bl)) {
}
};
struct WriteSame {
bufferlist bl;
WriteSame(bufferlist&& bl) : bl(std::move(bl)) {
}
};
struct CompareAndWrite {
bufferlist cmp_bl;
bufferlist bl;
uint64_t *mismatch_offset;
CompareAndWrite(bufferlist&& cmp_bl, bufferlist&& bl,
uint64_t *mismatch_offset)
: cmp_bl(std::move(cmp_bl)), bl(std::move(bl)),
mismatch_offset(mismatch_offset) {
}
};
struct Flush {
FlushSource flush_source;
Flush(FlushSource flush_source) : flush_source(flush_source) {
}
};
struct ListSnaps {
SnapIds snap_ids;
int list_snaps_flags;
SnapshotDelta* snapshot_delta;
ListSnaps(SnapIds&& snap_ids, int list_snaps_flags,
SnapshotDelta* snapshot_delta)
: snap_ids(std::move(snap_ids)), list_snaps_flags(list_snaps_flags),
snapshot_delta(snapshot_delta) {
}
};
typedef boost::variant<Read,
Discard,
Write,
WriteSame,
CompareAndWrite,
Flush,
ListSnaps> Request;
C_Dispatcher dispatcher_ctx;
ImageDispatcherInterface* image_dispatcher;
ImageDispatchLayer dispatch_layer;
std::atomic<uint32_t> image_dispatch_flags = 0;
DispatchResult dispatch_result = DISPATCH_RESULT_INVALID;
AioCompletion* aio_comp;
Extents image_extents;
Request request;
IOContext io_context;
int op_flags;
ZTracer::Trace parent_trace;
uint64_t tid = 0;
template <typename ImageCtxT = ImageCtx>
static ImageDispatchSpec* create_read(
ImageCtxT &image_ctx, ImageDispatchLayer image_dispatch_layer,
AioCompletion *aio_comp, Extents &&image_extents, ImageArea area,
ReadResult &&read_result, IOContext io_context, int op_flags,
int read_flags, const ZTracer::Trace &parent_trace) {
return new ImageDispatchSpec(image_ctx.io_image_dispatcher,
image_dispatch_layer, aio_comp,
std::move(image_extents), area,
Read{std::move(read_result), read_flags},
io_context, op_flags, parent_trace);
}
template <typename ImageCtxT = ImageCtx>
static ImageDispatchSpec* create_discard(
ImageCtxT &image_ctx, ImageDispatchLayer image_dispatch_layer,
AioCompletion *aio_comp, Extents &&image_extents, ImageArea area,
uint32_t discard_granularity_bytes, const ZTracer::Trace &parent_trace) {
return new ImageDispatchSpec(image_ctx.io_image_dispatcher,
image_dispatch_layer, aio_comp,
std::move(image_extents), area,
Discard{discard_granularity_bytes},
{}, 0, parent_trace);
}
template <typename ImageCtxT = ImageCtx>
static ImageDispatchSpec* create_write(
ImageCtxT &image_ctx, ImageDispatchLayer image_dispatch_layer,
AioCompletion *aio_comp, Extents &&image_extents, ImageArea area,
bufferlist &&bl, int op_flags, const ZTracer::Trace &parent_trace) {
return new ImageDispatchSpec(image_ctx.io_image_dispatcher,
image_dispatch_layer, aio_comp,
std::move(image_extents), area,
Write{std::move(bl)},
{}, op_flags, parent_trace);
}
template <typename ImageCtxT = ImageCtx>
static ImageDispatchSpec* create_write_same(
ImageCtxT &image_ctx, ImageDispatchLayer image_dispatch_layer,
AioCompletion *aio_comp, Extents &&image_extents, ImageArea area,
bufferlist &&bl, int op_flags, const ZTracer::Trace &parent_trace) {
return new ImageDispatchSpec(image_ctx.io_image_dispatcher,
image_dispatch_layer, aio_comp,
std::move(image_extents), area,
WriteSame{std::move(bl)},
{}, op_flags, parent_trace);
}
template <typename ImageCtxT = ImageCtx>
static ImageDispatchSpec* create_compare_and_write(
ImageCtxT &image_ctx, ImageDispatchLayer image_dispatch_layer,
AioCompletion *aio_comp, Extents &&image_extents, ImageArea area,
bufferlist &&cmp_bl, bufferlist &&bl, uint64_t *mismatch_offset,
int op_flags, const ZTracer::Trace &parent_trace) {
return new ImageDispatchSpec(image_ctx.io_image_dispatcher,
image_dispatch_layer, aio_comp,
std::move(image_extents), area,
CompareAndWrite{std::move(cmp_bl),
std::move(bl),
mismatch_offset},
{}, op_flags, parent_trace);
}
template <typename ImageCtxT = ImageCtx>
static ImageDispatchSpec* create_flush(
ImageCtxT &image_ctx, ImageDispatchLayer image_dispatch_layer,
AioCompletion *aio_comp, FlushSource flush_source,
const ZTracer::Trace &parent_trace) {
return new ImageDispatchSpec(image_ctx.io_image_dispatcher,
image_dispatch_layer, aio_comp, {},
ImageArea::DATA /* dummy for {} */,
Flush{flush_source}, {}, 0, parent_trace);
}
template <typename ImageCtxT = ImageCtx>
static ImageDispatchSpec* create_list_snaps(
ImageCtxT &image_ctx, ImageDispatchLayer image_dispatch_layer,
AioCompletion *aio_comp, Extents &&image_extents, ImageArea area,
SnapIds&& snap_ids, int list_snaps_flags, SnapshotDelta* snapshot_delta,
const ZTracer::Trace &parent_trace) {
return new ImageDispatchSpec(image_ctx.io_image_dispatcher,
image_dispatch_layer, aio_comp,
std::move(image_extents), area,
ListSnaps{std::move(snap_ids),
list_snaps_flags, snapshot_delta},
{}, 0, parent_trace);
}
~ImageDispatchSpec() {
aio_comp->put();
}
void send();
void fail(int r);
private:
struct SendVisitor;
struct IsWriteOpVisitor;
struct TokenRequestedVisitor;
ImageDispatchSpec(ImageDispatcherInterface* image_dispatcher,
ImageDispatchLayer image_dispatch_layer,
AioCompletion* aio_comp, Extents&& image_extents,
ImageArea area, Request&& request, IOContext io_context,
int op_flags, const ZTracer::Trace& parent_trace)
: dispatcher_ctx(this), image_dispatcher(image_dispatcher),
dispatch_layer(image_dispatch_layer), aio_comp(aio_comp),
image_extents(std::move(image_extents)), request(std::move(request)),
io_context(io_context), op_flags(op_flags), parent_trace(parent_trace) {
ceph_assert(aio_comp->image_dispatcher_ctx == nullptr);
aio_comp->image_dispatcher_ctx = &dispatcher_ctx;
aio_comp->get();
switch (area) {
case ImageArea::DATA:
break;
case ImageArea::CRYPTO_HEADER:
image_dispatch_flags |= IMAGE_DISPATCH_FLAG_CRYPTO_HEADER;
break;
default:
ceph_abort();
}
}
};
} // namespace io
} // namespace librbd
#endif // CEPH_LIBRBD_IO_IMAGE_DISPATCH_SPEC_H
|