xref: /openbmc/linux/fs/nfs/read.c (revision ab75bff1140733f1b43e81f055acd7d27af7ac05)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/nfs/read.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Block I/O for NFS
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Partial copy of Linus' read cache modifications to fs/nfs/file.c
81da177e4SLinus Torvalds  * modified for async RPC by okir@monad.swb.de
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/time.h>
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/errno.h>
141da177e4SLinus Torvalds #include <linux/fcntl.h>
151da177e4SLinus Torvalds #include <linux/stat.h>
161da177e4SLinus Torvalds #include <linux/mm.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
191da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
201da177e4SLinus Torvalds #include <linux/nfs_fs.h>
211da177e4SLinus Torvalds #include <linux/nfs_page.h>
2264419a9bSAndy Adamson #include <linux/module.h>
231da177e4SLinus Torvalds 
24f11c88afSAndy Adamson #include "nfs4_fs.h"
2549a70f27STrond Myklebust #include "internal.h"
2691d5b470SChuck Lever #include "iostat.h"
279a9fc1c0SDavid Howells #include "fscache.h"
28fab5fc25SChristoph Hellwig #include "pnfs.h"
298224b273SChuck Lever #include "nfstrace.h"
3091d5b470SChuck Lever 
311da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
321da177e4SLinus Torvalds 
33061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
344a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_read_ops;
351da177e4SLinus Torvalds 
36e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep;
371da177e4SLinus Torvalds 
381e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_readhdr_alloc(void)
393feb2d49STrond Myklebust {
40fbe77c30SBenjamin Coddington 	struct nfs_pgio_header *p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
41fbe77c30SBenjamin Coddington 
42fbe77c30SBenjamin Coddington 	if (p)
43fbe77c30SBenjamin Coddington 		p->rw_mode = FMODE_READ;
44fbe77c30SBenjamin Coddington 	return p;
454db6e0b7SFred Isaman }
464db6e0b7SFred Isaman 
471e7f3a48SWeston Andros Adamson static void nfs_readhdr_free(struct nfs_pgio_header *rhdr)
483feb2d49STrond Myklebust {
49cd841605SFred Isaman 	kmem_cache_free(nfs_rdata_cachep, rhdr);
503feb2d49STrond Myklebust }
513feb2d49STrond Myklebust 
52*ab75bff1STrond Myklebust static int nfs_return_empty_folio(struct folio *folio)
531da177e4SLinus Torvalds {
54*ab75bff1STrond Myklebust 	folio_zero_segment(folio, 0, folio_size(folio));
55*ab75bff1STrond Myklebust 	folio_mark_uptodate(folio);
56*ab75bff1STrond Myklebust 	folio_unlock(folio);
571da177e4SLinus Torvalds 	return 0;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
601abb5088SBryan Schumaker void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
61fab5fc25SChristoph Hellwig 			      struct inode *inode, bool force_mds,
62061ae2edSFred Isaman 			      const struct nfs_pgio_completion_ops *compl_ops)
631751c363STrond Myklebust {
64fab5fc25SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
6541d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
66fab5fc25SChristoph Hellwig 
67fab5fc25SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
68fab5fc25SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
69fab5fc25SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_read_ops;
70fab5fc25SChristoph Hellwig #endif
714a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
723bde7afdSTrond Myklebust 			server->rsize, 0);
731751c363STrond Myklebust }
74ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
751751c363STrond Myklebust 
76b42ad64fSDave Wysochanski static void nfs_pageio_complete_read(struct nfs_pageio_descriptor *pgio)
771e83b173SDave Wysochanski {
781e83b173SDave Wysochanski 	struct nfs_pgio_mirror *pgm;
791e83b173SDave Wysochanski 	unsigned long npages;
801e83b173SDave Wysochanski 
811e83b173SDave Wysochanski 	nfs_pageio_complete(pgio);
821e83b173SDave Wysochanski 
831e83b173SDave Wysochanski 	/* It doesn't make sense to do mirrored reads! */
841e83b173SDave Wysochanski 	WARN_ON_ONCE(pgio->pg_mirror_count != 1);
851e83b173SDave Wysochanski 
861e83b173SDave Wysochanski 	pgm = &pgio->pg_mirrors[0];
87b42ad64fSDave Wysochanski 	NFS_I(pgio->pg_inode)->read_io += pgm->pg_bytes_written;
881e83b173SDave Wysochanski 	npages = (pgm->pg_bytes_written + PAGE_SIZE - 1) >> PAGE_SHIFT;
89b42ad64fSDave Wysochanski 	nfs_add_stats(pgio->pg_inode, NFSIOS_READPAGES, npages);
901e83b173SDave Wysochanski }
911e83b173SDave Wysochanski 
921e83b173SDave Wysochanski 
93493292ddSTrond Myklebust void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
94493292ddSTrond Myklebust {
95a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
96a7d42ddbSWeston Andros Adamson 
976f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
986f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
996f29b9bbSKinglong Mee 
10041d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
101a7d42ddbSWeston Andros Adamson 
102a7d42ddbSWeston Andros Adamson 	/* read path should never have more than one mirror */
103a7d42ddbSWeston Andros Adamson 	WARN_ON_ONCE(pgio->pg_mirror_count != 1);
104a7d42ddbSWeston Andros Adamson 
105a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
106a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
107493292ddSTrond Myklebust }
1081f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
109493292ddSTrond Myklebust 
1108f54c7a4STrond Myklebust static void nfs_readpage_release(struct nfs_page *req, int error)
1110bcbf039SPeng Tao {
1129fcd5960STrond Myklebust 	struct inode *inode = d_inode(nfs_req_openctx(req)->dentry);
113*ab75bff1STrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
1140bcbf039SPeng Tao 
1150bcbf039SPeng Tao 	dprintk("NFS: read done (%s/%llu %d@%lld)\n", inode->i_sb->s_id,
1160bcbf039SPeng Tao 		(unsigned long long)NFS_FILEID(inode), req->wb_bytes,
1170bcbf039SPeng Tao 		(long long)req_offset(req));
1180bcbf039SPeng Tao 
1198f54c7a4STrond Myklebust 	if (nfs_error_is_fatal_on_server(error) && error != -ETIMEDOUT)
120*ab75bff1STrond Myklebust 		folio_set_error(folio);
1210bcbf039SPeng Tao 	if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) {
122*ab75bff1STrond Myklebust 		if (folio_test_uptodate(folio))
123*ab75bff1STrond Myklebust 			nfs_fscache_write_page(inode, &folio->page);
124*ab75bff1STrond Myklebust 		folio_unlock(folio);
1250bcbf039SPeng Tao 	}
1260bcbf039SPeng Tao 	nfs_release_request(req);
1270bcbf039SPeng Tao }
1280bcbf039SPeng Tao 
1291af7e7f8SDave Wysochanski struct nfs_readdesc {
1301af7e7f8SDave Wysochanski 	struct nfs_pageio_descriptor pgio;
1311af7e7f8SDave Wysochanski 	struct nfs_open_context *ctx;
1321af7e7f8SDave Wysochanski };
1331af7e7f8SDave Wysochanski 
13467d0338eSWeston Andros Adamson static void nfs_page_group_set_uptodate(struct nfs_page *req)
13567d0338eSWeston Andros Adamson {
13667d0338eSWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_UPTODATE))
137*ab75bff1STrond Myklebust 		folio_mark_uptodate(nfs_page_to_folio(req));
13867d0338eSWeston Andros Adamson }
13967d0338eSWeston Andros Adamson 
140061ae2edSFred Isaman static void nfs_read_completion(struct nfs_pgio_header *hdr)
1414db6e0b7SFred Isaman {
1424db6e0b7SFred Isaman 	unsigned long bytes = 0;
1438f54c7a4STrond Myklebust 	int error;
1444db6e0b7SFred Isaman 
1454db6e0b7SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
1464db6e0b7SFred Isaman 		goto out;
1474db6e0b7SFred Isaman 	while (!list_empty(&hdr->pages)) {
1484db6e0b7SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
149*ab75bff1STrond Myklebust 		struct folio *folio = nfs_page_to_folio(req);
15068072992SWeston Andros Adamson 		unsigned long start = req->wb_pgbase;
15168072992SWeston Andros Adamson 		unsigned long end = req->wb_pgbase + req->wb_bytes;
1524db6e0b7SFred Isaman 
1534db6e0b7SFred Isaman 		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
15468072992SWeston Andros Adamson 			/* note: regions of the page not covered by a
1551e83b173SDave Wysochanski 			 * request are zeroed in readpage_async_filler */
15668072992SWeston Andros Adamson 			if (bytes > hdr->good_bytes) {
15768072992SWeston Andros Adamson 				/* nothing in this request was good, so zero
15868072992SWeston Andros Adamson 				 * the full extent of the request */
159*ab75bff1STrond Myklebust 				folio_zero_segment(folio, start, end);
16068072992SWeston Andros Adamson 
16168072992SWeston Andros Adamson 			} else if (hdr->good_bytes - bytes < req->wb_bytes) {
16268072992SWeston Andros Adamson 				/* part of this request has good bytes, but
16368072992SWeston Andros Adamson 				 * not all. zero the bad bytes */
16468072992SWeston Andros Adamson 				start += hdr->good_bytes - bytes;
16568072992SWeston Andros Adamson 				WARN_ON(start < req->wb_pgbase);
166*ab75bff1STrond Myklebust 				folio_zero_segment(folio, start, end);
16768072992SWeston Andros Adamson 			}
1684db6e0b7SFred Isaman 		}
1698f54c7a4STrond Myklebust 		error = 0;
1704bd8b010STrond Myklebust 		bytes += req->wb_bytes;
1714bd8b010STrond Myklebust 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
1724bd8b010STrond Myklebust 			if (bytes <= hdr->good_bytes)
17367d0338eSWeston Andros Adamson 				nfs_page_group_set_uptodate(req);
1748f54c7a4STrond Myklebust 			else {
1758f54c7a4STrond Myklebust 				error = hdr->error;
1768f54c7a4STrond Myklebust 				xchg(&nfs_req_openctx(req)->error, error);
1778f54c7a4STrond Myklebust 			}
1784bd8b010STrond Myklebust 		} else
17967d0338eSWeston Andros Adamson 			nfs_page_group_set_uptodate(req);
1804db6e0b7SFred Isaman 		nfs_list_remove_request(req);
1818f54c7a4STrond Myklebust 		nfs_readpage_release(req, error);
1824db6e0b7SFred Isaman 	}
1834db6e0b7SFred Isaman out:
1844db6e0b7SFred Isaman 	hdr->release(hdr);
1854db6e0b7SFred Isaman }
1864db6e0b7SFred Isaman 
187d45f60c6SWeston Andros Adamson static void nfs_initiate_read(struct nfs_pgio_header *hdr,
188d45f60c6SWeston Andros Adamson 			      struct rpc_message *msg,
189abde71f4STom Haynes 			      const struct nfs_rpc_ops *rpc_ops,
1901ed26f33SAnna Schumaker 			      struct rpc_task_setup *task_setup_data, int how)
19164419a9bSAndy Adamson {
192abde71f4STom Haynes 	rpc_ops->read_setup(hdr, msg);
1932343172dSTrond Myklebust 	trace_nfs_initiate_read(hdr);
19464419a9bSAndy Adamson }
19564419a9bSAndy Adamson 
196061ae2edSFred Isaman static void
197df3accb8STrond Myklebust nfs_async_read_error(struct list_head *head, int error)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	struct nfs_page	*req;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	while (!list_empty(head)) {
2021da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
2031da177e4SLinus Torvalds 		nfs_list_remove_request(req);
2048f54c7a4STrond Myklebust 		nfs_readpage_release(req, error);
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
208061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
209061ae2edSFred Isaman 	.error_cleanup = nfs_async_read_error,
210061ae2edSFred Isaman 	.completion = nfs_read_completion,
211061ae2edSFred Isaman };
212061ae2edSFred Isaman 
2131da177e4SLinus Torvalds /*
2140b671301STrond Myklebust  * This is the callback from RPC telling us whether a reply was
2150b671301STrond Myklebust  * received or some error occurred (timeout or socket shutdown).
2160b671301STrond Myklebust  */
217d45f60c6SWeston Andros Adamson static int nfs_readpage_done(struct rpc_task *task,
218d45f60c6SWeston Andros Adamson 			     struct nfs_pgio_header *hdr,
2190eecb214SAnna Schumaker 			     struct inode *inode)
2200b671301STrond Myklebust {
221d45f60c6SWeston Andros Adamson 	int status = NFS_PROTO(inode)->read_done(task, hdr);
2220b671301STrond Myklebust 	if (status != 0)
2230b671301STrond Myklebust 		return status;
2240b671301STrond Myklebust 
225d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count);
2262343172dSTrond Myklebust 	trace_nfs_readpage_done(task, hdr);
2270b671301STrond Myklebust 
2280b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
22993ce4af7STrond Myklebust 		nfs_set_inode_stale(inode);
230cd841605SFred Isaman 		nfs_mark_for_revalidate(inode);
2310b671301STrond Myklebust 	}
2320b671301STrond Myklebust 	return 0;
2330b671301STrond Myklebust }
2340b671301STrond Myklebust 
235d45f60c6SWeston Andros Adamson static void nfs_readpage_retry(struct rpc_task *task,
236d45f60c6SWeston Andros Adamson 			       struct nfs_pgio_header *hdr)
2370b671301STrond Myklebust {
238d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args *argp = &hdr->args;
239d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res  *resp = &hdr->res;
2400b671301STrond Myklebust 
2410b671301STrond Myklebust 	/* This is a short read! */
242d45f60c6SWeston Andros Adamson 	nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD);
243fd2b6121SChuck Lever 	trace_nfs_readpage_short(task, hdr);
244fd2b6121SChuck Lever 
2450b671301STrond Myklebust 	/* Has the server at least made some progress? */
2464db6e0b7SFred Isaman 	if (resp->count == 0) {
247d45f60c6SWeston Andros Adamson 		nfs_set_pgio_error(hdr, -EIO, argp->offset);
248d61e612aSTrond Myklebust 		return;
2494db6e0b7SFred Isaman 	}
250f8417b48SKinglong Mee 
251f8417b48SKinglong Mee 	/* For non rpc-based layout drivers, retry-through-MDS */
252f8417b48SKinglong Mee 	if (!task->tk_ops) {
253f8417b48SKinglong Mee 		hdr->pnfs_error = -EAGAIN;
254f8417b48SKinglong Mee 		return;
255f8417b48SKinglong Mee 	}
256f8417b48SKinglong Mee 
257d45f60c6SWeston Andros Adamson 	/* Yes, so retry the read at the end of the hdr */
258d45f60c6SWeston Andros Adamson 	hdr->mds_offset += resp->count;
2590b671301STrond Myklebust 	argp->offset += resp->count;
2600b671301STrond Myklebust 	argp->pgbase += resp->count;
2610b671301STrond Myklebust 	argp->count -= resp->count;
2628c9cb714STrond Myklebust 	resp->count = 0;
2638c9cb714STrond Myklebust 	resp->eof = 0;
264d00c5d43STrond Myklebust 	rpc_restart_call_prepare(task);
2650b671301STrond Myklebust }
2660b671301STrond Myklebust 
267d45f60c6SWeston Andros Adamson static void nfs_readpage_result(struct rpc_task *task,
268d45f60c6SWeston Andros Adamson 				struct nfs_pgio_header *hdr)
2691da177e4SLinus Torvalds {
270d45f60c6SWeston Andros Adamson 	if (hdr->res.eof) {
2711c6c4b74STrond Myklebust 		loff_t pos = hdr->args.offset + hdr->res.count;
2721c6c4b74STrond Myklebust 		unsigned int new = pos - hdr->io_start;
273fdd1e74cSTrond Myklebust 
2741c6c4b74STrond Myklebust 		if (hdr->good_bytes > new) {
2751c6c4b74STrond Myklebust 			hdr->good_bytes = new;
2764db6e0b7SFred Isaman 			set_bit(NFS_IOHDR_EOF, &hdr->flags);
2774db6e0b7SFred Isaman 			clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
2784db6e0b7SFred Isaman 		}
279f8417b48SKinglong Mee 	} else if (hdr->res.count < hdr->args.count)
280d45f60c6SWeston Andros Adamson 		nfs_readpage_retry(task, hdr);
2810b671301STrond Myklebust }
282fdd1e74cSTrond Myklebust 
283*ab75bff1STrond Myklebust static int readpage_async_filler(struct nfs_readdesc *desc, struct folio *folio)
2841e83b173SDave Wysochanski {
285*ab75bff1STrond Myklebust 	struct inode *inode = folio_file_mapping(folio)->host;
286*ab75bff1STrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
287*ab75bff1STrond Myklebust 	size_t fsize = folio_size(folio);
288*ab75bff1STrond Myklebust 	unsigned int rsize = server->rsize;
2891e83b173SDave Wysochanski 	struct nfs_page *new;
2908cfb9015STrond Myklebust 	unsigned int len, aligned_len;
2911e83b173SDave Wysochanski 	int error;
2921e83b173SDave Wysochanski 
293*ab75bff1STrond Myklebust 	len = nfs_folio_length(folio);
2941e83b173SDave Wysochanski 	if (len == 0)
295*ab75bff1STrond Myklebust 		return nfs_return_empty_folio(folio);
2961e83b173SDave Wysochanski 
297*ab75bff1STrond Myklebust 	aligned_len = min_t(unsigned int, ALIGN(len, rsize), fsize);
2988cfb9015STrond Myklebust 
299*ab75bff1STrond Myklebust 	if (!IS_SYNC(inode)) {
300*ab75bff1STrond Myklebust 		error = nfs_fscache_read_page(inode, &folio->page);
30116f2f4e6SDavid Howells 		if (error == 0)
30216f2f4e6SDavid Howells 			goto out_unlock;
30316f2f4e6SDavid Howells 	}
30416f2f4e6SDavid Howells 
305*ab75bff1STrond Myklebust 	new = nfs_page_create_from_folio(desc->ctx, folio, 0, aligned_len);
3061e83b173SDave Wysochanski 	if (IS_ERR(new))
3071e83b173SDave Wysochanski 		goto out_error;
3081e83b173SDave Wysochanski 
309*ab75bff1STrond Myklebust 	if (len < fsize)
310*ab75bff1STrond Myklebust 		folio_zero_segment(folio, len, fsize);
3111e83b173SDave Wysochanski 	if (!nfs_pageio_add_request(&desc->pgio, new)) {
3121e83b173SDave Wysochanski 		nfs_list_remove_request(new);
3131e83b173SDave Wysochanski 		error = desc->pgio.pg_error;
3141e83b173SDave Wysochanski 		nfs_readpage_release(new, error);
3151e83b173SDave Wysochanski 		goto out;
3161e83b173SDave Wysochanski 	}
3171e83b173SDave Wysochanski 	return 0;
3181e83b173SDave Wysochanski out_error:
3191e83b173SDave Wysochanski 	error = PTR_ERR(new);
32016f2f4e6SDavid Howells out_unlock:
321*ab75bff1STrond Myklebust 	folio_unlock(folio);
3221e83b173SDave Wysochanski out:
3231e83b173SDave Wysochanski 	return error;
3241e83b173SDave Wysochanski }
3251e83b173SDave Wysochanski 
3261da177e4SLinus Torvalds /*
3271da177e4SLinus Torvalds  * Read a page over NFS.
3281da177e4SLinus Torvalds  * We read the page synchronously in the following case:
3291da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
3301da177e4SLinus Torvalds  *	previous async read operation failed.
3311da177e4SLinus Torvalds  */
33265d023afSMatthew Wilcox (Oracle) int nfs_read_folio(struct file *file, struct folio *folio)
3331da177e4SLinus Torvalds {
3341af7e7f8SDave Wysochanski 	struct nfs_readdesc desc;
335*ab75bff1STrond Myklebust 	struct inode *inode = file_inode(file);
33649dee700SDave Wysochanski 	int ret;
3371da177e4SLinus Torvalds 
338*ab75bff1STrond Myklebust 	trace_nfs_aop_readpage(inode, folio);
33991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
34091d5b470SChuck Lever 
3411da177e4SLinus Torvalds 	/*
3421da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
3431da177e4SLinus Torvalds 	 *
344*ab75bff1STrond Myklebust 	 * NOTE! Because we own the folio lock, there cannot
3451da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
346*ab75bff1STrond Myklebust 	 * for this folio (other folios can be written to).
3471da177e4SLinus Torvalds 	 */
348*ab75bff1STrond Myklebust 	ret = nfs_wb_folio(inode, folio);
34949dee700SDave Wysochanski 	if (ret)
350de05a0ccSTrond Myklebust 		goto out_unlock;
351*ab75bff1STrond Myklebust 	if (folio_test_uptodate(folio))
352de05a0ccSTrond Myklebust 		goto out_unlock;
3531da177e4SLinus Torvalds 
35449dee700SDave Wysochanski 	ret = -ESTALE;
3555f004cf2STrond Myklebust 	if (NFS_STALE(inode))
356de05a0ccSTrond Myklebust 		goto out_unlock;
3575f004cf2STrond Myklebust 
3581da177e4SLinus Torvalds 	if (file == NULL) {
35949dee700SDave Wysochanski 		ret = -EBADF;
3601af7e7f8SDave Wysochanski 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
3611af7e7f8SDave Wysochanski 		if (desc.ctx == NULL)
362de05a0ccSTrond Myklebust 			goto out_unlock;
3631da177e4SLinus Torvalds 	} else
3641af7e7f8SDave Wysochanski 		desc.ctx = get_nfs_open_context(nfs_file_open_context(file));
3651da177e4SLinus Torvalds 
366ba512c1bSDave Wysochanski 	xchg(&desc.ctx->error, 0);
3671e83b173SDave Wysochanski 	nfs_pageio_init_read(&desc.pgio, inode, false,
3681e83b173SDave Wysochanski 			     &nfs_async_read_completion_ops);
3691e83b173SDave Wysochanski 
370*ab75bff1STrond Myklebust 	ret = readpage_async_filler(&desc, folio);
371e0340f16SDave Wysochanski 	if (ret)
372e0340f16SDave Wysochanski 		goto out;
3731e83b173SDave Wysochanski 
374b42ad64fSDave Wysochanski 	nfs_pageio_complete_read(&desc.pgio);
3751e83b173SDave Wysochanski 	ret = desc.pgio.pg_error < 0 ? desc.pgio.pg_error : 0;
37649dee700SDave Wysochanski 	if (!ret) {
377*ab75bff1STrond Myklebust 		ret = folio_wait_locked_killable(folio);
378*ab75bff1STrond Myklebust 		if (!folio_test_uptodate(folio) && !ret)
3791af7e7f8SDave Wysochanski 			ret = xchg(&desc.ctx->error, 0);
3808f54c7a4STrond Myklebust 	}
3819a9fc1c0SDavid Howells out:
3821af7e7f8SDave Wysochanski 	put_nfs_open_context(desc.ctx);
383*ab75bff1STrond Myklebust 	trace_nfs_aop_readpage_done(inode, folio, ret);
38449dee700SDave Wysochanski 	return ret;
385de05a0ccSTrond Myklebust out_unlock:
386*ab75bff1STrond Myklebust 	folio_unlock(folio);
387*ab75bff1STrond Myklebust 	trace_nfs_aop_readpage_done(inode, folio, ret);
38849dee700SDave Wysochanski 	return ret;
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3918786fde8SMatthew Wilcox (Oracle) void nfs_readahead(struct readahead_control *ractl)
3921da177e4SLinus Torvalds {
3938786fde8SMatthew Wilcox (Oracle) 	unsigned int nr_pages = readahead_count(ractl);
3948786fde8SMatthew Wilcox (Oracle) 	struct file *file = ractl->file;
39549dee700SDave Wysochanski 	struct nfs_readdesc desc;
3968786fde8SMatthew Wilcox (Oracle) 	struct inode *inode = ractl->mapping->host;
397*ab75bff1STrond Myklebust 	struct folio *folio;
39849dee700SDave Wysochanski 	int ret;
3991da177e4SLinus Torvalds 
4008786fde8SMatthew Wilcox (Oracle) 	trace_nfs_aop_readahead(inode, readahead_pos(ractl), nr_pages);
40191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
4021da177e4SLinus Torvalds 
40349dee700SDave Wysochanski 	ret = -ESTALE;
4045f004cf2STrond Myklebust 	if (NFS_STALE(inode))
4055f004cf2STrond Myklebust 		goto out;
4065f004cf2STrond Myklebust 
40749dee700SDave Wysochanski 	if (file == NULL) {
40849dee700SDave Wysochanski 		ret = -EBADF;
409d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
4101da177e4SLinus Torvalds 		if (desc.ctx == NULL)
41149dee700SDave Wysochanski 			goto out;
4121da177e4SLinus Torvalds 	} else
41349dee700SDave Wysochanski 		desc.ctx = get_nfs_open_context(nfs_file_open_context(file));
4149a9fc1c0SDavid Howells 
4151af7e7f8SDave Wysochanski 	nfs_pageio_init_read(&desc.pgio, inode, false,
416fab5fc25SChristoph Hellwig 			     &nfs_async_read_completion_ops);
4178b09bee3STrond Myklebust 
418*ab75bff1STrond Myklebust 	while ((folio = readahead_folio(ractl)) != NULL) {
419*ab75bff1STrond Myklebust 		ret = readpage_async_filler(&desc, folio);
4208786fde8SMatthew Wilcox (Oracle) 		if (ret)
4218786fde8SMatthew Wilcox (Oracle) 			break;
4228786fde8SMatthew Wilcox (Oracle) 	}
423a7d42ddbSWeston Andros Adamson 
424b42ad64fSDave Wysochanski 	nfs_pageio_complete_read(&desc.pgio);
425a7d42ddbSWeston Andros Adamson 
4261da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
4275f004cf2STrond Myklebust out:
428d9f87743SChuck Lever 	trace_nfs_aop_readahead_done(inode, nr_pages, ret);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
431f7b422b1SDavid Howells int __init nfs_init_readpagecache(void)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
4341e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
4351da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
43620c2df83SPaul Mundt 					     NULL);
4371da177e4SLinus Torvalds 	if (nfs_rdata_cachep == NULL)
4381da177e4SLinus Torvalds 		return -ENOMEM;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	return 0;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
443266bee88SDavid Brownell void nfs_destroy_readpagecache(void)
4441da177e4SLinus Torvalds {
4451a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_rdata_cachep);
4461da177e4SLinus Torvalds }
4474a0de55cSAnna Schumaker 
4484a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_read_ops = {
4494a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_readhdr_alloc,
4504a0de55cSAnna Schumaker 	.rw_free_header		= nfs_readhdr_free,
4510eecb214SAnna Schumaker 	.rw_done		= nfs_readpage_done,
4520eecb214SAnna Schumaker 	.rw_result		= nfs_readpage_result,
4531ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_read,
4544a0de55cSAnna Schumaker };
455