From: Jeffle Xu jefflexu@linux.alibaba.com
anolis inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/IB5UKT
Reference: https://gitee.com/anolis/cloud-kernel/commit/04adba02e0fb
--------------------------------
ANBZ: #1666
commit 9032b6e8589f269743984aac53e82e4835be16dc upstream.
Implement the data plane of on-demand read mode.
The early implementation [1] place the entry to cachefiles_ondemand_read() in fscache_read(). However, fscache_read() can only detect if the requested file range is fully cache miss, whilst we need to notify the user daemon as long as there's a hole inside the requested file range.
Thus the entry is now placed in cachefiles_prepare_read(). When working in on-demand read mode, once a hole detected, the read routine will send a READ request to the user daemon. The user daemon needs to fetch the data and write it to the cache file. After sending the READ request, the read routine will hang there, until the READ request is handled by the user daemon. Then it will retry to read from the same file range. If no progress encountered, the read routine will fail then.
A new NETFS_SREQ_ONDEMAND flag is introduced to indicate that on-demand read should be done when a cache miss encountered.
[1] https://lore.kernel.org/all/20220406075612.60298-6-jefflexu@linux.alibaba.co... #v8
Signed-off-by: Jeffle Xu jefflexu@linux.alibaba.com Acked-by: David Howells dhowells@redhat.com Link: https://lore.kernel.org/r/20220425122143.56815-6-jefflexu@linux.alibaba.com Signed-off-by: Gao Xiang hsiangkao@linux.alibaba.com Signed-off-by: Huang Jianan jnhuang@linux.alibaba.com Reviewed-by: Gao Xiang hsiangkao@linux.alibaba.com Reviewed-by: Jeffle Xu jefflexu@linux.alibaba.com Signed-off-by: Baokun Li libaokun1@huawei.com --- fs/cachefiles/internal.h | 8 +++- fs/cachefiles/ondemand.c | 86 +++++++++++++++++++++++++++++++++++ fs/cachefiles/rdwr.c | 21 +++++++-- fs/fscache/page.c | 4 +- include/linux/fscache-cache.h | 1 + include/linux/fscache.h | 18 +++++++- 6 files changed, 129 insertions(+), 9 deletions(-)
diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index c09a405ef42b..7440c3e4fd14 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -258,7 +258,8 @@ extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache,
extern int cachefiles_ondemand_init_object(struct cachefiles_object *object); extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object); - +extern int cachefiles_ondemand_read(struct cachefiles_object *object, + loff_t pos, size_t len); #else static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, char __user *_buffer, size_t buflen, loff_t *pos) @@ -274,6 +275,11 @@ static inline int cachefiles_ondemand_init_object(struct cachefiles_object *obje static inline void cachefiles_ondemand_clean_object(struct cachefiles_object *object) { } +static inline int cachefiles_ondemand_read(struct cachefiles_object *object, + loff_t pos, size_t len) +{ + return -EOPNOTSUPP; +} #endif
/* diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index 26bdcbba92c5..adc257b80b8e 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -11,11 +11,32 @@ static int cachefiles_ondemand_fd_release(struct inode *inode, struct cachefiles_object *object = file->private_data; int object_id = object->ondemand_id; struct cachefiles_cache *cache; + void **slot; + struct radix_tree_iter iter; + struct cachefiles_req *req;
cache = container_of(object->fscache.cache, struct cachefiles_cache, cache);
+ xa_lock(&cache->reqs); object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED; + /* + * Flush all pending READ requests since their completion depends on + * anon_fd. + */ + radix_tree_for_each_slot(slot, &cache->reqs, &iter, 0) { + req = radix_tree_deref_slot_protected(slot, + &cache->reqs.xa_lock); + BUG_ON(!req); + + if (req->msg.opcode == CACHEFILES_OP_READ) { + req->error = -EIO; + complete(&req->done); + radix_tree_iter_delete(&cache->reqs, &iter, slot); + } + } + xa_unlock(&cache->reqs); + xa_lock(&cache->ondemand_ids.idr_rt); idr_remove(&cache->ondemand_ids, object_id); xa_unlock(&cache->ondemand_ids.idr_rt); @@ -58,10 +79,39 @@ static ssize_t cachefiles_ondemand_fd_write_iter(struct kiocb *kiocb, return len; }
+static long cachefiles_ondemand_fd_ioctl(struct file *filp, unsigned int ioctl, + unsigned long arg) +{ + struct cachefiles_object *object = filp->private_data; + struct cachefiles_cache *cache; + struct cachefiles_req *req; + unsigned long id; + + if (ioctl != CACHEFILES_IOC_READ_COMPLETE) + return -EINVAL; + + cache = container_of(object->fscache.cache, + struct cachefiles_cache, cache); + + if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) + return -EOPNOTSUPP; + + id = arg; + xa_lock(&cache->reqs); + req = radix_tree_delete(&cache->reqs, id); + xa_unlock(&cache->reqs); + if (!req) + return -EINVAL; + + complete(&req->done); + return 0; +} + static const struct file_operations cachefiles_ondemand_fd_fops = { .owner = THIS_MODULE, .release = cachefiles_ondemand_fd_release, .write_iter = cachefiles_ondemand_fd_write_iter, + .unlocked_ioctl = cachefiles_ondemand_fd_ioctl, };
/* @@ -402,6 +452,32 @@ static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req, return 0; }
+struct cachefiles_read_ctx { + loff_t off; + size_t len; +}; + +static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req, + void *private) +{ + struct cachefiles_object *object = req->object; + struct cachefiles_read *load = (void *)req->msg.data; + struct cachefiles_read_ctx *read_ctx = private; + int object_id = object->ondemand_id; + + /* Stop enqueuing requests when daemon has closed anon_fd. */ + if (object_id <= 0) { + WARN_ON_ONCE(object_id == 0); + pr_info_once("READ: anonymous fd closed prematurely.\n"); + return -EIO; + } + + req->msg.object_id = object_id; + load->off = read_ctx->off; + load->len = read_ctx->len; + return 0; +} + int cachefiles_ondemand_init_object(struct cachefiles_object *object) { struct fscache_cookie *cookie = object->fscache.cookie; @@ -429,3 +505,13 @@ void cachefiles_ondemand_clean_object(struct cachefiles_object *object) cachefiles_ondemand_send_req(object, CACHEFILES_OP_CLOSE, 0, cachefiles_ondemand_init_close_req, NULL); } + +int cachefiles_ondemand_read(struct cachefiles_object *object, + loff_t pos, size_t len) +{ + struct cachefiles_read_ctx read_ctx = {pos, len}; + + return cachefiles_ondemand_send_req(object, CACHEFILES_OP_READ, + sizeof(struct cachefiles_read), + cachefiles_ondemand_init_read_req, &read_ctx); +} diff --git a/fs/cachefiles/rdwr.c b/fs/cachefiles/rdwr.c index 8ffc40e84a59..7cfbbeee9e87 100644 --- a/fs/cachefiles/rdwr.c +++ b/fs/cachefiles/rdwr.c @@ -233,12 +233,13 @@ static int cachefiles_read_backing_file_one(struct cachefiles_object *object, struct cachefiles_one_read *monitor; struct address_space *bmapping; struct page *newpage, *backpage; + pgoff_t index = op->offset >> PAGE_SHIFT; int ret;
_enter("");
_debug("read back %p{%lu,%d}", - netpage, netpage->index, page_count(netpage)); + netpage, index, page_count(netpage));
monitor = kzalloc(sizeof(*monitor), cachefiles_gfp); if (!monitor) @@ -254,7 +255,7 @@ static int cachefiles_read_backing_file_one(struct cachefiles_object *object, newpage = NULL;
for (;;) { - backpage = find_get_page(bmapping, netpage->index); + backpage = find_get_page(bmapping, index); if (backpage) goto backing_page_already_present;
@@ -265,7 +266,7 @@ static int cachefiles_read_backing_file_one(struct cachefiles_object *object, }
ret = add_to_page_cache_lru(newpage, bmapping, - netpage->index, cachefiles_gfp); + index, cachefiles_gfp); if (ret == 0) goto installed_new_backing_page; if (ret != -EEXIST) @@ -399,6 +400,8 @@ int cachefiles_read_or_alloc_page(struct fscache_retrieval *op, sector_t block; unsigned shift; int ret, ret2; + bool again = true; + loff_t pos = op->offset;
object = container_of(op->op.object, struct cachefiles_object, fscache); @@ -426,14 +429,15 @@ int cachefiles_read_or_alloc_page(struct fscache_retrieval *op, * enough for this as it doesn't indicate errors, but it's all we've * got for the moment */ - block = page->index; +retry: + block = pos >> PAGE_SHIFT; block <<= shift;
ret2 = bmap(inode, &block); ASSERT(ret2 == 0);
_debug("%llx -> %llx", - (unsigned long long) (page->index << shift), + (unsigned long long) (pos >> PAGE_SHIFT << shift), (unsigned long long) block);
if (block) { @@ -441,6 +445,13 @@ int cachefiles_read_or_alloc_page(struct fscache_retrieval *op, * read from disk */ ret = cachefiles_read_backing_file_one(object, op, page); } else if (cachefiles_has_space(cache, 0, 1) == 0) { + if (cachefiles_in_ondemand_mode(cache) && again) { + ret = cachefiles_ondemand_read(object, pos, PAGE_SIZE); + if (!ret) { + again = false; + goto retry; + } + } /* there's space in the cache we can use */ fscache_mark_page_cached(op, page); fscache_retrieval_complete(op, 1); diff --git a/fs/fscache/page.c b/fs/fscache/page.c index 26af6fdf1538..888ace2cc6e1 100644 --- a/fs/fscache/page.c +++ b/fs/fscache/page.c @@ -430,7 +430,7 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie, struct page *page, fscache_rw_complete_t end_io_func, void *context, - gfp_t gfp) + gfp_t gfp, loff_t pos) { struct fscache_retrieval *op; struct fscache_object *object; @@ -493,6 +493,8 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie, if (ret < 0) goto error;
+ op->offset = pos; + /* ask the cache to honour the operation */ if (test_bit(FSCACHE_COOKIE_NO_DATA_YET, &object->cookie->flags)) { fscache_stat(&fscache_n_cop_allocate_page); diff --git a/include/linux/fscache-cache.h b/include/linux/fscache-cache.h index 3f0b19dcfae7..71ee23f78f1d 100644 --- a/include/linux/fscache-cache.h +++ b/include/linux/fscache-cache.h @@ -149,6 +149,7 @@ struct fscache_retrieval { struct list_head to_do; /* list of things to be done by the backend */ unsigned long start_time; /* time at which retrieval started */ atomic_t n_pages; /* number of pages to be retrieved */ + loff_t offset; };
typedef int (*fscache_page_retrieval_func_t)(struct fscache_retrieval *op, diff --git a/include/linux/fscache.h b/include/linux/fscache.h index a1c928fe98e7..ce51b915ad43 100644 --- a/include/linux/fscache.h +++ b/include/linux/fscache.h @@ -204,7 +204,7 @@ extern int __fscache_read_or_alloc_page(struct fscache_cookie *, struct page *, fscache_rw_complete_t, void *, - gfp_t); + gfp_t, loff_t); extern int __fscache_read_or_alloc_pages(struct fscache_cookie *, struct address_space *, struct list_head *, @@ -545,7 +545,21 @@ int fscache_read_or_alloc_page(struct fscache_cookie *cookie, { if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie)) return __fscache_read_or_alloc_page(cookie, page, end_io_func, - context, gfp); + context, gfp, page_offset(page)); + else + return -ENOBUFS; +} + +static inline +int fscache_read_or_alloc_page2(struct fscache_cookie *cookie, + struct page *page, + fscache_rw_complete_t end_io_func, + void *context, + gfp_t gfp, loff_t pos) +{ + if (fscache_cookie_valid(cookie) && fscache_cookie_enabled(cookie)) + return __fscache_read_or_alloc_page(cookie, page, end_io_func, + context, gfp, pos); else return -ENOBUFS; }