All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: linux-cachefs@redhat.com
Cc: Marc Dionne <marc.dionne@auristor.com>,
	linux-afs@lists.infradead.org, dhowells@redhat.com,
	Trond Myklebust <trondmy@hammerspace.com>,
	Anna Schumaker <anna.schumaker@netapp.com>,
	Steve French <sfrench@samba.org>,
	Dominique Martinet <asmadeus@codewreck.org>,
	Jeff Layton <jlayton@kernel.org>,
	Matthew Wilcox <willy@infradead.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Omar Sandoval <osandov@osandov.com>,
	JeffleXu <jefflexu@linux.alibaba.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	linux-afs@lists.infradead.org, linux-nfs@vger.kernel.org,
	linux-cifs@vger.kernel.org, ceph-devel@vger.kernel.org,
	v9fs-developer@lists.sourceforge.net,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3 59/68] afs: Copy local writes to the cache when writing to the server
Date: Thu, 16 Dec 2021 16:22:45 +0000	[thread overview]
Message-ID: <163967176564.1823006.16666056085593949570.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <163967073889.1823006.12237147297060239168.stgit@warthog.procyon.org.uk>

When writing to the server from afs_writepage() or afs_writepages(), copy
the data to the cache object too.

To make this possible, the cookie must have its active users count
incremented when the page is dirtied and kept incremented until we manage
to clean up all the pages.  This allows the writeback to take place after
the last file struct is released.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: linux-afs@lists.infradead.org
cc: linux-cachefs@redhat.com
Link: https://lore.kernel.org/r/163819662333.215744.7531373404219224438.stgit@warthog.procyon.org.uk/ # v1
Link: https://lore.kernel.org/r/163906970998.143852.674420788614608063.stgit@warthog.procyon.org.uk/ # v2
---

 fs/afs/file.c     |    6 ++++
 fs/afs/inode.c    |    8 +++--
 fs/afs/internal.h |    5 +++
 fs/afs/super.c    |    1 +
 fs/afs/write.c    |   87 ++++++++++++++++++++++++++++++++++++++++++++++-------
 5 files changed, 92 insertions(+), 15 deletions(-)

diff --git a/fs/afs/file.c b/fs/afs/file.c
index be23635f35b8..572063dad0b3 100644
--- a/fs/afs/file.c
+++ b/fs/afs/file.c
@@ -416,6 +416,12 @@ static void afs_readahead(struct readahead_control *ractl)
 	netfs_readahead(ractl, &afs_req_ops, NULL);
 }
 
+int afs_write_inode(struct inode *inode, struct writeback_control *wbc)
+{
+	fscache_unpin_writeback(wbc, afs_vnode_cache(AFS_FS_I(inode)));
+	return 0;
+}
+
 /*
  * Adjust the dirty region of the page on truncation or full invalidation,
  * getting rid of the markers altogether if the region is entirely invalidated.
diff --git a/fs/afs/inode.c b/fs/afs/inode.c
index 509208825907..8db902405031 100644
--- a/fs/afs/inode.c
+++ b/fs/afs/inode.c
@@ -761,9 +761,8 @@ int afs_drop_inode(struct inode *inode)
  */
 void afs_evict_inode(struct inode *inode)
 {
-	struct afs_vnode *vnode;
-
-	vnode = AFS_FS_I(inode);
+	struct afs_vnode_cache_aux aux;
+	struct afs_vnode *vnode = AFS_FS_I(inode);
 
 	_enter("{%llx:%llu.%d}",
 	       vnode->fid.vid,
@@ -775,6 +774,9 @@ void afs_evict_inode(struct inode *inode)
 	ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
 
 	truncate_inode_pages_final(&inode->i_data);
+
+	afs_set_cache_aux(vnode, &aux);
+	fscache_clear_inode_writeback(afs_vnode_cache(vnode), inode, &aux);
 	clear_inode(inode);
 
 	while (!list_empty(&vnode->wb_keys)) {
diff --git a/fs/afs/internal.h b/fs/afs/internal.h
index 1d80649aec72..b6f02321fc09 100644
--- a/fs/afs/internal.h
+++ b/fs/afs/internal.h
@@ -1072,6 +1072,7 @@ extern int afs_release(struct inode *, struct file *);
 extern int afs_fetch_data(struct afs_vnode *, struct afs_read *);
 extern struct afs_read *afs_alloc_read(gfp_t);
 extern void afs_put_read(struct afs_read *);
+extern int afs_write_inode(struct inode *, struct writeback_control *);
 
 static inline struct afs_read *afs_get_read(struct afs_read *req)
 {
@@ -1519,7 +1520,11 @@ extern int afs_check_volume_status(struct afs_volume *, struct afs_operation *);
 /*
  * write.c
  */
+#ifdef CONFIG_AFS_FSCACHE
 extern int afs_set_page_dirty(struct page *);
+#else
+#define afs_set_page_dirty __set_page_dirty_nobuffers
+#endif
 extern int afs_write_begin(struct file *file, struct address_space *mapping,
 			loff_t pos, unsigned len, unsigned flags,
 			struct page **pagep, void **fsdata);
diff --git a/fs/afs/super.c b/fs/afs/super.c
index d110def8aa8e..af7cbd9949c5 100644
--- a/fs/afs/super.c
+++ b/fs/afs/super.c
@@ -55,6 +55,7 @@ int afs_net_id;
 static const struct super_operations afs_super_ops = {
 	.statfs		= afs_statfs,
 	.alloc_inode	= afs_alloc_inode,
+	.write_inode	= afs_write_inode,
 	.drop_inode	= afs_drop_inode,
 	.destroy_inode	= afs_destroy_inode,
 	.free_inode	= afs_free_inode,
diff --git a/fs/afs/write.c b/fs/afs/write.c
index ac1341af31e9..e186a3432999 100644
--- a/fs/afs/write.c
+++ b/fs/afs/write.c
@@ -14,14 +14,28 @@
 #include <linux/netfs.h>
 #include "internal.h"
 
+static void afs_write_to_cache(struct afs_vnode *vnode, loff_t start, size_t len,
+			       loff_t i_size, bool caching);
+
+#ifdef CONFIG_AFS_FSCACHE
 /*
- * mark a page as having been made dirty and thus needing writeback
+ * Mark a page as having been made dirty and thus needing writeback.  We also
+ * need to pin the cache object to write back to.
  */
 int afs_set_page_dirty(struct page *page)
 {
-	_enter("");
-	return __set_page_dirty_nobuffers(page);
+	return fscache_set_page_dirty(page, afs_vnode_cache(AFS_FS_I(page->mapping->host)));
+}
+static void afs_folio_start_fscache(bool caching, struct folio *folio)
+{
+	if (caching)
+		folio_start_fscache(folio);
+}
+#else
+static void afs_folio_start_fscache(bool caching, struct folio *folio)
+{
 }
+#endif
 
 /*
  * Prepare to perform part of a write to a page.  Note that len may extend
@@ -114,7 +128,7 @@ int afs_write_end(struct file *file, struct address_space *mapping,
 	unsigned long priv;
 	unsigned int f, from = offset_in_folio(folio, pos);
 	unsigned int t, to = from + copied;
-	loff_t i_size, maybe_i_size;
+	loff_t i_size, write_end_pos;
 
 	_enter("{%llx:%llu},{%lx}",
 	       vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
@@ -132,15 +146,16 @@ int afs_write_end(struct file *file, struct address_space *mapping,
 	if (copied == 0)
 		goto out;
 
-	maybe_i_size = pos + copied;
+	write_end_pos = pos + copied;
 
 	i_size = i_size_read(&vnode->vfs_inode);
-	if (maybe_i_size > i_size) {
+	if (write_end_pos > i_size) {
 		write_seqlock(&vnode->cb_lock);
 		i_size = i_size_read(&vnode->vfs_inode);
-		if (maybe_i_size > i_size)
-			afs_set_i_size(vnode, maybe_i_size);
+		if (write_end_pos > i_size)
+			afs_set_i_size(vnode, write_end_pos);
 		write_sequnlock(&vnode->cb_lock);
+		fscache_update_cookie(afs_vnode_cache(vnode), NULL, &write_end_pos);
 	}
 
 	if (folio_test_private(folio)) {
@@ -419,6 +434,7 @@ static void afs_extend_writeback(struct address_space *mapping,
 				 loff_t start,
 				 loff_t max_len,
 				 bool new_content,
+				 bool caching,
 				 unsigned int *_len)
 {
 	struct pagevec pvec;
@@ -465,7 +481,9 @@ static void afs_extend_writeback(struct address_space *mapping,
 				folio_put(folio);
 				break;
 			}
-			if (!folio_test_dirty(folio) || folio_test_writeback(folio)) {
+			if (!folio_test_dirty(folio) ||
+			    folio_test_writeback(folio) ||
+			    folio_test_fscache(folio)) {
 				folio_unlock(folio);
 				folio_put(folio);
 				break;
@@ -513,6 +531,7 @@ static void afs_extend_writeback(struct address_space *mapping,
 				BUG();
 			if (folio_start_writeback(folio))
 				BUG();
+			afs_folio_start_fscache(caching, folio);
 
 			*_count -= folio_nr_pages(folio);
 			folio_unlock(folio);
@@ -540,6 +559,7 @@ static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping,
 	unsigned int offset, to, len, max_len;
 	loff_t i_size = i_size_read(&vnode->vfs_inode);
 	bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
+	bool caching = fscache_cookie_enabled(afs_vnode_cache(vnode));
 	long count = wbc->nr_to_write;
 	int ret;
 
@@ -547,6 +567,7 @@ static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping,
 
 	if (folio_start_writeback(folio))
 		BUG();
+	afs_folio_start_fscache(caching, folio);
 
 	count -= folio_nr_pages(folio);
 
@@ -573,7 +594,8 @@ static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping,
 		if (len < max_len &&
 		    (to == folio_size(folio) || new_content))
 			afs_extend_writeback(mapping, vnode, &count,
-					     start, max_len, new_content, &len);
+					     start, max_len, new_content,
+					     caching, &len);
 		len = min_t(loff_t, len, max_len);
 	}
 
@@ -586,12 +608,19 @@ static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping,
 	if (start < i_size) {
 		_debug("write back %x @%llx [%llx]", len, start, i_size);
 
+		/* Speculatively write to the cache.  We have to fix this up
+		 * later if the store fails.
+		 */
+		afs_write_to_cache(vnode, start, len, i_size, caching);
+
 		iov_iter_xarray(&iter, WRITE, &mapping->i_pages, start, len);
 		ret = afs_store_data(vnode, &iter, start, false);
 	} else {
 		_debug("write discard %x @%llx [%llx]", len, start, i_size);
 
 		/* The dirty region was entirely beyond the EOF. */
+		fscache_clear_page_bits(afs_vnode_cache(vnode),
+					mapping, start, len, caching);
 		afs_pages_written_back(vnode, start, len);
 		ret = 0;
 	}
@@ -650,6 +679,10 @@ int afs_writepage(struct page *subpage, struct writeback_control *wbc)
 
 	_enter("{%lx},", folio_index(folio));
 
+#ifdef CONFIG_AFS_FSCACHE
+	folio_wait_fscache(folio);
+#endif
+
 	start = folio_index(folio) * PAGE_SIZE;
 	ret = afs_write_back_from_locked_folio(folio_mapping(folio), wbc,
 					       folio, start, LLONG_MAX - start);
@@ -715,10 +748,15 @@ static int afs_writepages_region(struct address_space *mapping,
 			continue;
 		}
 
-		if (folio_test_writeback(folio)) {
+		if (folio_test_writeback(folio) ||
+		    folio_test_fscache(folio)) {
 			folio_unlock(folio);
-			if (wbc->sync_mode != WB_SYNC_NONE)
+			if (wbc->sync_mode != WB_SYNC_NONE) {
 				folio_wait_writeback(folio);
+#ifdef CONFIG_AFS_FSCACHE
+				folio_wait_fscache(folio);
+#endif
+			}
 			folio_put(folio);
 			continue;
 		}
@@ -971,3 +1009,28 @@ int afs_launder_page(struct page *subpage)
 	folio_wait_fscache(folio);
 	return ret;
 }
+
+/*
+ * Deal with the completion of writing the data to the cache.
+ */
+static void afs_write_to_cache_done(void *priv, ssize_t transferred_or_error,
+				    bool was_async)
+{
+	struct afs_vnode *vnode = priv;
+
+	if (IS_ERR_VALUE(transferred_or_error) &&
+	    transferred_or_error != -ENOBUFS)
+		afs_invalidate_cache(vnode, 0);
+}
+
+/*
+ * Save the write to the cache also.
+ */
+static void afs_write_to_cache(struct afs_vnode *vnode,
+			       loff_t start, size_t len, loff_t i_size,
+			       bool caching)
+{
+	fscache_write_to_cache(afs_vnode_cache(vnode),
+			       vnode->vfs_inode.i_mapping, start, len, i_size,
+			       afs_write_to_cache_done, vnode, caching);
+}



  parent reply	other threads:[~2021-12-16 16:22 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-16 16:05 [PATCH v3 00/68] fscache, cachefiles: Rewrite David Howells
2021-12-16 16:05 ` [PATCH v3 01/68] fscache, cachefiles: Disable configuration David Howells
2021-12-16 16:06 ` [PATCH v3 02/68] cachefiles: Delete the cachefiles driver pending rewrite David Howells
2021-12-16 16:06 ` [PATCH v3 03/68] fscache: Remove the contents of the fscache driver, " David Howells
2021-12-16 16:06 ` [PATCH v3 04/68] netfs: Display the netfs inode number in the netfs_read tracepoint David Howells
2021-12-16 16:06 ` [PATCH v3 05/68] netfs: Pass a flag to ->prepare_write() to say if there's no alloc'd space David Howells
2021-12-16 16:06 ` [PATCH v3 06/68] fscache: Introduce new driver David Howells
2021-12-16 16:07 ` [PATCH v3 07/68] fscache: Implement a hash function David Howells
2021-12-16 16:07 ` [PATCH v3 08/68] fscache: Implement cache registration David Howells
2021-12-16 16:07 ` [PATCH v3 09/68] fscache: Implement volume registration David Howells
2021-12-16 16:08 ` [PATCH v3 10/68] fscache: Implement cookie registration David Howells
2021-12-16 16:08 ` [PATCH v3 11/68] fscache: Implement cache-level access helpers David Howells
2021-12-16 16:09 ` [PATCH v3 12/68] fscache: Implement volume-level " David Howells
2021-12-16 16:09 ` [PATCH v3 13/68] fscache: Implement cookie-level " David Howells
2021-12-16 16:09 ` [PATCH v3 14/68] fscache: Implement functions add/remove a cache David Howells
2021-12-16 16:09 ` [PATCH v3 15/68] fscache: Provide and use cache methods to lookup/create/free a volume David Howells
2021-12-16 16:10 ` [PATCH v3 16/68] fscache: Add a function for a cache backend to note an I/O error David Howells
2021-12-16 16:10 ` [PATCH v3 17/68] fscache: Implement simple cookie state machine David Howells
2021-12-17 19:07   ` Jeff Layton
2021-12-17 19:45   ` David Howells
2021-12-16 16:11 ` [PATCH v3 18/68] fscache: Implement cookie user counting and resource pinning David Howells
2021-12-17 19:42   ` Jeff Layton
2021-12-17 20:43   ` David Howells
2021-12-16 16:11 ` [PATCH v3 19/68] fscache: Implement cookie invalidation David Howells
2021-12-16 16:11 ` [PATCH v3 20/68] fscache: Provide a means to begin an operation David Howells
2021-12-16 16:11 ` [PATCH v3 21/68] fscache: Count data storage objects in a cache David Howells
2021-12-16 16:12 ` [PATCH v3 22/68] fscache: Provide read/write stat counters for the cache David Howells
2021-12-16 16:12 ` [PATCH v3 23/68] fscache: Provide a function to let the netfs update its coherency data David Howells
2021-12-16 16:13 ` [PATCH v3 24/68] netfs: Pass more information on how to deal with a hole in the cache David Howells
2021-12-16 16:13 ` [PATCH v3 25/68] fscache: Implement raw I/O interface David Howells
2021-12-16 16:13 ` [PATCH v3 26/68] fscache: Implement higher-level write " David Howells
2021-12-16 16:14 ` [PATCH v3 27/68] vfs, fscache: Implement pinning of cache usage for writeback David Howells
2021-12-16 16:14 ` [PATCH v3 28/68] fscache: Provide a function to note the release of a page David Howells
2021-12-16 16:14 ` [PATCH v3 29/68] fscache: Provide a function to resize a cookie David Howells
2021-12-16 16:15 ` [PATCH v3 30/68] cachefiles: Introduce rewritten driver David Howells
2021-12-16 16:15 ` [PATCH v3 31/68] cachefiles: Define structs David Howells
2021-12-16 16:15 ` [PATCH v3 32/68] cachefiles: Add some error injection support David Howells
2021-12-16 16:15 ` [PATCH v3 33/68] cachefiles: Add a couple of tracepoints for logging errors David Howells
2021-12-16 16:16 ` [PATCH v3 34/68] cachefiles: Add cache error reporting macro David Howells
2021-12-16 16:16 ` [PATCH v3 35/68] cachefiles: Add security derivation David Howells
2021-12-16 16:16 ` [PATCH v3 36/68] cachefiles: Register a miscdev and parse commands over it David Howells
2021-12-16 16:16 ` [PATCH v3 37/68] cachefiles: Provide a function to check how much space there is David Howells
2021-12-16 16:16 ` [PATCH v3 38/68] vfs, cachefiles: Mark a backing file in use with an inode flag David Howells
2021-12-16 16:16 ` [PATCH v3 39/68] cachefiles: Implement a function to get/create a directory in the cache David Howells
2021-12-16 16:17 ` [PATCH v3 40/68] cachefiles: Implement cache registration and withdrawal David Howells
2021-12-16 16:17 ` [PATCH v3 41/68] cachefiles: Implement volume support David Howells
2021-12-16 16:17 ` [PATCH v3 42/68] cachefiles: Add tracepoints for calls to the VFS David Howells
2021-12-16 16:18 ` [PATCH v3 43/68] cachefiles: Implement object lifecycle funcs David Howells
2021-12-16 16:18 ` [PATCH v3 44/68] cachefiles: Implement key to filename encoding David Howells
2021-12-16 16:18 ` [PATCH v3 45/68] cachefiles: Implement metadata/coherency data storage in xattrs David Howells
2021-12-16 16:19 ` [PATCH v3 46/68] cachefiles: Mark a backing file in use with an inode flag David Howells
2021-12-16 16:19 ` [PATCH v3 47/68] cachefiles: Implement culling daemon commands David Howells
2021-12-16 16:19 ` [PATCH v3 48/68] cachefiles: Implement backing file wrangling David Howells
2021-12-16 16:20 ` [PATCH v3 49/68] cachefiles: Implement begin and end I/O operation David Howells
2021-12-16 16:20 ` [PATCH v3 50/68] cachefiles: Implement cookie resize for truncate David Howells
2021-12-16 16:20 ` [PATCH v3 51/68] cachefiles: Implement the I/O routines David Howells
2021-12-16 16:20 ` [PATCH v3 52/68] fscache, cachefiles: Store the volume coherency data David Howells
2021-12-16 16:20 ` [PATCH v3 53/68] cachefiles: Allow cachefiles to actually function David Howells
2021-12-16 16:21 ` [PATCH v3 54/68] fscache, cachefiles: Display stats of no-space events David Howells
2021-12-16 16:21 ` [PATCH v3 55/68] fscache, cachefiles: Display stat of culling events David Howells
2021-12-16 16:21 ` [PATCH v3 56/68] afs: Handle len being extending over page end in write_begin/write_end David Howells
2021-12-16 16:31   ` Linus Torvalds
2021-12-16 19:28     ` Matthew Wilcox
2021-12-16 19:46       ` Linus Torvalds
2021-12-16 20:20         ` Matthew Wilcox
2021-12-16 21:17       ` David Howells
2021-12-16 16:47   ` David Howells
2021-12-16 16:22 ` [PATCH v3 57/68] afs: Fix afs_write_end() to handle len > page size David Howells
2021-12-16 16:27   ` Linus Torvalds
2021-12-16 16:22 ` [PATCH v3 58/68] afs: Convert afs to use the new fscache API David Howells
2021-12-16 16:22 ` David Howells [this message]
2021-12-16 16:22 ` [PATCH v3 60/68] afs: Skip truncation on the server of data we haven't written yet David Howells
2021-12-16 16:23 ` [PATCH v3 61/68] 9p: Use fscache indexing rewrite and reenable caching David Howells
2021-12-16 16:23 ` [PATCH v3 62/68] 9p: Copy local writes to the cache when writing to the server David Howells
2021-12-16 16:23 ` [PATCH v3 63/68] nfs: Convert to new fscache volume/cookie API David Howells
2021-12-17 12:53   ` David Wysochanski
2021-12-17 13:21   ` David Howells
2021-12-17 13:35     ` David Wysochanski
2021-12-16 16:24 ` [PATCH v3 64/68] nfs: Implement cache I/O by accessing the cache directly David Howells
2021-12-16 16:24 ` [PATCH v3 65/68] cifs: Support fscache indexing rewrite (untested) David Howells
2021-12-16 16:24 ` [PATCH v3 66/68] ceph: conversion to new fscache API David Howells
2021-12-16 16:25 ` [PATCH v3 67/68] ceph: add fscache writeback support David Howells
2021-12-16 16:25 ` [PATCH v3 68/68] fscache: Rewrite documentation David Howells
2021-12-17 20:13 ` [PATCH v3 00/68] fscache, cachefiles: Rewrite Jeff Layton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=163967176564.1823006.16666056085593949570.stgit@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=anna.schumaker@netapp.com \
    --cc=asmadeus@codewreck.org \
    --cc=ceph-devel@vger.kernel.org \
    --cc=jefflexu@linux.alibaba.com \
    --cc=jlayton@kernel.org \
    --cc=linux-afs@lists.infradead.org \
    --cc=linux-cachefs@redhat.com \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=marc.dionne@auristor.com \
    --cc=osandov@osandov.com \
    --cc=sfrench@samba.org \
    --cc=torvalds@linux-foundation.org \
    --cc=trondmy@hammerspace.com \
    --cc=v9fs-developer@lists.sourceforge.net \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.