1 // SPDX-License-Identifier: GPL-2.0-only
7 * Partial copy of Linus' read cache modifications to fs/nfs/file.c
8 * modified for async RPC by okir@monad.swb.de
11 #include <linux/time.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/fcntl.h>
15 #include <linux/stat.h>
17 #include <linux/slab.h>
18 #include <linux/task_io_accounting_ops.h>
19 #include <linux/pagemap.h>
20 #include <linux/sunrpc/clnt.h>
21 #include <linux/nfs_fs.h>
22 #include <linux/nfs_page.h>
23 #include <linux/module.h>
32 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
34 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
35 static const struct nfs_rw_ops nfs_rw_read_ops;
37 static struct kmem_cache *nfs_rdata_cachep;
39 static struct nfs_pgio_header *nfs_readhdr_alloc(void)
41 struct nfs_pgio_header *p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
44 p->rw_mode = FMODE_READ;
48 static void nfs_readhdr_free(struct nfs_pgio_header *rhdr)
50 if (rhdr->res.scratch != NULL)
51 kfree(rhdr->res.scratch);
52 kmem_cache_free(nfs_rdata_cachep, rhdr);
56 int nfs_return_empty_page(struct page *page)
58 zero_user(page, 0, PAGE_SIZE);
59 SetPageUptodate(page);
64 void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
65 struct inode *inode, bool force_mds,
66 const struct nfs_pgio_completion_ops *compl_ops)
68 struct nfs_server *server = NFS_SERVER(inode);
69 const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
71 #ifdef CONFIG_NFS_V4_1
72 if (server->pnfs_curr_ld && !force_mds)
73 pg_ops = server->pnfs_curr_ld->pg_read_ops;
75 nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
78 EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
80 static void nfs_pageio_complete_read(struct nfs_pageio_descriptor *pgio)
82 struct nfs_pgio_mirror *pgm;
85 nfs_pageio_complete(pgio);
87 /* It doesn't make sense to do mirrored reads! */
88 WARN_ON_ONCE(pgio->pg_mirror_count != 1);
90 pgm = &pgio->pg_mirrors[0];
91 NFS_I(pgio->pg_inode)->read_io += pgm->pg_bytes_written;
92 npages = (pgm->pg_bytes_written + PAGE_SIZE - 1) >> PAGE_SHIFT;
93 nfs_add_stats(pgio->pg_inode, NFSIOS_READPAGES, npages);
97 void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
99 struct nfs_pgio_mirror *mirror;
101 if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
102 pgio->pg_ops->pg_cleanup(pgio);
104 pgio->pg_ops = &nfs_pgio_rw_ops;
106 /* read path should never have more than one mirror */
107 WARN_ON_ONCE(pgio->pg_mirror_count != 1);
109 mirror = &pgio->pg_mirrors[0];
110 mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
112 EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
114 bool nfs_read_alloc_scratch(struct nfs_pgio_header *hdr, size_t size)
116 WARN_ON(hdr->res.scratch != NULL);
117 hdr->res.scratch = kmalloc(size, GFP_KERNEL);
118 return hdr->res.scratch != NULL;
120 EXPORT_SYMBOL_GPL(nfs_read_alloc_scratch);
122 static void nfs_readpage_release(struct nfs_page *req, int error)
124 struct inode *inode = d_inode(nfs_req_openctx(req)->dentry);
125 struct page *page = req->wb_page;
127 dprintk("NFS: read done (%s/%llu %d@%lld)\n", inode->i_sb->s_id,
128 (unsigned long long)NFS_FILEID(inode), req->wb_bytes,
129 (long long)req_offset(req));
131 if (nfs_error_is_fatal_on_server(error) && error != -ETIMEDOUT)
133 if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) {
134 if (PageUptodate(page))
135 nfs_fscache_write_page(inode, page);
138 nfs_release_request(req);
141 struct nfs_readdesc {
142 struct nfs_pageio_descriptor pgio;
143 struct nfs_open_context *ctx;
146 static void nfs_page_group_set_uptodate(struct nfs_page *req)
148 if (nfs_page_group_sync_on_bit(req, PG_UPTODATE))
149 SetPageUptodate(req->wb_page);
152 static void nfs_read_completion(struct nfs_pgio_header *hdr)
154 unsigned long bytes = 0;
157 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
159 while (!list_empty(&hdr->pages)) {
160 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
161 struct page *page = req->wb_page;
162 unsigned long start = req->wb_pgbase;
163 unsigned long end = req->wb_pgbase + req->wb_bytes;
165 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
166 /* note: regions of the page not covered by a
167 * request are zeroed in readpage_async_filler */
168 if (bytes > hdr->good_bytes) {
169 /* nothing in this request was good, so zero
170 * the full extent of the request */
171 zero_user_segment(page, start, end);
173 } else if (hdr->good_bytes - bytes < req->wb_bytes) {
174 /* part of this request has good bytes, but
175 * not all. zero the bad bytes */
176 start += hdr->good_bytes - bytes;
177 WARN_ON(start < req->wb_pgbase);
178 zero_user_segment(page, start, end);
182 bytes += req->wb_bytes;
183 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
184 if (bytes <= hdr->good_bytes)
185 nfs_page_group_set_uptodate(req);
188 xchg(&nfs_req_openctx(req)->error, error);
191 nfs_page_group_set_uptodate(req);
192 nfs_list_remove_request(req);
193 nfs_readpage_release(req, error);
199 static void nfs_initiate_read(struct nfs_pgio_header *hdr,
200 struct rpc_message *msg,
201 const struct nfs_rpc_ops *rpc_ops,
202 struct rpc_task_setup *task_setup_data, int how)
204 rpc_ops->read_setup(hdr, msg);
205 trace_nfs_initiate_read(hdr);
209 nfs_async_read_error(struct list_head *head, int error)
211 struct nfs_page *req;
213 while (!list_empty(head)) {
214 req = nfs_list_entry(head->next);
215 nfs_list_remove_request(req);
216 nfs_readpage_release(req, error);
220 static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
221 .error_cleanup = nfs_async_read_error,
222 .completion = nfs_read_completion,
226 * This is the callback from RPC telling us whether a reply was
227 * received or some error occurred (timeout or socket shutdown).
229 static int nfs_readpage_done(struct rpc_task *task,
230 struct nfs_pgio_header *hdr,
233 int status = NFS_PROTO(inode)->read_done(task, hdr);
237 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count);
238 trace_nfs_readpage_done(task, hdr);
240 if (task->tk_status == -ESTALE) {
241 nfs_set_inode_stale(inode);
242 nfs_mark_for_revalidate(inode);
247 static void nfs_readpage_retry(struct rpc_task *task,
248 struct nfs_pgio_header *hdr)
250 struct nfs_pgio_args *argp = &hdr->args;
251 struct nfs_pgio_res *resp = &hdr->res;
253 /* This is a short read! */
254 nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD);
255 trace_nfs_readpage_short(task, hdr);
257 /* Has the server at least made some progress? */
258 if (resp->count == 0) {
259 nfs_set_pgio_error(hdr, -EIO, argp->offset);
263 /* For non rpc-based layout drivers, retry-through-MDS */
265 hdr->pnfs_error = -EAGAIN;
269 /* Yes, so retry the read at the end of the hdr */
270 hdr->mds_offset += resp->count;
271 argp->offset += resp->count;
272 argp->pgbase += resp->count;
273 argp->count -= resp->count;
276 rpc_restart_call_prepare(task);
279 static void nfs_readpage_result(struct rpc_task *task,
280 struct nfs_pgio_header *hdr)
283 loff_t pos = hdr->args.offset + hdr->res.count;
284 unsigned int new = pos - hdr->io_start;
286 if (hdr->good_bytes > new) {
287 hdr->good_bytes = new;
288 set_bit(NFS_IOHDR_EOF, &hdr->flags);
289 clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
291 } else if (hdr->res.count < hdr->args.count)
292 nfs_readpage_retry(task, hdr);
296 readpage_async_filler(struct nfs_readdesc *desc, struct page *page)
298 struct inode *inode = page_file_mapping(page)->host;
299 unsigned int rsize = NFS_SERVER(inode)->rsize;
300 struct nfs_page *new;
301 unsigned int len, aligned_len;
304 len = nfs_page_length(page);
306 return nfs_return_empty_page(page);
308 aligned_len = min_t(unsigned int, ALIGN(len, rsize), PAGE_SIZE);
310 if (!IS_SYNC(page->mapping->host)) {
311 error = nfs_fscache_read_page(page->mapping->host, page);
316 new = nfs_create_request(desc->ctx, page, 0, aligned_len);
321 zero_user_segment(page, len, PAGE_SIZE);
322 if (!nfs_pageio_add_request(&desc->pgio, new)) {
323 nfs_list_remove_request(new);
324 error = desc->pgio.pg_error;
325 nfs_readpage_release(new, error);
330 error = PTR_ERR(new);
338 * Read a page over NFS.
339 * We read the page synchronously in the following case:
340 * - The error flag is set for this page. This happens only when a
341 * previous async read operation failed.
343 int nfs_read_folio(struct file *file, struct folio *folio)
345 struct page *page = &folio->page;
346 struct nfs_readdesc desc;
347 struct inode *inode = page_file_mapping(page)->host;
350 trace_nfs_aop_readpage(inode, page);
351 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
352 task_io_account_read(folio_size(folio));
355 * Try to flush any pending writes to the file..
357 * NOTE! Because we own the page lock, there cannot
358 * be any new pending writes generated at this point
359 * for this page (other pages can be written to).
361 ret = nfs_wb_page(inode, page);
364 if (PageUptodate(page))
368 if (NFS_STALE(inode))
373 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
374 if (desc.ctx == NULL)
377 desc.ctx = get_nfs_open_context(nfs_file_open_context(file));
379 xchg(&desc.ctx->error, 0);
380 nfs_pageio_init_read(&desc.pgio, inode, false,
381 &nfs_async_read_completion_ops);
383 ret = readpage_async_filler(&desc, page);
387 nfs_pageio_complete_read(&desc.pgio);
388 ret = desc.pgio.pg_error < 0 ? desc.pgio.pg_error : 0;
390 ret = wait_on_page_locked_killable(page);
391 if (!PageUptodate(page) && !ret)
392 ret = xchg(&desc.ctx->error, 0);
395 put_nfs_open_context(desc.ctx);
396 trace_nfs_aop_readpage_done(inode, page, ret);
400 trace_nfs_aop_readpage_done(inode, page, ret);
404 void nfs_readahead(struct readahead_control *ractl)
406 unsigned int nr_pages = readahead_count(ractl);
407 struct file *file = ractl->file;
408 struct nfs_readdesc desc;
409 struct inode *inode = ractl->mapping->host;
413 trace_nfs_aop_readahead(inode, readahead_pos(ractl), nr_pages);
414 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
415 task_io_account_read(readahead_length(ractl));
418 if (NFS_STALE(inode))
423 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
424 if (desc.ctx == NULL)
427 desc.ctx = get_nfs_open_context(nfs_file_open_context(file));
429 nfs_pageio_init_read(&desc.pgio, inode, false,
430 &nfs_async_read_completion_ops);
432 while ((page = readahead_page(ractl)) != NULL) {
433 ret = readpage_async_filler(&desc, page);
439 nfs_pageio_complete_read(&desc.pgio);
441 put_nfs_open_context(desc.ctx);
443 trace_nfs_aop_readahead_done(inode, nr_pages, ret);
446 int __init nfs_init_readpagecache(void)
448 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
449 sizeof(struct nfs_pgio_header),
450 0, SLAB_HWCACHE_ALIGN,
452 if (nfs_rdata_cachep == NULL)
458 void nfs_destroy_readpagecache(void)
460 kmem_cache_destroy(nfs_rdata_cachep);
463 static const struct nfs_rw_ops nfs_rw_read_ops = {
464 .rw_alloc_header = nfs_readhdr_alloc,
465 .rw_free_header = nfs_readhdr_free,
466 .rw_done = nfs_readpage_done,
467 .rw_result = nfs_readpage_result,
468 .rw_initiate = nfs_initiate_read,