[PATCH v3 12/22] erofs: add erofs_fscache_read_page() helper

Jeffle Xu jefflexu at linux.alibaba.com
Wed Feb 9 17:00:58 AEDT 2022


Add erofs_fscache_read_page() helper reading from fscache. It supports
on-demand read semantics. That is, it will make the backend prepare for
the data when cache miss. Once data ready, it will reinitiate a read
from the cache.

This helper can then be used to implement .readpage() of on-demand
reading semantics.

Signed-off-by: Jeffle Xu <jefflexu at linux.alibaba.com>
---
 fs/erofs/fscache.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
index 3addd9aa549c..f4aade711664 100644
--- a/fs/erofs/fscache.c
+++ b/fs/erofs/fscache.c
@@ -6,6 +6,42 @@
 
 static struct fscache_volume *volume;
 
+static int erofs_fscache_read_page(struct fscache_cookie *cookie,
+				   struct page *page, loff_t start_pos)
+{
+	struct netfs_cache_resources cres;
+	struct bio_vec bvec[1];
+	struct iov_iter iter;
+	int ret;
+
+	memset(&cres, 0, sizeof(cres));
+
+	ret = fscache_begin_read_operation(&cres, cookie);
+	if (ret)
+		return ret;
+
+	bvec[0].bv_page         = page;
+	bvec[0].bv_offset       = 0;
+	bvec[0].bv_len          = PAGE_SIZE;
+	iov_iter_bvec(&iter, READ, bvec, ARRAY_SIZE(bvec), PAGE_SIZE);
+
+	ret = fscache_read(&cres, start_pos, &iter,
+			   NETFS_READ_HOLE_FAIL, NULL, NULL);
+	/*
+	 * -ENODATA will be returned when cache miss. In this case, make the
+	 * backend prepare for the data and then reinitiate a read from cache.
+	 */
+	if (ret == -ENODATA) {
+		ret = fscache_ondemand_read(&cres, start_pos, PAGE_SIZE);
+		if (ret == 0)
+			ret = fscache_read(&cres, start_pos, &iter,
+					   NETFS_READ_HOLE_FAIL, NULL, NULL);
+	}
+
+	fscache_end_operation(&cres);
+	return ret;
+}
+
 static const struct address_space_operations erofs_fscache_blob_aops = {
 };
 
-- 
2.27.0



More information about the Linux-erofs mailing list