Skip to content

Commit

Permalink
Refactored the updates of in-flight files/dirs
Browse files Browse the repository at this point in the history
Updated to account for changes as a result of commits/compacts. And
changed instances of iteration over both files and dirs to use a single
nested loop.

This does rely implicitly on the structure layout of dirs/files and
their location in lfs_t, which isn't great. But it gets the job done
with less code duplication.
  • Loading branch information
geky committed Oct 16, 2018
1 parent d9a24d0 commit 392b2ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
33 changes: 14 additions & 19 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,6 @@ static int lfs_dir_compact(lfs_t *lfs,
break;

split:
// TODO update dirs that get split here?

// commit no longer fits, need to split dir,
// drop caches and create tail
lfs->pcache.block = 0xffffffff;
Expand Down Expand Up @@ -923,6 +921,18 @@ static int lfs_dir_compact(lfs_t *lfs,
}
}

// update any dirs/files that are affected
for (int i = 0; i < 2; i++) {
for (lfs_file_t *f = ((lfs_file_t**)&lfs->files)[i]; f; f = f->next) {
if (lfs_paircmp(f->pair, dir->pair) == 0 &&
f->id >= begin && f->id < end) {
f->pair[0] = dir->pair[0];
f->pair[1] = dir->pair[1];
f->id -= begin;
}
}
}

return 0;
}

Expand Down Expand Up @@ -1052,19 +1062,17 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir,
}

// update any directories that are affected
// TODO what about pairs? what if we're splitting??
for (lfs_dir_t *d = lfs->dirs; d; d = d->next) {
if (lfs_paircmp(d->m.pair, dir->pair) == 0) {
d->m = *dir;
if (d->id > lfs_tagid(deletetag)) {
d->id -= 1;
d->pos -= 1;
}
}
}

for (lfs_file_t *f = lfs->files; f; f = f->next) {
if (lfs_paircmp(f->pair, dir->pair) == 0) {
for (int i = 0; i < 2; i++) {
for (lfs_file_t *f = ((lfs_file_t**)&lfs->files)[i]; f; f = f->next) {
if (f->id == lfs_tagid(deletetag)) {
f->pair[0] = 0xffffffff;
f->pair[1] = 0xffffffff;
Expand Down Expand Up @@ -3186,8 +3194,6 @@ static int lfs_relocate(lfs_t *lfs,
lfs->root[1] = newpair[1];
}

// TODO update dir list!!?

// clean up bad block, which should now be a desync
return lfs_deorphan(lfs);
}
Expand All @@ -3212,17 +3218,6 @@ static int lfs_relocate(lfs_t *lfs,
}
}

// shift over any dirs/files that are affected
for (int i = 0; i < 2; i++) {
for (lfs_dir_t *d = ((void*[2]){lfs->dirs, lfs->files})[i];
d; d = d->next) {
if (lfs_paircmp(d->m.pair, oldpair) == 0) {
d->m.pair[0] = newpair[0];
d->m.pair[1] = newpair[1];
}
}
}

return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ typedef struct lfs_cache {

typedef struct lfs_file {
struct lfs_file *next;
lfs_block_t pair[2];
uint16_t id;
lfs_block_t pair[2];
struct lfs_ctz {
lfs_block_t head;
lfs_size_t size;
Expand All @@ -313,10 +313,10 @@ typedef struct lfs_file {

typedef struct lfs_dir {
struct lfs_dir *next;
uint16_t id;
struct lfs_mdir m;

lfs_block_t head[2];
uint16_t id;
lfs_off_t pos;
} lfs_dir_t;

Expand Down

0 comments on commit 392b2ac

Please sign in to comment.