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 <[email protected]>
---
 fs/fscache/cookie.c     |  8 +++++++-
 include/linux/fscache.h | 15 +++++++++------
 2 files changed, 16 insertions(+), 7 deletions(-)

v2: don't invalidate if DISABLED bit is already set

diff --git a/fs/fscache/cookie.c b/fs/fscache/cookie.c
index d01d92fbb8d2..c47306589739 100644
--- a/fs/fscache/cookie.c
+++ b/fs/fscache/cookie.c
@@ -427,8 +427,10 @@ void __fscache_unuse_cookie(struct fscache_cookie *cookie,
 {
        if (aux_data || object_size)
                __fscache_update_cookie(cookie, aux_data, object_size);
-       if (atomic_dec_and_test(&cookie->n_active))
+       if (atomic_dec_and_test(&cookie->n_active)) {
+               clear_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags);
                wake_up_var(&cookie->n_active);
+       }
 }
 EXPORT_SYMBOL(__fscache_unuse_cookie);
 
@@ -550,6 +552,10 @@ void __fscache_invalidate(struct fscache_cookie *cookie,
         */
        ASSERTCMP(cookie->type, !=, FSCACHE_COOKIE_TYPE_INDEX);
 
+       if ((flags & FSCACHE_INVAL_DIO_WRITE) &&
+           test_and_set_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
+               return;
+
        spin_lock(&cookie->lock);
        fscache_update_aux(cookie, aux_data, &new_size);
        cookie->zero_point = new_size;
diff --git a/include/linux/fscache.h b/include/linux/fscache.h
index a9c296a23812..2aa7df585b25 100644
--- a/include/linux/fscache.h
+++ b/include/linux/fscache.h
@@ -23,9 +23,11 @@
 #if defined(CONFIG_FSCACHE) || defined(CONFIG_FSCACHE_MODULE)
 #define fscache_available() (1)
 #define fscache_cookie_valid(cookie) (cookie)
+#define fscache_cookie_enabled(cookie) (cookie && 
!test_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
 #else
 #define fscache_available() (0)
 #define fscache_cookie_valid(cookie) (0)
+#define fscache_cookie_enabled(cookie) (0)
 #endif
 
 
@@ -113,6 +115,7 @@ struct fscache_cookie {
 
        unsigned long                   flags;
 #define FSCACHE_COOKIE_RELINQUISHED    6               /* T if cookie has been 
relinquished */
+#define FSCACHE_COOKIE_DISABLED                7               /* T if cookie 
has been disabled */
 
        enum fscache_cookie_stage       stage;
        enum fscache_cookie_type        type:8;
@@ -434,7 +437,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);
 }
 
@@ -451,7 +454,7 @@ void fscache_update_cookie(struct fscache_cookie *cookie, 
const void *aux_data,
 static inline
 void fscache_resize_cookie(struct fscache_cookie *cookie, loff_t new_size)
 {
-       if (fscache_cookie_valid(cookie))
+       if (fscache_cookie_enabled(cookie))
                __fscache_resize_cookie(cookie, new_size);
 }
 
@@ -509,7 +512,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);
 }
 
@@ -568,7 +571,7 @@ void fscache_shape_request(struct fscache_cookie *cookie,
        shape->actual_nr_pages  = shape->proposed_nr_pages;
        shape->actual_start     = shape->proposed_start;
 
-       if (fscache_cookie_valid(cookie))
+       if (fscache_cookie_enabled(cookie))
                __fscache_shape_request(cookie, shape);
        else if (((loff_t)shape->proposed_start << PAGE_SHIFT) >= shape->i_size)
                shape->to_be_done = FSCACHE_FILL_WITH_ZERO;
@@ -599,7 +602,7 @@ void fscache_shape_request(struct fscache_cookie *cookie,
 static inline
 int fscache_read(struct fscache_io_request *req, struct iov_iter *iter)
 {
-       if (fscache_cookie_valid(req->cookie))
+       if (fscache_cookie_enabled(req->cookie))
                return __fscache_read(req, iter);
        req->error = -ENODATA;
        if (req->io_done)
@@ -633,7 +636,7 @@ int fscache_read(struct fscache_io_request *req, struct 
iov_iter *iter)
 static inline
 int fscache_write(struct fscache_io_request *req, struct iov_iter *iter)
 {
-       if (fscache_cookie_valid(req->cookie))
+       if (fscache_cookie_enabled(req->cookie))
                return __fscache_write(req, iter);
        req->error = -ENOBUFS;
        if (req->io_done)
-- 
2.26.2


--
Linux-cachefs mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/linux-cachefs

Reply via email to