-
Notifications
You must be signed in to change notification settings - Fork 0
/
cowfuse.c
495 lines (439 loc) · 11.9 KB
/
cowfuse.c
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
#define FUSE_USE_VERSION 26
#define _XOPEN_SOURCE 600 // atoll
#define _GNU_SOURCE
#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <pthread.h>
#include <fcntl.h>
#include <signal.h>
#include <stdint.h>
#include <stdarg.h>
#include <dirent.h>
#include <limits.h>
#include <pwd.h>
#include <stddef.h>
#include <linux/falloc.h>
#define BLOCK_SIZE 4194304 // 4 MB
#define DEFAULT_ROOT "/srv"
struct options {
char *root;
};
struct options options;
typedef struct filetype
{
int (*read)(struct filetype *, char *, size_t, off_t);
int (*write)(struct filetype *, const char *, size_t, off_t);
int (*fsync)(struct filetype *, int);
int (*close)(struct filetype *);
} filetype;
typedef struct volumefile
{
struct filetype of; // has to be the first item so we can pretend it's a ft
int fd;
char path[PATH_MAX];
uint8_t *map;
uint64_t size;
struct volumefile *next;
struct snapfile *snapshots;
} volumefile;
typedef struct snapfile
{
struct filetype of;
int fd;
uint8_t *map;
uint64_t mapsize;
struct volumefile *parent;
struct snapfile *next;
} snapfile;
// mutext that protects the global volume_cache and per-vf linked lists
static pthread_mutex_t dmut;
volumefile *global_volume_cache = NULL;
int global_debug = 0;
// UTIL SHIT
void debugf(char *fmt, ...)
{
if (global_debug)
{
va_list args;
va_start(args, fmt);
fputs("!!! ", stderr);
vfprintf(stderr, fmt, args);
va_end(args);
putc('\n', stderr);
}
}
void debugperror(char *msg)
{
if (global_debug)
{
char buf[1024];
strerror_r(errno, buf, sizeof(buf));
fprintf(stderr, "!!! %s: %s", msg, buf);
}
}
void restore_block(volumefile *vf, int block)
{
// TODO fetch from cold store
vf->map[block] = 1;
}
void volume_path(char *out, const char *volume_path, const char *filename)
{
strcpy(out, volume_path);
if (out[strlen(out)] != '/')
strcat(out, "/");
strcat(out, filename);
}
uint64_t volume_size(const char *path)
{
char sizefile[PATH_MAX];
volume_path(sizefile, path, "size");
FILE *fp = fopen(sizefile, "r");
if (fread(sizefile, 1, sizeof(sizefile), fp))
{
fclose(fp);
return atoll(sizefile);
}
return -1;
}
void copy_block(int from_fd, int to_fd, int block)
{
char block_buf[64 * 1024];
uint64_t pos = block * BLOCK_SIZE, to = (block + 1) * BLOCK_SIZE;
while (pos < to)
{
int read_amt = pread(from_fd, block_buf, pos, sizeof(block_buf));
if (read_amt + pos > to)
read_amt = to - pos;
pos += pwrite(to_fd, block_buf, read_amt, pos);
}
}
void lock_block(int fd, int block, int lock)
{
struct flock fl = {
.l_type = lock ? F_WRLCK : F_UNLCK,
.l_whence = SEEK_SET,
.l_start = block * BLOCK_SIZE,
.l_len = 1,
};
fcntl(fd, F_SETLKW, &fl);
}
// VOLUME SHIT
static int volume_read(struct filetype *of, char *buf, size_t len, off_t offset)
{
volumefile *vf = (volumefile *)of;
int block = offset / BLOCK_SIZE;
if (len > (BLOCK_SIZE - (offset % BLOCK_SIZE)))
len = (BLOCK_SIZE - (offset % BLOCK_SIZE));
if (!vf->map[block])
{
lock_block(vf->fd, block, 1);
if (!vf->map[block])
restore_block(vf, block);
lock_block(vf->fd, block, 0);
}
return pread(vf->fd, (void *)buf, len, offset);
}
static int volume_write(struct filetype *of, const char *buf,
size_t len, off_t offset)
{
int block = offset / BLOCK_SIZE;
volumefile *vf = (volumefile *)of;
if (len > (BLOCK_SIZE - (offset % BLOCK_SIZE)))
len = (BLOCK_SIZE - (offset % BLOCK_SIZE));
if (!vf->map[block] || vf->snapshots)
{
struct snapfile *sf;
lock_block(vf->fd, block, 1);
if (!vf->map[block])
restore_block(vf, block);
for (sf = vf->snapshots; sf; sf = sf->next)
if (!sf->map[block])
{
lock_block(sf->fd, block, 1);
if (!sf->map[block])
{
copy_block(vf->fd, sf->fd, block);
sf->map[block] = 1;
}
lock_block(sf->fd, block, 0);
}
lock_block(vf->fd, block, 0);
}
return pwrite(vf->fd, (void *)buf, len, offset);
}
static int volume_fsync(struct filetype *of, int data)
{
volumefile *vf = (volumefile *)of;
msync(vf->map, vf->size / BLOCK_SIZE, MS_ASYNC);
return fdatasync(vf->fd);
}
struct filetype *open_volume(const char *path)
{
volumefile *vf;
pthread_mutex_lock(&dmut);
for (vf = global_volume_cache; vf; vf = vf->next)
{
if (!strncasecmp(vf->path, path, sizeof(vf->path)))
{
pthread_mutex_unlock(&dmut);
return (filetype *)vf;
}
}
vf = (volumefile *)malloc(sizeof(volumefile));
vf->size = volume_size(path);
strncpy(vf->path, path, sizeof(vf->path));
vf->of.read = volume_read;
vf->of.write = volume_write;
vf->of.fsync = volume_fsync;
vf->of.close = NULL;
vf->next = global_volume_cache;
global_volume_cache = vf;
vf->snapshots = NULL;
char datafile[PATH_MAX];
volume_path(datafile, path, "volume");
vf->fd = open(datafile, O_CREAT | O_RDWR, 0666);
if (posix_fallocate(vf->fd, 0, vf->size))
debugperror("posix_fallocate volume");
volume_path(datafile, path, "map");
int mapfile = open(datafile, O_CREAT | O_RDWR, 0666);
if (posix_fallocate(mapfile, 0, vf->size / BLOCK_SIZE))
debugperror("posix_fallocate mapfile");
vf->map = mmap(NULL, vf->size / BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED,
mapfile, 0);
memset(vf->map, 0x1, vf->size / BLOCK_SIZE); // TODO: temporary for demo purposes
close(mapfile);
pthread_mutex_unlock(&dmut);
return (filetype *)vf;
}
// SNAPSHOT SHIT
static int snapshot_read(struct filetype *of, char *buf, size_t len,
off_t offset)
{
snapfile *sf = (snapfile *)of;
int block = offset / BLOCK_SIZE;
if (len > (BLOCK_SIZE - (offset % BLOCK_SIZE)))
len = (BLOCK_SIZE - (offset % BLOCK_SIZE));
if (sf->map[block])
return pread(sf->fd, (void *)buf, len, offset);
else if (sf->parent->map[block])
return sf->parent->of.read((struct filetype *)(sf->parent), buf,
BLOCK_SIZE, block * BLOCK_SIZE);
else
return -ENODATA;
}
static int snapshot_write(struct filetype *of, const char *buf,
size_t len, off_t offset)
{
return -ENOSYS; // let's not worry about writable snapshots for now...
}
static int snapshot_fsync(struct filetype *of, int data)
{
return -ENOSYS;
}
static int snapshot_close(struct filetype *of)
{
pthread_mutex_lock(&dmut);
snapfile *sf = (snapfile *)of;
if (sf->parent->snapshots == sf)
sf->parent->snapshots = sf->next;
else
{
snapfile *itf;
for (itf = sf->parent->snapshots; itf->next; itf = itf->next)
if (itf->next == sf)
{
itf->next = itf->next->next;
break;
}
}
close(sf->fd);
free(sf->map);
free(sf);
pthread_mutex_unlock(&dmut);
return 0;
}
struct filetype *open_snapshot(const char *path)
{
pthread_mutex_lock(&dmut);
char volumepath[PATH_MAX], datafile[PATH_MAX];
int len = strlen(path);
strcpy(volumepath, path);
if (len > 9 && !strcmp(&volumepath[len - 9], ".snapshot"))
strcpy(&volumepath[len - 9], ".volume");
snapfile *sf = (snapfile *)malloc(sizeof(snapfile));
sf->of.read = snapshot_read;
sf->of.write = snapshot_write;
sf->of.fsync = snapshot_fsync;
sf->of.close = snapshot_close;
uint64_t size = volume_size(volumepath);
volume_path(datafile, volumepath, "snapshot");
sf->fd = open(datafile, O_CREAT | O_RDWR, 0666);
unlink(datafile);
if (ftruncate(sf->fd, size))
debugperror("ftruncate snapshot");
sf->map = malloc(size / BLOCK_SIZE);
volumefile *vf;
for (vf = global_volume_cache; vf; vf = vf->next)
{
if (!strncasecmp(vf->path, volumepath, sizeof(vf->path)))
{
sf->next = vf->snapshots;
vf->snapshots = sf;
sf->parent = vf;
}
}
pthread_mutex_unlock(&dmut);
return (filetype *)sf;
}
// FUSE SHIT
static int cbs_getattr(const char *path, struct stat *stbuf)
{
char localpath[PATH_MAX] = ".";
strcat(localpath, path);
int len = strlen(localpath);
if (len > 9 && !strcmp(&localpath[len - 9], ".snapshot"))
{
strcpy(&localpath[len - 9], ".volume");
len -= 2;
}
if (len > 7 && !strcmp(&localpath[len - 7], ".volume"))
{
stat(localpath, stbuf);
stbuf->st_size = volume_size(localpath);
stbuf->st_mode = S_IFREG | 0666;
stbuf->st_nlink = 1;
return 0;
}
else
return -stat(localpath, stbuf);
}
static int cbs_fgetattr(const char *path, struct stat *stbuf,
struct fuse_file_info *info)
{
return cbs_getattr(path, stbuf);
}
static int cbs_readdir(const char *path, void *buf, fuse_fill_dir_t filldir,
off_t offset, struct fuse_file_info *info)
{
char localpath[PATH_MAX] = ".";
strcat(localpath, path);
DIR *dir = opendir(localpath);
struct dirent entry, *result;
if (!dir)
return -errno;
filldir(buf, ".", NULL, 0);
filldir(buf, "..", NULL, 0);
do
{
readdir_r(dir, &entry, &result);
if (result)
{
int len = strlen(result->d_name);
filldir(buf, result->d_name, NULL, 0);
if (len > 7 && !strcmp(&result->d_name[len - 7], ".volume"))
{
char snappath[PATH_MAX];
strcpy(snappath, result->d_name);
strcpy(&snappath[len - 7], ".snapshot");
filldir(buf, snappath, NULL, 0);
}
}
} while (result);
return 0;
}
static int cbs_open(const char *path, struct fuse_file_info *info)
{
char localpath[PATH_MAX] = ".";
strcat(localpath, path);
int len = strlen(localpath);
info->direct_io = 0;
if (len > 7 && !strcmp(&localpath[len - 7], ".volume"))
info->fh = (uintptr_t)open_volume(localpath);
else if (len > 9 && !strcmp(&localpath[len - 9], ".snapshot"))
{
strcpy(&localpath[len - 9], ".volume");
info->fh = (uintptr_t)open_volume(localpath);
}
return 0;
}
static int cbs_read(const char *path, char *buf, size_t size,
off_t offset, struct fuse_file_info *info)
{
return ((filetype *)(uintptr_t)info->fh)->read(
(filetype *)(uintptr_t)info->fh, buf, size, offset);
}
static int cbs_release(const char *path, struct fuse_file_info *info)
{
filetype *of = (filetype *)(uintptr_t)info->fh;
if (of->close)
return of->close(of);
else
return 0;
}
static int cbs_write(const char *path, const char *buf, size_t length,
off_t offset, struct fuse_file_info *info)
{
return ((filetype *)(uintptr_t)info->fh)->write(
(filetype *)(uintptr_t)info->fh, buf, length, offset);
}
static int cbs_fsync(const char *path, int data, struct fuse_file_info *info)
{
return ((filetype *)(uintptr_t)info->fh)->fsync(
(filetype *)(uintptr_t)info->fh, data);
}
static void *cbs_init(struct fuse_conn_info *conn)
{
if (chdir(options.root))
debugperror("chdir");
return NULL;
}
static int cbs_statfs(const char *path, struct statvfs *stat)
{
stat->f_bsize = 4096;
stat->f_frsize = 4096;
stat->f_blocks = INT_MAX;
stat->f_bfree = INT_MAX;
stat->f_bavail = INT_MAX;
stat->f_files = INT_MAX;
stat->f_ffree = INT_MAX;
stat->f_favail = INT_MAX;
stat->f_namemax = INT_MAX;
return 0;
}
int main(int argc, char **argv)
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv),
_tmpargs = FUSE_ARGS_INIT(argc, argv);
fuse_parse_cmdline(&_tmpargs, NULL, NULL, &global_debug);
static struct fuse_operations cbs_oper =
{
.readdir = cbs_readdir,
.open = cbs_open,
.fgetattr = cbs_fgetattr,
.getattr = cbs_getattr,
.release = cbs_release,
.read = cbs_read,
.write = cbs_write,
.fsync = cbs_fsync,
.statfs = cbs_statfs,
.init = cbs_init,
};
options.root = DEFAULT_ROOT;
static struct fuse_opt cbs_opts[] =
{
{"root=%s", offsetof(struct options, root), 0},
FUSE_OPT_END
};
pthread_mutex_init(&dmut, NULL);
signal(SIGPIPE, SIG_IGN);
fuse_opt_parse(&args, &options, cbs_opts, NULL);
return fuse_main(args.argc, args.argv, &cbs_oper, NULL);
}