WIP Better implementation of inline files, now with overflowing

This commit is contained in:
Christopher Haster
2018-03-17 20:32:16 -05:00
parent fcc5c764e4
commit 9064fa6dbb
2 changed files with 60 additions and 33 deletions

91
lfs.c
View File

@@ -544,6 +544,7 @@ static int lfs_commit_disk(lfs_t *lfs, struct lfs_commit *c,
} }
} }
// TODO handle overflowing reads (zero?)
static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir, static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir,
struct lfs_region *regions) { struct lfs_region *regions) {
// state for copying over // state for copying over
@@ -1142,6 +1143,8 @@ int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) {
break; break;
} }
// TODO common info constructor?
// TODO also used in lfs_stat
info->type = 0xf & entry.d.type; info->type = 0xf & entry.d.type;
if (entry.d.type == (LFS_STRUCT_CTZ | LFS_TYPE_REG)) { if (entry.d.type == (LFS_STRUCT_CTZ | LFS_TYPE_REG)) {
info->size = entry.d.u.file.size; info->size = entry.d.u.file.size;
@@ -1442,23 +1445,6 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
return LFS_ERR_EXIST; return LFS_ERR_EXIST;
} }
// setup file struct
file->pair[0] = cwd.pair[0];
file->pair[1] = cwd.pair[1];
file->poff = entry.off;
file->head = entry.d.u.file.head;
file->size = entry.d.u.file.size;
file->flags = flags;
file->pos = 0;
if (flags & LFS_O_TRUNC) {
if (file->size != 0) {
file->flags |= LFS_F_DIRTY;
}
file->head = 0xffffffff;
file->size = 0;
}
// allocate buffer if needed // allocate buffer if needed
file->cache.block = 0xffffffff; file->cache.block = 0xffffffff;
if (lfs->cfg->file_buffer) { if (lfs->cfg->file_buffer) {
@@ -1475,6 +1461,25 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
} }
} }
// TODO combine these below?
// setup file struct
file->pair[0] = cwd.pair[0];
file->pair[1] = cwd.pair[1];
file->poff = entry.off;
file->head = entry.d.u.file.head;
file->size = entry.d.u.file.size;
file->flags = flags;
file->pos = 0;
if (flags & LFS_O_TRUNC) {
if (file->size != 0) {
file->flags |= LFS_F_DIRTY;
}
entry.d.type = LFS_STRUCT_INLINE | LFS_TYPE_REG;
entry.d.elen = 0;
}
// load inline files // load inline files
if ((0x70 & entry.d.type) == LFS_STRUCT_INLINE) { if ((0x70 & entry.d.type) == LFS_STRUCT_INLINE) {
file->head = 0xfffffffe; file->head = 0xfffffffe;
@@ -1518,9 +1523,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
} }
static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) { static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) {
relocate: relocate:;
LFS_DEBUG("Bad block at %d", file->block);
// just relocate what exists into new block // just relocate what exists into new block
lfs_block_t nblock; lfs_block_t nblock;
int err = lfs_alloc(lfs, &nblock); int err = lfs_alloc(lfs, &nblock);
@@ -1573,6 +1576,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
} }
if (file->flags & LFS_F_WRITING) { if (file->flags & LFS_F_WRITING) {
file->size = lfs_max(file->pos, file->size);
file->flags &= ~LFS_F_WRITING; file->flags &= ~LFS_F_WRITING;
file->flags |= LFS_F_DIRTY; file->flags |= LFS_F_DIRTY;
} }
@@ -1632,6 +1636,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
break; break;
relocate: relocate:
LFS_DEBUG("Bad block at %d", file->block);
err = lfs_file_relocate(lfs, file); err = lfs_file_relocate(lfs, file);
if (err) { if (err) {
return err; return err;
@@ -1666,7 +1671,7 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) {
return err; return err;
} }
// TODO entry read? // TODO entry read function?
lfs_entry_t entry = {.off = file->poff}; lfs_entry_t entry = {.off = file->poff};
err = lfs_bd_read(lfs, cwd.pair[0], entry.off, err = lfs_bd_read(lfs, cwd.pair[0], entry.off,
&entry.d, sizeof(entry.d)); &entry.d, sizeof(entry.d));
@@ -1675,12 +1680,12 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) {
return err; return err;
} }
LFS_ASSERT((0xf & entry.d.type) == LFS_TYPE_REG); LFS_ASSERT((0xf & entry.d.type) == LFS_TYPE_REG);
entry.size = 4 + entry.d.elen + entry.d.alen + entry.d.nlen;
if (file->flags & LFS_F_INLINE) { if (file->flags & LFS_F_INLINE) {
file->size = lfs_max(file->pos, file->size);
lfs_ssize_t diff = file->size - entry.d.elen; lfs_ssize_t diff = file->size - entry.d.elen;
entry.d.type = LFS_STRUCT_INLINE | LFS_TYPE_REG;
entry.d.elen = file->size; entry.d.elen = file->size;
entry.size = 4 + entry.d.elen + entry.d.alen + entry.d.nlen;
err = lfs_dir_update(lfs, &cwd, &entry, err = lfs_dir_update(lfs, &cwd, &entry,
&(struct lfs_region){ &(struct lfs_region){
@@ -1693,12 +1698,17 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) {
return err; return err;
} }
} else { } else {
lfs_ssize_t diff = sizeof(entry.d) - entry.d.elen;
entry.d.type = LFS_STRUCT_CTZ | LFS_TYPE_REG;
entry.d.elen = sizeof(entry.d);
entry.d.u.file.head = file->head; entry.d.u.file.head = file->head;
entry.d.u.file.size = file->size; entry.d.u.file.size = file->size;
entry.size = 4 + entry.d.elen + entry.d.alen + entry.d.nlen;
// TODO combine up?
err = lfs_dir_update(lfs, &cwd, &entry, err = lfs_dir_update(lfs, &cwd, &entry,
&(struct lfs_region){ &(struct lfs_region){
0, 0, 0, diff,
lfs_commit_mem, &entry.d, sizeof(entry.d)}); lfs_commit_mem, &entry.d, sizeof(entry.d)});
if (err) { if (err) {
return err; return err;
@@ -1737,12 +1747,13 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
nsize = size; nsize = size;
while (nsize > 0) { while (nsize > 0) {
// TODO can this be collapsed?
// check if we need a new block // check if we need a new block
if (!(file->flags & LFS_F_READING) || if (!(file->flags & LFS_F_READING) ||
file->off == lfs->cfg->block_size) { file->off == lfs->cfg->block_size) {
if (file->flags & LFS_F_INLINE) { if (file->flags & LFS_F_INLINE) {
file->block = 0xfffffffe; file->block = 0xfffffffe;
file->off = 0; file->off = file->pos;
} else { } else {
int err = lfs_ctz_find(lfs, &file->cache, NULL, int err = lfs_ctz_find(lfs, &file->cache, NULL,
file->head, file->size, file->head, file->size,
@@ -1806,19 +1817,31 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
} }
} }
while (nsize > 0) { // TODO combine with block allocation?
//printf("pos %d\n", file->pos + nsize); // TODO need to move out if no longer fits in block also
// TODO combine with block allocation? // TODO store INLINE_MAX in superblock?
if (file->pos + nsize >= LFS_INLINE_MAX) { if ((file->pos + nsize >= LFS_INLINE_MAX) ||
file->flags &= ~LFS_F_INLINE; (file->pos + nsize >= lfs->cfg->read_size)) {
int err = lfs_file_relocate(lfs, file);
if (err) {
file->flags |= LFS_F_ERRED;
return err;
} }
file->flags &= ~LFS_F_INLINE;
file->flags |= LFS_F_WRITING;
}
while (nsize > 0) {
// TODO can this be collapsed?
// TODO can we reduce this now that block 0 is never allocated?
// TODO actually, how does this behave if inline max == 0?
// check if we need a new block // check if we need a new block
if (!(file->flags & LFS_F_WRITING) || if (!(file->flags & LFS_F_WRITING) ||
file->off == lfs->cfg->block_size) { file->off == lfs->cfg->block_size) {
if (file->flags & LFS_F_INLINE) { if (file->flags & LFS_F_INLINE) {
file->block = 0xfffffffe; file->block = 0xfffffffe;
file->off = 0; file->off = file->pos;
} else { } else {
if (!(file->flags & LFS_F_WRITING) && file->pos > 0) { if (!(file->flags & LFS_F_WRITING) && file->pos > 0) {
// find out which block we're extending from // find out which block we're extending from
@@ -1910,6 +1933,8 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
return file->pos; return file->pos;
} }
// TODO handle inlining?
// TODO note at least needs tests for trunc on inlined file
int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
if ((file->flags & 3) == LFS_O_RDONLY) { if ((file->flags & 3) == LFS_O_RDONLY) {
return LFS_ERR_BADF; return LFS_ERR_BADF;
@@ -2002,8 +2027,10 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
memset(info, 0, sizeof(*info)); memset(info, 0, sizeof(*info));
info->type = 0xf & entry.d.type; info->type = 0xf & entry.d.type;
if (info->type == LFS_TYPE_REG) { if (entry.d.type == (LFS_STRUCT_CTZ | LFS_TYPE_REG)) {
info->size = entry.d.u.file.size; info->size = entry.d.u.file.size;
} else if (entry.d.type == (LFS_STRUCT_INLINE | LFS_TYPE_REG)) {
info->size = entry.d.elen;
} }
if (lfs_paircmp(entry.d.u.dir, lfs->root) == 0) { if (lfs_paircmp(entry.d.u.dir, lfs->root) == 0) {

View File

@@ -357,7 +357,7 @@ tests/test.py << TEST
TEST TEST
echo "--- Multi-block directory with files ---" echo "--- Multi-block directory with files ---"
tests/test.py -s << TEST tests/test.py << TEST
lfs_mount(&lfs, &cfg) => 0; lfs_mount(&lfs, &cfg) => 0;
lfs_mkdir(&lfs, "prickly-pear") => 0; lfs_mkdir(&lfs, "prickly-pear") => 0;
for (int i = 0; i < $LARGESIZE; i++) { for (int i = 0; i < $LARGESIZE; i++) {