-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfuse.c
More file actions
571 lines (449 loc) · 11.7 KB
/
fuse.c
File metadata and controls
571 lines (449 loc) · 11.7 KB
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#define FUSE_USE_VERSION 26
#define _GNU_SOURCE
#include <assert.h>
#include <stdlib.h>
#include <fuse.h>
#include <fuse_opt.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <limits.h>
#include <dirent.h>
#include <pthread.h>
#include <ctype.h>
#include <stdarg.h>
#include <pthread.h>
#include <sys/mman.h>
#include <libgen.h>
#include "zunkfs.h"
#include "chunk-db.h"
#include "utils.h"
#include "dir.h"
#include "file.h"
#ifndef BLOCK_SIZE
#define BLOCK_SIZE 512
#endif
#ifndef CHUNK_BLOCKS
#define CHUNK_BLOCKS (CHUNK_SIZE / BLOCK_SIZE)
#endif
static int zunkfs_calc_size(struct dentry *dentry, void *data)
{
struct statvfs *stbuf = data;
if (S_ISREG(dentry->mode))
stbuf->f_files ++;
stbuf->f_blocks += (uint64_t)dentry_chunk_count(dentry) * CHUNK_BLOCKS;
if (S_ISREG(dentry->mode))
return 0;
return scan_dir(dentry, zunkfs_calc_size, data);
}
static int zunkfs_statfs(const char *path, struct statvfs *stbuf)
{
struct dentry *dentry;
int error;
TRACE("%s\n", path);
/*
* This is a bit painful, as there's no size information
* in the root dentry, nor is there a superblock to record
* total size. So instead, we need to iterate the entire FS
* and calculate the size of each dentry.
*/
dentry = find_dentry(path, NULL);
if (IS_ERR(dentry))
return -PTR_ERR(dentry);
if (!S_ISDIR(dentry->mode)) {
put_dentry(dentry);
return -ENOTDIR;
}
memset(stbuf, 0, sizeof(struct statvfs));
stbuf->f_bsize = BLOCK_SIZE;
stbuf->f_blocks = 2 * CHUNK_BLOCKS; /* root's data & secret chunks */
stbuf->f_bfree = ~0UL;
stbuf->f_bavail = ~0UL;
stbuf->f_files = 0;
stbuf->f_ffree = ~0UL;
stbuf->f_namemax = DDENT_NAME_MAX;
error = scan_dir(dentry, zunkfs_calc_size, stbuf);
put_dentry(dentry);
return error;
}
static int zunkfs_getattr(const char *path, struct stat *stbuf)
{
struct dentry *dentry;
int dir_as_file = 0;
TRACE("%s\n", path);
dentry = find_dentry(path, &dir_as_file);
if (IS_ERR(dentry))
return -PTR_ERR(dentry);
memset(stbuf, 0, sizeof(struct stat));
lock(&dentry->mutex);
/*
* secret_digest does not change during the lifetime
* of a dentry, and it's supposed to be random.
* Hopefully that's good enough to generate a unique
* inode number.
*/
memcpy(&stbuf->st_ino, dentry->ddent->secret_digest, sizeof(ino_t));
stbuf->st_mode = dentry->mode;
/* hardlinks aren't supported */
stbuf->st_nlink = 1;
/* same for different users and groups */
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
stbuf->st_size = dentry->size;
stbuf->st_atime = dentry->mtime.tv_sec;
stbuf->st_mtime = dentry->mtime.tv_sec;
stbuf->st_ctime = le32toh(dentry->ddent->ctime);
stbuf->st_blksize = 4096;
stbuf->st_blocks = (dentry->size + 4095) / 4096;
/* directory should really be treated as a file... */
if (dir_as_file) {
stbuf->st_mode &= ~S_IFDIR;
stbuf->st_mode |= S_IFREG;
stbuf->st_size *= sizeof(struct disk_dentry);
}
unlock(&dentry->mutex);
put_dentry(dentry);
return 0;
}
struct filldir_data {
fuse_fill_dir_t func;
void *buf;
};
static int zunkfs_filldir(struct dentry *dentry, void *data)
{
struct filldir_data *fdd = data;
if (fdd->func(fdd->buf, (char *)dentry->ddent->name, NULL, 0))
return -ENOBUFS;
return 0;
}
static int zunkfs_readdir(const char *path, void *filldir_buf,
fuse_fill_dir_t filldir, off_t offset,
struct fuse_file_info *fuse_file)
{
struct filldir_data fdd;
struct dentry *dentry;
int err;
TRACE("path=%s offset=%llu\n", path, offset);
if (offset)
return -EINVAL;
dentry = find_dentry(path, NULL);
if (IS_ERR(dentry))
return -PTR_ERR(dentry);
err = -ENOTDIR;
if (!S_ISDIR(dentry->mode))
goto out;
err = -ENOBUFS;
if (filldir(filldir_buf, ".", NULL, 0) ||
filldir(filldir_buf, "..", NULL, 0))
goto out;
fdd.func = filldir;
fdd.buf = filldir_buf;
err = scan_dir(dentry, zunkfs_filldir, &fdd);
out:
put_dentry(dentry);
return err;
}
static int zunkfs_open(const char *path, struct fuse_file_info *fuse_file)
{
struct open_file *ofile;
TRACE("%s\n", path);
ofile = open_file(path);
if (IS_ERR(ofile))
return -PTR_ERR(ofile);
fuse_file->fh = (uint64_t)(uintptr_t)ofile;
return 0;
}
static int zunkfs_read(const char *path, char *buf, size_t bufsz, off_t offset,
struct fuse_file_info *fuse_file)
{
struct open_file *ofile;
TRACE("path=%p bufsz=%zd offset=%zd\n", path, bufsz, offset);
ofile = (struct open_file *)(uintptr_t)fuse_file->fh;
if (!ofile)
return -EINVAL;
return read_file(ofile, buf, bufsz, offset);
}
static int zunkfs_write(const char *path, const char *buf, size_t bufsz,
off_t offset, struct fuse_file_info *fuse_file)
{
struct open_file *ofile;
TRACE("path=%p bufsz=%zd offset=%zd\n", path, bufsz, offset);
ofile = (struct open_file *)(uintptr_t)fuse_file->fh;
if (!ofile)
return -EINVAL;
return write_file(ofile, buf, bufsz, offset);
}
static int zunkfs_release(const char *path, struct fuse_file_info *fuse_file)
{
struct open_file *ofile;
TRACE("%s\n", path);
ofile = (struct open_file *)(uintptr_t)fuse_file->fh;
if (!ofile)
return -EINVAL;
fuse_file->fh = 0;
return close_file(ofile);
}
static int zunkfs_mkdir(const char *path, mode_t mode)
{
struct dentry *dentry;
TRACE("%s %o\n", path, mode);
dentry = create_dentry(path, mode | S_IFDIR);
if (IS_ERR(dentry))
return -PTR_ERR(dentry);
put_dentry(dentry);
return 0;
}
static int zunkfs_create(const char *path, mode_t mode,
struct fuse_file_info *fuse_file)
{
struct open_file *ofile;
TRACE("%s mode=%o\n", path, mode);
ofile = create_file(path, mode);
if (IS_ERR(ofile))
return -PTR_ERR(ofile);
if (fuse_file)
fuse_file->fh = (uint64_t)(uintptr_t)ofile;
else
close_file(ofile);
return 0;
}
static int zunkfs_flush(const char *path, struct fuse_file_info *fuse_file)
{
struct open_file *ofile;
TRACE("%s\n", path);
ofile = (struct open_file *)(uintptr_t)fuse_file->fh;
if (!ofile)
return -EINVAL;
return flush_file(ofile);
}
static int zunkfs_unlink(const char *path)
{
struct dentry *dentry;
int err;
TRACE("%s\n", path);
dentry = find_dentry(path, NULL);
err = -PTR_ERR(dentry);
if (!IS_ERR(dentry)) {
err = del_dentry(dentry);
put_dentry(dentry);
}
return err;
}
static int zunkfs_rmdir(const char *path)
{
struct dentry *dentry;
int err;
TRACE("%s\n", path);
dentry = find_dentry(path, NULL);
if (IS_ERR(dentry))
return -PTR_ERR(dentry);
err = -ENOTDIR;
if (!S_ISDIR(dentry->mode))
goto out;
err = -EBUSY;
if (dentry->size)
goto out;
err = del_dentry(dentry);
out:
put_dentry(dentry);
return err;
}
static int zunkfs_utimens(const char *path, const struct timespec tv[2])
{
struct dentry *dentry;
TRACE("%s\n", path);
dentry = find_dentry(path, NULL);
if (IS_ERR(dentry))
return -PTR_ERR(dentry);
lock(&dentry->mutex);
dentry->mtime.tv_sec = tv[1].tv_sec;
dentry->mtime.tv_usec = tv[1].tv_nsec / 1000;
dentry->dirty = 1;
unlock(&dentry->mutex);
put_dentry(dentry);
return 0;
}
static int zunkfs_rename(const char *src, const char *dst)
{
struct dentry *dentry;
struct dentry *dst_parent;
int err;
dentry = find_dentry_parent(dst, &dst_parent, &dst);
if (IS_ERR(dentry))
return -PTR_ERR(dentry);
err = -EEXIST;
if (dentry)
goto out;
dentry = find_dentry(src, NULL);
if (IS_ERR(dentry)) {
err = -PTR_ERR(dentry);
goto out;
}
err = rename_dentry(dentry, dst, dst_parent);
put_dentry(dentry);
out:
put_dentry(dst_parent);
return err;
}
static int zunkfs_chmod(const char *path, mode_t mode)
{
struct dentry *dentry;
if ((mode & S_IFMT) != 0)
return -EINVAL;
dentry = find_dentry(path, NULL);
if (IS_ERR(dentry))
return -PTR_ERR(dentry);
dentry_chmod(dentry, mode & ~S_IFMT);
put_dentry(dentry);
return 0;
}
static struct fuse_operations zunkfs_operations = {
.statfs = zunkfs_statfs,
.getattr = zunkfs_getattr,
.readdir = zunkfs_readdir,
.open = zunkfs_open,
.read = zunkfs_read,
.write = zunkfs_write,
.release = zunkfs_release,
.mkdir = zunkfs_mkdir,
.create = zunkfs_create,
.flush = zunkfs_flush,
.unlink = zunkfs_unlink,
.utimens = zunkfs_utimens,
.rmdir = zunkfs_rmdir,
.rename = zunkfs_rename,
.chmod = zunkfs_chmod
};
static void set_root_file(const char *fs_descr)
{
static DECLARE_MUTEX(root_mutex);
struct disk_dentry *root_ddent;
struct timeval now;
int err, fd;
fd = open(fs_descr, O_RDWR|O_CREAT, 0600);
if (fd < 0) {
ERROR("open(%s): %s\n", fs_descr, strerror(errno));
exit(-1);
}
root_ddent = mmap(NULL, 4096, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
if (root_ddent == MAP_FAILED) {
ERROR("mmap(%s): %s\n", fs_descr, strerror(errno));
exit(-2);
}
if (root_ddent->name[0] == '\0') {
namcpy(root_ddent->name, "/");
gettimeofday(&now, NULL);
root_ddent->mode = htole16(S_IFDIR | S_IRWXU);
root_ddent->size = htole64(0);
root_ddent->ctime = htole32(now.tv_sec);
root_ddent->mtime = htole32(now.tv_sec);
root_ddent->mtime_csec = now.tv_usec / 10000;
err = random_chunk_digest(root_ddent->secret_digest);
if (err < 0) {
ERROR("random_chunk_digest: %s\n", strerror(-err));
exit(-3);
}
memcpy(root_ddent->digest, root_ddent->secret_digest,
CHUNK_DIGEST_LEN);
} else if (root_ddent->name[0] != '/' || root_ddent->name[1]) {
ERROR("Bad superblock.\n");
exit(-4);
}
err = set_root(root_ddent, &root_mutex);
if (err) {
ERROR("Failed to set root: %s\n", strerror(-err));
exit(-5);
}
}
enum {
OPT_HELP,
OPT_LOG,
OPT_CHUNK_DB
};
static struct fuse_opt zunkfs_opts[] = {
FUSE_OPT_KEY("--help", OPT_HELP),
FUSE_OPT_KEY("-h", OPT_HELP),
FUSE_OPT_KEY("--log=%s", OPT_LOG),
FUSE_OPT_KEY("--chunk-db=%s", OPT_CHUNK_DB),
FUSE_OPT_END
};
static const char *prog = NULL;
static void usage(void)
{
/* FIXME: Need to play nicely with FUSE's --help. */
fprintf(stderr,
"usage: %s [options] root_ddent mountpt [mount options]\n"
"zunkfs options:\n"
" --log=[level,]<file> Log zunkfs events. Level is one of (E)rror,\n"
" (W)arning, or (T)race. File can be a file,\n"
" stdout, or stderr.\n"
" --chunk-db=<mode>,<spec> Add chunk storage. Mode is either ro or rw.\n"
" rw can be followed by wt and/or nc. When writing,\n"
" zunkfs will stop at the first writable db, unless\n"
" the db is marked as write-through (wt). A read\n"
" satisfied by chunkdb N will be cached by chunkdbs\n"
" 1...N-1 that are not marked as non-cachable (nc).\n"
" Examples: \n"
" --chunk-db=ro,dir:/foo\n"
" --chunk-db=rw,wt,nc,mem=1000\n"
"\n"
"Available chunk databases:\n", prog);
help_chunkdb();
fprintf(stderr, "\n");
}
static int opt_proc(void *data, const char *arg, int key,
struct fuse_args *args)
{
static unsigned root_set = 0;
char *errstr;
int err;
switch(key) {
case OPT_HELP:
usage();
if (fuse_opt_insert_arg(args, 1, "-ho"))
return -1;
return 0;
case OPT_LOG:
err = set_logging(arg + 6);
if (err) {
fprintf(stderr, "Failed to enable logging: %s\n",
strerror(-err));
return -1;
}
return 0;
case OPT_CHUNK_DB:
arg += 11;
errstr = add_chunkdb(arg);
if (errstr) {
fprintf(stderr, "Failed to add chunkdb \"%s\": %s\n",
arg, STR_OR_ERROR(errstr));
return -1;
}
return 0;
default:
if (arg[0] == '-' || root_set)
return 1;
set_root_file(arg);
root_set = 1;
return 0;
}
}
int main(int argc, char **argv)
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
int err;
prog = basename(argv[0]);
if (fuse_opt_parse(&args, NULL, zunkfs_opts, opt_proc)) {
return -1;
}
err = fuse_main(args.argc, args.argv, &zunkfs_operations, NULL);
if (!err)
flush_root();
return err;
}