]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
netfs: Update i_blocks when write committed to pagecache
authorDavid Howells <dhowells@redhat.com>
Fri, 23 Feb 2024 08:04:33 +0000 (08:04 +0000)
committerDavid Howells <dhowells@redhat.com>
Mon, 29 Apr 2024 14:01:42 +0000 (15:01 +0100)
Update i_blocks when i_size is updated when we finish making a write to the
pagecache to reflect the amount of space we think will be consumed.

This maintains cifs commit dbfdff402d89854126658376cbcb08363194d3cd ("smb3:
update allocation size more accurately on write completion") which would
otherwise be removed by the cifs part of the netfs writeback rewrite.

Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
cc: Steve French <sfrench@samba.org>
cc: Shyam Prasad N <nspmangalore@gmail.com>
cc: Rohith Surabattula <rohiths.msft@gmail.com>
cc: linux-cifs@vger.kernel.org
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
cc: linux-mm@kvack.org

fs/netfs/buffered_write.c

index 267b622d923b1fc63507300831c3163ba38d8a19..f7455a579f213405bf6576cbd8055c672abfe48b 100644 (file)
@@ -130,6 +130,37 @@ static struct folio *netfs_grab_folio_for_write(struct address_space *mapping,
                                   mapping_gfp_mask(mapping));
 }
 
+/*
+ * Update i_size and estimate the update to i_blocks to reflect the additional
+ * 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)
+{
+       blkcnt_t add;
+       size_t gap;
+
+       if (ctx->ops->update_i_size) {
+               ctx->ops->update_i_size(inode, pos);
+               return;
+       }
+
+       i_size_write(inode, pos);
+#if IS_ENABLED(CONFIG_FSCACHE)
+       fscache_update_cookie(ctx->cache, NULL, &pos);
+#endif
+
+       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);
+       }
+}
+
 /**
  * netfs_perform_write - Copy data into the pagecache.
  * @iocb: The operation parameters
@@ -351,18 +382,10 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
                trace_netfs_folio(folio, trace);
 
                /* Update the inode size if we moved the EOF marker */
-               i_size = i_size_read(inode);
                pos += copied;
-               if (pos > i_size) {
-                       if (ctx->ops->update_i_size) {
-                               ctx->ops->update_i_size(inode, pos);
-                       } else {
-                               i_size_write(inode, pos);
-#if IS_ENABLED(CONFIG_FSCACHE)
-                               fscache_update_cookie(ctx->cache, NULL, &pos);
-#endif
-                       }
-               }
+               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)) {