xref: /openbmc/linux/fs/nfs/read.c (revision 8786fde8421ce755a842051f9528674a1b1f0b9a)
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 
521da177e4SLinus Torvalds static
531da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page)
541da177e4SLinus Torvalds {
5509cbfeafSKirill A. Shutemov 	zero_user(page, 0, PAGE_SIZE);
561da177e4SLinus Torvalds 	SetPageUptodate(page);
571da177e4SLinus Torvalds 	unlock_page(page);
581da177e4SLinus Torvalds 	return 0;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
611abb5088SBryan Schumaker void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
62fab5fc25SChristoph Hellwig 			      struct inode *inode, bool force_mds,
63061ae2edSFred Isaman 			      const struct nfs_pgio_completion_ops *compl_ops)
641751c363STrond Myklebust {
65fab5fc25SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
6641d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
67fab5fc25SChristoph Hellwig 
68fab5fc25SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
69fab5fc25SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
70fab5fc25SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_read_ops;
71fab5fc25SChristoph Hellwig #endif
724a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
733bde7afdSTrond Myklebust 			server->rsize, 0);
741751c363STrond Myklebust }
75ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
761751c363STrond Myklebust 
77b42ad64fSDave Wysochanski static void nfs_pageio_complete_read(struct nfs_pageio_descriptor *pgio)
781e83b173SDave Wysochanski {
791e83b173SDave Wysochanski 	struct nfs_pgio_mirror *pgm;
801e83b173SDave Wysochanski 	unsigned long npages;
811e83b173SDave Wysochanski 
821e83b173SDave Wysochanski 	nfs_pageio_complete(pgio);
831e83b173SDave Wysochanski 
841e83b173SDave Wysochanski 	/* It doesn't make sense to do mirrored reads! */
851e83b173SDave Wysochanski 	WARN_ON_ONCE(pgio->pg_mirror_count != 1);
861e83b173SDave Wysochanski 
871e83b173SDave Wysochanski 	pgm = &pgio->pg_mirrors[0];
88b42ad64fSDave Wysochanski 	NFS_I(pgio->pg_inode)->read_io += pgm->pg_bytes_written;
891e83b173SDave Wysochanski 	npages = (pgm->pg_bytes_written + PAGE_SIZE - 1) >> PAGE_SHIFT;
90b42ad64fSDave Wysochanski 	nfs_add_stats(pgio->pg_inode, NFSIOS_READPAGES, npages);
911e83b173SDave Wysochanski }
921e83b173SDave Wysochanski 
931e83b173SDave Wysochanski 
94493292ddSTrond Myklebust void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
95493292ddSTrond Myklebust {
96a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
97a7d42ddbSWeston Andros Adamson 
986f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
996f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
1006f29b9bbSKinglong Mee 
10141d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
102a7d42ddbSWeston Andros Adamson 
103a7d42ddbSWeston Andros Adamson 	/* read path should never have more than one mirror */
104a7d42ddbSWeston Andros Adamson 	WARN_ON_ONCE(pgio->pg_mirror_count != 1);
105a7d42ddbSWeston Andros Adamson 
106a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
107a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
108493292ddSTrond Myklebust }
1091f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
110493292ddSTrond Myklebust 
1118f54c7a4STrond Myklebust static void nfs_readpage_release(struct nfs_page *req, int error)
1120bcbf039SPeng Tao {
1139fcd5960STrond Myklebust 	struct inode *inode = d_inode(nfs_req_openctx(req)->dentry);
1148f54c7a4STrond Myklebust 	struct page *page = req->wb_page;
1150bcbf039SPeng Tao 
1160bcbf039SPeng Tao 	dprintk("NFS: read done (%s/%llu %d@%lld)\n", inode->i_sb->s_id,
1170bcbf039SPeng Tao 		(unsigned long long)NFS_FILEID(inode), req->wb_bytes,
1180bcbf039SPeng Tao 		(long long)req_offset(req));
1190bcbf039SPeng Tao 
1208f54c7a4STrond Myklebust 	if (nfs_error_is_fatal_on_server(error) && error != -ETIMEDOUT)
1218f54c7a4STrond Myklebust 		SetPageError(page);
1220bcbf039SPeng Tao 	if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) {
1238f54c7a4STrond Myklebust 		struct address_space *mapping = page_file_mapping(page);
1240bcbf039SPeng Tao 
1258f54c7a4STrond Myklebust 		if (PageUptodate(page))
12616f2f4e6SDavid Howells 			nfs_readpage_to_fscache(inode, page);
1278f54c7a4STrond Myklebust 		else if (!PageError(page) && !PagePrivate(page))
1288f54c7a4STrond Myklebust 			generic_error_remove_page(mapping, page);
1298f54c7a4STrond Myklebust 		unlock_page(page);
1300bcbf039SPeng Tao 	}
1310bcbf039SPeng Tao 	nfs_release_request(req);
1320bcbf039SPeng Tao }
1330bcbf039SPeng Tao 
1341af7e7f8SDave Wysochanski struct nfs_readdesc {
1351af7e7f8SDave Wysochanski 	struct nfs_pageio_descriptor pgio;
1361af7e7f8SDave Wysochanski 	struct nfs_open_context *ctx;
1371af7e7f8SDave Wysochanski };
1381af7e7f8SDave Wysochanski 
13967d0338eSWeston Andros Adamson static void nfs_page_group_set_uptodate(struct nfs_page *req)
14067d0338eSWeston Andros Adamson {
14167d0338eSWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_UPTODATE))
14267d0338eSWeston Andros Adamson 		SetPageUptodate(req->wb_page);
14367d0338eSWeston Andros Adamson }
14467d0338eSWeston Andros Adamson 
145061ae2edSFred Isaman static void nfs_read_completion(struct nfs_pgio_header *hdr)
1464db6e0b7SFred Isaman {
1474db6e0b7SFred Isaman 	unsigned long bytes = 0;
1488f54c7a4STrond Myklebust 	int error;
1494db6e0b7SFred Isaman 
1504db6e0b7SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
1514db6e0b7SFred Isaman 		goto out;
1524db6e0b7SFred Isaman 	while (!list_empty(&hdr->pages)) {
1534db6e0b7SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
1544db6e0b7SFred Isaman 		struct page *page = req->wb_page;
15568072992SWeston Andros Adamson 		unsigned long start = req->wb_pgbase;
15668072992SWeston Andros Adamson 		unsigned long end = req->wb_pgbase + req->wb_bytes;
1574db6e0b7SFred Isaman 
1584db6e0b7SFred Isaman 		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
15968072992SWeston Andros Adamson 			/* note: regions of the page not covered by a
1601e83b173SDave Wysochanski 			 * request are zeroed in readpage_async_filler */
16168072992SWeston Andros Adamson 			if (bytes > hdr->good_bytes) {
16268072992SWeston Andros Adamson 				/* nothing in this request was good, so zero
16368072992SWeston Andros Adamson 				 * the full extent of the request */
16468072992SWeston Andros Adamson 				zero_user_segment(page, start, end);
16568072992SWeston Andros Adamson 
16668072992SWeston Andros Adamson 			} else if (hdr->good_bytes - bytes < req->wb_bytes) {
16768072992SWeston Andros Adamson 				/* part of this request has good bytes, but
16868072992SWeston Andros Adamson 				 * not all. zero the bad bytes */
16968072992SWeston Andros Adamson 				start += hdr->good_bytes - bytes;
17068072992SWeston Andros Adamson 				WARN_ON(start < req->wb_pgbase);
17168072992SWeston Andros Adamson 				zero_user_segment(page, start, end);
17268072992SWeston Andros Adamson 			}
1734db6e0b7SFred Isaman 		}
1748f54c7a4STrond Myklebust 		error = 0;
1754bd8b010STrond Myklebust 		bytes += req->wb_bytes;
1764bd8b010STrond Myklebust 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
1774bd8b010STrond Myklebust 			if (bytes <= hdr->good_bytes)
17867d0338eSWeston Andros Adamson 				nfs_page_group_set_uptodate(req);
1798f54c7a4STrond Myklebust 			else {
1808f54c7a4STrond Myklebust 				error = hdr->error;
1818f54c7a4STrond Myklebust 				xchg(&nfs_req_openctx(req)->error, error);
1828f54c7a4STrond Myklebust 			}
1834bd8b010STrond Myklebust 		} else
18467d0338eSWeston Andros Adamson 			nfs_page_group_set_uptodate(req);
1854db6e0b7SFred Isaman 		nfs_list_remove_request(req);
1868f54c7a4STrond Myklebust 		nfs_readpage_release(req, error);
1874db6e0b7SFred Isaman 	}
1884db6e0b7SFred Isaman out:
1894db6e0b7SFred Isaman 	hdr->release(hdr);
1904db6e0b7SFred Isaman }
1914db6e0b7SFred Isaman 
192d45f60c6SWeston Andros Adamson static void nfs_initiate_read(struct nfs_pgio_header *hdr,
193d45f60c6SWeston Andros Adamson 			      struct rpc_message *msg,
194abde71f4STom Haynes 			      const struct nfs_rpc_ops *rpc_ops,
1951ed26f33SAnna Schumaker 			      struct rpc_task_setup *task_setup_data, int how)
19664419a9bSAndy Adamson {
197d45f60c6SWeston Andros Adamson 	struct inode *inode = hdr->inode;
19864419a9bSAndy Adamson 	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
19964419a9bSAndy Adamson 
2001ed26f33SAnna Schumaker 	task_setup_data->flags |= swap_flags;
201abde71f4STom Haynes 	rpc_ops->read_setup(hdr, msg);
2022343172dSTrond Myklebust 	trace_nfs_initiate_read(hdr);
20364419a9bSAndy Adamson }
20464419a9bSAndy Adamson 
205061ae2edSFred Isaman static void
206df3accb8STrond Myklebust nfs_async_read_error(struct list_head *head, int error)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	struct nfs_page	*req;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	while (!list_empty(head)) {
2111da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
2121da177e4SLinus Torvalds 		nfs_list_remove_request(req);
2138f54c7a4STrond Myklebust 		nfs_readpage_release(req, error);
2141da177e4SLinus Torvalds 	}
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
217061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
218061ae2edSFred Isaman 	.error_cleanup = nfs_async_read_error,
219061ae2edSFred Isaman 	.completion = nfs_read_completion,
220061ae2edSFred Isaman };
221061ae2edSFred Isaman 
2221da177e4SLinus Torvalds /*
2230b671301STrond Myklebust  * This is the callback from RPC telling us whether a reply was
2240b671301STrond Myklebust  * received or some error occurred (timeout or socket shutdown).
2250b671301STrond Myklebust  */
226d45f60c6SWeston Andros Adamson static int nfs_readpage_done(struct rpc_task *task,
227d45f60c6SWeston Andros Adamson 			     struct nfs_pgio_header *hdr,
2280eecb214SAnna Schumaker 			     struct inode *inode)
2290b671301STrond Myklebust {
230d45f60c6SWeston Andros Adamson 	int status = NFS_PROTO(inode)->read_done(task, hdr);
2310b671301STrond Myklebust 	if (status != 0)
2320b671301STrond Myklebust 		return status;
2330b671301STrond Myklebust 
234d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count);
2352343172dSTrond Myklebust 	trace_nfs_readpage_done(task, hdr);
2360b671301STrond Myklebust 
2370b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
23893ce4af7STrond Myklebust 		nfs_set_inode_stale(inode);
239cd841605SFred Isaman 		nfs_mark_for_revalidate(inode);
2400b671301STrond Myklebust 	}
2410b671301STrond Myklebust 	return 0;
2420b671301STrond Myklebust }
2430b671301STrond Myklebust 
244d45f60c6SWeston Andros Adamson static void nfs_readpage_retry(struct rpc_task *task,
245d45f60c6SWeston Andros Adamson 			       struct nfs_pgio_header *hdr)
2460b671301STrond Myklebust {
247d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args *argp = &hdr->args;
248d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res  *resp = &hdr->res;
2490b671301STrond Myklebust 
2500b671301STrond Myklebust 	/* This is a short read! */
251d45f60c6SWeston Andros Adamson 	nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD);
252fd2b6121SChuck Lever 	trace_nfs_readpage_short(task, hdr);
253fd2b6121SChuck Lever 
2540b671301STrond Myklebust 	/* Has the server at least made some progress? */
2554db6e0b7SFred Isaman 	if (resp->count == 0) {
256d45f60c6SWeston Andros Adamson 		nfs_set_pgio_error(hdr, -EIO, argp->offset);
257d61e612aSTrond Myklebust 		return;
2584db6e0b7SFred Isaman 	}
259f8417b48SKinglong Mee 
260f8417b48SKinglong Mee 	/* For non rpc-based layout drivers, retry-through-MDS */
261f8417b48SKinglong Mee 	if (!task->tk_ops) {
262f8417b48SKinglong Mee 		hdr->pnfs_error = -EAGAIN;
263f8417b48SKinglong Mee 		return;
264f8417b48SKinglong Mee 	}
265f8417b48SKinglong Mee 
266d45f60c6SWeston Andros Adamson 	/* Yes, so retry the read at the end of the hdr */
267d45f60c6SWeston Andros Adamson 	hdr->mds_offset += resp->count;
2680b671301STrond Myklebust 	argp->offset += resp->count;
2690b671301STrond Myklebust 	argp->pgbase += resp->count;
2700b671301STrond Myklebust 	argp->count -= resp->count;
2718c9cb714STrond Myklebust 	resp->count = 0;
2728c9cb714STrond Myklebust 	resp->eof = 0;
273d00c5d43STrond Myklebust 	rpc_restart_call_prepare(task);
2740b671301STrond Myklebust }
2750b671301STrond Myklebust 
276d45f60c6SWeston Andros Adamson static void nfs_readpage_result(struct rpc_task *task,
277d45f60c6SWeston Andros Adamson 				struct nfs_pgio_header *hdr)
2781da177e4SLinus Torvalds {
279d45f60c6SWeston Andros Adamson 	if (hdr->res.eof) {
2801c6c4b74STrond Myklebust 		loff_t pos = hdr->args.offset + hdr->res.count;
2811c6c4b74STrond Myklebust 		unsigned int new = pos - hdr->io_start;
282fdd1e74cSTrond Myklebust 
2831c6c4b74STrond Myklebust 		if (hdr->good_bytes > new) {
2841c6c4b74STrond Myklebust 			hdr->good_bytes = new;
2854db6e0b7SFred Isaman 			set_bit(NFS_IOHDR_EOF, &hdr->flags);
2864db6e0b7SFred Isaman 			clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
2874db6e0b7SFred Isaman 		}
288f8417b48SKinglong Mee 	} else if (hdr->res.count < hdr->args.count)
289d45f60c6SWeston Andros Adamson 		nfs_readpage_retry(task, hdr);
2900b671301STrond Myklebust }
291fdd1e74cSTrond Myklebust 
2921e83b173SDave Wysochanski static int
293*8786fde8SMatthew Wilcox (Oracle) readpage_async_filler(struct nfs_readdesc *desc, struct page *page)
2941e83b173SDave Wysochanski {
2958cfb9015STrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
2968cfb9015STrond Myklebust 	unsigned int rsize = NFS_SERVER(inode)->rsize;
2971e83b173SDave Wysochanski 	struct nfs_page *new;
2988cfb9015STrond Myklebust 	unsigned int len, aligned_len;
2991e83b173SDave Wysochanski 	int error;
3001e83b173SDave Wysochanski 
3011e83b173SDave Wysochanski 	len = nfs_page_length(page);
3021e83b173SDave Wysochanski 	if (len == 0)
3031e83b173SDave Wysochanski 		return nfs_return_empty_page(page);
3041e83b173SDave Wysochanski 
3058cfb9015STrond Myklebust 	aligned_len = min_t(unsigned int, ALIGN(len, rsize), PAGE_SIZE);
3068cfb9015STrond Myklebust 
30716f2f4e6SDavid Howells 	if (!IS_SYNC(page->mapping->host)) {
30816f2f4e6SDavid Howells 		error = nfs_readpage_from_fscache(page->mapping->host, page);
30916f2f4e6SDavid Howells 		if (error == 0)
31016f2f4e6SDavid Howells 			goto out_unlock;
31116f2f4e6SDavid Howells 	}
31216f2f4e6SDavid Howells 
3138cfb9015STrond Myklebust 	new = nfs_create_request(desc->ctx, page, 0, aligned_len);
3141e83b173SDave Wysochanski 	if (IS_ERR(new))
3151e83b173SDave Wysochanski 		goto out_error;
3161e83b173SDave Wysochanski 
3171e83b173SDave Wysochanski 	if (len < PAGE_SIZE)
3181e83b173SDave Wysochanski 		zero_user_segment(page, len, PAGE_SIZE);
3191e83b173SDave Wysochanski 	if (!nfs_pageio_add_request(&desc->pgio, new)) {
3201e83b173SDave Wysochanski 		nfs_list_remove_request(new);
3211e83b173SDave Wysochanski 		error = desc->pgio.pg_error;
3221e83b173SDave Wysochanski 		nfs_readpage_release(new, error);
3231e83b173SDave Wysochanski 		goto out;
3241e83b173SDave Wysochanski 	}
3251e83b173SDave Wysochanski 	return 0;
3261e83b173SDave Wysochanski out_error:
3271e83b173SDave Wysochanski 	error = PTR_ERR(new);
32816f2f4e6SDavid Howells out_unlock:
3291e83b173SDave Wysochanski 	unlock_page(page);
3301e83b173SDave Wysochanski out:
3311e83b173SDave Wysochanski 	return error;
3321e83b173SDave Wysochanski }
3331e83b173SDave Wysochanski 
3341da177e4SLinus Torvalds /*
3351da177e4SLinus Torvalds  * Read a page over NFS.
3361da177e4SLinus Torvalds  * We read the page synchronously in the following case:
3371da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
3381da177e4SLinus Torvalds  *	previous async read operation failed.
3391da177e4SLinus Torvalds  */
3401da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page)
3411da177e4SLinus Torvalds {
3421af7e7f8SDave Wysochanski 	struct nfs_readdesc desc;
343d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
34449dee700SDave Wysochanski 	int ret;
3451da177e4SLinus Torvalds 
346d9f87743SChuck Lever 	trace_nfs_aop_readpage(inode, page);
34791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
34891d5b470SChuck Lever 
3491da177e4SLinus Torvalds 	/*
3501da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
3511da177e4SLinus Torvalds 	 *
3521da177e4SLinus Torvalds 	 * NOTE! Because we own the page lock, there cannot
3531da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
3541da177e4SLinus Torvalds 	 * for this page (other pages can be written to).
3551da177e4SLinus Torvalds 	 */
35649dee700SDave Wysochanski 	ret = nfs_wb_page(inode, page);
35749dee700SDave Wysochanski 	if (ret)
358de05a0ccSTrond Myklebust 		goto out_unlock;
359de05a0ccSTrond Myklebust 	if (PageUptodate(page))
360de05a0ccSTrond Myklebust 		goto out_unlock;
3611da177e4SLinus Torvalds 
36249dee700SDave Wysochanski 	ret = -ESTALE;
3635f004cf2STrond Myklebust 	if (NFS_STALE(inode))
364de05a0ccSTrond Myklebust 		goto out_unlock;
3655f004cf2STrond Myklebust 
3661da177e4SLinus Torvalds 	if (file == NULL) {
36749dee700SDave Wysochanski 		ret = -EBADF;
3681af7e7f8SDave Wysochanski 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
3691af7e7f8SDave Wysochanski 		if (desc.ctx == NULL)
370de05a0ccSTrond Myklebust 			goto out_unlock;
3711da177e4SLinus Torvalds 	} else
3721af7e7f8SDave Wysochanski 		desc.ctx = get_nfs_open_context(nfs_file_open_context(file));
3731da177e4SLinus Torvalds 
374ba512c1bSDave Wysochanski 	xchg(&desc.ctx->error, 0);
3751e83b173SDave Wysochanski 	nfs_pageio_init_read(&desc.pgio, inode, false,
3761e83b173SDave Wysochanski 			     &nfs_async_read_completion_ops);
3771e83b173SDave Wysochanski 
3781e83b173SDave Wysochanski 	ret = readpage_async_filler(&desc, page);
379e0340f16SDave Wysochanski 	if (ret)
380e0340f16SDave Wysochanski 		goto out;
3811e83b173SDave Wysochanski 
382b42ad64fSDave Wysochanski 	nfs_pageio_complete_read(&desc.pgio);
3831e83b173SDave Wysochanski 	ret = desc.pgio.pg_error < 0 ? desc.pgio.pg_error : 0;
38449dee700SDave Wysochanski 	if (!ret) {
38549dee700SDave Wysochanski 		ret = wait_on_page_locked_killable(page);
38649dee700SDave Wysochanski 		if (!PageUptodate(page) && !ret)
3871af7e7f8SDave Wysochanski 			ret = xchg(&desc.ctx->error, 0);
3888f54c7a4STrond Myklebust 	}
3899a9fc1c0SDavid Howells out:
3901af7e7f8SDave Wysochanski 	put_nfs_open_context(desc.ctx);
391d9f87743SChuck Lever 	trace_nfs_aop_readpage_done(inode, page, ret);
39249dee700SDave Wysochanski 	return ret;
393de05a0ccSTrond Myklebust out_unlock:
3941da177e4SLinus Torvalds 	unlock_page(page);
395d9f87743SChuck Lever 	trace_nfs_aop_readpage_done(inode, page, ret);
39649dee700SDave Wysochanski 	return ret;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
399*8786fde8SMatthew Wilcox (Oracle) void nfs_readahead(struct readahead_control *ractl)
4001da177e4SLinus Torvalds {
401*8786fde8SMatthew Wilcox (Oracle) 	unsigned int nr_pages = readahead_count(ractl);
402*8786fde8SMatthew Wilcox (Oracle) 	struct file *file = ractl->file;
40349dee700SDave Wysochanski 	struct nfs_readdesc desc;
404*8786fde8SMatthew Wilcox (Oracle) 	struct inode *inode = ractl->mapping->host;
405*8786fde8SMatthew Wilcox (Oracle) 	struct page *page;
40649dee700SDave Wysochanski 	int ret;
4071da177e4SLinus Torvalds 
408*8786fde8SMatthew Wilcox (Oracle) 	trace_nfs_aop_readahead(inode, readahead_pos(ractl), nr_pages);
40991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
4101da177e4SLinus Torvalds 
41149dee700SDave Wysochanski 	ret = -ESTALE;
4125f004cf2STrond Myklebust 	if (NFS_STALE(inode))
4135f004cf2STrond Myklebust 		goto out;
4145f004cf2STrond Myklebust 
41549dee700SDave Wysochanski 	if (file == NULL) {
41649dee700SDave Wysochanski 		ret = -EBADF;
417d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
4181da177e4SLinus Torvalds 		if (desc.ctx == NULL)
41949dee700SDave Wysochanski 			goto out;
4201da177e4SLinus Torvalds 	} else
42149dee700SDave Wysochanski 		desc.ctx = get_nfs_open_context(nfs_file_open_context(file));
4229a9fc1c0SDavid Howells 
4231af7e7f8SDave Wysochanski 	nfs_pageio_init_read(&desc.pgio, inode, false,
424fab5fc25SChristoph Hellwig 			     &nfs_async_read_completion_ops);
4258b09bee3STrond Myklebust 
426*8786fde8SMatthew Wilcox (Oracle) 	while ((page = readahead_page(ractl)) != NULL) {
427*8786fde8SMatthew Wilcox (Oracle) 		ret = readpage_async_filler(&desc, page);
428*8786fde8SMatthew Wilcox (Oracle) 		put_page(page);
429*8786fde8SMatthew Wilcox (Oracle) 		if (ret)
430*8786fde8SMatthew Wilcox (Oracle) 			break;
431*8786fde8SMatthew Wilcox (Oracle) 	}
432a7d42ddbSWeston Andros Adamson 
433b42ad64fSDave Wysochanski 	nfs_pageio_complete_read(&desc.pgio);
434a7d42ddbSWeston Andros Adamson 
4351da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
4365f004cf2STrond Myklebust out:
437d9f87743SChuck Lever 	trace_nfs_aop_readahead_done(inode, nr_pages, ret);
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
440f7b422b1SDavid Howells int __init nfs_init_readpagecache(void)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
4431e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
4441da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
44520c2df83SPaul Mundt 					     NULL);
4461da177e4SLinus Torvalds 	if (nfs_rdata_cachep == NULL)
4471da177e4SLinus Torvalds 		return -ENOMEM;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	return 0;
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
452266bee88SDavid Brownell void nfs_destroy_readpagecache(void)
4531da177e4SLinus Torvalds {
4541a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_rdata_cachep);
4551da177e4SLinus Torvalds }
4564a0de55cSAnna Schumaker 
4574a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_read_ops = {
4584a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_readhdr_alloc,
4594a0de55cSAnna Schumaker 	.rw_free_header		= nfs_readhdr_free,
4600eecb214SAnna Schumaker 	.rw_done		= nfs_readpage_done,
4610eecb214SAnna Schumaker 	.rw_result		= nfs_readpage_result,
4621ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_read,
4634a0de55cSAnna Schumaker };
464