]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
netfs: Merge i_size update functions
authorDavid Howells <dhowells@redhat.com>
Tue, 1 Jul 2025 16:38:46 +0000 (17:38 +0100)
committerChristian Brauner <brauner@kernel.org>
Tue, 1 Jul 2025 20:37:14 +0000 (22:37 +0200)
Netfslib has two functions for updating the i_size after a write: one for
buffered writes into the pagecache and one for direct/unbuffered writes.
However, what needs to be done is much the same in both cases, so merge
them together.

This does raise one question, though: should updating the i_size after a
direct write do the same estimated update of i_blocks as is done for
buffered writes.

Also get rid of the cleanup function pointer from netfs_io_request as it's
only used for direct write to update i_size; instead do the i_size setting
directly from write collection.

Signed-off-by: David Howells <dhowells@redhat.com>
Link: https://lore.kernel.org/20250701163852.2171681-12-dhowells@redhat.com
cc: Steve French <sfrench@samba.org>
cc: Paulo Alcantara <pc@manguebit.org>
cc: linux-cifs@vger.kernel.org
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/netfs/buffered_write.c
fs/netfs/direct_write.c
fs/netfs/internal.h
fs/netfs/write_collect.c
include/linux/netfs.h

index b87ef3fe4ea486bd8feeaf7f1aef346deb2cd3f8..f27ea5099a6813002c793b542633b2504d881aad 100644 (file)
@@ -53,30 +53,38 @@ static struct folio *netfs_grab_folio_for_write(struct address_space *mapping,
  * data written into the pagecache until we can find out from the server what
  * the values actually are.
  */
-static void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
-                               loff_t i_size, loff_t pos, size_t copied)
+void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
+                        loff_t pos, size_t copied)
 {
+       loff_t i_size, end = pos + copied;
        blkcnt_t add;
        size_t gap;
 
+       if (end <= i_size_read(inode))
+               return;
+
        if (ctx->ops->update_i_size) {
-               ctx->ops->update_i_size(inode, pos);
+               ctx->ops->update_i_size(inode, end);
                return;
        }
 
        spin_lock(&inode->i_lock);
-       i_size_write(inode, pos);
+
+       i_size = i_size_read(inode);
+       if (end > i_size) {
+               i_size_write(inode, end);
 #if IS_ENABLED(CONFIG_FSCACHE)
-       fscache_update_cookie(ctx->cache, NULL, &pos);
+               fscache_update_cookie(ctx->cache, NULL, &end);
 #endif
 
-       gap = SECTOR_SIZE - (i_size & (SECTOR_SIZE - 1));
-       if (copied > gap) {
-               add = DIV_ROUND_UP(copied - gap, SECTOR_SIZE);
+               gap = SECTOR_SIZE - (i_size & (SECTOR_SIZE - 1));
+               if (copied > gap) {
+                       add = DIV_ROUND_UP(copied - gap, SECTOR_SIZE);
 
-               inode->i_blocks = min_t(blkcnt_t,
-                                       DIV_ROUND_UP(pos, SECTOR_SIZE),
-                                       inode->i_blocks + add);
+                       inode->i_blocks = min_t(blkcnt_t,
+                                               DIV_ROUND_UP(end, SECTOR_SIZE),
+                                               inode->i_blocks + add);
+               }
        }
        spin_unlock(&inode->i_lock);
 }
@@ -113,7 +121,7 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
        struct folio *folio = NULL, *writethrough = NULL;
        unsigned int bdp_flags = (iocb->ki_flags & IOCB_NOWAIT) ? BDP_ASYNC : 0;
        ssize_t written = 0, ret, ret2;
-       loff_t i_size, pos = iocb->ki_pos;
+       loff_t pos = iocb->ki_pos;
        size_t max_chunk = mapping_max_folio_size(mapping);
        bool maybe_trouble = false;
 
@@ -346,10 +354,8 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
                flush_dcache_folio(folio);
 
                /* Update the inode size if we moved the EOF marker */
+               netfs_update_i_size(ctx, inode, pos, copied);
                pos += copied;
-               i_size = i_size_read(inode);
-               if (pos > i_size)
-                       netfs_update_i_size(ctx, inode, i_size, pos, copied);
                written += copied;
 
                if (likely(!wreq)) {
index 3efa5894b2c07d88763989527fa1aea24610f7e7..dcf2b096cc4e13de69a9b2fe0ef73ab65eaadc85 100644 (file)
@@ -9,24 +9,6 @@
 #include <linux/uio.h>
 #include "internal.h"
 
-static void netfs_cleanup_dio_write(struct netfs_io_request *wreq)
-{
-       struct inode *inode = wreq->inode;
-       unsigned long long end = wreq->start + wreq->transferred;
-
-       if (wreq->error || end <= i_size_read(inode))
-               return;
-
-       spin_lock(&inode->i_lock);
-       if (end > i_size_read(inode)) {
-               if (wreq->netfs_ops->update_i_size)
-                       wreq->netfs_ops->update_i_size(inode, end);
-               else
-                       i_size_write(inode, end);
-       }
-       spin_unlock(&inode->i_lock);
-}
-
 /*
  * Perform an unbuffered write where we may have to do an RMW operation on an
  * encrypted file.  This can also be used for direct I/O writes.
@@ -102,7 +84,6 @@ ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *
        if (async)
                wreq->iocb = iocb;
        wreq->len = iov_iter_count(&wreq->buffer.iter);
-       wreq->cleanup = netfs_cleanup_dio_write;
        ret = netfs_unbuffered_write(wreq, is_sync_kiocb(iocb), wreq->len);
        if (ret < 0) {
                _debug("begin = %zd", ret);
index d6656d2b54abd2faaa4c7e3487b199df04635ffa..f9bb9464a147de84c626a3528f76472795000c0a 100644 (file)
@@ -27,6 +27,12 @@ void netfs_cache_read_terminated(void *priv, ssize_t transferred_or_error);
 int netfs_prefetch_for_write(struct file *file, struct folio *folio,
                             size_t offset, size_t len);
 
+/*
+ * buffered_write.c
+ */
+void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
+                        loff_t pos, size_t copied);
+
 /*
  * main.c
  */
index 2ac85a819b71db70da524c286de420ec165256dc..33a93258f36e1b49b29b2a17d5d59a84ee585bd0 100644 (file)
@@ -393,8 +393,10 @@ bool netfs_write_collection(struct netfs_io_request *wreq)
                ictx->ops->invalidate_cache(wreq);
        }
 
-       if (wreq->cleanup)
-               wreq->cleanup(wreq);
+       if ((wreq->origin == NETFS_UNBUFFERED_WRITE ||
+            wreq->origin == NETFS_DIO_WRITE) &&
+           !wreq->error)
+               netfs_update_i_size(ictx, &ictx->inode, wreq->start, wreq->transferred);
 
        if (wreq->origin == NETFS_DIO_WRITE &&
            wreq->mapping->nrpages) {
index 065c17385e5314bcc1797ed95e4251d74c37c381..d8186b90fb38ac12f775e4c2d936ac7f2ea471af 100644 (file)
@@ -279,7 +279,6 @@ struct netfs_io_request {
 #define NETFS_RREQ_USE_PGPRIV2         31      /* [DEPRECATED] Use PG_private_2 to mark
                                                 * write to cache on read */
        const struct netfs_request_ops *netfs_ops;
-       void (*cleanup)(struct netfs_io_request *req);
 };
 
 /*