Skip to content

Commit 5638b06

Browse files
committed
cachefiles: Calculate the blockshift in terms of bytes, not pages
Cachefiles keeps track of how much space is available on the backing filesystem and refuses new writes permission to start if there isn't enough (we especially don't want ENOSPC happening). It also tracks the amount of data pending in DIO writes (cache->b_writing) and reduces the amount of free space available by this amount before deciding if it can set up a new write. However, the old fscache I/O API was very much page-granularity dependent and, as such, cachefiles's cache->bshift was meant to be a multiplier to get from PAGE_SIZE to block size (ie. a blocksize of 512 would give a shift of 3 for a 4KiB page) - and this was incorrectly being used to turn the number of bytes in a DIO write into a number of blocks, leading to a massive over estimation of the amount of data in flight. Fix this by changing cache->bshift to be a multiplier from bytes to blocksize and deal with quantities of blocks, not quantities of pages. Fix also the rounding in the calculation in cachefiles_write() which needs a "- 1" inserting. Fixes: 047487c ("cachefiles: Implement the I/O routines") Signed-off-by: David Howells <[email protected]> Reviewed-by: Jeff Layton <[email protected]> cc: [email protected] Link: https://lore.kernel.org/r/164251398954.3435901.7138806620218474123.stgit@warthog.procyon.org.uk/ # v1
1 parent 80a00ab commit 5638b06

File tree

3 files changed

+4
-7
lines changed

3 files changed

+4
-7
lines changed

fs/cachefiles/cache.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ int cachefiles_add_cache(struct cachefiles_cache *cache)
8484
goto error_unsupported;
8585

8686
cache->bsize = stats.f_bsize;
87-
cache->bshift = 0;
88-
if (stats.f_bsize < PAGE_SIZE)
89-
cache->bshift = PAGE_SHIFT - ilog2(stats.f_bsize);
87+
cache->bshift = ilog2(stats.f_bsize);
9088

9189
_debug("blksize %u (shift %u)",
9290
cache->bsize, cache->bshift);
@@ -106,7 +104,6 @@ int cachefiles_add_cache(struct cachefiles_cache *cache)
106104
(unsigned long long) cache->fcull,
107105
(unsigned long long) cache->fstop);
108106

109-
stats.f_blocks >>= cache->bshift;
110107
do_div(stats.f_blocks, 100);
111108
cache->bstop = stats.f_blocks * cache->bstop_percent;
112109
cache->bcull = stats.f_blocks * cache->bcull_percent;
@@ -209,7 +206,7 @@ int cachefiles_has_space(struct cachefiles_cache *cache,
209206
return ret;
210207
}
211208

212-
b_avail = stats.f_bavail >> cache->bshift;
209+
b_avail = stats.f_bavail;
213210
b_writing = atomic_long_read(&cache->b_writing);
214211
if (b_avail > b_writing)
215212
b_avail -= b_writing;

fs/cachefiles/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ struct cachefiles_cache {
8686
unsigned bcull_percent; /* when to start culling (% blocks) */
8787
unsigned bstop_percent; /* when to stop allocating (% blocks) */
8888
unsigned bsize; /* cache's block size */
89-
unsigned bshift; /* min(ilog2(PAGE_SIZE / bsize), 0) */
89+
unsigned bshift; /* ilog2(bsize) */
9090
uint64_t frun; /* when to stop culling */
9191
uint64_t fcull; /* when to start culling */
9292
uint64_t fstop; /* when to stop allocating */

fs/cachefiles/io.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ static int cachefiles_write(struct netfs_cache_resources *cres,
264264
ki->term_func = term_func;
265265
ki->term_func_priv = term_func_priv;
266266
ki->was_async = true;
267-
ki->b_writing = (len + (1 << cache->bshift)) >> cache->bshift;
267+
ki->b_writing = (len + (1 << cache->bshift) - 1) >> cache->bshift;
268268

269269
if (ki->term_func)
270270
ki->iocb.ki_complete = cachefiles_write_complete;

0 commit comments

Comments
 (0)