xref: /openbmc/linux/fs/nfs/read.c (revision 2343172d34c6296f79b404a0eb291e15ab19e5ca)
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 
77493292ddSTrond Myklebust void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
78493292ddSTrond Myklebust {
79a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
80a7d42ddbSWeston Andros Adamson 
816f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
826f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
836f29b9bbSKinglong Mee 
8441d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
85a7d42ddbSWeston Andros Adamson 
86a7d42ddbSWeston Andros Adamson 	/* read path should never have more than one mirror */
87a7d42ddbSWeston Andros Adamson 	WARN_ON_ONCE(pgio->pg_mirror_count != 1);
88a7d42ddbSWeston Andros Adamson 
89a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
90a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
91493292ddSTrond Myklebust }
921f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
93493292ddSTrond Myklebust 
948f54c7a4STrond Myklebust static void nfs_readpage_release(struct nfs_page *req, int error)
950bcbf039SPeng Tao {
969fcd5960STrond Myklebust 	struct inode *inode = d_inode(nfs_req_openctx(req)->dentry);
978f54c7a4STrond Myklebust 	struct page *page = req->wb_page;
980bcbf039SPeng Tao 
990bcbf039SPeng Tao 	dprintk("NFS: read done (%s/%llu %d@%lld)\n", inode->i_sb->s_id,
1000bcbf039SPeng Tao 		(unsigned long long)NFS_FILEID(inode), req->wb_bytes,
1010bcbf039SPeng Tao 		(long long)req_offset(req));
1020bcbf039SPeng Tao 
1038f54c7a4STrond Myklebust 	if (nfs_error_is_fatal_on_server(error) && error != -ETIMEDOUT)
1048f54c7a4STrond Myklebust 		SetPageError(page);
1050bcbf039SPeng Tao 	if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) {
1068f54c7a4STrond Myklebust 		struct address_space *mapping = page_file_mapping(page);
1070bcbf039SPeng Tao 
1088f54c7a4STrond Myklebust 		if (PageUptodate(page))
1098f54c7a4STrond Myklebust 			nfs_readpage_to_fscache(inode, page, 0);
1108f54c7a4STrond Myklebust 		else if (!PageError(page) && !PagePrivate(page))
1118f54c7a4STrond Myklebust 			generic_error_remove_page(mapping, page);
1128f54c7a4STrond Myklebust 		unlock_page(page);
1130bcbf039SPeng Tao 	}
1140bcbf039SPeng Tao 	nfs_release_request(req);
1150bcbf039SPeng Tao }
1160bcbf039SPeng Tao 
117f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
1181da177e4SLinus Torvalds 		       struct page *page)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	struct nfs_page	*new;
1211da177e4SLinus Torvalds 	unsigned int len;
122c76069bdSFred Isaman 	struct nfs_pageio_descriptor pgio;
123a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *pgm;
1241da177e4SLinus Torvalds 
12549a70f27STrond Myklebust 	len = nfs_page_length(page);
1261da177e4SLinus Torvalds 	if (len == 0)
1271da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
12828b1d3f5STrond Myklebust 	new = nfs_create_request(ctx, page, 0, len);
1291da177e4SLinus Torvalds 	if (IS_ERR(new)) {
1301da177e4SLinus Torvalds 		unlock_page(page);
1311da177e4SLinus Torvalds 		return PTR_ERR(new);
1321da177e4SLinus Torvalds 	}
13309cbfeafSKirill A. Shutemov 	if (len < PAGE_SIZE)
13409cbfeafSKirill A. Shutemov 		zero_user_segment(page, len, PAGE_SIZE);
1351da177e4SLinus Torvalds 
136fab5fc25SChristoph Hellwig 	nfs_pageio_init_read(&pgio, inode, false,
137fab5fc25SChristoph Hellwig 			     &nfs_async_read_completion_ops);
1380bcbf039SPeng Tao 	if (!nfs_pageio_add_request(&pgio, new)) {
1390bcbf039SPeng Tao 		nfs_list_remove_request(new);
1408f54c7a4STrond Myklebust 		nfs_readpage_release(new, pgio.pg_error);
1410bcbf039SPeng Tao 	}
1421751c363STrond Myklebust 	nfs_pageio_complete(&pgio);
143a7d42ddbSWeston Andros Adamson 
144a7d42ddbSWeston Andros Adamson 	/* It doesn't make sense to do mirrored reads! */
145a7d42ddbSWeston Andros Adamson 	WARN_ON_ONCE(pgio.pg_mirror_count != 1);
146a7d42ddbSWeston Andros Adamson 
147a7d42ddbSWeston Andros Adamson 	pgm = &pgio.pg_mirrors[0];
148a7d42ddbSWeston Andros Adamson 	NFS_I(inode)->read_io += pgm->pg_bytes_written;
149a7d42ddbSWeston Andros Adamson 
150d600ad1fSPeng Tao 	return pgio.pg_error < 0 ? pgio.pg_error : 0;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
15367d0338eSWeston Andros Adamson static void nfs_page_group_set_uptodate(struct nfs_page *req)
15467d0338eSWeston Andros Adamson {
15567d0338eSWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_UPTODATE))
15667d0338eSWeston Andros Adamson 		SetPageUptodate(req->wb_page);
15767d0338eSWeston Andros Adamson }
15867d0338eSWeston Andros Adamson 
159061ae2edSFred Isaman static void nfs_read_completion(struct nfs_pgio_header *hdr)
1604db6e0b7SFred Isaman {
1614db6e0b7SFred Isaman 	unsigned long bytes = 0;
1628f54c7a4STrond Myklebust 	int error;
1634db6e0b7SFred Isaman 
1644db6e0b7SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
1654db6e0b7SFred Isaman 		goto out;
1664db6e0b7SFred Isaman 	while (!list_empty(&hdr->pages)) {
1674db6e0b7SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
1684db6e0b7SFred Isaman 		struct page *page = req->wb_page;
16968072992SWeston Andros Adamson 		unsigned long start = req->wb_pgbase;
17068072992SWeston Andros Adamson 		unsigned long end = req->wb_pgbase + req->wb_bytes;
1714db6e0b7SFred Isaman 
1724db6e0b7SFred Isaman 		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
17368072992SWeston Andros Adamson 			/* note: regions of the page not covered by a
17468072992SWeston Andros Adamson 			 * request are zeroed in nfs_readpage_async /
17568072992SWeston Andros Adamson 			 * readpage_async_filler */
17668072992SWeston Andros Adamson 			if (bytes > hdr->good_bytes) {
17768072992SWeston Andros Adamson 				/* nothing in this request was good, so zero
17868072992SWeston Andros Adamson 				 * the full extent of the request */
17968072992SWeston Andros Adamson 				zero_user_segment(page, start, end);
18068072992SWeston Andros Adamson 
18168072992SWeston Andros Adamson 			} else if (hdr->good_bytes - bytes < req->wb_bytes) {
18268072992SWeston Andros Adamson 				/* part of this request has good bytes, but
18368072992SWeston Andros Adamson 				 * not all. zero the bad bytes */
18468072992SWeston Andros Adamson 				start += hdr->good_bytes - bytes;
18568072992SWeston Andros Adamson 				WARN_ON(start < req->wb_pgbase);
18668072992SWeston Andros Adamson 				zero_user_segment(page, start, end);
18768072992SWeston Andros Adamson 			}
1884db6e0b7SFred Isaman 		}
1898f54c7a4STrond Myklebust 		error = 0;
1904bd8b010STrond Myklebust 		bytes += req->wb_bytes;
1914bd8b010STrond Myklebust 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
1924bd8b010STrond Myklebust 			if (bytes <= hdr->good_bytes)
19367d0338eSWeston Andros Adamson 				nfs_page_group_set_uptodate(req);
1948f54c7a4STrond Myklebust 			else {
1958f54c7a4STrond Myklebust 				error = hdr->error;
1968f54c7a4STrond Myklebust 				xchg(&nfs_req_openctx(req)->error, error);
1978f54c7a4STrond Myklebust 			}
1984bd8b010STrond Myklebust 		} else
19967d0338eSWeston Andros Adamson 			nfs_page_group_set_uptodate(req);
2004db6e0b7SFred Isaman 		nfs_list_remove_request(req);
2018f54c7a4STrond Myklebust 		nfs_readpage_release(req, error);
2024db6e0b7SFred Isaman 	}
2034db6e0b7SFred Isaman out:
2044db6e0b7SFred Isaman 	hdr->release(hdr);
2054db6e0b7SFred Isaman }
2064db6e0b7SFred Isaman 
207d45f60c6SWeston Andros Adamson static void nfs_initiate_read(struct nfs_pgio_header *hdr,
208d45f60c6SWeston Andros Adamson 			      struct rpc_message *msg,
209abde71f4STom Haynes 			      const struct nfs_rpc_ops *rpc_ops,
2101ed26f33SAnna Schumaker 			      struct rpc_task_setup *task_setup_data, int how)
21164419a9bSAndy Adamson {
212d45f60c6SWeston Andros Adamson 	struct inode *inode = hdr->inode;
21364419a9bSAndy Adamson 	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
21464419a9bSAndy Adamson 
2151ed26f33SAnna Schumaker 	task_setup_data->flags |= swap_flags;
216abde71f4STom Haynes 	rpc_ops->read_setup(hdr, msg);
217*2343172dSTrond Myklebust 	trace_nfs_initiate_read(hdr);
21864419a9bSAndy Adamson }
21964419a9bSAndy Adamson 
220061ae2edSFred Isaman static void
221df3accb8STrond Myklebust nfs_async_read_error(struct list_head *head, int error)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	struct nfs_page	*req;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	while (!list_empty(head)) {
2261da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
2271da177e4SLinus Torvalds 		nfs_list_remove_request(req);
2288f54c7a4STrond Myklebust 		nfs_readpage_release(req, error);
2291da177e4SLinus Torvalds 	}
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
232061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
233061ae2edSFred Isaman 	.error_cleanup = nfs_async_read_error,
234061ae2edSFred Isaman 	.completion = nfs_read_completion,
235061ae2edSFred Isaman };
236061ae2edSFred Isaman 
2371da177e4SLinus Torvalds /*
2380b671301STrond Myklebust  * This is the callback from RPC telling us whether a reply was
2390b671301STrond Myklebust  * received or some error occurred (timeout or socket shutdown).
2400b671301STrond Myklebust  */
241d45f60c6SWeston Andros Adamson static int nfs_readpage_done(struct rpc_task *task,
242d45f60c6SWeston Andros Adamson 			     struct nfs_pgio_header *hdr,
2430eecb214SAnna Schumaker 			     struct inode *inode)
2440b671301STrond Myklebust {
245d45f60c6SWeston Andros Adamson 	int status = NFS_PROTO(inode)->read_done(task, hdr);
2460b671301STrond Myklebust 	if (status != 0)
2470b671301STrond Myklebust 		return status;
2480b671301STrond Myklebust 
249d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count);
250*2343172dSTrond Myklebust 	trace_nfs_readpage_done(task, hdr);
2510b671301STrond Myklebust 
2520b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
253cd841605SFred Isaman 		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
254cd841605SFred Isaman 		nfs_mark_for_revalidate(inode);
2550b671301STrond Myklebust 	}
2560b671301STrond Myklebust 	return 0;
2570b671301STrond Myklebust }
2580b671301STrond Myklebust 
259d45f60c6SWeston Andros Adamson static void nfs_readpage_retry(struct rpc_task *task,
260d45f60c6SWeston Andros Adamson 			       struct nfs_pgio_header *hdr)
2610b671301STrond Myklebust {
262d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args *argp = &hdr->args;
263d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res  *resp = &hdr->res;
2640b671301STrond Myklebust 
2650b671301STrond Myklebust 	/* This is a short read! */
266d45f60c6SWeston Andros Adamson 	nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD);
2670b671301STrond Myklebust 	/* Has the server at least made some progress? */
2684db6e0b7SFred Isaman 	if (resp->count == 0) {
269d45f60c6SWeston Andros Adamson 		nfs_set_pgio_error(hdr, -EIO, argp->offset);
270d61e612aSTrond Myklebust 		return;
2714db6e0b7SFred Isaman 	}
272f8417b48SKinglong Mee 
273f8417b48SKinglong Mee 	/* For non rpc-based layout drivers, retry-through-MDS */
274f8417b48SKinglong Mee 	if (!task->tk_ops) {
275f8417b48SKinglong Mee 		hdr->pnfs_error = -EAGAIN;
276f8417b48SKinglong Mee 		return;
277f8417b48SKinglong Mee 	}
278f8417b48SKinglong Mee 
279d45f60c6SWeston Andros Adamson 	/* Yes, so retry the read at the end of the hdr */
280d45f60c6SWeston Andros Adamson 	hdr->mds_offset += resp->count;
2810b671301STrond Myklebust 	argp->offset += resp->count;
2820b671301STrond Myklebust 	argp->pgbase += resp->count;
2830b671301STrond Myklebust 	argp->count -= resp->count;
284d00c5d43STrond Myklebust 	rpc_restart_call_prepare(task);
2850b671301STrond Myklebust }
2860b671301STrond Myklebust 
287d45f60c6SWeston Andros Adamson static void nfs_readpage_result(struct rpc_task *task,
288d45f60c6SWeston Andros Adamson 				struct nfs_pgio_header *hdr)
2891da177e4SLinus Torvalds {
290d45f60c6SWeston Andros Adamson 	if (hdr->res.eof) {
2911c6c4b74STrond Myklebust 		loff_t pos = hdr->args.offset + hdr->res.count;
2921c6c4b74STrond Myklebust 		unsigned int new = pos - hdr->io_start;
293fdd1e74cSTrond Myklebust 
2941c6c4b74STrond Myklebust 		if (hdr->good_bytes > new) {
2951c6c4b74STrond Myklebust 			hdr->good_bytes = new;
2964db6e0b7SFred Isaman 			set_bit(NFS_IOHDR_EOF, &hdr->flags);
2974db6e0b7SFred Isaman 			clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
2984db6e0b7SFred Isaman 		}
299f8417b48SKinglong Mee 	} else if (hdr->res.count < hdr->args.count)
300d45f60c6SWeston Andros Adamson 		nfs_readpage_retry(task, hdr);
3010b671301STrond Myklebust }
302fdd1e74cSTrond Myklebust 
3031da177e4SLinus Torvalds /*
3041da177e4SLinus Torvalds  * Read a page over NFS.
3051da177e4SLinus Torvalds  * We read the page synchronously in the following case:
3061da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
3071da177e4SLinus Torvalds  *	previous async read operation failed.
3081da177e4SLinus Torvalds  */
3091da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
312d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
3131da177e4SLinus Torvalds 	int		error;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
3168cd79788SHuang Ying 		page, PAGE_SIZE, page_index(page));
31791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
3183708f842SNicolas Iooss 	nfs_add_stats(inode, NFSIOS_READPAGES, 1);
31991d5b470SChuck Lever 
3201da177e4SLinus Torvalds 	/*
3211da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
3221da177e4SLinus Torvalds 	 *
3231da177e4SLinus Torvalds 	 * NOTE! Because we own the page lock, there cannot
3241da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
3251da177e4SLinus Torvalds 	 * for this page (other pages can be written to).
3261da177e4SLinus Torvalds 	 */
3271da177e4SLinus Torvalds 	error = nfs_wb_page(inode, page);
3281da177e4SLinus Torvalds 	if (error)
329de05a0ccSTrond Myklebust 		goto out_unlock;
330de05a0ccSTrond Myklebust 	if (PageUptodate(page))
331de05a0ccSTrond Myklebust 		goto out_unlock;
3321da177e4SLinus Torvalds 
3335f004cf2STrond Myklebust 	error = -ESTALE;
3345f004cf2STrond Myklebust 	if (NFS_STALE(inode))
335de05a0ccSTrond Myklebust 		goto out_unlock;
3365f004cf2STrond Myklebust 
3371da177e4SLinus Torvalds 	if (file == NULL) {
338cf1308ffSTrond Myklebust 		error = -EBADF;
339d530838bSTrond Myklebust 		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
3401da177e4SLinus Torvalds 		if (ctx == NULL)
341de05a0ccSTrond Myklebust 			goto out_unlock;
3421da177e4SLinus Torvalds 	} else
343cd3758e3STrond Myklebust 		ctx = get_nfs_open_context(nfs_file_open_context(file));
3441da177e4SLinus Torvalds 
3459a9fc1c0SDavid Howells 	if (!IS_SYNC(inode)) {
3469a9fc1c0SDavid Howells 		error = nfs_readpage_from_fscache(ctx, inode, page);
3479a9fc1c0SDavid Howells 		if (error == 0)
3489a9fc1c0SDavid Howells 			goto out;
3499a9fc1c0SDavid Howells 	}
3509a9fc1c0SDavid Howells 
3518f54c7a4STrond Myklebust 	xchg(&ctx->error, 0);
3528e0969f0STrond Myklebust 	error = nfs_readpage_async(ctx, inode, page);
3538f54c7a4STrond Myklebust 	if (!error) {
3548f54c7a4STrond Myklebust 		error = wait_on_page_locked_killable(page);
3558f54c7a4STrond Myklebust 		if (!PageUptodate(page) && !error)
3568f54c7a4STrond Myklebust 			error = xchg(&ctx->error, 0);
3578f54c7a4STrond Myklebust 	}
3589a9fc1c0SDavid Howells out:
3591da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
3601da177e4SLinus Torvalds 	return error;
361de05a0ccSTrond Myklebust out_unlock:
3621da177e4SLinus Torvalds 	unlock_page(page);
3631da177e4SLinus Torvalds 	return error;
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds struct nfs_readdesc {
3678b09bee3STrond Myklebust 	struct nfs_pageio_descriptor *pgio;
3681da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
3691da177e4SLinus Torvalds };
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds static int
3721da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page)
3731da177e4SLinus Torvalds {
3741da177e4SLinus Torvalds 	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
3751da177e4SLinus Torvalds 	struct nfs_page *new;
3761da177e4SLinus Torvalds 	unsigned int len;
377de05a0ccSTrond Myklebust 	int error;
3781da177e4SLinus Torvalds 
37949a70f27STrond Myklebust 	len = nfs_page_length(page);
3801da177e4SLinus Torvalds 	if (len == 0)
3811da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
382de05a0ccSTrond Myklebust 
38328b1d3f5STrond Myklebust 	new = nfs_create_request(desc->ctx, page, 0, len);
384de05a0ccSTrond Myklebust 	if (IS_ERR(new))
385de05a0ccSTrond Myklebust 		goto out_error;
386de05a0ccSTrond Myklebust 
38709cbfeafSKirill A. Shutemov 	if (len < PAGE_SIZE)
38809cbfeafSKirill A. Shutemov 		zero_user_segment(page, len, PAGE_SIZE);
389f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(desc->pgio, new)) {
3900bcbf039SPeng Tao 		nfs_list_remove_request(new);
391f8512ad0SFred Isaman 		error = desc->pgio->pg_error;
3928f54c7a4STrond Myklebust 		nfs_readpage_release(new, error);
393cbebaf89STrond Myklebust 		goto out;
394f8512ad0SFred Isaman 	}
3951da177e4SLinus Torvalds 	return 0;
396de05a0ccSTrond Myklebust out_error:
397de05a0ccSTrond Myklebust 	error = PTR_ERR(new);
398de05a0ccSTrond Myklebust 	unlock_page(page);
399cbebaf89STrond Myklebust out:
400de05a0ccSTrond Myklebust 	return error;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping,
4041da177e4SLinus Torvalds 		struct list_head *pages, unsigned nr_pages)
4051da177e4SLinus Torvalds {
4068b09bee3STrond Myklebust 	struct nfs_pageio_descriptor pgio;
407a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *pgm;
4081da177e4SLinus Torvalds 	struct nfs_readdesc desc = {
4098b09bee3STrond Myklebust 		.pgio = &pgio,
4101da177e4SLinus Torvalds 	};
4111da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
4128b09bee3STrond Myklebust 	unsigned long npages;
4135f004cf2STrond Myklebust 	int ret = -ESTALE;
4141da177e4SLinus Torvalds 
4151e8968c5SNiels de Vos 	dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
4161da177e4SLinus Torvalds 			inode->i_sb->s_id,
4171e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(inode),
4181da177e4SLinus Torvalds 			nr_pages);
41991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
4201da177e4SLinus Torvalds 
4215f004cf2STrond Myklebust 	if (NFS_STALE(inode))
4225f004cf2STrond Myklebust 		goto out;
4235f004cf2STrond Myklebust 
4241da177e4SLinus Torvalds 	if (filp == NULL) {
425d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
4261da177e4SLinus Torvalds 		if (desc.ctx == NULL)
4271da177e4SLinus Torvalds 			return -EBADF;
4281da177e4SLinus Torvalds 	} else
429cd3758e3STrond Myklebust 		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
4309a9fc1c0SDavid Howells 
4319a9fc1c0SDavid Howells 	/* attempt to read as many of the pages as possible from the cache
4329a9fc1c0SDavid Howells 	 * - this returns -ENOBUFS immediately if the cookie is negative
4339a9fc1c0SDavid Howells 	 */
4349a9fc1c0SDavid Howells 	ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
4359a9fc1c0SDavid Howells 					 pages, &nr_pages);
4369a9fc1c0SDavid Howells 	if (ret == 0)
4379a9fc1c0SDavid Howells 		goto read_complete; /* all pages were read */
4389a9fc1c0SDavid Howells 
439fab5fc25SChristoph Hellwig 	nfs_pageio_init_read(&pgio, inode, false,
440fab5fc25SChristoph Hellwig 			     &nfs_async_read_completion_ops);
4418b09bee3STrond Myklebust 
4421da177e4SLinus Torvalds 	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
4438b09bee3STrond Myklebust 	nfs_pageio_complete(&pgio);
444a7d42ddbSWeston Andros Adamson 
445a7d42ddbSWeston Andros Adamson 	/* It doesn't make sense to do mirrored reads! */
446a7d42ddbSWeston Andros Adamson 	WARN_ON_ONCE(pgio.pg_mirror_count != 1);
447a7d42ddbSWeston Andros Adamson 
448a7d42ddbSWeston Andros Adamson 	pgm = &pgio.pg_mirrors[0];
449a7d42ddbSWeston Andros Adamson 	NFS_I(inode)->read_io += pgm->pg_bytes_written;
45009cbfeafSKirill A. Shutemov 	npages = (pgm->pg_bytes_written + PAGE_SIZE - 1) >>
45109cbfeafSKirill A. Shutemov 		 PAGE_SHIFT;
4528b09bee3STrond Myklebust 	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
4539a9fc1c0SDavid Howells read_complete:
4541da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
4555f004cf2STrond Myklebust out:
4561da177e4SLinus Torvalds 	return ret;
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
459f7b422b1SDavid Howells int __init nfs_init_readpagecache(void)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
4621e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
4631da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
46420c2df83SPaul Mundt 					     NULL);
4651da177e4SLinus Torvalds 	if (nfs_rdata_cachep == NULL)
4661da177e4SLinus Torvalds 		return -ENOMEM;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	return 0;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
471266bee88SDavid Brownell void nfs_destroy_readpagecache(void)
4721da177e4SLinus Torvalds {
4731a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_rdata_cachep);
4741da177e4SLinus Torvalds }
4754a0de55cSAnna Schumaker 
4764a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_read_ops = {
4774a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_readhdr_alloc,
4784a0de55cSAnna Schumaker 	.rw_free_header		= nfs_readhdr_free,
4790eecb214SAnna Schumaker 	.rw_done		= nfs_readpage_done,
4800eecb214SAnna Schumaker 	.rw_result		= nfs_readpage_result,
4811ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_read,
4824a0de55cSAnna Schumaker };
483