From 916b30855878edb7d5af77d9cd2b661a3cd4f733 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 14 Jan 2019 17:53:41 -0600 Subject: [PATCH] Fixed excessive waste from overly large inline files Before this, there were some safety limits, but there was no real default limit to the size of inline files other than the amount of RAM available. On PCs, this meant that inline files were free to fill up directory blocks to a little under the block size. However this is very wasteful in terms of storage space. Because of splitting limits to keep the compact runtime reasonable, each byte of an inline files uses 4x the amount. Fortunately we can find an optimal inline limit: Inline file waste for n bytes = 3n CTZ file waste for n bytes = B - n Where B = block size Solving for n = B/4 So the optimal inline limit is B/4. However, this assumes a perfect inline file and no metadata. We can decrease this to B/8 to give a bit more breathing room for directory+file metadata. --- lfs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lfs.c b/lfs.c index 08ba3f0..5d00d78 100644 --- a/lfs.c +++ b/lfs.c @@ -2710,7 +2710,8 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, if ((file->flags & LFS_F_INLINE) && lfs_max(file->pos+nsize, file->ctz.size) > - lfs_min(lfs->cfg->cache_size, LFS_ATTR_MAX)) { + lfs_min(LFS_ATTR_MAX, lfs_min( + lfs->cfg->cache_size, lfs->cfg->block_size/8))) { // inline file doesn't fit anymore file->off = file->pos; int err = lfs_file_relocate(lfs, file);