forked from geky/littlefs-fuse-test-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfs_fuse.c
528 lines (427 loc) · 13.4 KB
/
lfs_fuse.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
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
/*
* FUSE wrapper for the littlefs
*
* Copyright (c) 2017, Arm Limited. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
*/
#define FUSE_USE_VERSION 26
#ifdef linux
// needed for a few things fuse depends on
#define _XOPEN_SOURCE 700
#endif
#include <fuse/fuse.h>
#include "lfs.h"
#include "lfs_util.h"
#include "lfs_fuse_bd.h"
#include <stdio.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
// config and other state
static struct lfs_config config = {0};
static const char *device = NULL;
static bool format = false;
static bool migrate = false;
static lfs_t lfs;
// actual fuse functions
void lfs_fuse_defaults(struct lfs_config *config) {
// default to 512 erase cycles, arbitrary value
if (!config->block_cycles) {
config->block_cycles = 512;
}
// defaults, ram is less of a concern here than what
// littlefs is used to, so these may end up a bit funny
if (!config->prog_size) {
config->prog_size = config->block_size;
}
if (!config->read_size) {
config->read_size = config->block_size;
}
if (!config->cache_size) {
config->cache_size = config->block_size;
}
// arbitrary, though we have a lot of RAM here
if (!config->lookahead_size) {
config->lookahead_size = 8192;
}
}
void *lfs_fuse_init(struct fuse_conn_info *conn) {
// set that we want to take care of O_TRUNC
conn->want |= FUSE_CAP_ATOMIC_O_TRUNC;
// we also support writes of any size
conn->want |= FUSE_CAP_BIG_WRITES;
return 0;
}
int lfs_fuse_format(void) {
int err = lfs_fuse_bd_create(&config, device);
if (err) {
return err;
}
lfs_fuse_defaults(&config);
err = lfs_format(&lfs, &config);
lfs_fuse_bd_destroy(&config);
return err;
}
int lfs_fuse_migrate(void) {
int err = lfs_fuse_bd_create(&config, device);
if (err) {
return err;
}
lfs_fuse_defaults(&config);
err = lfs_migrate(&lfs, &config);
lfs_fuse_bd_destroy(&config);
return err;
}
int lfs_fuse_mount(void) {
int err = lfs_fuse_bd_create(&config, device);
if (err) {
return err;
}
lfs_fuse_defaults(&config);
return lfs_mount(&lfs, &config);
}
void lfs_fuse_destroy(void *eh) {
lfs_unmount(&lfs);
lfs_fuse_bd_destroy(&config);
}
int lfs_fuse_statfs(const char *path, struct statvfs *s) {
memset(s, 0, sizeof(struct statvfs));
lfs_ssize_t in_use = lfs_fs_size(&lfs);
if (in_use < 0) {
return in_use;
}
s->f_bsize = config.block_size;
s->f_frsize = config.block_size;
s->f_blocks = config.block_count;
s->f_bfree = config.block_count - in_use;
s->f_bavail = config.block_count - in_use;
s->f_namemax = config.name_max;
return 0;
}
static void lfs_fuse_tostat(struct stat *s, struct lfs_info *info) {
memset(s, 0, sizeof(struct stat));
s->st_size = info->size;
s->st_mode = S_IRWXU | S_IRWXG | S_IRWXO;
switch (info->type) {
case LFS_TYPE_DIR: s->st_mode |= S_IFDIR; break;
case LFS_TYPE_REG: s->st_mode |= S_IFREG; break;
}
}
int lfs_fuse_getattr(const char *path, struct stat *s) {
struct lfs_info info;
int err = lfs_stat(&lfs, path, &info);
if (err) {
return err;
}
lfs_fuse_tostat(s, &info);
return 0;
}
int lfs_fuse_access(const char *path, int mask) {
struct lfs_info info;
return lfs_stat(&lfs, path, &info);
}
int lfs_fuse_mkdir(const char *path, mode_t mode) {
return lfs_mkdir(&lfs, path);
}
int lfs_fuse_opendir(const char *path, struct fuse_file_info *fi) {
lfs_dir_t *dir = malloc(sizeof(lfs_dir_t));
memset(dir, 0, sizeof(lfs_dir_t));
int err = lfs_dir_open(&lfs, dir, path);
if (err) {
free(dir);
return err;
}
fi->fh = (uint64_t)dir;
return 0;
}
int lfs_fuse_releasedir(const char *path, struct fuse_file_info *fi) {
lfs_dir_t *dir = (lfs_dir_t*)fi->fh;
int err = lfs_dir_close(&lfs, dir);
free(dir);
return err;
}
int lfs_fuse_readdir(const char *path, void *buf,
fuse_fill_dir_t filler, off_t offset,
struct fuse_file_info *fi) {
lfs_dir_t *dir = (lfs_dir_t*)fi->fh;
struct stat s;
struct lfs_info info;
while (true) {
int err = lfs_dir_read(&lfs, dir, &info);
if (err != 1) {
return err;
}
lfs_fuse_tostat(&s, &info);
filler(buf, info.name, &s, 0);
}
}
int lfs_fuse_rename(const char *from, const char *to) {
return lfs_rename(&lfs, from, to);
}
int lfs_fuse_unlink(const char *path) {
return lfs_remove(&lfs, path);
}
int lfs_fuse_open(const char *path, struct fuse_file_info *fi) {
lfs_file_t *file = malloc(sizeof(lfs_file_t));
memset(file, 0, sizeof(lfs_file_t));
int flags = 0;
if ((fi->flags & 3) == O_RDONLY) flags |= LFS_O_RDONLY;
if ((fi->flags & 3) == O_WRONLY) flags |= LFS_O_WRONLY;
if ((fi->flags & 3) == O_RDWR) flags |= LFS_O_RDWR;
if (fi->flags & O_CREAT) flags |= LFS_O_CREAT;
if (fi->flags & O_EXCL) flags |= LFS_O_EXCL;
if (fi->flags & O_TRUNC) flags |= LFS_O_TRUNC;
if (fi->flags & O_APPEND) flags |= LFS_O_APPEND;
int err = lfs_file_open(&lfs, file, path, flags);
if (err) {
free(file);
return err;
}
fi->fh = (uint64_t)file;
return 0;
}
int lfs_fuse_release(const char *path, struct fuse_file_info *fi) {
lfs_file_t *file = (lfs_file_t*)fi->fh;
int err = lfs_file_close(&lfs, file);
free(file);
return err;
}
int lfs_fuse_fgetattr(const char *path, struct stat *s,
struct fuse_file_info *fi) {
lfs_file_t *file = (lfs_file_t*)fi->fh;
lfs_fuse_tostat(s, &(struct lfs_info){
.size = lfs_file_size(&lfs, file),
.type = LFS_TYPE_REG,
});
return 0;
}
int lfs_fuse_read(const char *path, char *buf, size_t size,
off_t off, struct fuse_file_info *fi) {
lfs_file_t *file = (lfs_file_t*)fi->fh;
if (lfs_file_tell(&lfs, file) != off) {
lfs_soff_t soff = lfs_file_seek(&lfs, file, off, LFS_SEEK_SET);
if (soff < 0) {
return soff;
}
}
return lfs_file_read(&lfs, file, buf, size);
}
int lfs_fuse_write(const char *path, const char *buf, size_t size,
off_t off, struct fuse_file_info *fi) {
lfs_file_t *file = (lfs_file_t*)fi->fh;
if (lfs_file_tell(&lfs, file) != off) {
lfs_soff_t soff = lfs_file_seek(&lfs, file, off, LFS_SEEK_SET);
if (soff < 0) {
return soff;
}
}
return lfs_file_write(&lfs, file, buf, size);
}
int lfs_fuse_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi) {
lfs_file_t *file = (lfs_file_t*)fi->fh;
return lfs_file_sync(&lfs, file);
}
int lfs_fuse_flush(const char *path, struct fuse_file_info *fi) {
lfs_file_t *file = (lfs_file_t*)fi->fh;
return lfs_file_sync(&lfs, file);
}
int lfs_fuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) {
int err = lfs_fuse_open(path, fi);
if (err) {
return err;
}
return lfs_fuse_fsync(path, 0, fi);
}
int lfs_fuse_ftruncate(const char *path, off_t size,
struct fuse_file_info *fi) {
lfs_file_t *file = (lfs_file_t*)fi->fh;
return lfs_file_truncate(&lfs, file, size);
}
int lfs_fuse_truncate(const char *path, off_t size) {
lfs_file_t file;
int err = lfs_file_open(&lfs, &file, path, LFS_O_WRONLY);
if (err) {
return err;
}
err = lfs_file_truncate(&lfs, &file, size);
if (err) {
return err;
}
return lfs_file_close(&lfs, &file);
}
// unsupported functions
int lfs_fuse_link(const char *from, const char *to) {
// not supported, fail
return -EPERM;
}
int lfs_fuse_mknod(const char *path, mode_t mode, dev_t dev) {
// not supported, fail
return -EPERM;
}
int lfs_fuse_chmod(const char *path, mode_t mode) {
// not supported, always succeed
return 0;
}
int lfs_fuse_chown(const char *path, uid_t uid, gid_t gid) {
// not supported, fail
return -EPERM;
}
int lfs_fuse_utimens(const char *path, const struct timespec ts[2]) {
// not supported, always succeed
return 0;
}
static struct fuse_operations lfs_fuse_ops = {
.init = lfs_fuse_init,
.destroy = lfs_fuse_destroy,
.statfs = lfs_fuse_statfs,
.getattr = lfs_fuse_getattr,
.access = lfs_fuse_access,
.mkdir = lfs_fuse_mkdir,
.rmdir = lfs_fuse_unlink,
.opendir = lfs_fuse_opendir,
.releasedir = lfs_fuse_releasedir,
.readdir = lfs_fuse_readdir,
.rename = lfs_fuse_rename,
.unlink = lfs_fuse_unlink,
.open = lfs_fuse_open,
.create = lfs_fuse_create,
.truncate = lfs_fuse_truncate,
.release = lfs_fuse_release,
.fgetattr = lfs_fuse_fgetattr,
.read = lfs_fuse_read,
.write = lfs_fuse_write,
.fsync = lfs_fuse_fsync,
.flush = lfs_fuse_flush,
.link = lfs_fuse_link,
.symlink = lfs_fuse_link,
.mknod = lfs_fuse_mknod,
.chmod = lfs_fuse_chmod,
.chown = lfs_fuse_chown,
.utimens = lfs_fuse_utimens,
};
// binding into fuse and general ui
enum lfs_fuse_keys {
KEY_HELP,
KEY_VERSION,
KEY_FORMAT,
KEY_MIGRATE,
};
#define OPT(t, p) { t, offsetof(struct lfs_config, p), 0}
static struct fuse_opt lfs_fuse_opts[] = {
FUSE_OPT_KEY("--format", KEY_FORMAT),
FUSE_OPT_KEY("--migrate", KEY_MIGRATE),
OPT("-b=%" SCNu32, block_size),
OPT("--block_size=%" SCNu32, block_size),
OPT("--block_count=%" SCNu32, block_count),
OPT("--block_cycles=%" SCNu32, block_cycles),
OPT("--read_size=%" SCNu32, read_size),
OPT("--prog_size=%" SCNu32, prog_size),
OPT("--cache_size=%" SCNu32, cache_size),
OPT("--lookahead_size=%" SCNu32, lookahead_size),
OPT("--name_max=%" SCNu32, name_max),
OPT("--file_max=%" SCNu32, file_max),
OPT("--attr_max=%" SCNu32, attr_max),
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_END
};
static const char help_text[] =
"usage: %s [options] device mountpoint\n"
"\n"
"general options:\n"
" -o opt,[opt...] FUSE options\n"
" -h --help print help\n"
" -V --version print version\n"
"\n"
"littlefs options:\n"
" --format format instead of mounting\n"
" --migrate migrate previous version instead of mounting\n"
" -b --block_size logical block size, overrides the block device\n"
" --block_count block count, overrides the block device\n"
" --block_cycles number of erase cycles before eviction (512)\n"
" --read_size readable unit (block_size)\n"
" --prog_size programmable unit (block_size)\n"
" --cache_size size of caches (block_size)\n"
" --lookahead_size size of lookahead buffer (8192)\n"
" --name_max max size of file names (255)\n"
" --file_max max size of file contents (2147483647)\n"
" --attr_max max size of custom attributes (1022)\n"
"\n";
int lfs_fuse_opt_proc(void *data, const char *arg,
int key, struct fuse_args *args) {
// option parsing
switch (key) {
case FUSE_OPT_KEY_NONOPT:
if (!device) {
device = strdup(arg);
return 0;
}
break;
case KEY_FORMAT:
format = true;
return 0;
case KEY_MIGRATE:
migrate = true;
return 0;
case KEY_HELP:
fprintf(stderr, help_text, args->argv[0]);
fuse_opt_add_arg(args, "-ho");
fuse_main(args->argc, args->argv, &lfs_fuse_ops, NULL);
exit(1);
case KEY_VERSION:
fprintf(stderr, "littlefs version: v%d.%d\n",
LFS_VERSION_MAJOR, LFS_VERSION_MINOR);
fprintf(stderr, "littlefs disk version: v%d.%d\n",
LFS_DISK_VERSION_MAJOR, LFS_DISK_VERSION_MINOR);
fuse_opt_add_arg(args, "--version");
fuse_main(args->argc, args->argv, &lfs_fuse_ops, NULL);
exit(0);
}
return 1;
}
int main(int argc, char *argv[]) {
// parse custom options
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
fuse_opt_parse(&args, &config, lfs_fuse_opts, lfs_fuse_opt_proc);
if (!device) {
fprintf(stderr, "missing device parameter\n");
exit(1);
}
if (format) {
// format time, no mount
int err = lfs_fuse_format();
if (err) {
LFS_ERROR("%s", strerror(-err));
exit(-err);
}
exit(0);
}
if (migrate) {
// migrate time, no mount
int err = lfs_fuse_migrate();
if (err) {
LFS_ERROR("%s", strerror(-err));
exit(-err);
}
exit(0);
}
// go ahead and mount so errors are reported before backgrounding
int err = lfs_fuse_mount();
if (err) {
LFS_ERROR("%s", strerror(-err));
exit(-err);
}
// always single-threaded
fuse_opt_add_arg(&args, "-s");
// enter fuse
err = fuse_main(args.argc, args.argv, &lfs_fuse_ops, NULL);
if (err) {
lfs_fuse_destroy(NULL);
}
return err;
}