-
Notifications
You must be signed in to change notification settings - Fork 4
/
ghost_fs.cc
435 lines (343 loc) · 11.8 KB
/
ghost_fs.cc
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
/*
Ghost File System, or simply GhostFS
Copyright (C) 2016 Raphael S. Carvalho
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
*/
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <thread>
#include <boost/filesystem.hpp>
#include "ghost_fs.h"
#include "utils.h"
#include "protocol/http_protocol.h"
#include "protocol/load_drivers.h"
#include "protocol/python_driver.h"
ghost_fs *get_ghost_fs() {
struct fuse_context* context = fuse_get_context();
return (struct ghost_fs*) context->private_data;
}
ghost_fs::ghost_fs()
: _c(CACHE_SIZE, BLOCK_SIZE) {}
void ghost_fs::add_file(const char *file_path, const char *content) {
_files.emplace(std::string(file_path), ghost_file(content));
}
void ghost_fs::add_file(const char *file_path) {
_files.emplace(std::string(file_path), ghost_file());
}
void ghost_fs::remove_file(const char *file_path) {
_files.erase(std::string(file_path));
}
bool ghost_fs::file_exists(const char *file_path) {
auto it = _files.find(std::string(file_path));
return (it == _files.end()) ? false : true;
}
std::unordered_map<std::string, ghost_file> &ghost_fs::files() {
return _files;
}
size_t ghost_fs::get_block_size() {
return _c.block_size();
}
cache &ghost_fs::get_cache() {
return _c;
}
// fuse handlers
static int ghost_getattr(const char *path, struct stat *stbuf)
{
struct ghost_fs* ghost = get_ghost_fs();
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
} else {
auto& files = ghost->files();
auto it = files.find(path);
if (it == files.end()) {
res = -ENOENT;
} else {
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = it->second.length();
}
}
return res;
}
static int ghost_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
struct ghost_fs* ghost = get_ghost_fs();
(void) offset;
(void) fi;
// Root is the unique directory supported so far.
if (strcmp(path, "/") != 0) {
return -ENOENT;
}
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
for (auto& it : ghost->files()) {
filler(buf, it.first.data() + 1, NULL, 0);
}
return 0;
}
static int ghost_open(const char *path, struct fuse_file_info *fi)
{
struct ghost_fs* ghost = get_ghost_fs();
log("fi=%p, path=%s\n", fi, path);
if (!ghost->file_exists(path)) {
return -ENOENT;
}
if ((fi->flags & 3) != O_RDONLY) {
return -EACCES;
}
return 0;
}
static int ghost_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
struct ghost_fs* ghost = get_ghost_fs();
bool exclusive = fi->flags & O_EXCL; // CHECK if it's correct
//log("CREATE: path=%s, create=%d, rdonly=%d, exclusive=%d\n", path, create, read_only, exclusive);
#if 0 /* Otherwise, command touch will not work */
if ((fi->flags & 3) != O_RDONLY) {
return -EACCES;
}
#endif
if (ghost->file_exists(path)) {
if (exclusive) {
return -EEXIST;
}
} else {
ghost->add_file(path);
}
return 0;
}
static void do_prefetch(cache& c, ghost_file& file, size_t blk_id, std::string file_url) {
std::vector<block_info>& file_blocks = file.get_file_blocks();
block_info& info = file_blocks[blk_id];
base_protocol* handler = get_handler(file_url.data());
if (!handler) return;
// XXX: handle possible failure here, so we will have to deallocate block, provide
// the function in cache.
handler->get_block(file_url.data(), blk_id, c.block_size(),
file.attributes(), info._blk->_data);
c.unlock_block(info._blk);
info._mtx.unlock();
log("Prefetched block %ld\n", blk_id);
}
static void try_prefetch(cache& c, ghost_file& file, size_t blk_id, const char* file_url) {
std::vector<block_info>& file_blocks = file.get_file_blocks();
block_info& info = file_blocks[blk_id];
if (!info._mtx.try_lock()) {
return;
}
c._mtx.lock();
if (info._present) {
c._mtx.unlock();
info._mtx.unlock();
return;
}
log("Prefetching block %ld\n", blk_id);
block* blk = c.allocate_block(&info);
c._mtx.unlock();
assert(info._blk == blk);
std::thread t(do_prefetch, std::ref(c), std::ref(file), blk_id, std::string(file_url));
t.detach();
}
static int ghost_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
struct ghost_fs* ghost = get_ghost_fs();
auto& files = ghost->files();
auto it = files.find(path);
if (it == files.end()) {
return -ENOENT;
}
auto& file = it->second;
size_t len = file.length();
if (offset < len) {
if (offset + size > len) {
size = len - offset;
}
} else {
return 0;
}
if (file.is_static()) {
memcpy(buf, file.data() + offset, size);
return size;
}
const char *file_url = file.get_url();
if (!file_url) {
return 0;
}
log("\nURL: %s\n", file_url);
base_protocol* handler = get_handler(file_url);
if (!handler) return 0;
std::vector<block_info>& file_blocks = file.get_file_blocks();
cache& c = ghost->get_cache();
size_t end = offset + size;
size_t buf_offset = 0;
size_t block_size = ghost->get_block_size();
size_t blk_id;
while (offset < end) {
blk_id = offset / block_size;
size_t blk_offset = offset % block_size;
size_t remaining = end - offset;
size_t to_read = std::min(remaining, block_size - blk_offset);
log("blk_id=%ld, blk_offset=%ld, to_read=%ld\n", blk_id, blk_offset, to_read);
block_info& info = file_blocks[blk_id];
block* blk = nullptr;
info._mtx.lock();
c._mtx.lock();
if (!info._present) {
c._misses++;
log("\tnot cached, hit ratio=%6.2f%%\n", c.get_hit_ratio());
blk = c.allocate_block(&info);
c._mtx.unlock();
size_t bytes_read = handler->get_block(file_url, blk_id, block_size, file.attributes(), blk->_data);
// If get_block() was unable to get the amount of bytes asked, then we should return EIO.
if (bytes_read < to_read) {
log("get_block failed for block %ld, expected=%ld, actual=%ld\n", blk_id, to_read, bytes_read);
// TODO: we're still leaking the block previously allocated, so let's provide a function
// in cache to deallocate a block. This function will have to reset block info because
// block content is invalid if get_block() failed.
info._mtx.unlock();
return -EIO;
}
// If bytes read is greater than block size, then there is an overflow in blk->_data
assert(bytes_read <= block_size);
} else {
c._hits++;
log("\tcached, hit ratio=%6.2f%%\n", c.get_hit_ratio());
blk = info._blk;
c.lock_block(blk);
c._mtx.unlock();
}
assert(info._blk->_info == &info);
assert(info._blk->_data == blk->_data);
assert(buf_offset + to_read <= size);
memcpy(buf + buf_offset, blk->_data + blk_offset, to_read);
// TODO: Possibly, we will have to acquire cache mutex to unlock block.
c.unlock_block(blk);
info._mtx.unlock();
buf_offset += to_read;
offset += to_read;
}
// Try to prefetch subsequent block.
if ((blk_id + 1) < file_blocks.size()) {
try_prefetch(c, file, blk_id+1, file_url);
}
return size;
}
int ghost_setxattr(const char *path, const char *name,
const char *value, size_t size, int flags) {
char value_buf[size+1];
memcpy((void*) &value_buf, value, size);
value_buf[size] = '\0';
log("* setxattr: path=%s, name=%s, value=%s, size=%ld\n",
path, name, value_buf, size);
struct ghost_fs* ghost = get_ghost_fs();
auto& files = ghost->files();
auto it = files.find(path);
if (it == files.end()) {
return -ENOENT;
}
auto& file = it->second;
if ((flags & XATTR_CREATE) && file.attribute_exists(name)) {
return -EEXIST;
} else if ((flags & XATTR_REPLACE) && !file.attribute_exists(name)) {
return -ENOATTR;
}
// WARNING: if url attribute gets replaced, we need to invalidate
// all cache entries of the file and update file size
file.add_attribute(name, value_buf);
base_protocol* handler = get_handler(value_buf);
if (!handler) return 0;
// Need to check if URL accepts range request, if not, we need to do something.
if (strcmp(name, "url") == 0 &&
handler->is_url_valid(value_buf)) {
file.update_length(handler->get_content_length_for_url(value_buf),
ghost->get_block_size());
try_prefetch(ghost->get_cache(), file, 0, value_buf);
}
return 0;
}
int ghost_getxattr(const char *path, const char *name, char *value, size_t size) {
log("* getxattr: path=%s, name=%s, value=%p, %s, size=%ld\n",
path, name, value, value, size);
struct ghost_fs* ghost = get_ghost_fs();
auto& files = ghost->files();
auto it = files.find(path);
if (it == files.end()) {
return -ENOENT;
}
auto& file = it->second;
auto& attributes = file.attributes();
auto it2 = attributes.find(name);
if (it2 == attributes.end()) {
return -ENOATTR;
}
auto& attribute_value = it2->second;
size_t attribute_value_size = attribute_value.size();
log("\tattribute=%s, attribute_value_size=%ld\n",
attribute_value.data(), attribute_value_size);
if (size > 0) {
// Size of buffer is small to hold the result.
if (attribute_value_size > size) {
return -ERANGE;
}
memcpy(value, attribute_value.data(), attribute_value_size);
}
return attribute_value_size;
}
int ghost_removexattr(const char *path, const char *name) {
struct ghost_fs* ghost = get_ghost_fs();
auto& files = ghost->files();
auto it = files.find(path);
if (it == files.end()) {
return -ENOENT;
}
auto& file = it->second;
if (!file.attribute_exists(name)) {
return -ENOATTR;
}
file.remove_attribute(name);
return 0;
}
// Utility functions
struct fuse_operations ghost_oper;
struct ghost_fs ghost;
void set_ghost_oper() {
ghost_oper.getattr = ghost_getattr;
ghost_oper.readdir = ghost_readdir;
ghost_oper.open = ghost_open;
ghost_oper.create = ghost_create;
ghost_oper.read = ghost_read;
ghost_oper.setxattr = ghost_setxattr;
ghost_oper.getxattr = ghost_getxattr;
ghost_oper.removexattr = ghost_removexattr;
}
void add_static_files() {
static const char *ghost_path = "/HELLO";
static const char *ghost_str = "Hello World!\n";
ghost.add_file(ghost_path, ghost_str);
static const char *credits_path = "/CREDITS";
static const char *credits_str = "Raphael S. Carvalho <[email protected]>\n";
ghost.add_file(credits_path, credits_str);
}
void register_handlers() {
register_handler(new http_protocol);
register_handler(new https_protocol);
register_handler(new file_protocol);
}
namespace fs = boost::filesystem;
int ghost_main(int argc, char *argv[]) {
fs::path current_path = fs::system_complete(fs::path(argv[0])).parent_path();
set_ghost_oper();
add_static_files();
register_handlers();
//load extern drivers
load_drivers(current_path);
python::initialize initialize;
load_python_drivers(current_path);
return fuse_main(argc, argv, &ghost_oper, (void*) &ghost);
}