xref: /openbmc/linux/fs/nfs/read.c (revision 1abb50886afe8a126705c93dab2b50c1252a9c19)
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"
2791d5b470SChuck Lever 
281da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
291da177e4SLinus Torvalds 
301751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops;
314db6e0b7SFred Isaman static const struct rpc_call_ops nfs_read_common_ops;
32061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
331da177e4SLinus Torvalds 
34e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep;
351da177e4SLinus Torvalds 
361385b811STrond Myklebust struct nfs_read_header *nfs_readhdr_alloc(void)
373feb2d49STrond Myklebust {
384db6e0b7SFred Isaman 	struct nfs_read_header *rhdr;
393feb2d49STrond Myklebust 
404db6e0b7SFred Isaman 	rhdr = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
414db6e0b7SFred Isaman 	if (rhdr) {
424db6e0b7SFred Isaman 		struct nfs_pgio_header *hdr = &rhdr->header;
43cd841605SFred Isaman 
44cd841605SFred Isaman 		INIT_LIST_HEAD(&hdr->pages);
454db6e0b7SFred Isaman 		INIT_LIST_HEAD(&hdr->rpc_list);
464db6e0b7SFred Isaman 		spin_lock_init(&hdr->lock);
474db6e0b7SFred Isaman 		atomic_set(&hdr->refcnt, 0);
484db6e0b7SFred Isaman 	}
494db6e0b7SFred Isaman 	return rhdr;
504db6e0b7SFred Isaman }
514db6e0b7SFred Isaman 
52584aa810SFred Isaman static struct nfs_read_data *nfs_readdata_alloc(struct nfs_pgio_header *hdr,
534db6e0b7SFred Isaman 						unsigned int pagecount)
544db6e0b7SFred Isaman {
554db6e0b7SFred Isaman 	struct nfs_read_data *data, *prealloc;
564db6e0b7SFred Isaman 
574db6e0b7SFred Isaman 	prealloc = &container_of(hdr, struct nfs_read_header, header)->rpc_data;
584db6e0b7SFred Isaman 	if (prealloc->header == NULL)
594db6e0b7SFred Isaman 		data = prealloc;
604db6e0b7SFred Isaman 	else
614db6e0b7SFred Isaman 		data = kzalloc(sizeof(*data), GFP_KERNEL);
624db6e0b7SFred Isaman 	if (!data)
634db6e0b7SFred Isaman 		goto out;
644db6e0b7SFred Isaman 
654db6e0b7SFred Isaman 	if (nfs_pgarray_set(&data->pages, pagecount)) {
66cd841605SFred Isaman 		data->header = hdr;
674db6e0b7SFred Isaman 		atomic_inc(&hdr->refcnt);
684db6e0b7SFred Isaman 	} else {
694db6e0b7SFred Isaman 		if (data != prealloc)
704db6e0b7SFred Isaman 			kfree(data);
714db6e0b7SFred Isaman 		data = NULL;
723feb2d49STrond Myklebust 	}
734db6e0b7SFred Isaman out:
744db6e0b7SFred Isaman 	return data;
753feb2d49STrond Myklebust }
763feb2d49STrond Myklebust 
77cd841605SFred Isaman void nfs_readhdr_free(struct nfs_pgio_header *hdr)
783feb2d49STrond Myklebust {
79cd841605SFred Isaman 	struct nfs_read_header *rhdr = container_of(hdr, struct nfs_read_header, header);
80cd841605SFred Isaman 
81cd841605SFred Isaman 	kmem_cache_free(nfs_rdata_cachep, rhdr);
823feb2d49STrond Myklebust }
833feb2d49STrond Myklebust 
84493292ddSTrond Myklebust void nfs_readdata_release(struct nfs_read_data *rdata)
851da177e4SLinus Torvalds {
864db6e0b7SFred Isaman 	struct nfs_pgio_header *hdr = rdata->header;
874db6e0b7SFred Isaman 	struct nfs_read_header *read_header = container_of(hdr, struct nfs_read_header, header);
884db6e0b7SFred Isaman 
89383ba719STrond Myklebust 	put_nfs_open_context(rdata->args.context);
9030dd374fSFred Isaman 	if (rdata->pages.pagevec != rdata->pages.page_array)
9130dd374fSFred Isaman 		kfree(rdata->pages.pagevec);
924db6e0b7SFred Isaman 	if (rdata != &read_header->rpc_data)
934db6e0b7SFred Isaman 		kfree(rdata);
944db6e0b7SFred Isaman 	else
954db6e0b7SFred Isaman 		rdata->header = NULL;
964db6e0b7SFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
97061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static
1011da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page)
1021da177e4SLinus Torvalds {
103eebd2aa3SChristoph Lameter 	zero_user(page, 0, PAGE_CACHE_SIZE);
1041da177e4SLinus Torvalds 	SetPageUptodate(page);
1051da177e4SLinus Torvalds 	unlock_page(page);
1061da177e4SLinus Torvalds 	return 0;
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
109*1abb5088SBryan Schumaker void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
110061ae2edSFred Isaman 			      struct inode *inode,
111061ae2edSFred Isaman 			      const struct nfs_pgio_completion_ops *compl_ops)
1121751c363STrond Myklebust {
113061ae2edSFred Isaman 	nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops, compl_ops,
1141751c363STrond Myklebust 			NFS_SERVER(inode)->rsize, 0);
1151751c363STrond Myklebust }
1161751c363STrond Myklebust 
117493292ddSTrond Myklebust void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
118493292ddSTrond Myklebust {
119493292ddSTrond Myklebust 	pgio->pg_ops = &nfs_pageio_read_ops;
120493292ddSTrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
121493292ddSTrond Myklebust }
1221f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
123493292ddSTrond Myklebust 
124f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
1251da177e4SLinus Torvalds 		       struct page *page)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	struct nfs_page	*new;
1281da177e4SLinus Torvalds 	unsigned int len;
129c76069bdSFred Isaman 	struct nfs_pageio_descriptor pgio;
1301da177e4SLinus Torvalds 
13149a70f27STrond Myklebust 	len = nfs_page_length(page);
1321da177e4SLinus Torvalds 	if (len == 0)
1331da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
1341da177e4SLinus Torvalds 	new = nfs_create_request(ctx, inode, page, 0, len);
1351da177e4SLinus Torvalds 	if (IS_ERR(new)) {
1361da177e4SLinus Torvalds 		unlock_page(page);
1371da177e4SLinus Torvalds 		return PTR_ERR(new);
1381da177e4SLinus Torvalds 	}
1391da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
140eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
1411da177e4SLinus Torvalds 
142*1abb5088SBryan Schumaker 	NFS_PROTO(inode)->read_pageio_init(&pgio, inode, &nfs_async_read_completion_ops);
143d8007d4dSTrond Myklebust 	nfs_pageio_add_request(&pgio, new);
1441751c363STrond Myklebust 	nfs_pageio_complete(&pgio);
1452701d086SAndy Adamson 	NFS_I(inode)->read_io += pgio.pg_bytes_written;
1461da177e4SLinus Torvalds 	return 0;
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req)
1501da177e4SLinus Torvalds {
1513d4ff43dSAl Viro 	struct inode *d_inode = req->wb_context->dentry->d_inode;
1527f8e05f6SDavid Howells 
1537f8e05f6SDavid Howells 	if (PageUptodate(req->wb_page))
1547f8e05f6SDavid Howells 		nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
1557f8e05f6SDavid Howells 
1561da177e4SLinus Torvalds 	unlock_page(req->wb_page);
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
1593d4ff43dSAl Viro 			req->wb_context->dentry->d_inode->i_sb->s_id,
1603d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1611da177e4SLinus Torvalds 			req->wb_bytes,
1621da177e4SLinus Torvalds 			(long long)req_offset(req));
16310d2c46fSNick Wilson 	nfs_release_request(req);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1664db6e0b7SFred Isaman /* Note io was page aligned */
167061ae2edSFred Isaman static void nfs_read_completion(struct nfs_pgio_header *hdr)
1684db6e0b7SFred Isaman {
1694db6e0b7SFred Isaman 	unsigned long bytes = 0;
1704db6e0b7SFred Isaman 
1714db6e0b7SFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
1724db6e0b7SFred Isaman 		goto out;
1734db6e0b7SFred Isaman 	while (!list_empty(&hdr->pages)) {
1744db6e0b7SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
1754db6e0b7SFred Isaman 		struct page *page = req->wb_page;
1764db6e0b7SFred Isaman 
1774db6e0b7SFred Isaman 		if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
1784db6e0b7SFred Isaman 			if (bytes > hdr->good_bytes)
1794db6e0b7SFred Isaman 				zero_user(page, 0, PAGE_SIZE);
1804db6e0b7SFred Isaman 			else if (hdr->good_bytes - bytes < PAGE_SIZE)
1814db6e0b7SFred Isaman 				zero_user_segment(page,
1824db6e0b7SFred Isaman 					hdr->good_bytes & ~PAGE_MASK,
1834db6e0b7SFred Isaman 					PAGE_SIZE);
1844db6e0b7SFred Isaman 		}
1854bd8b010STrond Myklebust 		bytes += req->wb_bytes;
1864bd8b010STrond Myklebust 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
1874bd8b010STrond Myklebust 			if (bytes <= hdr->good_bytes)
1884bd8b010STrond Myklebust 				SetPageUptodate(page);
1894bd8b010STrond Myklebust 		} else
1904db6e0b7SFred Isaman 			SetPageUptodate(page);
1914db6e0b7SFred Isaman 		nfs_list_remove_request(req);
1924db6e0b7SFred Isaman 		nfs_readpage_release(req);
1934db6e0b7SFred Isaman 	}
1944db6e0b7SFred Isaman out:
1954db6e0b7SFred Isaman 	hdr->release(hdr);
1964db6e0b7SFred Isaman }
1974db6e0b7SFred Isaman 
198c5996c4eSFred Isaman int nfs_initiate_read(struct rpc_clnt *clnt,
199c5996c4eSFred Isaman 		      struct nfs_read_data *data,
2009f0ec176SAndy Adamson 		      const struct rpc_call_ops *call_ops, int flags)
20164419a9bSAndy Adamson {
202cd841605SFred Isaman 	struct inode *inode = data->header->inode;
20364419a9bSAndy Adamson 	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
20464419a9bSAndy Adamson 	struct rpc_task *task;
20564419a9bSAndy Adamson 	struct rpc_message msg = {
20664419a9bSAndy Adamson 		.rpc_argp = &data->args,
20764419a9bSAndy Adamson 		.rpc_resp = &data->res,
208cd841605SFred Isaman 		.rpc_cred = data->header->cred,
20964419a9bSAndy Adamson 	};
21064419a9bSAndy Adamson 	struct rpc_task_setup task_setup_data = {
21164419a9bSAndy Adamson 		.task = &data->task,
21264419a9bSAndy Adamson 		.rpc_client = clnt,
21364419a9bSAndy Adamson 		.rpc_message = &msg,
21464419a9bSAndy Adamson 		.callback_ops = call_ops,
21564419a9bSAndy Adamson 		.callback_data = data,
21664419a9bSAndy Adamson 		.workqueue = nfsiod_workqueue,
2179f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | swap_flags | flags,
21864419a9bSAndy Adamson 	};
21964419a9bSAndy Adamson 
22064419a9bSAndy Adamson 	/* Set up the initial task struct. */
22164419a9bSAndy Adamson 	NFS_PROTO(inode)->read_setup(data, &msg);
22264419a9bSAndy Adamson 
22364419a9bSAndy Adamson 	dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
22464419a9bSAndy Adamson 			"offset %llu)\n",
22564419a9bSAndy Adamson 			data->task.tk_pid,
22664419a9bSAndy Adamson 			inode->i_sb->s_id,
22764419a9bSAndy Adamson 			(long long)NFS_FILEID(inode),
22864419a9bSAndy Adamson 			data->args.count,
22964419a9bSAndy Adamson 			(unsigned long long)data->args.offset);
23064419a9bSAndy Adamson 
23164419a9bSAndy Adamson 	task = rpc_run_task(&task_setup_data);
23264419a9bSAndy Adamson 	if (IS_ERR(task))
23364419a9bSAndy Adamson 		return PTR_ERR(task);
23464419a9bSAndy Adamson 	rpc_put_task(task);
23564419a9bSAndy Adamson 	return 0;
23664419a9bSAndy Adamson }
237dc70d7b3SAndy Adamson EXPORT_SYMBOL_GPL(nfs_initiate_read);
23864419a9bSAndy Adamson 
2391da177e4SLinus Torvalds /*
2401da177e4SLinus Torvalds  * Set up the NFS read request struct
2411da177e4SLinus Torvalds  */
2424db6e0b7SFred Isaman static void nfs_read_rpcsetup(struct nfs_read_data *data,
2436e4efd56STrond Myklebust 		unsigned int count, unsigned int offset)
2441da177e4SLinus Torvalds {
2454db6e0b7SFred Isaman 	struct nfs_page *req = data->header->req;
2461da177e4SLinus Torvalds 
2474db6e0b7SFred Isaman 	data->args.fh     = NFS_FH(data->header->inode);
2481da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
2491da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
25030dd374fSFred Isaman 	data->args.pages  = data->pages.pagevec;
2511da177e4SLinus Torvalds 	data->args.count  = count;
252383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
253f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
2561da177e4SLinus Torvalds 	data->res.count   = count;
2571da177e4SLinus Torvalds 	data->res.eof     = 0;
2580e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
2596e4efd56STrond Myklebust }
2601da177e4SLinus Torvalds 
2616e4efd56STrond Myklebust static int nfs_do_read(struct nfs_read_data *data,
262493292ddSTrond Myklebust 		const struct rpc_call_ops *call_ops)
2636e4efd56STrond Myklebust {
264cd841605SFred Isaman 	struct inode *inode = data->header->inode;
2656e4efd56STrond Myklebust 
2669f0ec176SAndy Adamson 	return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops, 0);
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
269275acaafSTrond Myklebust static int
270275acaafSTrond Myklebust nfs_do_multiple_reads(struct list_head *head,
271493292ddSTrond Myklebust 		const struct rpc_call_ops *call_ops)
272275acaafSTrond Myklebust {
273275acaafSTrond Myklebust 	struct nfs_read_data *data;
274275acaafSTrond Myklebust 	int ret = 0;
275275acaafSTrond Myklebust 
276275acaafSTrond Myklebust 	while (!list_empty(head)) {
277275acaafSTrond Myklebust 		int ret2;
278275acaafSTrond Myklebust 
2794db6e0b7SFred Isaman 		data = list_first_entry(head, struct nfs_read_data, list);
280275acaafSTrond Myklebust 		list_del_init(&data->list);
281275acaafSTrond Myklebust 
282493292ddSTrond Myklebust 		ret2 = nfs_do_read(data, call_ops);
283275acaafSTrond Myklebust 		if (ret == 0)
284275acaafSTrond Myklebust 			ret = ret2;
285275acaafSTrond Myklebust 	}
286275acaafSTrond Myklebust 	return ret;
287275acaafSTrond Myklebust }
288275acaafSTrond Myklebust 
289061ae2edSFred Isaman static void
2901da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	struct nfs_page	*req;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	while (!list_empty(head)) {
2951da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
2961da177e4SLinus Torvalds 		nfs_list_remove_request(req);
2971da177e4SLinus Torvalds 		nfs_readpage_release(req);
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
301061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
302061ae2edSFred Isaman 	.error_cleanup = nfs_async_read_error,
303061ae2edSFred Isaman 	.completion = nfs_read_completion,
304061ae2edSFred Isaman };
305061ae2edSFred Isaman 
30625b11dcdSTrond Myklebust static void nfs_pagein_error(struct nfs_pageio_descriptor *desc,
30725b11dcdSTrond Myklebust 		struct nfs_pgio_header *hdr)
30825b11dcdSTrond Myklebust {
30925b11dcdSTrond Myklebust 	set_bit(NFS_IOHDR_REDO, &hdr->flags);
31025b11dcdSTrond Myklebust 	while (!list_empty(&hdr->rpc_list)) {
31125b11dcdSTrond Myklebust 		struct nfs_read_data *data = list_first_entry(&hdr->rpc_list,
31225b11dcdSTrond Myklebust 				struct nfs_read_data, list);
31325b11dcdSTrond Myklebust 		list_del(&data->list);
31425b11dcdSTrond Myklebust 		nfs_readdata_release(data);
31525b11dcdSTrond Myklebust 	}
31625b11dcdSTrond Myklebust 	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
31725b11dcdSTrond Myklebust }
31825b11dcdSTrond Myklebust 
3191da177e4SLinus Torvalds /*
3201da177e4SLinus Torvalds  * Generate multiple requests to fill a single page.
3211da177e4SLinus Torvalds  *
3221da177e4SLinus Torvalds  * We optimize to reduce the number of read operations on the wire.  If we
3231da177e4SLinus Torvalds  * detect that we're reading a page, or an area of a page, that is past the
3241da177e4SLinus Torvalds  * end of file, we do not generate NFS read operations but just clear the
3251da177e4SLinus Torvalds  * parts of the page that would have come back zero from the server anyway.
3261da177e4SLinus Torvalds  *
3271da177e4SLinus Torvalds  * We rely on the cached value of i_size to make this determination; another
3281da177e4SLinus Torvalds  * client can fill pages on the server past our cached end-of-file, but we
3291da177e4SLinus Torvalds  * won't see the new data until our attribute cache is updated.  This is more
3301da177e4SLinus Torvalds  * or less conventional NFS client behavior.
3311da177e4SLinus Torvalds  */
3324db6e0b7SFred Isaman static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc,
3334db6e0b7SFred Isaman 			    struct nfs_pgio_header *hdr)
3341da177e4SLinus Torvalds {
3354db6e0b7SFred Isaman 	struct nfs_page *req = hdr->req;
3361da177e4SLinus Torvalds 	struct page *page = req->wb_page;
3371da177e4SLinus Torvalds 	struct nfs_read_data *data;
338d097971dSTrond Myklebust 	size_t rsize = desc->pg_bsize, nbytes;
339e9f7bee1STrond Myklebust 	unsigned int offset;
3401da177e4SLinus Torvalds 
341275acaafSTrond Myklebust 	offset = 0;
342c76069bdSFred Isaman 	nbytes = desc->pg_count;
343e9f7bee1STrond Myklebust 	do {
344e9f7bee1STrond Myklebust 		size_t len = min(nbytes,rsize);
345e9f7bee1STrond Myklebust 
3464db6e0b7SFred Isaman 		data = nfs_readdata_alloc(hdr, 1);
34725b11dcdSTrond Myklebust 		if (!data) {
34825b11dcdSTrond Myklebust 			nfs_pagein_error(desc, hdr);
34925b11dcdSTrond Myklebust 			return -ENOMEM;
35025b11dcdSTrond Myklebust 		}
35130dd374fSFred Isaman 		data->pages.pagevec[0] = page;
3524db6e0b7SFred Isaman 		nfs_read_rpcsetup(data, len, offset);
3534db6e0b7SFred Isaman 		list_add(&data->list, &hdr->rpc_list);
354e9f7bee1STrond Myklebust 		nbytes -= len;
355275acaafSTrond Myklebust 		offset += len;
356e9f7bee1STrond Myklebust 	} while (nbytes != 0);
35725b11dcdSTrond Myklebust 
35825b11dcdSTrond Myklebust 	nfs_list_remove_request(req);
35925b11dcdSTrond Myklebust 	nfs_list_add_request(req, &hdr->pages);
3604db6e0b7SFred Isaman 	desc->pg_rpc_callops = &nfs_read_common_ops;
3619146ab50STrond Myklebust 	return 0;
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
3644db6e0b7SFred Isaman static int nfs_pagein_one(struct nfs_pageio_descriptor *desc,
3654db6e0b7SFred Isaman 			  struct nfs_pgio_header *hdr)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	struct nfs_page		*req;
3681da177e4SLinus Torvalds 	struct page		**pages;
3691da177e4SLinus Torvalds 	struct nfs_read_data    *data;
370c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
3711da177e4SLinus Torvalds 
3724db6e0b7SFred Isaman 	data = nfs_readdata_alloc(hdr, nfs_page_array_len(desc->pg_base,
373c76069bdSFred Isaman 							  desc->pg_count));
3744db6e0b7SFred Isaman 	if (!data) {
37525b11dcdSTrond Myklebust 		nfs_pagein_error(desc, hdr);
3769146ab50STrond Myklebust 		return -ENOMEM;
377bae724efSFred Isaman 	}
3781da177e4SLinus Torvalds 
37930dd374fSFred Isaman 	pages = data->pages.pagevec;
3801da177e4SLinus Torvalds 	while (!list_empty(head)) {
3811da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
3821da177e4SLinus Torvalds 		nfs_list_remove_request(req);
3834db6e0b7SFred Isaman 		nfs_list_add_request(req, &hdr->pages);
3841da177e4SLinus Torvalds 		*pages++ = req->wb_page;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
3874db6e0b7SFred Isaman 	nfs_read_rpcsetup(data, desc->pg_count, 0);
3884db6e0b7SFred Isaman 	list_add(&data->list, &hdr->rpc_list);
3894db6e0b7SFred Isaman 	desc->pg_rpc_callops = &nfs_read_common_ops;
3909146ab50STrond Myklebust 	return 0;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3934db6e0b7SFred Isaman int nfs_generic_pagein(struct nfs_pageio_descriptor *desc,
3944db6e0b7SFred Isaman 		       struct nfs_pgio_header *hdr)
395493292ddSTrond Myklebust {
396493292ddSTrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
3974db6e0b7SFred Isaman 		return nfs_pagein_multi(desc, hdr);
3984db6e0b7SFred Isaman 	return nfs_pagein_one(desc, hdr);
399493292ddSTrond Myklebust }
400493292ddSTrond Myklebust 
401493292ddSTrond Myklebust static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
4021751c363STrond Myklebust {
4034db6e0b7SFred Isaman 	struct nfs_read_header *rhdr;
4044db6e0b7SFred Isaman 	struct nfs_pgio_header *hdr;
405275acaafSTrond Myklebust 	int ret;
406275acaafSTrond Myklebust 
4074db6e0b7SFred Isaman 	rhdr = nfs_readhdr_alloc();
4084db6e0b7SFred Isaman 	if (!rhdr) {
409061ae2edSFred Isaman 		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
4104db6e0b7SFred Isaman 		return -ENOMEM;
4114db6e0b7SFred Isaman 	}
4124db6e0b7SFred Isaman 	hdr = &rhdr->header;
4134db6e0b7SFred Isaman 	nfs_pgheader_init(desc, hdr, nfs_readhdr_free);
4144db6e0b7SFred Isaman 	atomic_inc(&hdr->refcnt);
4154db6e0b7SFred Isaman 	ret = nfs_generic_pagein(desc, hdr);
416275acaafSTrond Myklebust 	if (ret == 0)
4174db6e0b7SFred Isaman 		ret = nfs_do_multiple_reads(&hdr->rpc_list,
4184db6e0b7SFred Isaman 					    desc->pg_rpc_callops);
4194db6e0b7SFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
420061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
421275acaafSTrond Myklebust 	return ret;
4221751c363STrond Myklebust }
4231751c363STrond Myklebust 
4241751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops = {
4251751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
4261751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_readpages,
4271751c363STrond Myklebust };
4281751c363STrond Myklebust 
4291da177e4SLinus Torvalds /*
4300b671301STrond Myklebust  * This is the callback from RPC telling us whether a reply was
4310b671301STrond Myklebust  * received or some error occurred (timeout or socket shutdown).
4320b671301STrond Myklebust  */
4330b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
4340b671301STrond Myklebust {
435cd841605SFred Isaman 	struct inode *inode = data->header->inode;
4360b671301STrond Myklebust 	int status;
4370b671301STrond Myklebust 
4383110ff80SHarvey Harrison 	dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
4390b671301STrond Myklebust 			task->tk_status);
4400b671301STrond Myklebust 
441cd841605SFred Isaman 	status = NFS_PROTO(inode)->read_done(task, data);
4420b671301STrond Myklebust 	if (status != 0)
4430b671301STrond Myklebust 		return status;
4440b671301STrond Myklebust 
445cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
4460b671301STrond Myklebust 
4470b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
448cd841605SFred Isaman 		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
449cd841605SFred Isaman 		nfs_mark_for_revalidate(inode);
4500b671301STrond Myklebust 	}
4510b671301STrond Myklebust 	return 0;
4520b671301STrond Myklebust }
4530b671301STrond Myklebust 
454fdd1e74cSTrond Myklebust static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
4550b671301STrond Myklebust {
4560b671301STrond Myklebust 	struct nfs_readargs *argp = &data->args;
4570b671301STrond Myklebust 	struct nfs_readres *resp = &data->res;
4580b671301STrond Myklebust 
4590b671301STrond Myklebust 	/* This is a short read! */
460cd841605SFred Isaman 	nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
4610b671301STrond Myklebust 	/* Has the server at least made some progress? */
4624db6e0b7SFred Isaman 	if (resp->count == 0) {
4634db6e0b7SFred Isaman 		nfs_set_pgio_error(data->header, -EIO, argp->offset);
464d61e612aSTrond Myklebust 		return;
4654db6e0b7SFred Isaman 	}
4660b671301STrond Myklebust 	/* Yes, so retry the read at the end of the data */
467cbdabc7fSAndy Adamson 	data->mds_offset += resp->count;
4680b671301STrond Myklebust 	argp->offset += resp->count;
4690b671301STrond Myklebust 	argp->pgbase += resp->count;
4700b671301STrond Myklebust 	argp->count -= resp->count;
471d00c5d43STrond Myklebust 	rpc_restart_call_prepare(task);
4720b671301STrond Myklebust }
4730b671301STrond Myklebust 
4744db6e0b7SFred Isaman static void nfs_readpage_result_common(struct rpc_task *task, void *calldata)
4751da177e4SLinus Torvalds {
476ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
4774db6e0b7SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
4781da177e4SLinus Torvalds 
4794db6e0b7SFred Isaman 	/* Note the only returns of nfs_readpage_result are 0 and -EAGAIN */
480ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
481ec06c096STrond Myklebust 		return;
482fdd1e74cSTrond Myklebust 	if (task->tk_status < 0)
4834db6e0b7SFred Isaman 		nfs_set_pgio_error(hdr, task->tk_status, data->args.offset);
4844db6e0b7SFred Isaman 	else if (data->res.eof) {
4854db6e0b7SFred Isaman 		loff_t bound;
486fdd1e74cSTrond Myklebust 
4874db6e0b7SFred Isaman 		bound = data->args.offset + data->res.count;
4884db6e0b7SFred Isaman 		spin_lock(&hdr->lock);
4894db6e0b7SFred Isaman 		if (bound < hdr->io_start + hdr->good_bytes) {
4904db6e0b7SFred Isaman 			set_bit(NFS_IOHDR_EOF, &hdr->flags);
4914db6e0b7SFred Isaman 			clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
4924db6e0b7SFred Isaman 			hdr->good_bytes = bound - hdr->io_start;
4934db6e0b7SFred Isaman 		}
4944db6e0b7SFred Isaman 		spin_unlock(&hdr->lock);
4954db6e0b7SFred Isaman 	} else if (data->res.count != data->args.count)
496fdd1e74cSTrond Myklebust 		nfs_readpage_retry(task, data);
4970b671301STrond Myklebust }
498fdd1e74cSTrond Myklebust 
4994db6e0b7SFred Isaman static void nfs_readpage_release_common(void *calldata)
500fdd1e74cSTrond Myklebust {
5014db6e0b7SFred Isaman 	nfs_readdata_release(calldata);
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
504f11c88afSAndy Adamson void nfs_read_prepare(struct rpc_task *task, void *calldata)
505f11c88afSAndy Adamson {
506f11c88afSAndy Adamson 	struct nfs_read_data *data = calldata;
507cd841605SFred Isaman 	NFS_PROTO(data->header->inode)->read_rpc_prepare(task, data);
508f11c88afSAndy Adamson }
509f11c88afSAndy Adamson 
5104db6e0b7SFred Isaman static const struct rpc_call_ops nfs_read_common_ops = {
511f11c88afSAndy Adamson 	.rpc_call_prepare = nfs_read_prepare,
5124db6e0b7SFred Isaman 	.rpc_call_done = nfs_readpage_result_common,
5134db6e0b7SFred Isaman 	.rpc_release = nfs_readpage_release_common,
514ec06c096STrond Myklebust };
515ec06c096STrond Myklebust 
5161da177e4SLinus Torvalds /*
5171da177e4SLinus Torvalds  * Read a page over NFS.
5181da177e4SLinus Torvalds  * We read the page synchronously in the following case:
5191da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
5201da177e4SLinus Torvalds  *	previous async read operation failed.
5211da177e4SLinus Torvalds  */
5221da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page)
5231da177e4SLinus Torvalds {
5241da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
5251da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
5261da177e4SLinus Torvalds 	int		error;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
5291da177e4SLinus Torvalds 		page, PAGE_CACHE_SIZE, page->index);
53091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
53191d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_READPAGES, 1);
53291d5b470SChuck Lever 
5331da177e4SLinus Torvalds 	/*
5341da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
5351da177e4SLinus Torvalds 	 *
5361da177e4SLinus Torvalds 	 * NOTE! Because we own the page lock, there cannot
5371da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
5381da177e4SLinus Torvalds 	 * for this page (other pages can be written to).
5391da177e4SLinus Torvalds 	 */
5401da177e4SLinus Torvalds 	error = nfs_wb_page(inode, page);
5411da177e4SLinus Torvalds 	if (error)
542de05a0ccSTrond Myklebust 		goto out_unlock;
543de05a0ccSTrond Myklebust 	if (PageUptodate(page))
544de05a0ccSTrond Myklebust 		goto out_unlock;
5451da177e4SLinus Torvalds 
5465f004cf2STrond Myklebust 	error = -ESTALE;
5475f004cf2STrond Myklebust 	if (NFS_STALE(inode))
548de05a0ccSTrond Myklebust 		goto out_unlock;
5495f004cf2STrond Myklebust 
5501da177e4SLinus Torvalds 	if (file == NULL) {
551cf1308ffSTrond Myklebust 		error = -EBADF;
552d530838bSTrond Myklebust 		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
5531da177e4SLinus Torvalds 		if (ctx == NULL)
554de05a0ccSTrond Myklebust 			goto out_unlock;
5551da177e4SLinus Torvalds 	} else
556cd3758e3STrond Myklebust 		ctx = get_nfs_open_context(nfs_file_open_context(file));
5571da177e4SLinus Torvalds 
5589a9fc1c0SDavid Howells 	if (!IS_SYNC(inode)) {
5599a9fc1c0SDavid Howells 		error = nfs_readpage_from_fscache(ctx, inode, page);
5609a9fc1c0SDavid Howells 		if (error == 0)
5619a9fc1c0SDavid Howells 			goto out;
5629a9fc1c0SDavid Howells 	}
5639a9fc1c0SDavid Howells 
5648e0969f0STrond Myklebust 	error = nfs_readpage_async(ctx, inode, page);
5658e0969f0STrond Myklebust 
5669a9fc1c0SDavid Howells out:
5671da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
5681da177e4SLinus Torvalds 	return error;
569de05a0ccSTrond Myklebust out_unlock:
5701da177e4SLinus Torvalds 	unlock_page(page);
5711da177e4SLinus Torvalds 	return error;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds struct nfs_readdesc {
5758b09bee3STrond Myklebust 	struct nfs_pageio_descriptor *pgio;
5761da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
5771da177e4SLinus Torvalds };
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds static int
5801da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
5831da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
5841da177e4SLinus Torvalds 	struct nfs_page *new;
5851da177e4SLinus Torvalds 	unsigned int len;
586de05a0ccSTrond Myklebust 	int error;
5871da177e4SLinus Torvalds 
58849a70f27STrond Myklebust 	len = nfs_page_length(page);
5891da177e4SLinus Torvalds 	if (len == 0)
5901da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
591de05a0ccSTrond Myklebust 
5921da177e4SLinus Torvalds 	new = nfs_create_request(desc->ctx, inode, page, 0, len);
593de05a0ccSTrond Myklebust 	if (IS_ERR(new))
594de05a0ccSTrond Myklebust 		goto out_error;
595de05a0ccSTrond Myklebust 
5961da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
597eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
598f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(desc->pgio, new)) {
599f8512ad0SFred Isaman 		error = desc->pgio->pg_error;
600f8512ad0SFred Isaman 		goto out_unlock;
601f8512ad0SFred Isaman 	}
6021da177e4SLinus Torvalds 	return 0;
603de05a0ccSTrond Myklebust out_error:
604de05a0ccSTrond Myklebust 	error = PTR_ERR(new);
605de05a0ccSTrond Myklebust out_unlock:
606de05a0ccSTrond Myklebust 	unlock_page(page);
607de05a0ccSTrond Myklebust 	return error;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping,
6111da177e4SLinus Torvalds 		struct list_head *pages, unsigned nr_pages)
6121da177e4SLinus Torvalds {
6138b09bee3STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6141da177e4SLinus Torvalds 	struct nfs_readdesc desc = {
6158b09bee3STrond Myklebust 		.pgio = &pgio,
6161da177e4SLinus Torvalds 	};
6171da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
6188b09bee3STrond Myklebust 	unsigned long npages;
6195f004cf2STrond Myklebust 	int ret = -ESTALE;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
6221da177e4SLinus Torvalds 			inode->i_sb->s_id,
6231da177e4SLinus Torvalds 			(long long)NFS_FILEID(inode),
6241da177e4SLinus Torvalds 			nr_pages);
62591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
6261da177e4SLinus Torvalds 
6275f004cf2STrond Myklebust 	if (NFS_STALE(inode))
6285f004cf2STrond Myklebust 		goto out;
6295f004cf2STrond Myklebust 
6301da177e4SLinus Torvalds 	if (filp == NULL) {
631d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
6321da177e4SLinus Torvalds 		if (desc.ctx == NULL)
6331da177e4SLinus Torvalds 			return -EBADF;
6341da177e4SLinus Torvalds 	} else
635cd3758e3STrond Myklebust 		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
6369a9fc1c0SDavid Howells 
6379a9fc1c0SDavid Howells 	/* attempt to read as many of the pages as possible from the cache
6389a9fc1c0SDavid Howells 	 * - this returns -ENOBUFS immediately if the cookie is negative
6399a9fc1c0SDavid Howells 	 */
6409a9fc1c0SDavid Howells 	ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
6419a9fc1c0SDavid Howells 					 pages, &nr_pages);
6429a9fc1c0SDavid Howells 	if (ret == 0)
6439a9fc1c0SDavid Howells 		goto read_complete; /* all pages were read */
6449a9fc1c0SDavid Howells 
645*1abb5088SBryan Schumaker 	NFS_PROTO(inode)->read_pageio_init(&pgio, inode, &nfs_async_read_completion_ops);
6468b09bee3STrond Myklebust 
6471da177e4SLinus Torvalds 	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
6488b09bee3STrond Myklebust 
6498b09bee3STrond Myklebust 	nfs_pageio_complete(&pgio);
6502701d086SAndy Adamson 	NFS_I(inode)->read_io += pgio.pg_bytes_written;
6518b09bee3STrond Myklebust 	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
6528b09bee3STrond Myklebust 	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
6539a9fc1c0SDavid Howells read_complete:
6541da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
6555f004cf2STrond Myklebust out:
6561da177e4SLinus Torvalds 	return ret;
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
659f7b422b1SDavid Howells int __init nfs_init_readpagecache(void)
6601da177e4SLinus Torvalds {
6611da177e4SLinus Torvalds 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
662cd841605SFred Isaman 					     sizeof(struct nfs_read_header),
6631da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
66420c2df83SPaul Mundt 					     NULL);
6651da177e4SLinus Torvalds 	if (nfs_rdata_cachep == NULL)
6661da177e4SLinus Torvalds 		return -ENOMEM;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	return 0;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
671266bee88SDavid Brownell void nfs_destroy_readpagecache(void)
6721da177e4SLinus Torvalds {
6731a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_rdata_cachep);
6741da177e4SLinus Torvalds }
675