xref: /openbmc/linux/fs/nfs/read.c (revision 457c89965399115e5cd8bf38f9c597293405703d)
1*457c8996SThomas 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 
940bcbf039SPeng Tao static void nfs_readpage_release(struct nfs_page *req)
950bcbf039SPeng Tao {
969fcd5960STrond Myklebust 	struct inode *inode = d_inode(nfs_req_openctx(req)->dentry);
970bcbf039SPeng Tao 
980bcbf039SPeng Tao 	dprintk("NFS: read done (%s/%llu %d@%lld)\n", inode->i_sb->s_id,
990bcbf039SPeng Tao 		(unsigned long long)NFS_FILEID(inode), req->wb_bytes,
1000bcbf039SPeng Tao 		(long long)req_offset(req));
1010bcbf039SPeng Tao 
1020bcbf039SPeng Tao 	if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) {
1030bcbf039SPeng Tao 		if (PageUptodate(req->wb_page))
1040bcbf039SPeng Tao 			nfs_readpage_to_fscache(inode, req->wb_page, 0);
1050bcbf039SPeng Tao 
1060bcbf039SPeng Tao 		unlock_page(req->wb_page);
1070bcbf039SPeng Tao 	}
1080bcbf039SPeng Tao 	nfs_release_request(req);
1090bcbf039SPeng Tao }
1100bcbf039SPeng Tao 
111f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
1121da177e4SLinus Torvalds 		       struct page *page)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	struct nfs_page	*new;
1151da177e4SLinus Torvalds 	unsigned int len;
116c76069bdSFred Isaman 	struct nfs_pageio_descriptor pgio;
117a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *pgm;
1181da177e4SLinus Torvalds 
11949a70f27STrond Myklebust 	len = nfs_page_length(page);
1201da177e4SLinus Torvalds 	if (len == 0)
1211da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
12228b1d3f5STrond Myklebust 	new = nfs_create_request(ctx, page, 0, len);
1231da177e4SLinus Torvalds 	if (IS_ERR(new)) {
1241da177e4SLinus Torvalds 		unlock_page(page);
1251da177e4SLinus Torvalds 		return PTR_ERR(new);
1261da177e4SLinus Torvalds 	}
12709cbfeafSKirill A. Shutemov 	if (len < PAGE_SIZE)
12809cbfeafSKirill A. Shutemov 		zero_user_segment(page, len, PAGE_SIZE);
1291da177e4SLinus Torvalds 
130fab5fc25SChristoph Hellwig 	nfs_pageio_init_read(&pgio, inode, false,
131fab5fc25SChristoph Hellwig 			     &nfs_async_read_completion_ops);
1320bcbf039SPeng Tao 	if (!nfs_pageio_add_request(&pgio, new)) {
1330bcbf039SPeng Tao 		nfs_list_remove_request(new);
1340bcbf039SPeng Tao 		nfs_readpage_release(new);
1350bcbf039SPeng Tao 	}
1361751c363STrond Myklebust 	nfs_pageio_complete(&pgio);
137a7d42ddbSWeston Andros Adamson 
138a7d42ddbSWeston Andros Adamson 	/* It doesn't make sense to do mirrored reads! */
139a7d42ddbSWeston Andros Adamson 	WARN_ON_ONCE(pgio.pg_mirror_count != 1);
140a7d42ddbSWeston Andros Adamson 
141a7d42ddbSWeston Andros Adamson 	pgm = &pgio.pg_mirrors[0];
142a7d42ddbSWeston Andros Adamson 	NFS_I(inode)->read_io += pgm->pg_bytes_written;
143a7d42ddbSWeston Andros Adamson 
144d600ad1fSPeng Tao 	return pgio.pg_error < 0 ? pgio.pg_error : 0;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
14767d0338eSWeston Andros Adamson static void nfs_page_group_set_uptodate(struct nfs_page *req)
14867d0338eSWeston Andros Adamson {
14967d0338eSWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_UPTODATE))
15067d0338eSWeston Andros Adamson 		SetPageUptodate(req->wb_page);
15167d0338eSWeston Andros Adamson }
15267d0338eSWeston Andros Adamson 
153061ae2edSFred Isaman static void nfs_read_completion(struct nfs_pgio_header *hdr)
1544db6e0b7SFred Isaman {
1554db6e0b7SFred Isaman 	unsigned long bytes = 0;
1564db6e0b7SFred Isaman 
1574db6e0b7SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
1584db6e0b7SFred Isaman 		goto out;
1594db6e0b7SFred Isaman 	while (!list_empty(&hdr->pages)) {
1604db6e0b7SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
1614db6e0b7SFred Isaman 		struct page *page = req->wb_page;
16268072992SWeston Andros Adamson 		unsigned long start = req->wb_pgbase;
16368072992SWeston Andros Adamson 		unsigned long end = req->wb_pgbase + req->wb_bytes;
1644db6e0b7SFred Isaman 
1654db6e0b7SFred Isaman 		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
16668072992SWeston Andros Adamson 			/* note: regions of the page not covered by a
16768072992SWeston Andros Adamson 			 * request are zeroed in nfs_readpage_async /
16868072992SWeston Andros Adamson 			 * readpage_async_filler */
16968072992SWeston Andros Adamson 			if (bytes > hdr->good_bytes) {
17068072992SWeston Andros Adamson 				/* nothing in this request was good, so zero
17168072992SWeston Andros Adamson 				 * the full extent of the request */
17268072992SWeston Andros Adamson 				zero_user_segment(page, start, end);
17368072992SWeston Andros Adamson 
17468072992SWeston Andros Adamson 			} else if (hdr->good_bytes - bytes < req->wb_bytes) {
17568072992SWeston Andros Adamson 				/* part of this request has good bytes, but
17668072992SWeston Andros Adamson 				 * not all. zero the bad bytes */
17768072992SWeston Andros Adamson 				start += hdr->good_bytes - bytes;
17868072992SWeston Andros Adamson 				WARN_ON(start < req->wb_pgbase);
17968072992SWeston Andros Adamson 				zero_user_segment(page, start, end);
18068072992SWeston Andros Adamson 			}
1814db6e0b7SFred Isaman 		}
1824bd8b010STrond Myklebust 		bytes += req->wb_bytes;
1834bd8b010STrond Myklebust 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
1844bd8b010STrond Myklebust 			if (bytes <= hdr->good_bytes)
18567d0338eSWeston Andros Adamson 				nfs_page_group_set_uptodate(req);
1864bd8b010STrond Myklebust 		} else
18767d0338eSWeston Andros Adamson 			nfs_page_group_set_uptodate(req);
1884db6e0b7SFred Isaman 		nfs_list_remove_request(req);
1894db6e0b7SFred Isaman 		nfs_readpage_release(req);
1904db6e0b7SFred Isaman 	}
1914db6e0b7SFred Isaman out:
1924db6e0b7SFred Isaman 	hdr->release(hdr);
1934db6e0b7SFred Isaman }
1944db6e0b7SFred Isaman 
195d45f60c6SWeston Andros Adamson static void nfs_initiate_read(struct nfs_pgio_header *hdr,
196d45f60c6SWeston Andros Adamson 			      struct rpc_message *msg,
197abde71f4STom Haynes 			      const struct nfs_rpc_ops *rpc_ops,
1981ed26f33SAnna Schumaker 			      struct rpc_task_setup *task_setup_data, int how)
19964419a9bSAndy Adamson {
200d45f60c6SWeston Andros Adamson 	struct inode *inode = hdr->inode;
20164419a9bSAndy Adamson 	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
20264419a9bSAndy Adamson 
2031ed26f33SAnna Schumaker 	task_setup_data->flags |= swap_flags;
204abde71f4STom Haynes 	rpc_ops->read_setup(hdr, msg);
2058224b273SChuck Lever 	trace_nfs_initiate_read(inode, hdr->io_start, hdr->good_bytes);
20664419a9bSAndy Adamson }
20764419a9bSAndy Adamson 
208061ae2edSFred Isaman static void
209df3accb8STrond Myklebust nfs_async_read_error(struct list_head *head, int error)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	struct nfs_page	*req;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	while (!list_empty(head)) {
2141da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
2151da177e4SLinus Torvalds 		nfs_list_remove_request(req);
2161da177e4SLinus Torvalds 		nfs_readpage_release(req);
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
220061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
221061ae2edSFred Isaman 	.error_cleanup = nfs_async_read_error,
222061ae2edSFred Isaman 	.completion = nfs_read_completion,
223061ae2edSFred Isaman };
224061ae2edSFred Isaman 
2251da177e4SLinus Torvalds /*
2260b671301STrond Myklebust  * This is the callback from RPC telling us whether a reply was
2270b671301STrond Myklebust  * received or some error occurred (timeout or socket shutdown).
2280b671301STrond Myklebust  */
229d45f60c6SWeston Andros Adamson static int nfs_readpage_done(struct rpc_task *task,
230d45f60c6SWeston Andros Adamson 			     struct nfs_pgio_header *hdr,
2310eecb214SAnna Schumaker 			     struct inode *inode)
2320b671301STrond Myklebust {
233d45f60c6SWeston Andros Adamson 	int status = NFS_PROTO(inode)->read_done(task, hdr);
2340b671301STrond Myklebust 	if (status != 0)
2350b671301STrond Myklebust 		return status;
2360b671301STrond Myklebust 
237d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count);
2388224b273SChuck Lever 	trace_nfs_readpage_done(inode, task->tk_status,
2398224b273SChuck Lever 				hdr->args.offset, hdr->res.eof);
2400b671301STrond Myklebust 
2410b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
242cd841605SFred Isaman 		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
243cd841605SFred Isaman 		nfs_mark_for_revalidate(inode);
2440b671301STrond Myklebust 	}
2450b671301STrond Myklebust 	return 0;
2460b671301STrond Myklebust }
2470b671301STrond Myklebust 
248d45f60c6SWeston Andros Adamson static void nfs_readpage_retry(struct rpc_task *task,
249d45f60c6SWeston Andros Adamson 			       struct nfs_pgio_header *hdr)
2500b671301STrond Myklebust {
251d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args *argp = &hdr->args;
252d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res  *resp = &hdr->res;
2530b671301STrond Myklebust 
2540b671301STrond Myklebust 	/* This is a short read! */
255d45f60c6SWeston Andros Adamson 	nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD);
2560b671301STrond Myklebust 	/* Has the server at least made some progress? */
2574db6e0b7SFred Isaman 	if (resp->count == 0) {
258d45f60c6SWeston Andros Adamson 		nfs_set_pgio_error(hdr, -EIO, argp->offset);
259d61e612aSTrond Myklebust 		return;
2604db6e0b7SFred Isaman 	}
261f8417b48SKinglong Mee 
262f8417b48SKinglong Mee 	/* For non rpc-based layout drivers, retry-through-MDS */
263f8417b48SKinglong Mee 	if (!task->tk_ops) {
264f8417b48SKinglong Mee 		hdr->pnfs_error = -EAGAIN;
265f8417b48SKinglong Mee 		return;
266f8417b48SKinglong Mee 	}
267f8417b48SKinglong Mee 
268d45f60c6SWeston Andros Adamson 	/* Yes, so retry the read at the end of the hdr */
269d45f60c6SWeston Andros Adamson 	hdr->mds_offset += resp->count;
2700b671301STrond Myklebust 	argp->offset += resp->count;
2710b671301STrond Myklebust 	argp->pgbase += resp->count;
2720b671301STrond Myklebust 	argp->count -= resp->count;
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 
2921da177e4SLinus Torvalds /*
2931da177e4SLinus Torvalds  * Read a page over NFS.
2941da177e4SLinus Torvalds  * We read the page synchronously in the following case:
2951da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
2961da177e4SLinus Torvalds  *	previous async read operation failed.
2971da177e4SLinus Torvalds  */
2981da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
301d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
3021da177e4SLinus Torvalds 	int		error;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
3058cd79788SHuang Ying 		page, PAGE_SIZE, page_index(page));
30691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
3073708f842SNicolas Iooss 	nfs_add_stats(inode, NFSIOS_READPAGES, 1);
30891d5b470SChuck Lever 
3091da177e4SLinus Torvalds 	/*
3101da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
3111da177e4SLinus Torvalds 	 *
3121da177e4SLinus Torvalds 	 * NOTE! Because we own the page lock, there cannot
3131da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
3141da177e4SLinus Torvalds 	 * for this page (other pages can be written to).
3151da177e4SLinus Torvalds 	 */
3161da177e4SLinus Torvalds 	error = nfs_wb_page(inode, page);
3171da177e4SLinus Torvalds 	if (error)
318de05a0ccSTrond Myklebust 		goto out_unlock;
319de05a0ccSTrond Myklebust 	if (PageUptodate(page))
320de05a0ccSTrond Myklebust 		goto out_unlock;
3211da177e4SLinus Torvalds 
3225f004cf2STrond Myklebust 	error = -ESTALE;
3235f004cf2STrond Myklebust 	if (NFS_STALE(inode))
324de05a0ccSTrond Myklebust 		goto out_unlock;
3255f004cf2STrond Myklebust 
3261da177e4SLinus Torvalds 	if (file == NULL) {
327cf1308ffSTrond Myklebust 		error = -EBADF;
328d530838bSTrond Myklebust 		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
3291da177e4SLinus Torvalds 		if (ctx == NULL)
330de05a0ccSTrond Myklebust 			goto out_unlock;
3311da177e4SLinus Torvalds 	} else
332cd3758e3STrond Myklebust 		ctx = get_nfs_open_context(nfs_file_open_context(file));
3331da177e4SLinus Torvalds 
3349a9fc1c0SDavid Howells 	if (!IS_SYNC(inode)) {
3359a9fc1c0SDavid Howells 		error = nfs_readpage_from_fscache(ctx, inode, page);
3369a9fc1c0SDavid Howells 		if (error == 0)
3379a9fc1c0SDavid Howells 			goto out;
3389a9fc1c0SDavid Howells 	}
3399a9fc1c0SDavid Howells 
3408e0969f0STrond Myklebust 	error = nfs_readpage_async(ctx, inode, page);
3418e0969f0STrond Myklebust 
3429a9fc1c0SDavid Howells out:
3431da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
3441da177e4SLinus Torvalds 	return error;
345de05a0ccSTrond Myklebust out_unlock:
3461da177e4SLinus Torvalds 	unlock_page(page);
3471da177e4SLinus Torvalds 	return error;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds struct nfs_readdesc {
3518b09bee3STrond Myklebust 	struct nfs_pageio_descriptor *pgio;
3521da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
3531da177e4SLinus Torvalds };
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds static int
3561da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page)
3571da177e4SLinus Torvalds {
3581da177e4SLinus Torvalds 	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
3591da177e4SLinus Torvalds 	struct nfs_page *new;
3601da177e4SLinus Torvalds 	unsigned int len;
361de05a0ccSTrond Myklebust 	int error;
3621da177e4SLinus Torvalds 
36349a70f27STrond Myklebust 	len = nfs_page_length(page);
3641da177e4SLinus Torvalds 	if (len == 0)
3651da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
366de05a0ccSTrond Myklebust 
36728b1d3f5STrond Myklebust 	new = nfs_create_request(desc->ctx, page, 0, len);
368de05a0ccSTrond Myklebust 	if (IS_ERR(new))
369de05a0ccSTrond Myklebust 		goto out_error;
370de05a0ccSTrond Myklebust 
37109cbfeafSKirill A. Shutemov 	if (len < PAGE_SIZE)
37209cbfeafSKirill A. Shutemov 		zero_user_segment(page, len, PAGE_SIZE);
373f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(desc->pgio, new)) {
3740bcbf039SPeng Tao 		nfs_list_remove_request(new);
3750bcbf039SPeng Tao 		nfs_readpage_release(new);
376f8512ad0SFred Isaman 		error = desc->pgio->pg_error;
377cbebaf89STrond Myklebust 		goto out;
378f8512ad0SFred Isaman 	}
3791da177e4SLinus Torvalds 	return 0;
380de05a0ccSTrond Myklebust out_error:
381de05a0ccSTrond Myklebust 	error = PTR_ERR(new);
382de05a0ccSTrond Myklebust 	unlock_page(page);
383cbebaf89STrond Myklebust out:
384de05a0ccSTrond Myklebust 	return error;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping,
3881da177e4SLinus Torvalds 		struct list_head *pages, unsigned nr_pages)
3891da177e4SLinus Torvalds {
3908b09bee3STrond Myklebust 	struct nfs_pageio_descriptor pgio;
391a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *pgm;
3921da177e4SLinus Torvalds 	struct nfs_readdesc desc = {
3938b09bee3STrond Myklebust 		.pgio = &pgio,
3941da177e4SLinus Torvalds 	};
3951da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
3968b09bee3STrond Myklebust 	unsigned long npages;
3975f004cf2STrond Myklebust 	int ret = -ESTALE;
3981da177e4SLinus Torvalds 
3991e8968c5SNiels de Vos 	dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
4001da177e4SLinus Torvalds 			inode->i_sb->s_id,
4011e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(inode),
4021da177e4SLinus Torvalds 			nr_pages);
40391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
4041da177e4SLinus Torvalds 
4055f004cf2STrond Myklebust 	if (NFS_STALE(inode))
4065f004cf2STrond Myklebust 		goto out;
4075f004cf2STrond Myklebust 
4081da177e4SLinus Torvalds 	if (filp == NULL) {
409d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
4101da177e4SLinus Torvalds 		if (desc.ctx == NULL)
4111da177e4SLinus Torvalds 			return -EBADF;
4121da177e4SLinus Torvalds 	} else
413cd3758e3STrond Myklebust 		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
4149a9fc1c0SDavid Howells 
4159a9fc1c0SDavid Howells 	/* attempt to read as many of the pages as possible from the cache
4169a9fc1c0SDavid Howells 	 * - this returns -ENOBUFS immediately if the cookie is negative
4179a9fc1c0SDavid Howells 	 */
4189a9fc1c0SDavid Howells 	ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
4199a9fc1c0SDavid Howells 					 pages, &nr_pages);
4209a9fc1c0SDavid Howells 	if (ret == 0)
4219a9fc1c0SDavid Howells 		goto read_complete; /* all pages were read */
4229a9fc1c0SDavid Howells 
423fab5fc25SChristoph Hellwig 	nfs_pageio_init_read(&pgio, inode, false,
424fab5fc25SChristoph Hellwig 			     &nfs_async_read_completion_ops);
4258b09bee3STrond Myklebust 
4261da177e4SLinus Torvalds 	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
4278b09bee3STrond Myklebust 	nfs_pageio_complete(&pgio);
428a7d42ddbSWeston Andros Adamson 
429a7d42ddbSWeston Andros Adamson 	/* It doesn't make sense to do mirrored reads! */
430a7d42ddbSWeston Andros Adamson 	WARN_ON_ONCE(pgio.pg_mirror_count != 1);
431a7d42ddbSWeston Andros Adamson 
432a7d42ddbSWeston Andros Adamson 	pgm = &pgio.pg_mirrors[0];
433a7d42ddbSWeston Andros Adamson 	NFS_I(inode)->read_io += pgm->pg_bytes_written;
43409cbfeafSKirill A. Shutemov 	npages = (pgm->pg_bytes_written + PAGE_SIZE - 1) >>
43509cbfeafSKirill A. Shutemov 		 PAGE_SHIFT;
4368b09bee3STrond Myklebust 	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
4379a9fc1c0SDavid Howells read_complete:
4381da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
4395f004cf2STrond Myklebust out:
4401da177e4SLinus Torvalds 	return ret;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
443f7b422b1SDavid Howells int __init nfs_init_readpagecache(void)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
4461e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
4471da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
44820c2df83SPaul Mundt 					     NULL);
4491da177e4SLinus Torvalds 	if (nfs_rdata_cachep == NULL)
4501da177e4SLinus Torvalds 		return -ENOMEM;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	return 0;
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
455266bee88SDavid Brownell void nfs_destroy_readpagecache(void)
4561da177e4SLinus Torvalds {
4571a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_rdata_cachep);
4581da177e4SLinus Torvalds }
4594a0de55cSAnna Schumaker 
4604a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_read_ops = {
4614a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_readhdr_alloc,
4624a0de55cSAnna Schumaker 	.rw_free_header		= nfs_readhdr_free,
4630eecb214SAnna Schumaker 	.rw_done		= nfs_readpage_done,
4640eecb214SAnna Schumaker 	.rw_result		= nfs_readpage_result,
4651ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_read,
4664a0de55cSAnna Schumaker };
467