]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
netfs: Fix decision whether to disallow write-streaming due to fscache use
authorDavid Howells <dhowells@redhat.com>
Thu, 25 Jun 2026 14:06:19 +0000 (15:06 +0100)
committerChristian Brauner <brauner@kernel.org>
Wed, 1 Jul 2026 13:26:28 +0000 (15:26 +0200)
netfs_perform_write() buffers data by writing it into the pagecache for
later writeback.  If the folio it wants to write to isn't present, it uses
"write streaming" in which is will store partial data in a non-uptodate,
but dirty folio.

However, when fscache is in use, this is a potential problem as writes to
the cache have to be aligned to the cache backend's DIO granularity, and so
netfs_perform_write() attempts to suppress write-streaming in such a case,
requiring the folio content to be fetched first unless the entire folio is
going to be overwritten.  This allows the content to be written to the
cache too.

Unfortunately, the test netfs_perform_write() uses isn't correct because it
doesn't take into account the fact that the object lookup is asynchronous
and farmed off to a work queue, so there's a short window in which the
cache is doing a lookup but the test fails because the answer is undefined.

This can be triggered by the generic/464 xfstest, and causes a warning to
be emitted in cachefiles (in code not yet upstream) because it sees a write
that doesn't have its bounds rounded out to DIO alignment.

Fix this by changing the condition to whether FSCACHE_COOKIE_IS_CACHING is
set on a cookie rather than whether the cookie is marked enabled.  Note
that this is really just a hint as to whether we allow write streaming or
not and no other aspects of the cookie or cache object are accessed.

Also apply the same fix to netfs_write_begin().

Reported-by: Marc Dionne <marc.dionne@auristor.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Link: https://patch.msgid.link/20260625140640.3116900-2-dhowells@redhat.com
cc: Paulo Alcantara <pc@manguebit.org>
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
fs/netfs/buffered_read.c
fs/netfs/buffered_write.c
fs/netfs/internal.h

index 76d0f6a29abab2a465444041af6d15a0d6a8783c..24a8a5418e31190355d56360ed5129835b12484d 100644 (file)
@@ -659,7 +659,7 @@ retry:
         * within the cache granule containing the EOF, in which case we need
         * to preload the granule.
         */
-       if (!netfs_is_cache_enabled(ctx) &&
+       if (!netfs_is_cache_maybe_enabled(ctx) &&
            netfs_skip_folio_read(folio, pos, len, false)) {
                netfs_stat(&netfs_n_rh_write_zskip);
                goto have_folio_no_wait;
index 6bde3320bcec6603b34dc811a2f079678424ad17..2cdb68e6b16fff67017557654f197a8b7e88cc13 100644 (file)
@@ -277,7 +277,7 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
                 * caching service temporarily because the backing store got
                 * culled.
                 */
-               if (netfs_is_cache_enabled(ctx)) {
+               if (netfs_is_cache_maybe_enabled(ctx)) {
                        if (finfo) {
                                netfs_stat(&netfs_n_wh_wstream_conflict);
                                goto flush_content;
index 645996ecfc80368af9cba9166194a2cadba51631..d889caa401dc2428910d25cf94d6ae2248ff9f67 100644 (file)
@@ -239,6 +239,18 @@ static inline bool netfs_is_cache_enabled(struct netfs_inode *ctx)
 #endif
 }
 
+static inline bool netfs_is_cache_maybe_enabled(struct netfs_inode *ctx)
+{
+#if IS_ENABLED(CONFIG_FSCACHE)
+       struct fscache_cookie *cookie = ctx->cache;
+
+       return fscache_cookie_valid(cookie) &&
+               test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags);
+#else
+       return false;
+#endif
+}
+
 /*
  * Get a ref on a netfs group attached to a dirty page (e.g. a ceph snap).
  */