xref: /openbmc/linux/fs/nfs/read.c (revision d45f60c67848b9f19160692581d78e5b4757a000)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/read.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Block I/O for NFS
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Partial copy of Linus' read cache modifications to fs/nfs/file.c
71da177e4SLinus Torvalds  * modified for async RPC by okir@monad.swb.de
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/time.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/errno.h>
131da177e4SLinus Torvalds #include <linux/fcntl.h>
141da177e4SLinus Torvalds #include <linux/stat.h>
151da177e4SLinus Torvalds #include <linux/mm.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/pagemap.h>
181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
191da177e4SLinus Torvalds #include <linux/nfs_fs.h>
201da177e4SLinus Torvalds #include <linux/nfs_page.h>
2164419a9bSAndy Adamson #include <linux/module.h>
221da177e4SLinus Torvalds 
23f11c88afSAndy Adamson #include "nfs4_fs.h"
2449a70f27STrond Myklebust #include "internal.h"
2591d5b470SChuck Lever #include "iostat.h"
269a9fc1c0SDavid Howells #include "fscache.h"
27fab5fc25SChristoph Hellwig #include "pnfs.h"
2891d5b470SChuck Lever 
291da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
301da177e4SLinus Torvalds 
31061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
324a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_read_ops;
331da177e4SLinus Torvalds 
34e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep;
351da177e4SLinus Torvalds 
361e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_readhdr_alloc(void)
373feb2d49STrond Myklebust {
384a0de55cSAnna Schumaker 	return kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
394db6e0b7SFred Isaman }
404db6e0b7SFred Isaman 
411e7f3a48SWeston Andros Adamson static void nfs_readhdr_free(struct nfs_pgio_header *rhdr)
423feb2d49STrond Myklebust {
43cd841605SFred Isaman 	kmem_cache_free(nfs_rdata_cachep, rhdr);
443feb2d49STrond Myklebust }
453feb2d49STrond Myklebust 
461da177e4SLinus Torvalds static
471da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page)
481da177e4SLinus Torvalds {
49eebd2aa3SChristoph Lameter 	zero_user(page, 0, PAGE_CACHE_SIZE);
501da177e4SLinus Torvalds 	SetPageUptodate(page);
511da177e4SLinus Torvalds 	unlock_page(page);
521da177e4SLinus Torvalds 	return 0;
531da177e4SLinus Torvalds }
541da177e4SLinus Torvalds 
551abb5088SBryan Schumaker void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
56fab5fc25SChristoph Hellwig 			      struct inode *inode, bool force_mds,
57061ae2edSFred Isaman 			      const struct nfs_pgio_completion_ops *compl_ops)
581751c363STrond Myklebust {
59fab5fc25SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
6041d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
61fab5fc25SChristoph Hellwig 
62fab5fc25SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
63fab5fc25SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
64fab5fc25SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_read_ops;
65fab5fc25SChristoph Hellwig #endif
664a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
674a0de55cSAnna Schumaker 			server->rsize, 0);
681751c363STrond Myklebust }
69ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
701751c363STrond Myklebust 
71493292ddSTrond Myklebust void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
72493292ddSTrond Myklebust {
7341d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
74493292ddSTrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
75493292ddSTrond Myklebust }
761f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
77493292ddSTrond Myklebust 
78f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
791da177e4SLinus Torvalds 		       struct page *page)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	struct nfs_page	*new;
821da177e4SLinus Torvalds 	unsigned int len;
83c76069bdSFred Isaman 	struct nfs_pageio_descriptor pgio;
841da177e4SLinus Torvalds 
8549a70f27STrond Myklebust 	len = nfs_page_length(page);
861da177e4SLinus Torvalds 	if (len == 0)
871da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
882bfc6e56SWeston Andros Adamson 	new = nfs_create_request(ctx, page, NULL, 0, len);
891da177e4SLinus Torvalds 	if (IS_ERR(new)) {
901da177e4SLinus Torvalds 		unlock_page(page);
911da177e4SLinus Torvalds 		return PTR_ERR(new);
921da177e4SLinus Torvalds 	}
931da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
94eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
951da177e4SLinus Torvalds 
96fab5fc25SChristoph Hellwig 	nfs_pageio_init_read(&pgio, inode, false,
97fab5fc25SChristoph Hellwig 			     &nfs_async_read_completion_ops);
98d8007d4dSTrond Myklebust 	nfs_pageio_add_request(&pgio, new);
991751c363STrond Myklebust 	nfs_pageio_complete(&pgio);
1002701d086SAndy Adamson 	NFS_I(inode)->read_io += pgio.pg_bytes_written;
1011da177e4SLinus Torvalds 	return 0;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req)
1051da177e4SLinus Torvalds {
1063d4ff43dSAl Viro 	struct inode *d_inode = req->wb_context->dentry->d_inode;
1077f8e05f6SDavid Howells 
10867d0338eSWeston Andros Adamson 	dprintk("NFS: read done (%s/%llu %d@%lld)\n", d_inode->i_sb->s_id,
10967d0338eSWeston Andros Adamson 		(unsigned long long)NFS_FILEID(d_inode), req->wb_bytes,
11067d0338eSWeston Andros Adamson 		(long long)req_offset(req));
11167d0338eSWeston Andros Adamson 
11267d0338eSWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) {
1137f8e05f6SDavid Howells 		if (PageUptodate(req->wb_page))
1147f8e05f6SDavid Howells 			nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
1157f8e05f6SDavid Howells 
1161da177e4SLinus Torvalds 		unlock_page(req->wb_page);
11767d0338eSWeston Andros Adamson 	}
1181da177e4SLinus Torvalds 
1191e8968c5SNiels de Vos 	dprintk("NFS: read done (%s/%Lu %d@%Ld)\n",
1203d4ff43dSAl Viro 			req->wb_context->dentry->d_inode->i_sb->s_id,
1211e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1221da177e4SLinus Torvalds 			req->wb_bytes,
1231da177e4SLinus Torvalds 			(long long)req_offset(req));
12410d2c46fSNick Wilson 	nfs_release_request(req);
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
12767d0338eSWeston Andros Adamson static void nfs_page_group_set_uptodate(struct nfs_page *req)
12867d0338eSWeston Andros Adamson {
12967d0338eSWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_UPTODATE))
13067d0338eSWeston Andros Adamson 		SetPageUptodate(req->wb_page);
13167d0338eSWeston Andros Adamson }
13267d0338eSWeston Andros Adamson 
133061ae2edSFred Isaman static void nfs_read_completion(struct nfs_pgio_header *hdr)
1344db6e0b7SFred Isaman {
1354db6e0b7SFred Isaman 	unsigned long bytes = 0;
1364db6e0b7SFred Isaman 
1374db6e0b7SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
1384db6e0b7SFred Isaman 		goto out;
1394db6e0b7SFred Isaman 	while (!list_empty(&hdr->pages)) {
1404db6e0b7SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
1414db6e0b7SFred Isaman 		struct page *page = req->wb_page;
14268072992SWeston Andros Adamson 		unsigned long start = req->wb_pgbase;
14368072992SWeston Andros Adamson 		unsigned long end = req->wb_pgbase + req->wb_bytes;
1444db6e0b7SFred Isaman 
1454db6e0b7SFred Isaman 		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
14668072992SWeston Andros Adamson 			/* note: regions of the page not covered by a
14768072992SWeston Andros Adamson 			 * request are zeroed in nfs_readpage_async /
14868072992SWeston Andros Adamson 			 * readpage_async_filler */
14968072992SWeston Andros Adamson 			if (bytes > hdr->good_bytes) {
15068072992SWeston Andros Adamson 				/* nothing in this request was good, so zero
15168072992SWeston Andros Adamson 				 * the full extent of the request */
15268072992SWeston Andros Adamson 				zero_user_segment(page, start, end);
15368072992SWeston Andros Adamson 
15468072992SWeston Andros Adamson 			} else if (hdr->good_bytes - bytes < req->wb_bytes) {
15568072992SWeston Andros Adamson 				/* part of this request has good bytes, but
15668072992SWeston Andros Adamson 				 * not all. zero the bad bytes */
15768072992SWeston Andros Adamson 				start += hdr->good_bytes - bytes;
15868072992SWeston Andros Adamson 				WARN_ON(start < req->wb_pgbase);
15968072992SWeston Andros Adamson 				zero_user_segment(page, start, end);
16068072992SWeston Andros Adamson 			}
1614db6e0b7SFred Isaman 		}
1624bd8b010STrond Myklebust 		bytes += req->wb_bytes;
1634bd8b010STrond Myklebust 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
1644bd8b010STrond Myklebust 			if (bytes <= hdr->good_bytes)
16567d0338eSWeston Andros Adamson 				nfs_page_group_set_uptodate(req);
1664bd8b010STrond Myklebust 		} else
16767d0338eSWeston Andros Adamson 			nfs_page_group_set_uptodate(req);
1684db6e0b7SFred Isaman 		nfs_list_remove_request(req);
1694db6e0b7SFred Isaman 		nfs_readpage_release(req);
1704db6e0b7SFred Isaman 	}
1714db6e0b7SFred Isaman out:
1724db6e0b7SFred Isaman 	hdr->release(hdr);
1734db6e0b7SFred Isaman }
1744db6e0b7SFred Isaman 
175*d45f60c6SWeston Andros Adamson static void nfs_initiate_read(struct nfs_pgio_header *hdr,
176*d45f60c6SWeston Andros Adamson 			      struct rpc_message *msg,
1771ed26f33SAnna Schumaker 			      struct rpc_task_setup *task_setup_data, int how)
17864419a9bSAndy Adamson {
179*d45f60c6SWeston Andros Adamson 	struct inode *inode = hdr->inode;
18064419a9bSAndy Adamson 	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
18164419a9bSAndy Adamson 
1821ed26f33SAnna Schumaker 	task_setup_data->flags |= swap_flags;
183*d45f60c6SWeston Andros Adamson 	NFS_PROTO(inode)->read_setup(hdr, msg);
18464419a9bSAndy Adamson }
18564419a9bSAndy Adamson 
186061ae2edSFred Isaman static void
1871da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	struct nfs_page	*req;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	while (!list_empty(head)) {
1921da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
1931da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1941da177e4SLinus Torvalds 		nfs_readpage_release(req);
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
198061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
199061ae2edSFred Isaman 	.error_cleanup = nfs_async_read_error,
200061ae2edSFred Isaman 	.completion = nfs_read_completion,
201061ae2edSFred Isaman };
202061ae2edSFred Isaman 
2031da177e4SLinus Torvalds /*
2040b671301STrond Myklebust  * This is the callback from RPC telling us whether a reply was
2050b671301STrond Myklebust  * received or some error occurred (timeout or socket shutdown).
2060b671301STrond Myklebust  */
207*d45f60c6SWeston Andros Adamson static int nfs_readpage_done(struct rpc_task *task,
208*d45f60c6SWeston Andros Adamson 			     struct nfs_pgio_header *hdr,
2090eecb214SAnna Schumaker 			     struct inode *inode)
2100b671301STrond Myklebust {
211*d45f60c6SWeston Andros Adamson 	int status = NFS_PROTO(inode)->read_done(task, hdr);
2120b671301STrond Myklebust 	if (status != 0)
2130b671301STrond Myklebust 		return status;
2140b671301STrond Myklebust 
215*d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count);
2160b671301STrond Myklebust 
2170b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
218cd841605SFred Isaman 		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
219cd841605SFred Isaman 		nfs_mark_for_revalidate(inode);
2200b671301STrond Myklebust 	}
2210b671301STrond Myklebust 	return 0;
2220b671301STrond Myklebust }
2230b671301STrond Myklebust 
224*d45f60c6SWeston Andros Adamson static void nfs_readpage_retry(struct rpc_task *task,
225*d45f60c6SWeston Andros Adamson 			       struct nfs_pgio_header *hdr)
2260b671301STrond Myklebust {
227*d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args *argp = &hdr->args;
228*d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res  *resp = &hdr->res;
2290b671301STrond Myklebust 
2300b671301STrond Myklebust 	/* This is a short read! */
231*d45f60c6SWeston Andros Adamson 	nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD);
2320b671301STrond Myklebust 	/* Has the server at least made some progress? */
2334db6e0b7SFred Isaman 	if (resp->count == 0) {
234*d45f60c6SWeston Andros Adamson 		nfs_set_pgio_error(hdr, -EIO, argp->offset);
235d61e612aSTrond Myklebust 		return;
2364db6e0b7SFred Isaman 	}
237*d45f60c6SWeston Andros Adamson 	/* Yes, so retry the read at the end of the hdr */
238*d45f60c6SWeston Andros Adamson 	hdr->mds_offset += resp->count;
2390b671301STrond Myklebust 	argp->offset += resp->count;
2400b671301STrond Myklebust 	argp->pgbase += resp->count;
2410b671301STrond Myklebust 	argp->count -= resp->count;
242d00c5d43STrond Myklebust 	rpc_restart_call_prepare(task);
2430b671301STrond Myklebust }
2440b671301STrond Myklebust 
245*d45f60c6SWeston Andros Adamson static void nfs_readpage_result(struct rpc_task *task,
246*d45f60c6SWeston Andros Adamson 				struct nfs_pgio_header *hdr)
2471da177e4SLinus Torvalds {
248*d45f60c6SWeston Andros Adamson 	if (hdr->res.eof) {
2494db6e0b7SFred Isaman 		loff_t bound;
250fdd1e74cSTrond Myklebust 
251*d45f60c6SWeston Andros Adamson 		bound = hdr->args.offset + hdr->res.count;
2524db6e0b7SFred Isaman 		spin_lock(&hdr->lock);
2534db6e0b7SFred Isaman 		if (bound < hdr->io_start + hdr->good_bytes) {
2544db6e0b7SFred Isaman 			set_bit(NFS_IOHDR_EOF, &hdr->flags);
2554db6e0b7SFred Isaman 			clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
2564db6e0b7SFred Isaman 			hdr->good_bytes = bound - hdr->io_start;
2574db6e0b7SFred Isaman 		}
2584db6e0b7SFred Isaman 		spin_unlock(&hdr->lock);
259*d45f60c6SWeston Andros Adamson 	} else if (hdr->res.count != hdr->args.count)
260*d45f60c6SWeston Andros Adamson 		nfs_readpage_retry(task, hdr);
2610b671301STrond Myklebust }
262fdd1e74cSTrond Myklebust 
2631da177e4SLinus Torvalds /*
2641da177e4SLinus Torvalds  * Read a page over NFS.
2651da177e4SLinus Torvalds  * We read the page synchronously in the following case:
2661da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
2671da177e4SLinus Torvalds  *	previous async read operation failed.
2681da177e4SLinus Torvalds  */
2691da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
272d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
2731da177e4SLinus Torvalds 	int		error;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
276d56b4ddfSMel Gorman 		page, PAGE_CACHE_SIZE, page_file_index(page));
27791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
27891d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_READPAGES, 1);
27991d5b470SChuck Lever 
2801da177e4SLinus Torvalds 	/*
2811da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
2821da177e4SLinus Torvalds 	 *
2831da177e4SLinus Torvalds 	 * NOTE! Because we own the page lock, there cannot
2841da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
2851da177e4SLinus Torvalds 	 * for this page (other pages can be written to).
2861da177e4SLinus Torvalds 	 */
2871da177e4SLinus Torvalds 	error = nfs_wb_page(inode, page);
2881da177e4SLinus Torvalds 	if (error)
289de05a0ccSTrond Myklebust 		goto out_unlock;
290de05a0ccSTrond Myklebust 	if (PageUptodate(page))
291de05a0ccSTrond Myklebust 		goto out_unlock;
2921da177e4SLinus Torvalds 
2935f004cf2STrond Myklebust 	error = -ESTALE;
2945f004cf2STrond Myklebust 	if (NFS_STALE(inode))
295de05a0ccSTrond Myklebust 		goto out_unlock;
2965f004cf2STrond Myklebust 
2971da177e4SLinus Torvalds 	if (file == NULL) {
298cf1308ffSTrond Myklebust 		error = -EBADF;
299d530838bSTrond Myklebust 		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
3001da177e4SLinus Torvalds 		if (ctx == NULL)
301de05a0ccSTrond Myklebust 			goto out_unlock;
3021da177e4SLinus Torvalds 	} else
303cd3758e3STrond Myklebust 		ctx = get_nfs_open_context(nfs_file_open_context(file));
3041da177e4SLinus Torvalds 
3059a9fc1c0SDavid Howells 	if (!IS_SYNC(inode)) {
3069a9fc1c0SDavid Howells 		error = nfs_readpage_from_fscache(ctx, inode, page);
3079a9fc1c0SDavid Howells 		if (error == 0)
3089a9fc1c0SDavid Howells 			goto out;
3099a9fc1c0SDavid Howells 	}
3109a9fc1c0SDavid Howells 
3118e0969f0STrond Myklebust 	error = nfs_readpage_async(ctx, inode, page);
3128e0969f0STrond Myklebust 
3139a9fc1c0SDavid Howells out:
3141da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
3151da177e4SLinus Torvalds 	return error;
316de05a0ccSTrond Myklebust out_unlock:
3171da177e4SLinus Torvalds 	unlock_page(page);
3181da177e4SLinus Torvalds 	return error;
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds struct nfs_readdesc {
3228b09bee3STrond Myklebust 	struct nfs_pageio_descriptor *pgio;
3231da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
3241da177e4SLinus Torvalds };
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds static int
3271da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
3301da177e4SLinus Torvalds 	struct nfs_page *new;
3311da177e4SLinus Torvalds 	unsigned int len;
332de05a0ccSTrond Myklebust 	int error;
3331da177e4SLinus Torvalds 
33449a70f27STrond Myklebust 	len = nfs_page_length(page);
3351da177e4SLinus Torvalds 	if (len == 0)
3361da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
337de05a0ccSTrond Myklebust 
3382bfc6e56SWeston Andros Adamson 	new = nfs_create_request(desc->ctx, page, NULL, 0, len);
339de05a0ccSTrond Myklebust 	if (IS_ERR(new))
340de05a0ccSTrond Myklebust 		goto out_error;
341de05a0ccSTrond Myklebust 
3421da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
343eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
344f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(desc->pgio, new)) {
345f8512ad0SFred Isaman 		error = desc->pgio->pg_error;
346f8512ad0SFred Isaman 		goto out_unlock;
347f8512ad0SFred Isaman 	}
3481da177e4SLinus Torvalds 	return 0;
349de05a0ccSTrond Myklebust out_error:
350de05a0ccSTrond Myklebust 	error = PTR_ERR(new);
351de05a0ccSTrond Myklebust out_unlock:
352de05a0ccSTrond Myklebust 	unlock_page(page);
353de05a0ccSTrond Myklebust 	return error;
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping,
3571da177e4SLinus Torvalds 		struct list_head *pages, unsigned nr_pages)
3581da177e4SLinus Torvalds {
3598b09bee3STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3601da177e4SLinus Torvalds 	struct nfs_readdesc desc = {
3618b09bee3STrond Myklebust 		.pgio = &pgio,
3621da177e4SLinus Torvalds 	};
3631da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
3648b09bee3STrond Myklebust 	unsigned long npages;
3655f004cf2STrond Myklebust 	int ret = -ESTALE;
3661da177e4SLinus Torvalds 
3671e8968c5SNiels de Vos 	dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
3681da177e4SLinus Torvalds 			inode->i_sb->s_id,
3691e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(inode),
3701da177e4SLinus Torvalds 			nr_pages);
37191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
3721da177e4SLinus Torvalds 
3735f004cf2STrond Myklebust 	if (NFS_STALE(inode))
3745f004cf2STrond Myklebust 		goto out;
3755f004cf2STrond Myklebust 
3761da177e4SLinus Torvalds 	if (filp == NULL) {
377d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
3781da177e4SLinus Torvalds 		if (desc.ctx == NULL)
3791da177e4SLinus Torvalds 			return -EBADF;
3801da177e4SLinus Torvalds 	} else
381cd3758e3STrond Myklebust 		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
3829a9fc1c0SDavid Howells 
3839a9fc1c0SDavid Howells 	/* attempt to read as many of the pages as possible from the cache
3849a9fc1c0SDavid Howells 	 * - this returns -ENOBUFS immediately if the cookie is negative
3859a9fc1c0SDavid Howells 	 */
3869a9fc1c0SDavid Howells 	ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
3879a9fc1c0SDavid Howells 					 pages, &nr_pages);
3889a9fc1c0SDavid Howells 	if (ret == 0)
3899a9fc1c0SDavid Howells 		goto read_complete; /* all pages were read */
3909a9fc1c0SDavid Howells 
391fab5fc25SChristoph Hellwig 	nfs_pageio_init_read(&pgio, inode, false,
392fab5fc25SChristoph Hellwig 			     &nfs_async_read_completion_ops);
3938b09bee3STrond Myklebust 
3941da177e4SLinus Torvalds 	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
3958b09bee3STrond Myklebust 
3968b09bee3STrond Myklebust 	nfs_pageio_complete(&pgio);
3972701d086SAndy Adamson 	NFS_I(inode)->read_io += pgio.pg_bytes_written;
3988b09bee3STrond Myklebust 	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
3998b09bee3STrond Myklebust 	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
4009a9fc1c0SDavid Howells read_complete:
4011da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
4025f004cf2STrond Myklebust out:
4031da177e4SLinus Torvalds 	return ret;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
406f7b422b1SDavid Howells int __init nfs_init_readpagecache(void)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
4091e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
4101da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
41120c2df83SPaul Mundt 					     NULL);
4121da177e4SLinus Torvalds 	if (nfs_rdata_cachep == NULL)
4131da177e4SLinus Torvalds 		return -ENOMEM;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	return 0;
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
418266bee88SDavid Brownell void nfs_destroy_readpagecache(void)
4191da177e4SLinus Torvalds {
4201a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_rdata_cachep);
4211da177e4SLinus Torvalds }
4224a0de55cSAnna Schumaker 
4234a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_read_ops = {
424a4cdda59SAnna Schumaker 	.rw_mode		= FMODE_READ,
4254a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_readhdr_alloc,
4264a0de55cSAnna Schumaker 	.rw_free_header		= nfs_readhdr_free,
4270eecb214SAnna Schumaker 	.rw_done		= nfs_readpage_done,
4280eecb214SAnna Schumaker 	.rw_result		= nfs_readpage_result,
4291ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_read,
4304a0de55cSAnna Schumaker };
431