[PATCH 44/67] fscache: disable cookie when doing an invalidation for DIO write

From: David Howells
Date: Mon Oct 18 2021 - 11:02:49 EST


From: Jeff Layton <jlayton@xxxxxxxxxx>

O_DIRECT I/O is probably a good indicator that we don't need to be
caching this file at the moment. Disable the cookie by treating it
as we would a NULL cookie after the invalidation completes. Reenable
when the last unuse is done.

Signed-off-by: Jeff Layton <jlayton@xxxxxxxxxx>
Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
---

fs/afs/file.c | 2 +-
fs/fscache/cookie.c | 5 +++++
include/linux/fscache.h | 15 ++++++++++-----
3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/fs/afs/file.c b/fs/afs/file.c
index 5e29d433960d..5e674a503c1b 100644
--- a/fs/afs/file.c
+++ b/fs/afs/file.c
@@ -357,7 +357,7 @@ static bool afs_is_cache_enabled(struct inode *inode)
{
struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode));

- return fscache_cookie_valid(cookie) && cookie->cache_priv;
+ return fscache_cookie_enabled(cookie) && cookie->cache_priv;
}

static int afs_begin_cache_operation(struct netfs_read_request *rreq)
diff --git a/fs/fscache/cookie.c b/fs/fscache/cookie.c
index 8731188a5ac7..70bfbd269652 100644
--- a/fs/fscache/cookie.c
+++ b/fs/fscache/cookie.c
@@ -522,6 +522,7 @@ void __fscache_unuse_cookie(struct fscache_cookie *cookie,
__fscache_update_cookie(cookie, aux_data, object_size);
cookie->unused_at = jiffies;
if (atomic_dec_return(&cookie->n_active) == 0) {
+ clear_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags);
if (test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags)) {
spin_lock(&fscache_cookie_lru_lock);
if (list_empty(&cookie->commit_link)) {
@@ -715,6 +716,10 @@ void __fscache_invalidate(struct fscache_cookie *cookie,
"Trying to invalidate relinquished cookie\n"))
return;

+ if ((flags & FSCACHE_INVAL_DIO_WRITE) &&
+ test_and_set_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
+ return;
+
spin_lock(&cookie->lock);
set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
fscache_update_aux(cookie, aux_data, &new_size);
diff --git a/include/linux/fscache.h b/include/linux/fscache.h
index a29bd81996ea..08663ad7feed 100644
--- a/include/linux/fscache.h
+++ b/include/linux/fscache.h
@@ -28,12 +28,14 @@
#define fscache_volume_valid(volume) (volume)
#define fscache_cookie_valid(cookie) (cookie)
#define fscache_resources_valid(cres) ((cres)->cache_priv)
+#define fscache_cookie_enabled(cookie) (cookie && !test_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
#else
#define __fscache_available (0)
#define fscache_available() (0)
#define fscache_volume_valid(volume) (0)
#define fscache_cookie_valid(cookie) (0)
#define fscache_resources_valid(cres) (false)
+#define fscache_cookie_enabled(cookie) (0)
#endif

struct fscache_cookie;
@@ -43,6 +45,8 @@ struct fscache_cookie;
#define FSCACHE_ADV_WRITE_NOCACHE 0x02 /* Don't cache if written to locally */
#define FSCACHE_ADV_FALLBACK_IO 0x04 /* Going to use the fallback I/O API (dangerous) */

+#define FSCACHE_INVAL_DIO_WRITE 0x01 /* Invalidate due to DIO write */
+
enum fscache_want_stage {
FSCACHE_WANT_PARAMS,
FSCACHE_WANT_WRITE,
@@ -120,6 +124,7 @@ struct fscache_cookie {
#define FSCACHE_COOKIE_NO_DATA_TO_READ 3 /* T if this cookie has nothing to read */
#define FSCACHE_COOKIE_NEEDS_UPDATE 4 /* T if attrs have been updated */
#define FSCACHE_COOKIE_HAS_BEEN_CACHED 5 /* T if cookie needs withdraw-on-relinq */
+#define FSCACHE_COOKIE_DISABLED 6 /* T if cookie has been disabled */
#define FSCACHE_COOKIE_NACC_ELEVATED 8 /* T if n_accesses is incremented */
#define FSCACHE_COOKIE_DO_RELINQUISH 9 /* T if this cookie needs relinquishment */
#define FSCACHE_COOKIE_DO_WITHDRAW 10 /* T if this cookie needs withdrawing */
@@ -352,7 +357,7 @@ static inline
void fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data,
const loff_t *object_size)
{
- if (fscache_cookie_valid(cookie))
+ if (fscache_cookie_enabled(cookie))
__fscache_update_cookie(cookie, aux_data, object_size);
}

@@ -410,7 +415,7 @@ static inline
void fscache_invalidate(struct fscache_cookie *cookie,
const void *aux_data, loff_t size, unsigned int flags)
{
- if (fscache_cookie_valid(cookie))
+ if (fscache_cookie_enabled(cookie))
__fscache_invalidate(cookie, aux_data, size, flags);
}

@@ -449,7 +454,7 @@ static inline
int fscache_begin_read_operation(struct netfs_cache_resources *cres,
struct fscache_cookie *cookie)
{
- if (fscache_cookie_valid(cookie))
+ if (fscache_cookie_enabled(cookie))
return __fscache_begin_read_operation(cres, cookie);
return -ENOBUFS;
}
@@ -578,7 +583,7 @@ static inline void fscache_clear_inode_writeback(struct fscache_cookie *cookie,
static inline
int fscache_fallback_read_page(struct fscache_cookie *cookie, struct page *page)
{
- if (fscache_cookie_valid(cookie))
+ if (fscache_cookie_enabled(cookie))
return __fscache_fallback_read_page(cookie, page);
return -ENOBUFS;
}
@@ -598,7 +603,7 @@ int fscache_fallback_read_page(struct fscache_cookie *cookie, struct page *page)
static inline
int fscache_fallback_write_page(struct fscache_cookie *cookie, struct page *page)
{
- if (fscache_cookie_valid(cookie))
+ if (fscache_cookie_enabled(cookie))
return __fscache_fallback_write_page(cookie, page);
return -ENOBUFS;
}