-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilesystem.h
534 lines (449 loc) · 14.1 KB
/
filesystem.h
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#pragma once
#include <atomic>
#include <cassert>
#include <cstring>
#include <fuse.h>
#include <fuse_lowlevel.h>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <linux/limits.h>
struct FileHandle;
class filesystem_base {
public:
filesystem_base() {
std::memset(&ops_, 0, sizeof(ops_));
ops_.destroy = ll_destroy;
ops_.create = ll_create;
ops_.release = ll_release;
ops_.unlink = ll_unlink;
ops_.forget_multi = ll_forget_multi;
ops_.getattr = ll_getattr;
ops_.lookup = ll_lookup;
ops_.opendir = ll_opendir;
ops_.readdir = ll_readdir;
ops_.releasedir = ll_releasedir;
ops_.open = ll_open;
ops_.write_buf = ll_write_buf;
ops_.read = ll_read;
ops_.mkdir = ll_mkdir;
ops_.rmdir = ll_rmdir;
ops_.rename = ll_rename;
ops_.setattr = ll_setattr;
ops_.readlink = ll_readlink;
ops_.symlink = ll_symlink;
ops_.fsync = ll_fsync;
ops_.fsyncdir = ll_fsyncdir;
ops_.statfs = ll_statfs;
ops_.link = ll_link;
ops_.access = ll_access;
ops_.mknod = ll_mknod;
ops_.fallocate = ll_fallocate;
}
const fuse_lowlevel_ops& ops() const { return ops_; }
public:
virtual void destroy() = 0;
virtual int
lookup(fuse_ino_t parent_ino, const std::string& name, struct stat* st)
= 0;
virtual void forget(fuse_ino_t ino, long unsigned nlookup) = 0;
virtual int statfs(fuse_ino_t ino, struct statvfs* stbuf) = 0;
virtual int mknod(
fuse_ino_t parent_ino,
const std::string& name,
mode_t mode,
dev_t rdev,
struct stat* st,
uid_t uid,
gid_t gid)
= 0;
virtual int symlink(
const std::string& link,
fuse_ino_t parent_ino,
const std::string& name,
struct stat* st,
uid_t uid,
gid_t gid)
= 0;
virtual int link(
fuse_ino_t ino,
fuse_ino_t newparent_ino,
const std::string& newname,
struct stat* st,
uid_t uid,
gid_t gid)
= 0;
virtual int rename(
fuse_ino_t parent_ino,
const std::string& name,
fuse_ino_t newparent_ino,
const std::string& newname,
uid_t uid,
gid_t gid)
= 0;
virtual int
unlink(fuse_ino_t parent_ino, const std::string& name, uid_t uid, gid_t gid)
= 0;
virtual int access(fuse_ino_t ino, int mask, uid_t uid, gid_t gid) = 0;
virtual int getattr(fuse_ino_t ino, struct stat* st, uid_t uid, gid_t gid)
= 0;
virtual int setattr(
fuse_ino_t ino,
FileHandle* fh,
struct stat* attr,
int to_set,
uid_t uid,
gid_t gid)
= 0;
virtual ssize_t
readlink(fuse_ino_t ino, char* path, size_t maxlen, uid_t uid, gid_t gid)
= 0;
virtual int mkdir(
fuse_ino_t parent_ino,
const std::string& name,
mode_t mode,
struct stat* st,
uid_t uid,
gid_t gid)
= 0;
virtual int opendir(fuse_ino_t ino, int flags, uid_t uid, gid_t gid) = 0;
virtual ssize_t readdir(
fuse_req_t req, fuse_ino_t ino, char* buf, size_t bufsize, off_t off)
= 0;
virtual int
rmdir(fuse_ino_t parent_ino, const std::string& name, uid_t uid, gid_t gid)
= 0;
virtual void releasedir(fuse_ino_t ino) = 0;
virtual int create(
fuse_ino_t parent_ino,
const std::string& name,
mode_t mode,
int flags,
struct stat* st,
FileHandle** fhp,
uid_t uid,
gid_t gid)
= 0;
virtual int
open(fuse_ino_t ino, int flags, FileHandle** fhp, uid_t uid, gid_t gid)
= 0;
virtual ssize_t
write_buf(FileHandle* fh, struct fuse_bufvec* bufv, off_t off)
= 0;
virtual ssize_t read(FileHandle* fh, off_t offset, size_t size, char* buf)
= 0;
virtual void release(fuse_ino_t ino, FileHandle* fh) = 0;
private:
static filesystem_base* get(fuse_req_t req) {
return get(fuse_req_userdata(req));
}
static filesystem_base* get(void* userdata) {
return reinterpret_cast<filesystem_base*>(userdata);
}
static void ll_destroy(void* userdata) {
auto fs = get(userdata);
fs->destroy();
}
static void ll_create(
fuse_req_t req,
fuse_ino_t parent,
const char* name,
mode_t mode,
struct fuse_file_info* fi) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
struct fuse_entry_param fe;
std::memset(&fe, 0, sizeof(fe));
FileHandle* fh;
int ret = fs->create(
parent, name, mode, fi->flags, &fe.attr, &fh, ctx->uid, ctx->gid);
if (ret == 0) {
fi->fh = reinterpret_cast<uint64_t>(fh);
fe.ino = fe.attr.st_ino;
fe.generation = 0;
fe.entry_timeout = 1.0;
fuse_reply_create(req, &fe, fi);
} else {
fuse_reply_err(req, -ret);
}
}
static void
ll_release(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
auto fs = get(req);
auto fh = reinterpret_cast<FileHandle*>(fi->fh);
fs->release(ino, fh);
fuse_reply_err(req, 0);
}
static void ll_unlink(fuse_req_t req, fuse_ino_t parent, const char* name) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
int ret = fs->unlink(parent, name, ctx->uid, ctx->gid);
fuse_reply_err(req, -ret);
}
static void
ll_forget(fuse_req_t req, fuse_ino_t ino, long unsigned nlookup) {
auto fs = get(req);
fs->forget(ino, nlookup);
fuse_reply_none(req);
}
static void ll_forget_multi(
fuse_req_t req, size_t count, struct fuse_forget_data* forgets) {
auto fs = get(req);
for (size_t i = 0; i < count; i++) {
const struct fuse_forget_data* f = forgets + i;
fs->forget(f->ino, f->nlookup);
}
fuse_reply_none(req);
}
static void
ll_getattr(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
struct stat st;
int ret = fs->getattr(ino, &st, ctx->uid, ctx->gid);
if (ret == 0)
fuse_reply_attr(req, &st, ret);
else
fuse_reply_err(req, -ret);
}
static void ll_lookup(fuse_req_t req, fuse_ino_t parent, const char* name) {
auto fs = get(req);
struct fuse_entry_param fe;
std::memset(&fe, 0, sizeof(fe));
int ret = fs->lookup(parent, name, &fe.attr);
if (ret == 0) {
fe.ino = fe.attr.st_ino;
fe.generation = 0;
fuse_reply_entry(req, &fe);
} else {
fuse_reply_err(req, -ret);
}
}
static void
ll_opendir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
int ret = fs->opendir(ino, fi->flags, ctx->uid, ctx->gid);
if (ret == 0) {
fuse_reply_open(req, fi);
} else {
fuse_reply_err(req, -ret);
}
}
static void ll_readdir(
fuse_req_t req,
fuse_ino_t ino,
size_t size,
off_t off,
struct fuse_file_info* fi) {
auto fs = get(req);
auto buf = std::unique_ptr<char[]>(new char[size]);
ssize_t ret = fs->readdir(req, ino, buf.get(), size, off);
if (ret >= 0) {
fuse_reply_buf(req, buf.get(), (size_t)ret);
} else {
int r = (int)ret;
fuse_reply_err(req, -r);
}
}
static void
ll_releasedir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
auto fs = get(req);
fs->releasedir(ino);
fuse_reply_err(req, 0);
}
static void
ll_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
// new files are handled by ll_create
assert(!(fi->flags & O_CREAT));
FileHandle* fh;
int ret = fs->open(ino, fi->flags, &fh, ctx->uid, ctx->gid);
if (ret == 0) {
fi->fh = reinterpret_cast<uint64_t>(fh);
fuse_reply_open(req, fi);
} else {
fuse_reply_err(req, -ret);
}
}
static void ll_write_buf(
fuse_req_t req,
fuse_ino_t ino,
struct fuse_bufvec* bufv,
off_t off,
struct fuse_file_info* fi) {
auto fs = get(req);
auto fh = reinterpret_cast<FileHandle*>(fi->fh);
ssize_t ret = fs->write_buf(fh, bufv, off);
if (ret >= 0)
fuse_reply_write(req, ret);
else
fuse_reply_err(req, -ret);
}
static void ll_read(
fuse_req_t req,
fuse_ino_t ino,
size_t size,
off_t off,
struct fuse_file_info* fi) {
auto fs = get(req);
auto fh = reinterpret_cast<FileHandle*>(fi->fh);
auto buf = std::unique_ptr<char[]>(new char[size]);
ssize_t ret = fs->read(fh, off, size, buf.get());
if (ret >= 0)
fuse_reply_buf(req, buf.get(), ret);
else
fuse_reply_err(req, -ret);
}
static void
ll_mkdir(fuse_req_t req, fuse_ino_t parent, const char* name, mode_t mode) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
struct fuse_entry_param fe;
std::memset(&fe, 0, sizeof(fe));
int ret = fs->mkdir(parent, name, mode, &fe.attr, ctx->uid, ctx->gid);
if (ret == 0) {
fe.ino = fe.attr.st_ino;
fe.generation = 0;
fe.entry_timeout = 1.0;
fuse_reply_entry(req, &fe);
} else {
fuse_reply_err(req, -ret);
}
}
static void ll_rmdir(fuse_req_t req, fuse_ino_t parent, const char* name) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
int ret = fs->rmdir(parent, name, ctx->uid, ctx->gid);
fuse_reply_err(req, -ret);
}
static void ll_rename(
fuse_req_t req,
fuse_ino_t parent,
const char* name,
fuse_ino_t newparent,
const char* newname) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
int ret = fs->rename(
parent, name, newparent, newname, ctx->uid, ctx->gid);
fuse_reply_err(req, -ret);
}
static void ll_setattr(
fuse_req_t req,
fuse_ino_t ino,
struct stat* attr,
int to_set,
struct fuse_file_info* fi) {
auto fs = get(req);
auto fh = fi ? reinterpret_cast<FileHandle*>(fi->fh) : nullptr;
const struct fuse_ctx* ctx = fuse_req_ctx(req);
int ret = fs->setattr(ino, fh, attr, to_set, ctx->uid, ctx->gid);
if (ret == 0)
fuse_reply_attr(req, attr, 0);
else
fuse_reply_err(req, -ret);
}
static void ll_readlink(fuse_req_t req, fuse_ino_t ino) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
char path[PATH_MAX + 1];
ssize_t ret = fs->readlink(
ino, path, sizeof(path) - 1, ctx->uid, ctx->gid);
if (ret >= 0) {
path[ret] = '\0';
fuse_reply_readlink(req, path);
} else {
int r = (int)ret;
fuse_reply_err(req, -r);
}
}
static void ll_symlink(
fuse_req_t req, const char* link, fuse_ino_t parent, const char* name) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
struct fuse_entry_param fe;
std::memset(&fe, 0, sizeof(fe));
int ret = fs->symlink(link, parent, name, &fe.attr, ctx->uid, ctx->gid);
if (ret == 0) {
fe.ino = fe.attr.st_ino;
fuse_reply_entry(req, &fe);
} else {
fuse_reply_err(req, -ret);
}
}
static void ll_fsync(
fuse_req_t req, fuse_ino_t ino, int datasync, struct fuse_file_info* fi) {
fuse_reply_err(req, 0);
}
static void ll_fsyncdir(
fuse_req_t req, fuse_ino_t ino, int datasync, struct fuse_file_info* fi) {
fuse_reply_err(req, 0);
}
static void ll_statfs(fuse_req_t req, fuse_ino_t ino) {
auto fs = get(req);
struct statvfs stbuf;
std::memset(&stbuf, 0, sizeof(stbuf));
int ret = fs->statfs(ino, &stbuf);
if (ret == 0)
fuse_reply_statfs(req, &stbuf);
else
fuse_reply_err(req, -ret);
}
static void ll_link(
fuse_req_t req,
fuse_ino_t ino,
fuse_ino_t newparent,
const char* newname) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
struct fuse_entry_param fe;
std::memset(&fe, 0, sizeof(fe));
int ret = fs->link(
ino, newparent, newname, &fe.attr, ctx->uid, ctx->gid);
if (ret == 0) {
fe.ino = fe.attr.st_ino;
fuse_reply_entry(req, &fe);
} else {
fuse_reply_err(req, -ret);
}
}
static void ll_access(fuse_req_t req, fuse_ino_t ino, int mask) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
int ret = fs->access(ino, mask, ctx->uid, ctx->gid);
fuse_reply_err(req, -ret);
}
static void ll_mknod(
fuse_req_t req,
fuse_ino_t parent,
const char* name,
mode_t mode,
dev_t rdev) {
auto fs = get(req);
const struct fuse_ctx* ctx = fuse_req_ctx(req);
struct fuse_entry_param fe;
std::memset(&fe, 0, sizeof(fe));
int ret = fs->mknod(
parent, name, mode, rdev, &fe.attr, ctx->uid, ctx->gid);
if (ret == 0) {
fe.ino = fe.attr.st_ino;
fuse_reply_entry(req, &fe);
} else {
fuse_reply_err(req, -ret);
}
}
static void ll_fallocate(
fuse_req_t req,
fuse_ino_t ino,
int mode,
off_t offset,
off_t length,
struct fuse_file_info* fi) {
fuse_reply_err(req, 0);
}
fuse_lowlevel_ops ops_;
};