xref: /openbmc/linux/fs/nfs/read.c (revision 1ed26f33008e954a8e91d26f97d4380dea8145db)
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 
311751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops;
32061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
334a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_read_ops;
341da177e4SLinus Torvalds 
35e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep;
361da177e4SLinus Torvalds 
374a0de55cSAnna Schumaker static struct nfs_rw_header *nfs_readhdr_alloc(void)
383feb2d49STrond Myklebust {
394a0de55cSAnna Schumaker 	return kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
404db6e0b7SFred Isaman }
414db6e0b7SFred Isaman 
424a0de55cSAnna Schumaker static void nfs_readhdr_free(struct nfs_rw_header *rhdr)
433feb2d49STrond Myklebust {
44cd841605SFred Isaman 	kmem_cache_free(nfs_rdata_cachep, rhdr);
453feb2d49STrond Myklebust }
463feb2d49STrond Myklebust 
471da177e4SLinus Torvalds static
481da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page)
491da177e4SLinus Torvalds {
50eebd2aa3SChristoph Lameter 	zero_user(page, 0, PAGE_CACHE_SIZE);
511da177e4SLinus Torvalds 	SetPageUptodate(page);
521da177e4SLinus Torvalds 	unlock_page(page);
531da177e4SLinus Torvalds 	return 0;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds 
561abb5088SBryan Schumaker void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
57fab5fc25SChristoph Hellwig 			      struct inode *inode, bool force_mds,
58061ae2edSFred Isaman 			      const struct nfs_pgio_completion_ops *compl_ops)
591751c363STrond Myklebust {
60fab5fc25SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
61fab5fc25SChristoph Hellwig 	const struct nfs_pageio_ops *pg_ops = &nfs_pageio_read_ops;
62fab5fc25SChristoph Hellwig 
63fab5fc25SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
64fab5fc25SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
65fab5fc25SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_read_ops;
66fab5fc25SChristoph Hellwig #endif
674a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
684a0de55cSAnna Schumaker 			server->rsize, 0);
691751c363STrond Myklebust }
70ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
711751c363STrond Myklebust 
72493292ddSTrond Myklebust void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
73493292ddSTrond Myklebust {
74493292ddSTrond Myklebust 	pgio->pg_ops = &nfs_pageio_read_ops;
75493292ddSTrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
76493292ddSTrond Myklebust }
771f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
78493292ddSTrond Myklebust 
79f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
801da177e4SLinus Torvalds 		       struct page *page)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	struct nfs_page	*new;
831da177e4SLinus Torvalds 	unsigned int len;
84c76069bdSFred Isaman 	struct nfs_pageio_descriptor pgio;
851da177e4SLinus Torvalds 
8649a70f27STrond Myklebust 	len = nfs_page_length(page);
871da177e4SLinus Torvalds 	if (len == 0)
881da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
891da177e4SLinus Torvalds 	new = nfs_create_request(ctx, inode, page, 0, len);
901da177e4SLinus Torvalds 	if (IS_ERR(new)) {
911da177e4SLinus Torvalds 		unlock_page(page);
921da177e4SLinus Torvalds 		return PTR_ERR(new);
931da177e4SLinus Torvalds 	}
941da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
95eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
961da177e4SLinus Torvalds 
97fab5fc25SChristoph Hellwig 	nfs_pageio_init_read(&pgio, inode, false,
98fab5fc25SChristoph Hellwig 			     &nfs_async_read_completion_ops);
99d8007d4dSTrond Myklebust 	nfs_pageio_add_request(&pgio, new);
1001751c363STrond Myklebust 	nfs_pageio_complete(&pgio);
1012701d086SAndy Adamson 	NFS_I(inode)->read_io += pgio.pg_bytes_written;
1021da177e4SLinus Torvalds 	return 0;
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req)
1061da177e4SLinus Torvalds {
1073d4ff43dSAl Viro 	struct inode *d_inode = req->wb_context->dentry->d_inode;
1087f8e05f6SDavid Howells 
1097f8e05f6SDavid Howells 	if (PageUptodate(req->wb_page))
1107f8e05f6SDavid Howells 		nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
1117f8e05f6SDavid Howells 
1121da177e4SLinus Torvalds 	unlock_page(req->wb_page);
1131da177e4SLinus Torvalds 
1141e8968c5SNiels de Vos 	dprintk("NFS: read done (%s/%Lu %d@%Ld)\n",
1153d4ff43dSAl Viro 			req->wb_context->dentry->d_inode->i_sb->s_id,
1161e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1171da177e4SLinus Torvalds 			req->wb_bytes,
1181da177e4SLinus Torvalds 			(long long)req_offset(req));
11910d2c46fSNick Wilson 	nfs_release_request(req);
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1224db6e0b7SFred Isaman /* Note io was page aligned */
123061ae2edSFred Isaman static void nfs_read_completion(struct nfs_pgio_header *hdr)
1244db6e0b7SFred Isaman {
1254db6e0b7SFred Isaman 	unsigned long bytes = 0;
1264db6e0b7SFred Isaman 
1274db6e0b7SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
1284db6e0b7SFred Isaman 		goto out;
1294db6e0b7SFred Isaman 	while (!list_empty(&hdr->pages)) {
1304db6e0b7SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
1314db6e0b7SFred Isaman 		struct page *page = req->wb_page;
1324db6e0b7SFred Isaman 
1334db6e0b7SFred Isaman 		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
1344db6e0b7SFred Isaman 			if (bytes > hdr->good_bytes)
1354db6e0b7SFred Isaman 				zero_user(page, 0, PAGE_SIZE);
1364db6e0b7SFred Isaman 			else if (hdr->good_bytes - bytes < PAGE_SIZE)
1374db6e0b7SFred Isaman 				zero_user_segment(page,
1384db6e0b7SFred Isaman 					hdr->good_bytes & ~PAGE_MASK,
1394db6e0b7SFred Isaman 					PAGE_SIZE);
1404db6e0b7SFred Isaman 		}
1414bd8b010STrond Myklebust 		bytes += req->wb_bytes;
1424bd8b010STrond Myklebust 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
1434bd8b010STrond Myklebust 			if (bytes <= hdr->good_bytes)
1444bd8b010STrond Myklebust 				SetPageUptodate(page);
1454bd8b010STrond Myklebust 		} else
1464db6e0b7SFred Isaman 			SetPageUptodate(page);
1474db6e0b7SFred Isaman 		nfs_list_remove_request(req);
1484db6e0b7SFred Isaman 		nfs_readpage_release(req);
1494db6e0b7SFred Isaman 	}
1504db6e0b7SFred Isaman out:
1514db6e0b7SFred Isaman 	hdr->release(hdr);
1524db6e0b7SFred Isaman }
1534db6e0b7SFred Isaman 
154*1ed26f33SAnna Schumaker static void nfs_initiate_read(struct nfs_pgio_data *data, struct rpc_message *msg,
155*1ed26f33SAnna Schumaker 			      struct rpc_task_setup *task_setup_data, int how)
15664419a9bSAndy Adamson {
157cd841605SFred Isaman 	struct inode *inode = data->header->inode;
15864419a9bSAndy Adamson 	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
15964419a9bSAndy Adamson 
160*1ed26f33SAnna Schumaker 	task_setup_data->flags |= swap_flags;
161*1ed26f33SAnna Schumaker 	NFS_PROTO(inode)->read_setup(data, msg);
16264419a9bSAndy Adamson }
16364419a9bSAndy Adamson 
1649c7e1b3dSAnna Schumaker static int nfs_do_read(struct nfs_pgio_data *data,
165493292ddSTrond Myklebust 		const struct rpc_call_ops *call_ops)
1666e4efd56STrond Myklebust {
167cd841605SFred Isaman 	struct inode *inode = data->header->inode;
1686e4efd56STrond Myklebust 
169*1ed26f33SAnna Schumaker 	return nfs_initiate_pgio(NFS_CLIENT(inode), data, call_ops, 0, 0);
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
172275acaafSTrond Myklebust static int
173275acaafSTrond Myklebust nfs_do_multiple_reads(struct list_head *head,
174493292ddSTrond Myklebust 		const struct rpc_call_ops *call_ops)
175275acaafSTrond Myklebust {
1769c7e1b3dSAnna Schumaker 	struct nfs_pgio_data *data;
177275acaafSTrond Myklebust 	int ret = 0;
178275acaafSTrond Myklebust 
179275acaafSTrond Myklebust 	while (!list_empty(head)) {
180275acaafSTrond Myklebust 		int ret2;
181275acaafSTrond Myklebust 
1829c7e1b3dSAnna Schumaker 		data = list_first_entry(head, struct nfs_pgio_data, list);
183275acaafSTrond Myklebust 		list_del_init(&data->list);
184275acaafSTrond Myklebust 
185493292ddSTrond Myklebust 		ret2 = nfs_do_read(data, call_ops);
186275acaafSTrond Myklebust 		if (ret == 0)
187275acaafSTrond Myklebust 			ret = ret2;
188275acaafSTrond Myklebust 	}
189275acaafSTrond Myklebust 	return ret;
190275acaafSTrond Myklebust }
191275acaafSTrond Myklebust 
192061ae2edSFred Isaman static void
1931da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	struct nfs_page	*req;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	while (!list_empty(head)) {
1981da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
1991da177e4SLinus Torvalds 		nfs_list_remove_request(req);
2001da177e4SLinus Torvalds 		nfs_readpage_release(req);
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
204061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
205061ae2edSFred Isaman 	.error_cleanup = nfs_async_read_error,
206061ae2edSFred Isaman 	.completion = nfs_read_completion,
207061ae2edSFred Isaman };
208061ae2edSFred Isaman 
209493292ddSTrond Myklebust static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2101751c363STrond Myklebust {
211c0752cdfSAnna Schumaker 	struct nfs_rw_header *rhdr;
2124db6e0b7SFred Isaman 	struct nfs_pgio_header *hdr;
213275acaafSTrond Myklebust 	int ret;
214275acaafSTrond Myklebust 
2154a0de55cSAnna Schumaker 	rhdr = nfs_rw_header_alloc(desc->pg_rw_ops);
2164db6e0b7SFred Isaman 	if (!rhdr) {
217061ae2edSFred Isaman 		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
2184db6e0b7SFred Isaman 		return -ENOMEM;
2194db6e0b7SFred Isaman 	}
2204db6e0b7SFred Isaman 	hdr = &rhdr->header;
2214a0de55cSAnna Schumaker 	nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
2224db6e0b7SFred Isaman 	atomic_inc(&hdr->refcnt);
223ef2c488cSAnna Schumaker 	ret = nfs_generic_pgio(desc, hdr);
224275acaafSTrond Myklebust 	if (ret == 0)
2254db6e0b7SFred Isaman 		ret = nfs_do_multiple_reads(&hdr->rpc_list,
2264db6e0b7SFred Isaman 					    desc->pg_rpc_callops);
2274db6e0b7SFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
228061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
229275acaafSTrond Myklebust 	return ret;
2301751c363STrond Myklebust }
2311751c363STrond Myklebust 
2321751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops = {
2331751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
2341751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_readpages,
2351751c363STrond Myklebust };
2361751c363STrond Myklebust 
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  */
2410eecb214SAnna Schumaker static int nfs_readpage_done(struct rpc_task *task, struct nfs_pgio_data *data,
2420eecb214SAnna Schumaker 			     struct inode *inode)
2430b671301STrond Myklebust {
2440eecb214SAnna Schumaker 	int status = NFS_PROTO(inode)->read_done(task, data);
2450b671301STrond Myklebust 	if (status != 0)
2460b671301STrond Myklebust 		return status;
2470b671301STrond Myklebust 
248cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
2490b671301STrond Myklebust 
2500b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
251cd841605SFred Isaman 		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
252cd841605SFred Isaman 		nfs_mark_for_revalidate(inode);
2530b671301STrond Myklebust 	}
2540b671301STrond Myklebust 	return 0;
2550b671301STrond Myklebust }
2560b671301STrond Myklebust 
2579c7e1b3dSAnna Schumaker static void nfs_readpage_retry(struct rpc_task *task, struct nfs_pgio_data *data)
2580b671301STrond Myklebust {
2593c6b899cSAnna Schumaker 	struct nfs_pgio_args *argp = &data->args;
2609137bdf3SAnna Schumaker 	struct nfs_pgio_res  *resp = &data->res;
2610b671301STrond Myklebust 
2620b671301STrond Myklebust 	/* This is a short read! */
263cd841605SFred Isaman 	nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
2640b671301STrond Myklebust 	/* Has the server at least made some progress? */
2654db6e0b7SFred Isaman 	if (resp->count == 0) {
2664db6e0b7SFred Isaman 		nfs_set_pgio_error(data->header, -EIO, argp->offset);
267d61e612aSTrond Myklebust 		return;
2684db6e0b7SFred Isaman 	}
2690b671301STrond Myklebust 	/* Yes, so retry the read at the end of the data */
270cbdabc7fSAndy Adamson 	data->mds_offset += resp->count;
2710b671301STrond Myklebust 	argp->offset += resp->count;
2720b671301STrond Myklebust 	argp->pgbase += resp->count;
2730b671301STrond Myklebust 	argp->count -= resp->count;
274d00c5d43STrond Myklebust 	rpc_restart_call_prepare(task);
2750b671301STrond Myklebust }
2760b671301STrond Myklebust 
2770eecb214SAnna Schumaker static void nfs_readpage_result(struct rpc_task *task, struct nfs_pgio_data *data)
2781da177e4SLinus Torvalds {
2794db6e0b7SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
2801da177e4SLinus Torvalds 
2810eecb214SAnna Schumaker 	if (data->res.eof) {
2824db6e0b7SFred Isaman 		loff_t bound;
283fdd1e74cSTrond Myklebust 
2844db6e0b7SFred Isaman 		bound = data->args.offset + data->res.count;
2854db6e0b7SFred Isaman 		spin_lock(&hdr->lock);
2864db6e0b7SFred Isaman 		if (bound < hdr->io_start + hdr->good_bytes) {
2874db6e0b7SFred Isaman 			set_bit(NFS_IOHDR_EOF, &hdr->flags);
2884db6e0b7SFred Isaman 			clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
2894db6e0b7SFred Isaman 			hdr->good_bytes = bound - hdr->io_start;
2904db6e0b7SFred Isaman 		}
2914db6e0b7SFred Isaman 		spin_unlock(&hdr->lock);
2924db6e0b7SFred Isaman 	} else if (data->res.count != data->args.count)
293fdd1e74cSTrond Myklebust 		nfs_readpage_retry(task, data);
2940b671301STrond Myklebust }
295fdd1e74cSTrond Myklebust 
2961da177e4SLinus Torvalds /*
2971da177e4SLinus Torvalds  * Read a page over NFS.
2981da177e4SLinus Torvalds  * We read the page synchronously in the following case:
2991da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
3001da177e4SLinus Torvalds  *	previous async read operation failed.
3011da177e4SLinus Torvalds  */
3021da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
305d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
3061da177e4SLinus Torvalds 	int		error;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
309d56b4ddfSMel Gorman 		page, PAGE_CACHE_SIZE, page_file_index(page));
31091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
31191d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_READPAGES, 1);
31291d5b470SChuck Lever 
3131da177e4SLinus Torvalds 	/*
3141da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
3151da177e4SLinus Torvalds 	 *
3161da177e4SLinus Torvalds 	 * NOTE! Because we own the page lock, there cannot
3171da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
3181da177e4SLinus Torvalds 	 * for this page (other pages can be written to).
3191da177e4SLinus Torvalds 	 */
3201da177e4SLinus Torvalds 	error = nfs_wb_page(inode, page);
3211da177e4SLinus Torvalds 	if (error)
322de05a0ccSTrond Myklebust 		goto out_unlock;
323de05a0ccSTrond Myklebust 	if (PageUptodate(page))
324de05a0ccSTrond Myklebust 		goto out_unlock;
3251da177e4SLinus Torvalds 
3265f004cf2STrond Myklebust 	error = -ESTALE;
3275f004cf2STrond Myklebust 	if (NFS_STALE(inode))
328de05a0ccSTrond Myklebust 		goto out_unlock;
3295f004cf2STrond Myklebust 
3301da177e4SLinus Torvalds 	if (file == NULL) {
331cf1308ffSTrond Myklebust 		error = -EBADF;
332d530838bSTrond Myklebust 		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
3331da177e4SLinus Torvalds 		if (ctx == NULL)
334de05a0ccSTrond Myklebust 			goto out_unlock;
3351da177e4SLinus Torvalds 	} else
336cd3758e3STrond Myklebust 		ctx = get_nfs_open_context(nfs_file_open_context(file));
3371da177e4SLinus Torvalds 
3389a9fc1c0SDavid Howells 	if (!IS_SYNC(inode)) {
3399a9fc1c0SDavid Howells 		error = nfs_readpage_from_fscache(ctx, inode, page);
3409a9fc1c0SDavid Howells 		if (error == 0)
3419a9fc1c0SDavid Howells 			goto out;
3429a9fc1c0SDavid Howells 	}
3439a9fc1c0SDavid Howells 
3448e0969f0STrond Myklebust 	error = nfs_readpage_async(ctx, inode, page);
3458e0969f0STrond Myklebust 
3469a9fc1c0SDavid Howells out:
3471da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
3481da177e4SLinus Torvalds 	return error;
349de05a0ccSTrond Myklebust out_unlock:
3501da177e4SLinus Torvalds 	unlock_page(page);
3511da177e4SLinus Torvalds 	return error;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds struct nfs_readdesc {
3558b09bee3STrond Myklebust 	struct nfs_pageio_descriptor *pgio;
3561da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
3571da177e4SLinus Torvalds };
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds static int
3601da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
363d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
3641da177e4SLinus Torvalds 	struct nfs_page *new;
3651da177e4SLinus Torvalds 	unsigned int len;
366de05a0ccSTrond Myklebust 	int error;
3671da177e4SLinus Torvalds 
36849a70f27STrond Myklebust 	len = nfs_page_length(page);
3691da177e4SLinus Torvalds 	if (len == 0)
3701da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
371de05a0ccSTrond Myklebust 
3721da177e4SLinus Torvalds 	new = nfs_create_request(desc->ctx, inode, page, 0, len);
373de05a0ccSTrond Myklebust 	if (IS_ERR(new))
374de05a0ccSTrond Myklebust 		goto out_error;
375de05a0ccSTrond Myklebust 
3761da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
377eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
378f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(desc->pgio, new)) {
379f8512ad0SFred Isaman 		error = desc->pgio->pg_error;
380f8512ad0SFred Isaman 		goto out_unlock;
381f8512ad0SFred Isaman 	}
3821da177e4SLinus Torvalds 	return 0;
383de05a0ccSTrond Myklebust out_error:
384de05a0ccSTrond Myklebust 	error = PTR_ERR(new);
385de05a0ccSTrond Myklebust out_unlock:
386de05a0ccSTrond Myklebust 	unlock_page(page);
387de05a0ccSTrond Myklebust 	return error;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping,
3911da177e4SLinus Torvalds 		struct list_head *pages, unsigned nr_pages)
3921da177e4SLinus Torvalds {
3938b09bee3STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3941da177e4SLinus Torvalds 	struct nfs_readdesc desc = {
3958b09bee3STrond Myklebust 		.pgio = &pgio,
3961da177e4SLinus Torvalds 	};
3971da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
3988b09bee3STrond Myklebust 	unsigned long npages;
3995f004cf2STrond Myklebust 	int ret = -ESTALE;
4001da177e4SLinus Torvalds 
4011e8968c5SNiels de Vos 	dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
4021da177e4SLinus Torvalds 			inode->i_sb->s_id,
4031e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(inode),
4041da177e4SLinus Torvalds 			nr_pages);
40591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
4061da177e4SLinus Torvalds 
4075f004cf2STrond Myklebust 	if (NFS_STALE(inode))
4085f004cf2STrond Myklebust 		goto out;
4095f004cf2STrond Myklebust 
4101da177e4SLinus Torvalds 	if (filp == NULL) {
411d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
4121da177e4SLinus Torvalds 		if (desc.ctx == NULL)
4131da177e4SLinus Torvalds 			return -EBADF;
4141da177e4SLinus Torvalds 	} else
415cd3758e3STrond Myklebust 		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
4169a9fc1c0SDavid Howells 
4179a9fc1c0SDavid Howells 	/* attempt to read as many of the pages as possible from the cache
4189a9fc1c0SDavid Howells 	 * - this returns -ENOBUFS immediately if the cookie is negative
4199a9fc1c0SDavid Howells 	 */
4209a9fc1c0SDavid Howells 	ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
4219a9fc1c0SDavid Howells 					 pages, &nr_pages);
4229a9fc1c0SDavid Howells 	if (ret == 0)
4239a9fc1c0SDavid Howells 		goto read_complete; /* all pages were read */
4249a9fc1c0SDavid Howells 
425fab5fc25SChristoph Hellwig 	nfs_pageio_init_read(&pgio, inode, false,
426fab5fc25SChristoph Hellwig 			     &nfs_async_read_completion_ops);
4278b09bee3STrond Myklebust 
4281da177e4SLinus Torvalds 	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
4298b09bee3STrond Myklebust 
4308b09bee3STrond Myklebust 	nfs_pageio_complete(&pgio);
4312701d086SAndy Adamson 	NFS_I(inode)->read_io += pgio.pg_bytes_written;
4328b09bee3STrond Myklebust 	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4338b09bee3STrond Myklebust 	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
4349a9fc1c0SDavid Howells read_complete:
4351da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
4365f004cf2STrond Myklebust out:
4371da177e4SLinus Torvalds 	return 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",
443c0752cdfSAnna Schumaker 					     sizeof(struct nfs_rw_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 = {
458a4cdda59SAnna Schumaker 	.rw_mode		= FMODE_READ,
4594a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_readhdr_alloc,
4604a0de55cSAnna Schumaker 	.rw_free_header		= nfs_readhdr_free,
4610eecb214SAnna Schumaker 	.rw_done		= nfs_readpage_done,
4620eecb214SAnna Schumaker 	.rw_result		= nfs_readpage_result,
463*1ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_read,
4644a0de55cSAnna Schumaker };
465