-
Notifications
You must be signed in to change notification settings - Fork 9
/
plotfs.hpp
485 lines (439 loc) · 18.2 KB
/
plotfs.hpp
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#pragma once
// local headers
#include "device.hpp"
#include "file.hpp"
#include "plot.hpp"
#include "plotfs_generated.h"
#include <pwd.h>
#include <sys/sendfile.h>
#include <random>
static const auto default_config_path = std::string("/var/local/plotfs/plotfs.bin");
static bool operator==(const flatbuffers::Vector<uint8_t>& a, const std::vector<uint8_t>& b)
{
return std::equal(a.begin(), a.end(), b.begin(), b.end());
}
static bool operator==(const flatbuffers::Vector<uint8_t>& a, const flatbuffers::Vector<uint8_t>& b)
{
return std::equal(a.begin(), a.end(), b.begin(), b.end());
}
std::string to_string(const std::vector<uint8_t>& data)
{
std::stringstream ss;
ss << std::hex;
for (auto c : data) {
ss << std::setw(2) << std::setfill('0') << (int)c;
}
return ss.str();
}
std::string to_string(const flatbuffers::Vector<uint8_t>& data)
{
std::stringstream ss;
ss << std::hex;
for (auto c : data) {
ss << std::setw(2) << std::setfill('0') << (int)c;
}
return ss.str();
}
const static int recovery_point_size = 64; // DONT MODIFY THIS VALUE
std::array<uint8_t, recovery_point_size> get_recovery_point(uint64_t size, const std::vector<uint8_t>& next_device_id = std::vector<uint8_t>(), uint64_t next_device_offset = 0)
{
std::vector<uint8_t> header;
static const auto text = std::string("PlotFS Recovery Point");
header.insert(header.end(), text.begin(), text.end());
header.push_back(0);
header.push_back(recovery_point_size);
header.insert(header.end(), { static_cast<uint8_t>(size >> 56), static_cast<uint8_t>(size >> 48), static_cast<uint8_t>(size >> 40), static_cast<uint8_t>(size >> 32), static_cast<uint8_t>(size >> 24), static_cast<uint8_t>(size >> 16), static_cast<uint8_t>(size >> 8), static_cast<uint8_t>(size) });
if (next_device_id.size() == 32) {
header.insert(header.end(), next_device_id.begin(), next_device_id.end());
} else {
header.insert(header.end(), 32, 0);
}
header.insert(header.end(), { static_cast<uint8_t>(next_device_offset >> 56), static_cast<uint8_t>(next_device_offset >> 48), static_cast<uint8_t>(next_device_offset >> 40), static_cast<uint8_t>(next_device_offset >> 32), static_cast<uint8_t>(next_device_offset >> 24), static_cast<uint8_t>(next_device_offset >> 16), static_cast<uint8_t>(next_device_offset >> 8), static_cast<uint8_t>(next_device_offset) });
std::array<uint8_t, recovery_point_size> array;
std::copy(header.begin(), header.end(), array.begin());
return array;
}
class PlotFS {
private:
GeometryT geom;
std::shared_ptr<FileHandle> fd;
bool save()
{
flatbuffers::FlatBufferBuilder fbb;
fbb.Finish(Geometry::Pack(fbb, &geom));
if (!fd->seek(0)) {
return false;
}
if (!fd->truncate()) {
return false;
}
auto data = fbb.GetBufferPointer();
auto size = fbb.GetSize();
if (!fd->write(data, size)) {
return false;
}
fd->sync();
return true;
}
public:
struct GeometryRO {
std::vector<uint8_t> buffer;
const Geometry* geom = nullptr;
};
static std::shared_ptr<const struct GeometryRO> loadGeometry(std::shared_ptr<FileHandle>& fd)
{
if (!fd->seek(0)) {
return nullptr;
}
std::vector<uint8_t> buffer(fd->size());
if (!fd->read(buffer.data(), buffer.size())) {
std::cerr << "Failed to read geometry" << std::endl;
return nullptr;
}
if (buffer.empty()) {
std::cerr << "Geometry file is empty" << std::endl;
return nullptr;
}
auto verifier = flatbuffers::Verifier(buffer.data(), buffer.size());
if (!VerifyGeometryBuffer(verifier)) {
std::cerr << "Failed to verify geometry" << std::endl;
return nullptr;
}
auto geom = GetGeometry(buffer.data());
return std::make_shared<const GeometryRO>(GeometryRO { std::move(buffer), geom });
}
static std::shared_ptr<const struct GeometryRO> loadGeometry(const std::string& path)
{
auto fd = FileHandle::open(path, O_RDONLY);
if (!fd) {
std::cerr << "Failed to open " << path << ": " << strerror(errno) << std::endl;
return nullptr;
}
if (!fd->lock(LOCK_SH)) {
std::cerr << "Failed to lock " << path << ": " << strerror(errno) << std::endl;
return nullptr;
}
auto g = loadGeometry(fd);
return g;
}
static bool init(const std::string& path, bool force)
{
auto fd = FileHandle::open(path, O_RDWR | O_CREAT, 0644);
if (!fd) {
return false;
}
if (!fd->lock(LOCK_EX)) {
std::cerr << "Failed exclusive lock on file " << path << std::endl;
return false;
}
if (fd->stat().st_size == 0 || force) {
GeometryT geom;
flatbuffers::FlatBufferBuilder fbb;
fbb.Finish(Geometry::Pack(fbb, &geom));
auto data = fbb.GetBufferPointer();
auto size = fbb.GetSize();
if (!fd->write(data, size)) {
std::cerr << "Failed writing to geometry file" << std::endl;
return false;
}
fd->sync();
} else {
std::cerr << "Geometry file is not empty." << std::endl;
return false;
}
return true;
}
PlotFS(const std::string& path)
{
fd = FileHandle::open(path, O_RDWR, 0644);
if (!fd) {
return;
}
if (!fd->lock(LOCK_EX)) {
fd->close();
return;
}
auto g = loadGeometry(fd);
if (!g) {
return;
}
g->geom->UnPackTo(&geom);
}
bool isOpen() const { return !!fd; }
bool removePlot(const std::vector<uint8_t>& plot_id)
{
auto plot_it = std::find_if(geom.plots.begin(), geom.plots.end(), [&](const auto& p) {
return p->id == plot_id;
});
if (plot_it == geom.plots.end()) {
std::cerr << "warning: plot not found" << std::endl;
return false;
}
geom.plots.erase(plot_it);
return save();
}
bool setPlotFlags(const std::vector<uint8_t>& plot_id, uint64_t flags, bool clear = false)
{
auto plot_it = std::find_if(geom.plots.begin(), geom.plots.end(), [&](const auto& p) {
return !!p && p->id == plot_id;
});
if (plot_it == geom.plots.end()) {
std::cerr << "warning: plot not found" << std::endl;
return false;
}
if (clear) {
(*plot_it)->flags = static_cast<PlotFlags>((*plot_it)->flags & ~flags);
} else {
(*plot_it)->flags = static_cast<PlotFlags>((*plot_it)->flags | flags);
}
return save();
}
bool clearPlotFlags(const std::vector<uint8_t>& plot_id, uint64_t flags)
{
return setPlotFlags(plot_id, flags, true);
}
bool addDevice(const std::string& dev_path, bool force)
{
auto device = DeviceHandle::open(dev_path);
if (device) {
int found = 0;
std::remove_if(geom.devices.begin(), geom.devices.end(), [&](const auto& d) {
if (d->path == dev_path) {
found++;
return true;
}
return false;
});
if (found == 0 && !force) {
std::cerr << "This looks like a PlotFS partition, but it is not registered." << std::endl;
return false;
}
device.reset();
}
// format partition
device = DeviceHandle::format(dev_path);
if (!device) {
std::cerr << "Failed to format device" << std::endl;
return false;
}
{
auto d = std::make_unique<DeviceT>();
d->path = dev_path;
d->id = device->id();
d->begin = device->begin();
d->end = device->end();
geom.devices.emplace_back(std::move(d));
}
return save();
}
bool removeDevice(const std::vector<uint8_t>& dev_id)
{
geom.devices.erase(std::remove_if(geom.devices.begin(), geom.devices.end(), [&](const auto& d){
return d->id == dev_id;
}), geom.devices.end());
return save();
}
bool fixDevice(const std::vector<uint8_t>& dev_id)
{
auto device = std::find_if(geom.devices.begin(), geom.devices.end(), [&](const auto& d){
return d->id == dev_id;
});
if(device == std::end(geom.devices)) {
std::cerr << "device not found: " << to_string(dev_id) << std::endl;
return false;
}
std::cerr << "Fixing signature of " << to_string(dev_id) << " at " << device->get()->path << std::endl;
return DeviceHandle::format(device->get()->path, dev_id) != nullptr;
}
bool addPlot(const std::string& plot_path)
{
if (geom.devices.empty()) {
std::cerr << "No devices registered" << std::endl;
return false;
}
auto plot_file = PlotFile::open(plot_path);
if (!plot_file) {
std::cerr << "failed to open plot: " << plot_path << std::endl;
return false;
}
auto plot_stat = plot_file->stat();
if (plot_stat.st_size == 0) {
std::cerr << "plot file is empty" << std::endl;
return false;
}
for (const auto& plot : geom.plots) {
if (plot->id == plot_file->id()) {
std::cerr << "plot already exists" << std::endl;
return false;
}
}
struct free_shard {
uint64_t begin;
uint64_t end;
std::shared_ptr<DeviceHandle> device;
std::shared_ptr<uint64_t> device_free;
};
std::vector<free_shard> freespace;
for (const auto& device : geom.devices) {
// Make sure we can open the device
auto dh = DeviceHandle::open(device->path, true, O_RDWR);
if (!dh) {
std::cerr << "warning: failed to open device: " << to_string(device->id) << " at " << device->path << std::endl;
continue;
}
if(dh->id() != device->id) {
std::cerr << "warning: wrong device id for " << device->path << " expected " << to_string(device->id) << " but was " << to_string(dh->id()) << std::endl;
}
freespace.push_back(free_shard { dh->begin(), dh->end(), dh, std::make_shared<uint64_t>(dh->end() - dh->begin()) });
}
// Caclulate the free space runs in the pool by assuming every device is empty
// then subtracting the used space from the free space resulting in fragmented runs
for (const auto& plot : geom.plots) {
for (const auto& shard : plot->shards) {
// The filesystem minimizes fragmentation, so the freepace vector should be small
auto freespace_iter = std::find_if(freespace.begin(), freespace.end(), [&](const auto& freespace) {
return shard->device_id == freespace.device->id()
&& shard->begin < freespace.end
&& shard->end > freespace.begin;
});
if (freespace_iter == freespace.end()) {
// this should not happen
std::cerr << "warning: plot block not found in freespace" << std::endl;
continue;
}
auto freeblock = *freespace_iter;
freespace.erase(freespace_iter);
// keep track of the free space so we can sort by it later
*freeblock.device_free -= (shard->end - shard->begin);
if (shard->end < freeblock.end) {
// shard: |----|
// freeblock: |-----------|
// new freeblock: |------|
freespace.push_back(free_shard { shard->end, freeblock.end, freeblock.device, freeblock.device_free });
}
if (shard->begin > freeblock.begin) {
// shard: |----|
// freeblock: |-----------|
// new freeblock: |------|
freespace.push_back(free_shard { freeblock.begin, shard->begin, freeblock.device, freeblock.device_free });
}
}
}
// Sort the freeruns decending by device free space, then run length
std::sort(freespace.begin(), freespace.end(), [](const auto& a, const auto& b) {
if (*a.device_free != *b.device_free) {
return *a.device_free > *b.device_free;
}
return (a.end - a.begin) > (b.end - b.begin);
});
// fragment (for testing purposes)
// if (false) {
// std::random_device rd;
// std::mt19937 mt(rd());
// std::uniform_int_distribution<uint64_t> dist(1, 1'000'000'000);
// std::vector<free_shard> fragmented;
// for (auto& shard : freespace) {
// while (shard.begin < shard.end) {
// auto shard_size = std::min((shard.end - shard.begin), dist(mt));
// auto shard_end = shard.begin + shard_size;
// fragmented.emplace_back(free_shard { shard.begin, shard_end, shard.device });
// shard.begin = shard_end;
// }
// }
// std::shuffle(fragmented.begin(), fragmented.end(), mt);
// freespace = fragmented;
// }
// iterate over the freeruns until we find enough combined space to fit the plot
// including the recovery point header
std::vector<free_shard> reserved_space;
auto space_needed = static_cast<uint64_t>(plot_stat.st_size);
for (const auto& shard : freespace) {
if (space_needed == 0) {
break;
}
auto reserved_size = std::min(space_needed + recovery_point_size, shard.end - shard.begin);
if (reserved_size <= recovery_point_size) {
continue;
}
reserved_space.push_back({ shard.begin, shard.begin + reserved_size, shard.device });
space_needed -= (reserved_size - recovery_point_size);
}
if (space_needed > 0) {
std::cerr << "not enough free space to fit plot" << std::endl;
return false;
}
// we are done with the freespace vector, clear it so unused file handles will be closed
freespace.clear();
// reserve the space in the device
std::vector<std::unique_ptr<ShardT>> shards;
for (const auto& reserved : reserved_space) {
auto s = std::make_unique<ShardT>();
s->device_id = reserved.device->id();
s->begin = reserved.begin;
s->end = reserved.end;
shards.emplace_back(std::move(s));
}
{ // scope newPlot we do dont accidentally use it after it is moved
auto newPlot = std::make_unique<PlotT>();
newPlot->k = plot_file->k();
newPlot->id = plot_file->id();
newPlot->flags = PlotFlags_Reserved;
newPlot->shards = std::move(shards);
geom.plots.emplace_back(std::move(newPlot));
}
if (!save()) {
return false;
}
if (!fd->lock(LOCK_UN)) {
removePlot(plot_file->id());
return false;
}
// copy the plot to the reserved space
std::cerr << "starting plot copy to " << reserved_space.size() << " shard(s)" << std::endl;
off64_t off_in = 0; // position of input file
while (!reserved_space.empty()) {
auto device = reserved_space.front().device;
auto device_offset = reserved_space.front().begin;
auto shard_size = reserved_space.front().end - reserved_space.front().begin;
reserved_space.erase(reserved_space.begin());
auto next_device_id = reserved_space.empty() ? std::vector<uint8_t>() : reserved_space.front().device->id();
auto next_shard_offset = reserved_space.empty() ? 0 : reserved_space.front().begin;
auto recovery_point = get_recovery_point(shard_size - recovery_point_size, next_device_id, next_shard_offset);
if (!device->seek(device_offset)) {
removePlot(plot_file->id());
return false;
}
std::cerr << "writing recovery point header" << std::endl;
if (device->write(recovery_point.data(), recovery_point.size()) != recovery_point.size()) {
std::cerr << "error writing recovery header" << std::endl;
removePlot(plot_file->id());
return false;
}
// fill the rest of the shard
shard_size -= recovery_point.size();
while (shard_size > 0) {
// split up the writes a little bit
uint64_t bytes_to_write = std::min(shard_size, static_cast<uint64_t>(1024 * 1024 * 1024));
std::cerr << int(100 * off_in / plot_stat.st_size) << "% writing up to " << bytes_to_write << " bytes from offset " << off_in << " to device " << to_string(device->id()) << std::endl;
auto bytes_written = sendfile64(device->fd(), plot_file->fd(), &off_in, bytes_to_write);
if (bytes_written < 0) {
removePlot(plot_file->id());
std::cerr << "failed to copy plot to device " << errno << std::endl;
return false;
}
shard_size -= bytes_written;
}
std::cerr << int(100 * off_in / plot_stat.st_size) << "% finished writing to device " << to_string(device->id()) << std::endl;
}
// Finished writing, clear the reserved flag
if (!fd->lock(LOCK_EX)) {
removePlot(plot_file->id());
}
auto g = loadGeometry(fd);
if (!g) {
return false;
}
g->geom->UnPackTo(&geom);
return clearPlotFlags(plot_file->id(), PlotFlags_Reserved);
}
};