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 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);
55 static int nfs_return_empty_folio(struct folio *folio)
57 folio_zero_segment(folio, 0, folio_size(folio));
58 folio_mark_uptodate(folio);
63 void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
64 struct inode *inode, bool force_mds,
65 const struct nfs_pgio_completion_ops *compl_ops)
67 struct nfs_server *server = NFS_SERVER(inode);
68 const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
70 #ifdef CONFIG_NFS_V4_1
71 if (server->pnfs_curr_ld && !force_mds)
72 pg_ops = server->pnfs_curr_ld->pg_read_ops;
74 nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
77 EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
79 void nfs_pageio_complete_read(struct nfs_pageio_descriptor *pgio)
81 struct nfs_pgio_mirror *pgm;
84 nfs_pageio_complete(pgio);
86 /* It doesn't make sense to do mirrored reads! */
87 WARN_ON_ONCE(pgio->pg_mirror_count != 1);
89 pgm = &pgio->pg_mirrors[0];
90 NFS_I(pgio->pg_inode)->read_io += pgm->pg_bytes_written;
91 npages = (pgm->pg_bytes_written + PAGE_SIZE - 1) >> PAGE_SHIFT;
92 nfs_add_stats(pgio->pg_inode, NFSIOS_READPAGES, npages);
96 void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
98 struct nfs_pgio_mirror *mirror;
100 if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
101 pgio->pg_ops->pg_cleanup(pgio);
103 pgio->pg_ops = &nfs_pgio_rw_ops;
105 /* read path should never have more than one mirror */
106 WARN_ON_ONCE(pgio->pg_mirror_count != 1);
108 mirror = &pgio->pg_mirrors[0];
109 mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
111 EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
113 bool nfs_read_alloc_scratch(struct nfs_pgio_header *hdr, size_t size)
115 WARN_ON(hdr->res.scratch != NULL);
116 hdr->res.scratch = kmalloc(size, GFP_KERNEL);
117 return hdr->res.scratch != NULL;
119 EXPORT_SYMBOL_GPL(nfs_read_alloc_scratch);
121 static void nfs_readpage_release(struct nfs_page *req, int error)
123 struct folio *folio = nfs_page_to_folio(req);
125 if (nfs_error_is_fatal_on_server(error) && error != -ETIMEDOUT)
126 folio_set_error(folio);
127 if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE))
128 if (nfs_netfs_folio_unlock(folio))
131 nfs_release_request(req);
134 static void nfs_page_group_set_uptodate(struct nfs_page *req)
136 if (nfs_page_group_sync_on_bit(req, PG_UPTODATE))
137 folio_mark_uptodate(nfs_page_to_folio(req));
140 static void nfs_read_completion(struct nfs_pgio_header *hdr)
142 unsigned long bytes = 0;
145 if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
147 while (!list_empty(&hdr->pages)) {
148 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
149 struct folio *folio = nfs_page_to_folio(req);
150 unsigned long start = req->wb_pgbase;
151 unsigned long end = req->wb_pgbase + req->wb_bytes;
153 if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
154 /* note: regions of the page not covered by a
155 * request are zeroed in nfs_read_add_folio
157 if (bytes > hdr->good_bytes) {
158 /* nothing in this request was good, so zero
159 * the full extent of the request */
160 folio_zero_segment(folio, start, end);
162 } else if (hdr->good_bytes - bytes < req->wb_bytes) {
163 /* part of this request has good bytes, but
164 * not all. zero the bad bytes */
165 start += hdr->good_bytes - bytes;
166 WARN_ON(start < req->wb_pgbase);
167 folio_zero_segment(folio, start, end);
171 bytes += req->wb_bytes;
172 if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
173 if (bytes <= hdr->good_bytes)
174 nfs_page_group_set_uptodate(req);
177 xchg(&nfs_req_openctx(req)->error, error);
180 nfs_page_group_set_uptodate(req);
181 nfs_list_remove_request(req);
182 nfs_readpage_release(req, error);
184 nfs_netfs_read_completion(hdr);
190 static void nfs_initiate_read(struct nfs_pgio_header *hdr,
191 struct rpc_message *msg,
192 const struct nfs_rpc_ops *rpc_ops,
193 struct rpc_task_setup *task_setup_data, int how)
195 rpc_ops->read_setup(hdr, msg);
196 nfs_netfs_initiate_read(hdr);
197 trace_nfs_initiate_read(hdr);
201 nfs_async_read_error(struct list_head *head, int error)
203 struct nfs_page *req;
205 while (!list_empty(head)) {
206 req = nfs_list_entry(head->next);
207 nfs_list_remove_request(req);
208 nfs_readpage_release(req, error);
212 const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
213 .error_cleanup = nfs_async_read_error,
214 .completion = nfs_read_completion,
218 * This is the callback from RPC telling us whether a reply was
219 * received or some error occurred (timeout or socket shutdown).
221 static int nfs_readpage_done(struct rpc_task *task,
222 struct nfs_pgio_header *hdr,
225 int status = NFS_PROTO(inode)->read_done(task, hdr);
229 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count);
230 trace_nfs_readpage_done(task, hdr);
232 if (task->tk_status == -ESTALE) {
233 nfs_set_inode_stale(inode);
234 nfs_mark_for_revalidate(inode);
239 static void nfs_readpage_retry(struct rpc_task *task,
240 struct nfs_pgio_header *hdr)
242 struct nfs_pgio_args *argp = &hdr->args;
243 struct nfs_pgio_res *resp = &hdr->res;
245 /* This is a short read! */
246 nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD);
247 trace_nfs_readpage_short(task, hdr);
249 /* Has the server at least made some progress? */
250 if (resp->count == 0) {
251 nfs_set_pgio_error(hdr, -EIO, argp->offset);
255 /* For non rpc-based layout drivers, retry-through-MDS */
257 hdr->pnfs_error = -EAGAIN;
261 /* Yes, so retry the read at the end of the hdr */
262 hdr->mds_offset += resp->count;
263 argp->offset += resp->count;
264 argp->pgbase += resp->count;
265 argp->count -= resp->count;
268 rpc_restart_call_prepare(task);
271 static void nfs_readpage_result(struct rpc_task *task,
272 struct nfs_pgio_header *hdr)
275 loff_t pos = hdr->args.offset + hdr->res.count;
276 unsigned int new = pos - hdr->io_start;
278 if (hdr->good_bytes > new) {
279 hdr->good_bytes = new;
280 set_bit(NFS_IOHDR_EOF, &hdr->flags);
281 clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
283 } else if (hdr->res.count < hdr->args.count)
284 nfs_readpage_retry(task, hdr);
287 int nfs_read_add_folio(struct nfs_pageio_descriptor *pgio,
288 struct nfs_open_context *ctx,
291 struct inode *inode = folio_file_mapping(folio)->host;
292 struct nfs_server *server = NFS_SERVER(inode);
293 size_t fsize = folio_size(folio);
294 unsigned int rsize = server->rsize;
295 struct nfs_page *new;
296 unsigned int len, aligned_len;
299 len = nfs_folio_length(folio);
301 return nfs_return_empty_folio(folio);
303 aligned_len = min_t(unsigned int, ALIGN(len, rsize), fsize);
305 new = nfs_page_create_from_folio(ctx, folio, 0, aligned_len);
307 error = PTR_ERR(new);
312 folio_zero_segment(folio, len, fsize);
313 if (!nfs_pageio_add_request(pgio, new)) {
314 nfs_list_remove_request(new);
315 error = pgio->pg_error;
316 nfs_readpage_release(new, error);
325 * Read a page over NFS.
326 * We read the page synchronously in the following case:
327 * - The error flag is set for this page. This happens only when a
328 * previous async read operation failed.
330 int nfs_read_folio(struct file *file, struct folio *folio)
332 struct inode *inode = file_inode(file);
333 struct nfs_pageio_descriptor pgio;
334 struct nfs_open_context *ctx;
337 trace_nfs_aop_readpage(inode, folio);
338 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
339 task_io_account_read(folio_size(folio));
342 * Try to flush any pending writes to the file..
344 * NOTE! Because we own the folio lock, there cannot
345 * be any new pending writes generated at this point
346 * for this folio (other folios can be written to).
348 ret = nfs_wb_folio(inode, folio);
351 if (folio_test_uptodate(folio))
355 if (NFS_STALE(inode))
358 ret = nfs_netfs_read_folio(file, folio);
362 ctx = get_nfs_open_context(nfs_file_open_context(file));
364 xchg(&ctx->error, 0);
365 nfs_pageio_init_read(&pgio, inode, false,
366 &nfs_async_read_completion_ops);
368 ret = nfs_read_add_folio(&pgio, ctx, folio);
372 nfs_pageio_complete_read(&pgio);
373 ret = pgio.pg_error < 0 ? pgio.pg_error : 0;
375 ret = folio_wait_locked_killable(folio);
376 if (!folio_test_uptodate(folio) && !ret)
377 ret = xchg(&ctx->error, 0);
380 put_nfs_open_context(ctx);
382 trace_nfs_aop_readpage_done(inode, folio, ret);
389 void nfs_readahead(struct readahead_control *ractl)
391 struct nfs_pageio_descriptor pgio;
392 struct nfs_open_context *ctx;
393 unsigned int nr_pages = readahead_count(ractl);
394 struct file *file = ractl->file;
395 struct inode *inode = ractl->mapping->host;
399 trace_nfs_aop_readahead(inode, readahead_pos(ractl), nr_pages);
400 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
401 task_io_account_read(readahead_length(ractl));
404 if (NFS_STALE(inode))
407 ret = nfs_netfs_readahead(ractl);
413 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
417 ctx = get_nfs_open_context(nfs_file_open_context(file));
419 nfs_pageio_init_read(&pgio, inode, false,
420 &nfs_async_read_completion_ops);
422 while ((folio = readahead_folio(ractl)) != NULL) {
423 ret = nfs_read_add_folio(&pgio, ctx, folio);
428 nfs_pageio_complete_read(&pgio);
430 put_nfs_open_context(ctx);
432 trace_nfs_aop_readahead_done(inode, nr_pages, ret);
435 int __init nfs_init_readpagecache(void)
437 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
438 sizeof(struct nfs_pgio_header),
439 0, SLAB_HWCACHE_ALIGN,
441 if (nfs_rdata_cachep == NULL)
447 void nfs_destroy_readpagecache(void)
449 kmem_cache_destroy(nfs_rdata_cachep);
452 static const struct nfs_rw_ops nfs_rw_read_ops = {
453 .rw_alloc_header = nfs_readhdr_alloc,
454 .rw_free_header = nfs_readhdr_free,
455 .rw_done = nfs_readpage_done,
456 .rw_result = nfs_readpage_result,
457 .rw_initiate = nfs_initiate_read,