-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmount.cpp
295 lines (257 loc) · 8.28 KB
/
mount.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
#define FUSE_USE_VERSION 31
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <fuse3/fuse.h>
#include <map>
#include <mutex>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "plotfs.hpp"
static struct options {
const char* config_path;
} options;
#define OPTION(t, p) \
{ \
t, offsetof(struct options, p), 1 \
}
static const struct fuse_opt option_spec[] = {
OPTION("--c=%s", config_path),
OPTION("--config=%s", config_path),
FUSE_OPT_END
};
auto loadGeometry(bool force)
{
static std::mutex m;
static std::shared_ptr<const struct PlotFS::GeometryRO> geometry;
std::lock_guard<std::mutex> lock(m);
if (!geometry || force) {
geometry = PlotFS::loadGeometry(options.config_path);
}
return geometry;
}
std::vector<uint8_t> path_to_plot_id(const std::string& path)
{
int k = 0;
std::vector<uint8_t> id(32);
if (33 != std::sscanf(path.c_str(), "/plot-k%d-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", //
&k, &id[0], &id[1], &id[2], &id[3], &id[4], &id[5], &id[6], &id[7], &id[8], &id[9], &id[10], &id[11], &id[12], &id[13], &id[14], &id[15], //
&id[16], &id[17], &id[18], &id[19], &id[20], &id[21], &id[22], &id[23], &id[24], &id[25], &id[26], &id[27], &id[28], &id[29], &id[30], &id[31])) {
return std::vector<uint8_t>();
}
return id;
}
std::string plot_filename(const Plot& plot)
{
return std::string("plot-k") + std::to_string(plot.k()) + "-" + to_string(*plot.id()) + ((plot.flags() & PlotFlags_Reserved) ? std::string(".tmp") : std::string(".plot"));
}
static void* init(struct fuse_conn_info* conn, struct fuse_config* cfg)
{
(void)conn;
cfg->kernel_cache = 0;
cfg->direct_io = 0;
return nullptr;
}
struct shard_data {
uint64_t begin;
uint64_t end;
std::string dev_path;
};
// TODO dont unpack the geometry to get shard_data
static const std::vector<shard_data> get_plot_data(const std::vector<uint8_t>& plot_id)
{
auto g = loadGeometry(false);
if (!g || !g->geom || !g->geom->plots() || !g->geom->devices()) {
return std::vector<shard_data>();
}
auto devpath = [&](const flatbuffers::Vector<uint8_t>& device_id) {
auto key = std::vector<uint8_t>(device_id.begin(), device_id.end());
for (const auto& device : *g->geom->devices()) {
if (*device->id() == key) {
return device->path()->str();
}
}
return std::string();
};
std::vector<shard_data> shards;
for (const auto plot : *g->geom->plots()) {
if (*plot->id() == plot_id) {
if (!plot->shards()) {
continue;
}
for (const auto shard : *plot->shards()) {
shards.emplace_back(shard_data { shard->begin() + recovery_point_size, shard->end(), devpath(*shard->device_id()) });
}
return shards;
}
}
return std::vector<shard_data>();
}
static int getattr(const char* path, struct stat* stbuf, struct fuse_file_info* fi)
{
(void)fi;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
return 0;
} else {
auto plot_id = path_to_plot_id(path);
if (plot_id.empty()) {
return -ENOENT;
}
auto shard_data = get_plot_data(plot_id);
if (shard_data.empty()) {
return -EIO;
}
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = 0;
for (const auto& shard : shard_data) {
stbuf->st_size += shard.end - shard.begin;
}
return 0;
}
return -ENOENT;
}
static int readdir(const char* path, void* buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi, enum fuse_readdir_flags flags)
{
(void)offset;
(void)fi;
(void)flags;
if (strcmp(path, "/") != 0) {
return -ENOENT;
}
auto g = loadGeometry(true);
if (!g) {
return -EIO;
}
filler(buf, ".", NULL, 0, static_cast<fuse_fill_dir_flags>(0));
filler(buf, "..", NULL, 0, static_cast<fuse_fill_dir_flags>(0));
if (g->geom->plots()) {
for (const auto plot : *g->geom->plots()) {
std::string filename = plot_filename(*plot);
if (filename.empty()) {
continue;
}
filler(buf, filename.c_str(), NULL, 0, static_cast<fuse_fill_dir_flags>(0));
}
}
return 0;
}
static int open(const char* path, struct fuse_file_info* fi)
{
auto plot_id = path_to_plot_id(path);
if (plot_id.empty()) {
return -ENOENT;
}
if ((fi->flags & O_ACCMODE) != O_RDONLY) {
return -EACCES;
}
auto data = get_plot_data(plot_id);
auto pd_ptr = new std::vector<shard_data>();
pd_ptr->swap(data);
fi->fh = reinterpret_cast<uint64_t>(pd_ptr);
return 0;
}
static int release(const char*, struct fuse_file_info* fi)
{
auto pd_ptr = reinterpret_cast<std::vector<shard_data>*>(fi->fh);
if (pd_ptr) {
delete pd_ptr;
}
return 0;
}
static int read(const char* path, char* buf, size_t size, off_t offset, struct fuse_file_info* fi)
{
auto pd_ptr = reinterpret_cast<std::vector<shard_data>*>(fi->fh);
if (!pd_ptr) {
return -EIO;
}
auto tsize = size;
for (auto& shard : *pd_ptr) {
if (size == 0) {
break;
}
// offset is after this shard ends
auto shard_size = shard.end - shard.begin;
if (offset >= shard_size) {
offset -= shard_size;
continue;
}
// offset is in this shard
auto device = FileHandle::open(shard.dev_path);
if (!device) {
std::cerr << "failed to open " << shard.dev_path << std::endl;
return -EIO;
}
// Seek to the right place
if (!device->seek(shard.begin + offset)) {
std::cerr << "seek failed" << std::endl;
return -EIO;
}
// Read the data
auto read = std::min(static_cast<uint64_t>(size), shard_size - offset);
read = device->read(reinterpret_cast<uint8_t*>(buf), read);
buf += read, size -= read, offset = 0;
}
return tsize - size;
}
static int statfs(const char*, struct statvfs* stat)
{
auto g = loadGeometry(false);
if (!g) {
return -EIO;
}
stat->f_bsize = 1; /* file system block size */
stat->f_frsize = 1; /* fragment size */
stat->f_blocks = 0; /* size of fs in f_frsize units */
stat->f_bfree = 0; /* # free blocks */
stat->f_bavail = 0; /* # free blocks for unprivileged users */
stat->f_files = 0; /* # inodes */
stat->f_ffree = 0x1fffffff; /* # free inodes */
stat->f_favail = 0x1fffffff; /* # free inodes for unprivileged users */
stat->f_fsid = 0; /* file system ID */
stat->f_flag = 0; /* mount flags */
stat->f_namemax = 255; /* maximum filename length */
if (g->geom->devices()) {
for (const auto device : *g->geom->devices()) {
stat->f_blocks += device->end() - device->begin();
}
}
if (g->geom->plots()) {
stat->f_bfree = stat->f_blocks;
for (const auto plot : *g->geom->plots()) {
for (const auto shard : *plot->shards()) {
stat->f_bfree -= shard->end() - shard->begin();
}
}
stat->f_files = g->geom->plots()->size();
stat->f_bavail = stat->f_bfree;
}
return 0;
}
static const struct fuse_operations oper = {
.getattr = getattr,
.open = open,
.read = read,
.statfs = statfs,
.release = release,
.readdir = readdir,
.init = init,
};
int main(int argc, char* argv[])
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1) {
return EXIT_FAILURE;
}
if (0 == options.config_path || 0 == strlen(options.config_path)) {
options.config_path = default_config_path.c_str();
}
fuse_opt_add_arg(&args, "-oallow_other");
auto ret = fuse_main(args.argc, args.argv, &oper, NULL);
fuse_opt_free_args(&args);
return ret;
}