-
Notifications
You must be signed in to change notification settings - Fork 21
/
raw_disk_io.cpp
461 lines (389 loc) · 12.8 KB
/
raw_disk_io.cpp
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <string>
#include <sys/mman.h>
#include <boost/assert.hpp>
#include <spdlog/spdlog.h>
#include <libtorrent/hasher.hpp>
#include "raw_disk_io.hpp"
#include "buffer_pool.hpp"
#include "store_buffer.hpp"
namespace ezio
{
class partition_storage
{
private:
// fd to partition.
int fd_{0};
void *mapping_addr_{nullptr};
size_t mapping_len_{0};
libtorrent::file_storage const &fs_;
public:
partition_storage(const std::string &path, libtorrent::file_storage const &fs) :
fs_(fs)
{
fd_ = open(path.c_str(), O_RDWR);
if (fd_ < 0) {
SPDLOG_CRITICAL("failed to open ({}) = {}", path, strerror(fd_));
exit(1);
}
}
~partition_storage()
{
int ec = close(fd_);
if (ec) {
SPDLOG_ERROR("close: {}", strerror(ec));
}
}
int piece_size(libtorrent::piece_index_t const piece)
{
return fs_.piece_size(piece);
}
int read(char *buffer, libtorrent::piece_index_t const piece, int const offset,
int const length, libtorrent::storage_error &error)
{
BOOST_ASSERT(buffer != nullptr);
auto file_slices = fs_.map_block(piece, offset, length);
int ret = 0;
for (const auto &file_slice : file_slices) {
const auto &file_index = file_slice.file_index;
int64_t partition_offset = 0;
// to find partition_offset from file name.
std::string file_name(fs_.file_name(file_index));
try {
partition_offset = std::stoll(file_name, 0, 16);
partition_offset += file_slice.offset;
} catch (const std::exception &e) {
SPDLOG_CRITICAL("failed to parse file_name({}) at ({}): {}",
file_name, static_cast<std::int32_t>(file_index), e.what());
error.file(file_index);
error.ec = libtorrent::errors::parse_failed;
error.operation = libtorrent::operation_t::file_read;
return ret;
}
pread(fd_, buffer, file_slice.size, partition_offset);
ret += file_slice.size;
buffer += file_slice.size;
}
return ret;
}
void write(char *buffer, libtorrent::piece_index_t const piece, int const offset,
int const length, libtorrent::storage_error &error)
{
BOOST_ASSERT(buffer != nullptr);
auto file_slices = fs_.map_block(piece, offset, length);
for (const auto &file_slice : file_slices) {
const auto &file_index = file_slice.file_index;
int64_t partition_offset = 0;
// to find partition_offset from file name.
std::string file_name(fs_.file_name(file_index));
try {
partition_offset = std::stoll(file_name, 0, 16);
partition_offset += file_slice.offset;
} catch (const std::exception &e) {
SPDLOG_CRITICAL("failed to parse file_name({}) at ({}): {}",
file_name, static_cast<std::int32_t>(file_index), e.what());
error.file(file_index);
error.ec = libtorrent::errors::parse_failed;
error.operation = libtorrent::operation_t::file_write;
return;
}
pwrite(fd_, buffer, file_slice.size, partition_offset);
buffer += file_slice.size;
}
}
};
std::unique_ptr<libtorrent::disk_interface> raw_disk_io_constructor(libtorrent::io_context &ioc,
libtorrent::settings_interface const &s,
libtorrent::counters &c)
{
return std::make_unique<raw_disk_io>(ioc);
}
raw_disk_io::raw_disk_io(libtorrent::io_context &ioc) :
ioc_(ioc),
read_buffer_pool_(ioc),
write_buffer_pool_(ioc),
read_thread_pool_(8),
write_thread_pool_(8),
hash_thread_pool_(8)
{
}
raw_disk_io::~raw_disk_io()
{
read_thread_pool_.join();
write_thread_pool_.join();
hash_thread_pool_.join();
}
libtorrent::storage_holder raw_disk_io::new_torrent(libtorrent::storage_params const &p,
std::shared_ptr<void> const &)
{
const std::string &target_partition = p.path;
int idx = storages_.size();
if (!free_slots_.empty()) {
// TODO need a lock
idx = free_slots_.front();
free_slots_.pop_front();
}
auto storage = std::make_unique<partition_storage>(target_partition, p.files);
storages_.emplace(idx, std::move(storage));
if (idx > 0) {
SPDLOG_WARN("new_torrent current idx => {}, should be 0", idx);
}
return libtorrent::storage_holder(idx, *this);
}
void raw_disk_io::remove_torrent(libtorrent::storage_index_t idx)
{
// TODO need a lock
storages_.erase(idx);
free_slots_.push_back(idx);
}
void raw_disk_io::async_read(
libtorrent::storage_index_t idx, libtorrent::peer_request const &r,
std::function<void(libtorrent::disk_buffer_holder, libtorrent::storage_error const &)> handler,
libtorrent::disk_job_flags_t flags)
{
BOOST_ASSERT(DEFAULT_BLOCK_SIZE >= r.length);
libtorrent::storage_error error;
if (r.length <= 0 || r.start < 0) {
error.ec = libtorrent::errors::invalid_request;
error.operation = libtorrent::operation_t::file_read;
handler(libtorrent::disk_buffer_holder{}, error);
return;
}
char *buf = read_buffer_pool_.allocate_buffer();
libtorrent::disk_buffer_holder buffer(read_buffer_pool_, buf, DEFAULT_BLOCK_SIZE);
if (!buf) {
error.ec = libtorrent::errors::no_memory;
error.operation = libtorrent::operation_t::alloc_cache_piece;
handler(libtorrent::disk_buffer_holder{}, error);
return;
}
int const block_offset = r.start - (r.start % DEFAULT_BLOCK_SIZE);
int const read_offset = r.start - block_offset;
// it might be unaligned request, refer libtorrent async_read
if (read_offset + r.length > DEFAULT_BLOCK_SIZE) {
// unaligned
torrent_location const loc1{idx, r.piece, block_offset};
torrent_location const loc2{idx, r.piece, block_offset + DEFAULT_BLOCK_SIZE};
std::ptrdiff_t const len1 = DEFAULT_BLOCK_SIZE - read_offset;
BOOST_ASSERT(r.length > len1);
int const ret = store_buffer_.get2(loc1, loc2, [&](char const *buf1, char const *buf2) {
if (buf1) {
std::memcpy(buf, buf1 + read_offset, std::size_t(len1));
}
if (buf2) {
std::memcpy(buf + len1, buf2, std::size_t(r.length - len1));
}
return (buf1 ? 2 : 0) | (buf2 ? 1 : 0);
});
if (ret == 3) {
// success get whole piece
// return immediately
handler(std::move(buffer), error);
return;
}
if (ret != 0) {
// partial
boost::asio::post(read_thread_pool_,
[=, this, handler = std::move(handler), buffer = std::move(buffer)]() mutable {
libtorrent::storage_error error;
auto offset = (ret == 1) ? r.start : block_offset + DEFAULT_BLOCK_SIZE;
auto len = (ret == 1) ? len1 : r.length - len1;
auto buf_offset = (ret == 1) ? 0 : len1;
storages_[idx]->read(buf + buf_offset, r.piece, offset, len, error);
post(ioc_, [h = std::move(handler), b = std::move(buffer), error]() mutable {
h(std::move(b), error);
});
});
return;
}
// if we cannot find any block, post it as normal job
} else {
// aligned block
if (store_buffer_.get({idx, r.piece, block_offset}, [&](char const *buf1) {
std::memcpy(buf, buf1 + read_offset, std::size_t(r.length));
})) {
handler(std::move(buffer), error);
return;
}
}
boost::asio::post(read_thread_pool_,
[=, this, handler = std::move(handler), buffer = std::move(buffer)]() mutable {
libtorrent::storage_error error;
storages_[idx]->read(buf, r.piece, r.start, r.length, error);
post(ioc_, [h = std::move(handler), b = std::move(buffer), error]() mutable {
h(std::move(b), error);
});
});
}
bool raw_disk_io::async_write(libtorrent::storage_index_t storage, libtorrent::peer_request const &r,
char const *buf, std::shared_ptr<libtorrent::disk_observer> o,
std::function<void(libtorrent::storage_error const &)> handler,
libtorrent::disk_job_flags_t flags)
{
BOOST_ASSERT(DEFAULT_BLOCK_SIZE >= r.length);
bool exceeded = false;
libtorrent::disk_buffer_holder buffer(write_buffer_pool_, write_buffer_pool_.allocate_buffer(exceeded, o), DEFAULT_BLOCK_SIZE);
if (buffer) {
// async
memcpy(buffer.data(), buf, r.length);
store_buffer_.insert({storage, r.piece, r.start}, buffer.data());
libtorrent::peer_request r2(r);
boost::asio::post(write_thread_pool_,
[=, this, handler = std::move(handler), buffer = std::move(buffer)]() {
libtorrent::storage_error error;
storages_[storage]->write(buffer.data(), r.piece, r.start, r.length, error);
store_buffer_.erase({storage, r.piece, r.start});
post(ioc_, [=, h = std::move(handler)] {
h(error);
});
});
return exceeded;
}
// sync
libtorrent::storage_error error;
storages_[storage]->write(const_cast<char *>(buf), r.piece, r.start, r.length, error);
post(ioc_, [=, h = std::move(handler)] {
h(error);
});
return exceeded;
}
void raw_disk_io::async_hash(
libtorrent::storage_index_t storage, libtorrent::piece_index_t piece, libtorrent::span<libtorrent::sha256_hash> v2,
libtorrent::disk_job_flags_t flags,
std::function<void(libtorrent::piece_index_t, libtorrent::sha1_hash const &, libtorrent::storage_error const &)>
handler)
{
libtorrent::storage_error error;
char *buf = read_buffer_pool_.allocate_buffer();
if (!buf) {
error.ec = libtorrent::errors::no_memory;
error.operation = libtorrent::operation_t::alloc_cache_piece;
post(ioc_, [=, h = std::move(handler)] {
h(piece, libtorrent::sha1_hash{}, error);
});
return;
}
auto buffer = libtorrent::disk_buffer_holder(read_buffer_pool_, buf, DEFAULT_BLOCK_SIZE);
boost::asio::post(hash_thread_pool_,
[=, this, handler = std::move(handler), buffer = std::move(buffer)]() {
libtorrent::storage_error error;
libtorrent::hasher ph;
partition_storage *st = storages_[storage].get();
int const piece_size = st->piece_size(piece);
int const blocks_in_piece = (piece_size + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE;
int offset = 0;
int const blocks_to_read = blocks_in_piece;
int len = 0;
int ret = 0;
for (int i = 0; i < blocks_to_read; i++) {
len = std::min(DEFAULT_BLOCK_SIZE, piece_size - offset);
bool hit = store_buffer_.get({storage, piece, offset}, [&](char const *buf1) {
ph.update(buf1, len);
ret = len;
});
if (!hit) {
ret = st->read(buf, piece, offset, len, error);
if (ret > 0) {
ph.update(buf, ret);
}
}
if (ret <= 0) {
break;
}
offset += ret;
}
libtorrent::sha1_hash const hash = ph.final();
post(ioc_, [=, h = std::move(handler)] {
h(piece, hash, error);
});
});
}
void raw_disk_io::async_hash2(
libtorrent::storage_index_t storage, libtorrent::piece_index_t piece, int offset,
libtorrent::disk_job_flags_t flags,
std::function<void(libtorrent::piece_index_t, libtorrent::sha256_hash const &, libtorrent::storage_error const &)>
handler)
{
}
void raw_disk_io::async_move_storage(
libtorrent::storage_index_t storage, std::string p, libtorrent::move_flags_t flags,
std::function<void(libtorrent::status_t, std::string const &, libtorrent::storage_error const &)>
handler)
{
post(ioc_, [=] {
handler(libtorrent::status_t::fatal_disk_error, p,
libtorrent::storage_error(
libtorrent::error_code(boost::system::errc::operation_not_supported, libtorrent::system_category())));
});
}
void raw_disk_io::async_release_files(libtorrent::storage_index_t storage,
std::function<void()> handler)
{
}
void raw_disk_io::async_check_files(
libtorrent::storage_index_t storage, libtorrent::add_torrent_params const *resume_data,
libtorrent::aux::vector<std::string, libtorrent::file_index_t> links,
std::function<void(libtorrent::status_t, libtorrent::storage_error const &)> handler)
{
post(ioc_, [=] {
handler(libtorrent::status_t::no_error, libtorrent::storage_error());
});
}
void raw_disk_io::async_stop_torrent(libtorrent::storage_index_t storage,
std::function<void()> handler)
{
post(ioc_, handler);
}
void raw_disk_io::async_rename_file(
libtorrent::storage_index_t storage, libtorrent::file_index_t index, std::string name,
std::function<void(std::string const &, libtorrent::file_index_t, libtorrent::storage_error const &)>
handler)
{
post(ioc_, [=] {
handler(name, index, libtorrent::storage_error());
});
}
void raw_disk_io::async_delete_files(
libtorrent::storage_index_t storage, libtorrent::remove_flags_t options,
std::function<void(libtorrent::storage_error const &)> handler)
{
post(ioc_, [=] {
handler(libtorrent::storage_error());
});
}
void raw_disk_io::async_set_file_priority(
libtorrent::storage_index_t storage, libtorrent::aux::vector<libtorrent::download_priority_t, libtorrent::file_index_t> prio,
std::function<void(libtorrent::storage_error const &,
libtorrent::aux::vector<libtorrent::download_priority_t, libtorrent::file_index_t>)>
handler)
{
post(ioc_, [=] {
handler(libtorrent::storage_error(libtorrent::error_code(
boost::system::errc::operation_not_supported, libtorrent::system_category())),
std::move(prio));
});
}
void raw_disk_io::async_clear_piece(libtorrent::storage_index_t storage,
libtorrent::piece_index_t index,
std::function<void(libtorrent::piece_index_t)> handler)
{
post(ioc_, [=] {
handler(index);
});
}
void raw_disk_io::update_stats_counters(libtorrent::counters &c) const
{
}
std::vector<libtorrent::open_file_state> raw_disk_io::get_status(libtorrent::storage_index_t) const
{
return {};
}
void raw_disk_io::abort(bool wait)
{
}
void raw_disk_io::submit_jobs()
{
}
void raw_disk_io::settings_updated()
{
}
} // namespace ezio