xref: /openbmc/linux/fs/nfs/read.c (revision cd841605)
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 
23bae724efSFred Isaman #include "pnfs.h"
241da177e4SLinus Torvalds 
25f11c88afSAndy Adamson #include "nfs4_fs.h"
2649a70f27STrond Myklebust #include "internal.h"
2791d5b470SChuck Lever #include "iostat.h"
289a9fc1c0SDavid Howells #include "fscache.h"
2991d5b470SChuck Lever 
301da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
311da177e4SLinus Torvalds 
321751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops;
33ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops;
34ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops;
351da177e4SLinus Torvalds 
36e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep;
371da177e4SLinus Torvalds 
38cd841605SFred Isaman struct nfs_read_header *nfs_readhdr_alloc(unsigned int pagecount)
393feb2d49STrond Myklebust {
40cd841605SFred Isaman 	struct nfs_read_header *p;
413feb2d49STrond Myklebust 
42b6ee8cd2STrond Myklebust 	p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
433feb2d49STrond Myklebust 	if (p) {
44cd841605SFred Isaman 		struct nfs_pgio_header *hdr = &p->header;
45cd841605SFred Isaman 		struct nfs_read_data *data = &p->rpc_data;
46cd841605SFred Isaman 
47cd841605SFred Isaman 		INIT_LIST_HEAD(&hdr->pages);
48cd841605SFred Isaman 		INIT_LIST_HEAD(&data->list);
49cd841605SFred Isaman 		data->npages = pagecount;
50cd841605SFred Isaman 		data->header = hdr;
51cd841605SFred Isaman 		if (pagecount <= ARRAY_SIZE(data->page_array))
52cd841605SFred Isaman 			data->pagevec = data->page_array;
533feb2d49STrond Myklebust 		else {
54cd841605SFred Isaman 			data->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
55cd841605SFred Isaman 			if (!data->pagevec) {
56b6ee8cd2STrond Myklebust 				kmem_cache_free(nfs_rdata_cachep, p);
573feb2d49STrond Myklebust 				p = NULL;
583feb2d49STrond Myklebust 			}
593feb2d49STrond Myklebust 		}
603feb2d49STrond Myklebust 	}
613feb2d49STrond Myklebust 	return p;
623feb2d49STrond Myklebust }
633feb2d49STrond Myklebust 
64cd841605SFred Isaman void nfs_readhdr_free(struct nfs_pgio_header *hdr)
653feb2d49STrond Myklebust {
66cd841605SFred Isaman 	struct nfs_read_header *rhdr = container_of(hdr, struct nfs_read_header, header);
67cd841605SFred Isaman 
68cd841605SFred Isaman 	kmem_cache_free(nfs_rdata_cachep, rhdr);
693feb2d49STrond Myklebust }
703feb2d49STrond Myklebust 
71493292ddSTrond Myklebust void nfs_readdata_release(struct nfs_read_data *rdata)
721da177e4SLinus Torvalds {
73383ba719STrond Myklebust 	put_nfs_open_context(rdata->args.context);
74cd841605SFred Isaman 	if (rdata->pagevec != rdata->page_array)
75cd841605SFred Isaman 		kfree(rdata->pagevec);
76cd841605SFred Isaman 	nfs_readhdr_free(rdata->header);
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static
801da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page)
811da177e4SLinus Torvalds {
82eebd2aa3SChristoph Lameter 	zero_user(page, 0, PAGE_CACHE_SIZE);
831da177e4SLinus Torvalds 	SetPageUptodate(page);
841da177e4SLinus Torvalds 	unlock_page(page);
851da177e4SLinus Torvalds 	return 0;
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
881de3fc12STrond Myklebust static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
891de3fc12STrond Myklebust {
901de3fc12STrond Myklebust 	unsigned int remainder = data->args.count - data->res.count;
911de3fc12STrond Myklebust 	unsigned int base = data->args.pgbase + data->res.count;
921de3fc12STrond Myklebust 	unsigned int pglen;
931de3fc12STrond Myklebust 	struct page **pages;
941de3fc12STrond Myklebust 
951de3fc12STrond Myklebust 	if (data->res.eof == 0 || remainder == 0)
961de3fc12STrond Myklebust 		return;
971de3fc12STrond Myklebust 	/*
981de3fc12STrond Myklebust 	 * Note: "remainder" can never be negative, since we check for
991de3fc12STrond Myklebust 	 * 	this in the XDR code.
1001de3fc12STrond Myklebust 	 */
1011de3fc12STrond Myklebust 	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
1021de3fc12STrond Myklebust 	base &= ~PAGE_CACHE_MASK;
1031de3fc12STrond Myklebust 	pglen = PAGE_CACHE_SIZE - base;
10479558f36STrond Myklebust 	for (;;) {
10579558f36STrond Myklebust 		if (remainder <= pglen) {
106eebd2aa3SChristoph Lameter 			zero_user(*pages, base, remainder);
10779558f36STrond Myklebust 			break;
10879558f36STrond Myklebust 		}
109eebd2aa3SChristoph Lameter 		zero_user(*pages, base, pglen);
11079558f36STrond Myklebust 		pages++;
11179558f36STrond Myklebust 		remainder -= pglen;
11279558f36STrond Myklebust 		pglen = PAGE_CACHE_SIZE;
11379558f36STrond Myklebust 		base = 0;
11479558f36STrond Myklebust 	}
1151de3fc12STrond Myklebust }
1161de3fc12STrond Myklebust 
11762e4a769STrond Myklebust void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor *pgio,
1181751c363STrond Myklebust 		struct inode *inode)
1191751c363STrond Myklebust {
1201751c363STrond Myklebust 	nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops,
1211751c363STrond Myklebust 			NFS_SERVER(inode)->rsize, 0);
1221751c363STrond Myklebust }
1231751c363STrond Myklebust 
124493292ddSTrond Myklebust void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
125493292ddSTrond Myklebust {
126493292ddSTrond Myklebust 	pgio->pg_ops = &nfs_pageio_read_ops;
127493292ddSTrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
128493292ddSTrond Myklebust }
1291f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
130493292ddSTrond Myklebust 
1311751c363STrond Myklebust static void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
1321751c363STrond Myklebust 		struct inode *inode)
1331751c363STrond Myklebust {
1341751c363STrond Myklebust 	if (!pnfs_pageio_init_read(pgio, inode))
1351751c363STrond Myklebust 		nfs_pageio_init_read_mds(pgio, inode);
1361751c363STrond Myklebust }
1371751c363STrond Myklebust 
138f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
1391da177e4SLinus Torvalds 		       struct page *page)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	struct nfs_page	*new;
1421da177e4SLinus Torvalds 	unsigned int len;
143c76069bdSFred Isaman 	struct nfs_pageio_descriptor pgio;
1441da177e4SLinus Torvalds 
14549a70f27STrond Myklebust 	len = nfs_page_length(page);
1461da177e4SLinus Torvalds 	if (len == 0)
1471da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
1481da177e4SLinus Torvalds 	new = nfs_create_request(ctx, inode, page, 0, len);
1491da177e4SLinus Torvalds 	if (IS_ERR(new)) {
1501da177e4SLinus Torvalds 		unlock_page(page);
1511da177e4SLinus Torvalds 		return PTR_ERR(new);
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
154eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
1551da177e4SLinus Torvalds 
1561751c363STrond Myklebust 	nfs_pageio_init_read(&pgio, inode);
157d8007d4dSTrond Myklebust 	nfs_pageio_add_request(&pgio, new);
1581751c363STrond Myklebust 	nfs_pageio_complete(&pgio);
1591da177e4SLinus Torvalds 	return 0;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req)
1631da177e4SLinus Torvalds {
1643d4ff43dSAl Viro 	struct inode *d_inode = req->wb_context->dentry->d_inode;
1657f8e05f6SDavid Howells 
1667f8e05f6SDavid Howells 	if (PageUptodate(req->wb_page))
1677f8e05f6SDavid Howells 		nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
1687f8e05f6SDavid Howells 
1691da177e4SLinus Torvalds 	unlock_page(req->wb_page);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
1723d4ff43dSAl Viro 			req->wb_context->dentry->d_inode->i_sb->s_id,
1733d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1741da177e4SLinus Torvalds 			req->wb_bytes,
1751da177e4SLinus Torvalds 			(long long)req_offset(req));
17610d2c46fSNick Wilson 	nfs_release_request(req);
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
179c5996c4eSFred Isaman int nfs_initiate_read(struct rpc_clnt *clnt,
180c5996c4eSFred Isaman 		      struct nfs_read_data *data,
18164419a9bSAndy Adamson 		      const struct rpc_call_ops *call_ops)
18264419a9bSAndy Adamson {
183cd841605SFred Isaman 	struct inode *inode = data->header->inode;
18464419a9bSAndy Adamson 	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
18564419a9bSAndy Adamson 	struct rpc_task *task;
18664419a9bSAndy Adamson 	struct rpc_message msg = {
18764419a9bSAndy Adamson 		.rpc_argp = &data->args,
18864419a9bSAndy Adamson 		.rpc_resp = &data->res,
189cd841605SFred Isaman 		.rpc_cred = data->header->cred,
19064419a9bSAndy Adamson 	};
19164419a9bSAndy Adamson 	struct rpc_task_setup task_setup_data = {
19264419a9bSAndy Adamson 		.task = &data->task,
19364419a9bSAndy Adamson 		.rpc_client = clnt,
19464419a9bSAndy Adamson 		.rpc_message = &msg,
19564419a9bSAndy Adamson 		.callback_ops = call_ops,
19664419a9bSAndy Adamson 		.callback_data = data,
19764419a9bSAndy Adamson 		.workqueue = nfsiod_workqueue,
19864419a9bSAndy Adamson 		.flags = RPC_TASK_ASYNC | swap_flags,
19964419a9bSAndy Adamson 	};
20064419a9bSAndy Adamson 
20164419a9bSAndy Adamson 	/* Set up the initial task struct. */
20264419a9bSAndy Adamson 	NFS_PROTO(inode)->read_setup(data, &msg);
20364419a9bSAndy Adamson 
20464419a9bSAndy Adamson 	dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
20564419a9bSAndy Adamson 			"offset %llu)\n",
20664419a9bSAndy Adamson 			data->task.tk_pid,
20764419a9bSAndy Adamson 			inode->i_sb->s_id,
20864419a9bSAndy Adamson 			(long long)NFS_FILEID(inode),
20964419a9bSAndy Adamson 			data->args.count,
21064419a9bSAndy Adamson 			(unsigned long long)data->args.offset);
21164419a9bSAndy Adamson 
21264419a9bSAndy Adamson 	task = rpc_run_task(&task_setup_data);
21364419a9bSAndy Adamson 	if (IS_ERR(task))
21464419a9bSAndy Adamson 		return PTR_ERR(task);
21564419a9bSAndy Adamson 	rpc_put_task(task);
21664419a9bSAndy Adamson 	return 0;
21764419a9bSAndy Adamson }
218dc70d7b3SAndy Adamson EXPORT_SYMBOL_GPL(nfs_initiate_read);
21964419a9bSAndy Adamson 
2201da177e4SLinus Torvalds /*
2211da177e4SLinus Torvalds  * Set up the NFS read request struct
2221da177e4SLinus Torvalds  */
2236e4efd56STrond Myklebust static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
2246e4efd56STrond Myklebust 		unsigned int count, unsigned int offset)
2251da177e4SLinus Torvalds {
226cd841605SFred Isaman 	struct inode *inode = data->header->inode;
2271da177e4SLinus Torvalds 
228cd841605SFred Isaman 	data->header->req	  = req;
229cd841605SFred Isaman 	data->header->inode	  = inode;
230cd841605SFred Isaman 	data->header->cred	  = req->wb_context->cred;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
2331da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
2341da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
2351da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
2361da177e4SLinus Torvalds 	data->args.count  = count;
237383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
238f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
2411da177e4SLinus Torvalds 	data->res.count   = count;
2421da177e4SLinus Torvalds 	data->res.eof     = 0;
2430e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
2446e4efd56STrond Myklebust }
2451da177e4SLinus Torvalds 
2466e4efd56STrond Myklebust static int nfs_do_read(struct nfs_read_data *data,
247493292ddSTrond Myklebust 		const struct rpc_call_ops *call_ops)
2486e4efd56STrond Myklebust {
249cd841605SFred Isaman 	struct inode *inode = data->header->inode;
2506e4efd56STrond Myklebust 
251c5996c4eSFred Isaman 	return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops);
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
254275acaafSTrond Myklebust static int
255275acaafSTrond Myklebust nfs_do_multiple_reads(struct list_head *head,
256493292ddSTrond Myklebust 		const struct rpc_call_ops *call_ops)
257275acaafSTrond Myklebust {
258275acaafSTrond Myklebust 	struct nfs_read_data *data;
259275acaafSTrond Myklebust 	int ret = 0;
260275acaafSTrond Myklebust 
261275acaafSTrond Myklebust 	while (!list_empty(head)) {
262275acaafSTrond Myklebust 		int ret2;
263275acaafSTrond Myklebust 
264275acaafSTrond Myklebust 		data = list_entry(head->next, struct nfs_read_data, list);
265275acaafSTrond Myklebust 		list_del_init(&data->list);
266275acaafSTrond Myklebust 
267493292ddSTrond Myklebust 		ret2 = nfs_do_read(data, call_ops);
268275acaafSTrond Myklebust 		if (ret == 0)
269275acaafSTrond Myklebust 			ret = ret2;
270275acaafSTrond Myklebust 	}
271275acaafSTrond Myklebust 	return ret;
272275acaafSTrond Myklebust }
273275acaafSTrond Myklebust 
2741da177e4SLinus Torvalds static void
2751da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head)
2761da177e4SLinus Torvalds {
2771da177e4SLinus Torvalds 	struct nfs_page	*req;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	while (!list_empty(head)) {
2801da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
2811da177e4SLinus Torvalds 		nfs_list_remove_request(req);
2821da177e4SLinus Torvalds 		nfs_readpage_release(req);
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds /*
2871da177e4SLinus Torvalds  * Generate multiple requests to fill a single page.
2881da177e4SLinus Torvalds  *
2891da177e4SLinus Torvalds  * We optimize to reduce the number of read operations on the wire.  If we
2901da177e4SLinus Torvalds  * detect that we're reading a page, or an area of a page, that is past the
2911da177e4SLinus Torvalds  * end of file, we do not generate NFS read operations but just clear the
2921da177e4SLinus Torvalds  * parts of the page that would have come back zero from the server anyway.
2931da177e4SLinus Torvalds  *
2941da177e4SLinus Torvalds  * We rely on the cached value of i_size to make this determination; another
2951da177e4SLinus Torvalds  * client can fill pages on the server past our cached end-of-file, but we
2961da177e4SLinus Torvalds  * won't see the new data until our attribute cache is updated.  This is more
2971da177e4SLinus Torvalds  * or less conventional NFS client behavior.
2981da177e4SLinus Torvalds  */
299275acaafSTrond Myklebust static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc, struct list_head *res)
3001da177e4SLinus Torvalds {
301c76069bdSFred Isaman 	struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
3021da177e4SLinus Torvalds 	struct page *page = req->wb_page;
303cd841605SFred Isaman 	struct nfs_read_header *rhdr;
3041da177e4SLinus Torvalds 	struct nfs_read_data *data;
305d097971dSTrond Myklebust 	size_t rsize = desc->pg_bsize, nbytes;
306e9f7bee1STrond Myklebust 	unsigned int offset;
3071da177e4SLinus Torvalds 	int requests = 0;
308dbae4c73STrond Myklebust 	int ret = 0;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	nfs_list_remove_request(req);
3111da177e4SLinus Torvalds 
312275acaafSTrond Myklebust 	offset = 0;
313c76069bdSFred Isaman 	nbytes = desc->pg_count;
314e9f7bee1STrond Myklebust 	do {
315e9f7bee1STrond Myklebust 		size_t len = min(nbytes,rsize);
316e9f7bee1STrond Myklebust 
317cd841605SFred Isaman 		rhdr = nfs_readhdr_alloc(1);
318cd841605SFred Isaman 		if (!rhdr)
3191da177e4SLinus Torvalds 			goto out_bad;
320cd841605SFred Isaman 		data = &rhdr->rpc_data;
321275acaafSTrond Myklebust 		data->pagevec[0] = page;
322275acaafSTrond Myklebust 		nfs_read_rpcsetup(req, data, len, offset);
323275acaafSTrond Myklebust 		list_add(&data->list, res);
3241da177e4SLinus Torvalds 		requests++;
325e9f7bee1STrond Myklebust 		nbytes -= len;
326275acaafSTrond Myklebust 		offset += len;
327e9f7bee1STrond Myklebust 	} while(nbytes != 0);
3281da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
32950828d7eSTrond Myklebust 	desc->pg_rpc_callops = &nfs_read_partial_ops;
330dbae4c73STrond Myklebust 	return ret;
3311da177e4SLinus Torvalds out_bad:
332275acaafSTrond Myklebust 	while (!list_empty(res)) {
333275acaafSTrond Myklebust 		data = list_entry(res->next, struct nfs_read_data, list);
3346e4efd56STrond Myklebust 		list_del(&data->list);
33573fb7bc7SFred Isaman 		nfs_readdata_release(data);
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 	nfs_readpage_release(req);
3381da177e4SLinus Torvalds 	return -ENOMEM;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
341275acaafSTrond Myklebust static int nfs_pagein_one(struct nfs_pageio_descriptor *desc, struct list_head *res)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	struct nfs_page		*req;
3441da177e4SLinus Torvalds 	struct page		**pages;
345cd841605SFred Isaman 	struct nfs_read_header	*rhdr;
3461da177e4SLinus Torvalds 	struct nfs_read_data	*data;
347c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
3483b609184SPeng Tao 	int ret = 0;
3491da177e4SLinus Torvalds 
350cd841605SFred Isaman 	rhdr = nfs_readhdr_alloc(nfs_page_array_len(desc->pg_base,
351c76069bdSFred Isaman 						    desc->pg_count));
352cd841605SFred Isaman 	if (!rhdr) {
353bae724efSFred Isaman 		nfs_async_read_error(head);
3543b609184SPeng Tao 		ret = -ENOMEM;
355bae724efSFred Isaman 		goto out;
356bae724efSFred Isaman 	}
3571da177e4SLinus Torvalds 
358cd841605SFred Isaman 	data = &rhdr->rpc_data;
3591da177e4SLinus Torvalds 	pages = data->pagevec;
3601da177e4SLinus Torvalds 	while (!list_empty(head)) {
3611da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
3621da177e4SLinus Torvalds 		nfs_list_remove_request(req);
363cd841605SFred Isaman 		nfs_list_add_request(req, &rhdr->header.pages);
3641da177e4SLinus Torvalds 		*pages++ = req->wb_page;
3651da177e4SLinus Torvalds 	}
366cd841605SFred Isaman 	req = nfs_list_entry(rhdr->header.pages.next);
3671da177e4SLinus Torvalds 
3686e4efd56STrond Myklebust 	nfs_read_rpcsetup(req, data, desc->pg_count, 0);
369275acaafSTrond Myklebust 	list_add(&data->list, res);
37050828d7eSTrond Myklebust 	desc->pg_rpc_callops = &nfs_read_full_ops;
371bae724efSFred Isaman out:
372dbae4c73STrond Myklebust 	return ret;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
375493292ddSTrond Myklebust int nfs_generic_pagein(struct nfs_pageio_descriptor *desc, struct list_head *head)
376493292ddSTrond Myklebust {
377493292ddSTrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
378493292ddSTrond Myklebust 		return nfs_pagein_multi(desc, head);
379493292ddSTrond Myklebust 	return nfs_pagein_one(desc, head);
380493292ddSTrond Myklebust }
381493292ddSTrond Myklebust 
382493292ddSTrond Myklebust static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
3831751c363STrond Myklebust {
384275acaafSTrond Myklebust 	LIST_HEAD(head);
385275acaafSTrond Myklebust 	int ret;
386275acaafSTrond Myklebust 
387493292ddSTrond Myklebust 	ret = nfs_generic_pagein(desc, &head);
388275acaafSTrond Myklebust 	if (ret == 0)
389493292ddSTrond Myklebust 		ret = nfs_do_multiple_reads(&head, desc->pg_rpc_callops);
390275acaafSTrond Myklebust 	return ret;
3911751c363STrond Myklebust }
3921751c363STrond Myklebust 
3931751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops = {
3941751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
3951751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_readpages,
3961751c363STrond Myklebust };
3971751c363STrond Myklebust 
3981da177e4SLinus Torvalds /*
3990b671301STrond Myklebust  * This is the callback from RPC telling us whether a reply was
4000b671301STrond Myklebust  * received or some error occurred (timeout or socket shutdown).
4010b671301STrond Myklebust  */
4020b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
4030b671301STrond Myklebust {
404cd841605SFred Isaman 	struct inode *inode = data->header->inode;
4050b671301STrond Myklebust 	int status;
4060b671301STrond Myklebust 
4073110ff80SHarvey Harrison 	dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
4080b671301STrond Myklebust 			task->tk_status);
4090b671301STrond Myklebust 
410cd841605SFred Isaman 	status = NFS_PROTO(inode)->read_done(task, data);
4110b671301STrond Myklebust 	if (status != 0)
4120b671301STrond Myklebust 		return status;
4130b671301STrond Myklebust 
414cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
4150b671301STrond Myklebust 
4160b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
417cd841605SFred Isaman 		set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
418cd841605SFred Isaman 		nfs_mark_for_revalidate(inode);
4190b671301STrond Myklebust 	}
4200b671301STrond Myklebust 	return 0;
4210b671301STrond Myklebust }
4220b671301STrond Myklebust 
423fdd1e74cSTrond Myklebust static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
4240b671301STrond Myklebust {
4250b671301STrond Myklebust 	struct nfs_readargs *argp = &data->args;
4260b671301STrond Myklebust 	struct nfs_readres *resp = &data->res;
4270b671301STrond Myklebust 
4280b671301STrond Myklebust 	if (resp->eof || resp->count == argp->count)
429d61e612aSTrond Myklebust 		return;
4300b671301STrond Myklebust 
4310b671301STrond Myklebust 	/* This is a short read! */
432cd841605SFred Isaman 	nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
4330b671301STrond Myklebust 	/* Has the server at least made some progress? */
4340b671301STrond Myklebust 	if (resp->count == 0)
435d61e612aSTrond Myklebust 		return;
4360b671301STrond Myklebust 
4370b671301STrond Myklebust 	/* Yes, so retry the read at the end of the data */
438cbdabc7fSAndy Adamson 	data->mds_offset += resp->count;
4390b671301STrond Myklebust 	argp->offset += resp->count;
4400b671301STrond Myklebust 	argp->pgbase += resp->count;
4410b671301STrond Myklebust 	argp->count -= resp->count;
442d00c5d43STrond Myklebust 	rpc_restart_call_prepare(task);
4430b671301STrond Myklebust }
4440b671301STrond Myklebust 
4450b671301STrond Myklebust /*
4461da177e4SLinus Torvalds  * Handle a read reply that fills part of a page.
4471da177e4SLinus Torvalds  */
448ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
4491da177e4SLinus Torvalds {
450ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
4511da177e4SLinus Torvalds 
452ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
453ec06c096STrond Myklebust 		return;
454fdd1e74cSTrond Myklebust 	if (task->tk_status < 0)
4550b671301STrond Myklebust 		return;
456fdd1e74cSTrond Myklebust 
457fdd1e74cSTrond Myklebust 	nfs_readpage_truncate_uninitialised_page(data);
458fdd1e74cSTrond Myklebust 	nfs_readpage_retry(task, data);
4590b671301STrond Myklebust }
460fdd1e74cSTrond Myklebust 
461fdd1e74cSTrond Myklebust static void nfs_readpage_release_partial(void *calldata)
462fdd1e74cSTrond Myklebust {
463fdd1e74cSTrond Myklebust 	struct nfs_read_data *data = calldata;
464cd841605SFred Isaman 	struct nfs_page *req = data->header->req;
465fdd1e74cSTrond Myklebust 	struct page *page = req->wb_page;
466fdd1e74cSTrond Myklebust 	int status = data->task.tk_status;
467fdd1e74cSTrond Myklebust 
468fdd1e74cSTrond Myklebust 	if (status < 0)
469fba73005STrond Myklebust 		set_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags);
470fdd1e74cSTrond Myklebust 
4711da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete)) {
472fba73005STrond Myklebust 		if (!test_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags))
4731da177e4SLinus Torvalds 			SetPageUptodate(page);
4741da177e4SLinus Torvalds 		nfs_readpage_release(req);
4751da177e4SLinus Torvalds 	}
476cd841605SFred Isaman 	nfs_readdata_release(data);
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
479f11c88afSAndy Adamson void nfs_read_prepare(struct rpc_task *task, void *calldata)
480f11c88afSAndy Adamson {
481f11c88afSAndy Adamson 	struct nfs_read_data *data = calldata;
482cd841605SFred Isaman 	NFS_PROTO(data->header->inode)->read_rpc_prepare(task, data);
483f11c88afSAndy Adamson }
484f11c88afSAndy Adamson 
485ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = {
486f11c88afSAndy Adamson 	.rpc_call_prepare = nfs_read_prepare,
487ec06c096STrond Myklebust 	.rpc_call_done = nfs_readpage_result_partial,
488fdd1e74cSTrond Myklebust 	.rpc_release = nfs_readpage_release_partial,
489ec06c096STrond Myklebust };
490ec06c096STrond Myklebust 
4911de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
4921de3fc12STrond Myklebust {
4931de3fc12STrond Myklebust 	unsigned int count = data->res.count;
4941de3fc12STrond Myklebust 	unsigned int base = data->args.pgbase;
4951de3fc12STrond Myklebust 	struct page **pages;
4961de3fc12STrond Myklebust 
49779558f36STrond Myklebust 	if (data->res.eof)
49879558f36STrond Myklebust 		count = data->args.count;
4991de3fc12STrond Myklebust 	if (unlikely(count == 0))
5001de3fc12STrond Myklebust 		return;
5011de3fc12STrond Myklebust 	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
5021de3fc12STrond Myklebust 	base &= ~PAGE_CACHE_MASK;
5031de3fc12STrond Myklebust 	count += base;
5041de3fc12STrond Myklebust 	for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
5051de3fc12STrond Myklebust 		SetPageUptodate(*pages);
5060b671301STrond Myklebust 	if (count == 0)
5070b671301STrond Myklebust 		return;
5080b671301STrond Myklebust 	/* Was this a short read? */
5090b671301STrond Myklebust 	if (data->res.eof || data->res.count == data->args.count)
5101de3fc12STrond Myklebust 		SetPageUptodate(*pages);
5111de3fc12STrond Myklebust }
5121de3fc12STrond Myklebust 
5131da177e4SLinus Torvalds /*
5141da177e4SLinus Torvalds  * This is the callback from RPC telling us whether a reply was
5151da177e4SLinus Torvalds  * received or some error occurred (timeout or socket shutdown).
5161da177e4SLinus Torvalds  */
517ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
5181da177e4SLinus Torvalds {
519ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
5201da177e4SLinus Torvalds 
5210b671301STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
5220b671301STrond Myklebust 		return;
523fdd1e74cSTrond Myklebust 	if (task->tk_status < 0)
524fdd1e74cSTrond Myklebust 		return;
5251de3fc12STrond Myklebust 	/*
5260b671301STrond Myklebust 	 * Note: nfs_readpage_retry may change the values of
5271de3fc12STrond Myklebust 	 * data->args. In the multi-page case, we therefore need
5280b671301STrond Myklebust 	 * to ensure that we call nfs_readpage_set_pages_uptodate()
5290b671301STrond Myklebust 	 * first.
5301de3fc12STrond Myklebust 	 */
5311de3fc12STrond Myklebust 	nfs_readpage_truncate_uninitialised_page(data);
5321de3fc12STrond Myklebust 	nfs_readpage_set_pages_uptodate(data);
533fdd1e74cSTrond Myklebust 	nfs_readpage_retry(task, data);
5340b671301STrond Myklebust }
535fdd1e74cSTrond Myklebust 
536fdd1e74cSTrond Myklebust static void nfs_readpage_release_full(void *calldata)
537fdd1e74cSTrond Myklebust {
538fdd1e74cSTrond Myklebust 	struct nfs_read_data *data = calldata;
539cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
540fdd1e74cSTrond Myklebust 
541cd841605SFred Isaman 	while (!list_empty(&hdr->pages)) {
542cd841605SFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
5431da177e4SLinus Torvalds 
5441de3fc12STrond Myklebust 		nfs_list_remove_request(req);
5451da177e4SLinus Torvalds 		nfs_readpage_release(req);
5461da177e4SLinus Torvalds 	}
547fdd1e74cSTrond Myklebust 	nfs_readdata_release(calldata);
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
550ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = {
551f11c88afSAndy Adamson 	.rpc_call_prepare = nfs_read_prepare,
552ec06c096STrond Myklebust 	.rpc_call_done = nfs_readpage_result_full,
553fdd1e74cSTrond Myklebust 	.rpc_release = nfs_readpage_release_full,
554ec06c096STrond Myklebust };
555ec06c096STrond Myklebust 
5561da177e4SLinus Torvalds /*
5571da177e4SLinus Torvalds  * Read a page over NFS.
5581da177e4SLinus Torvalds  * We read the page synchronously in the following case:
5591da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
5601da177e4SLinus Torvalds  *	previous async read operation failed.
5611da177e4SLinus Torvalds  */
5621da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page)
5631da177e4SLinus Torvalds {
5641da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
5651da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
5661da177e4SLinus Torvalds 	int		error;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
5691da177e4SLinus Torvalds 		page, PAGE_CACHE_SIZE, page->index);
57091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
57191d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_READPAGES, 1);
57291d5b470SChuck Lever 
5731da177e4SLinus Torvalds 	/*
5741da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
5751da177e4SLinus Torvalds 	 *
5761da177e4SLinus Torvalds 	 * NOTE! Because we own the page lock, there cannot
5771da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
5781da177e4SLinus Torvalds 	 * for this page (other pages can be written to).
5791da177e4SLinus Torvalds 	 */
5801da177e4SLinus Torvalds 	error = nfs_wb_page(inode, page);
5811da177e4SLinus Torvalds 	if (error)
582de05a0ccSTrond Myklebust 		goto out_unlock;
583de05a0ccSTrond Myklebust 	if (PageUptodate(page))
584de05a0ccSTrond Myklebust 		goto out_unlock;
5851da177e4SLinus Torvalds 
5865f004cf2STrond Myklebust 	error = -ESTALE;
5875f004cf2STrond Myklebust 	if (NFS_STALE(inode))
588de05a0ccSTrond Myklebust 		goto out_unlock;
5895f004cf2STrond Myklebust 
5901da177e4SLinus Torvalds 	if (file == NULL) {
591cf1308ffSTrond Myklebust 		error = -EBADF;
592d530838bSTrond Myklebust 		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
5931da177e4SLinus Torvalds 		if (ctx == NULL)
594de05a0ccSTrond Myklebust 			goto out_unlock;
5951da177e4SLinus Torvalds 	} else
596cd3758e3STrond Myklebust 		ctx = get_nfs_open_context(nfs_file_open_context(file));
5971da177e4SLinus Torvalds 
5989a9fc1c0SDavid Howells 	if (!IS_SYNC(inode)) {
5999a9fc1c0SDavid Howells 		error = nfs_readpage_from_fscache(ctx, inode, page);
6009a9fc1c0SDavid Howells 		if (error == 0)
6019a9fc1c0SDavid Howells 			goto out;
6029a9fc1c0SDavid Howells 	}
6039a9fc1c0SDavid Howells 
6048e0969f0STrond Myklebust 	error = nfs_readpage_async(ctx, inode, page);
6058e0969f0STrond Myklebust 
6069a9fc1c0SDavid Howells out:
6071da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
6081da177e4SLinus Torvalds 	return error;
609de05a0ccSTrond Myklebust out_unlock:
6101da177e4SLinus Torvalds 	unlock_page(page);
6111da177e4SLinus Torvalds 	return error;
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds struct nfs_readdesc {
6158b09bee3STrond Myklebust 	struct nfs_pageio_descriptor *pgio;
6161da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
6171da177e4SLinus Torvalds };
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds static int
6201da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
6231da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
6241da177e4SLinus Torvalds 	struct nfs_page *new;
6251da177e4SLinus Torvalds 	unsigned int len;
626de05a0ccSTrond Myklebust 	int error;
6271da177e4SLinus Torvalds 
62849a70f27STrond Myklebust 	len = nfs_page_length(page);
6291da177e4SLinus Torvalds 	if (len == 0)
6301da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
631de05a0ccSTrond Myklebust 
6321da177e4SLinus Torvalds 	new = nfs_create_request(desc->ctx, inode, page, 0, len);
633de05a0ccSTrond Myklebust 	if (IS_ERR(new))
634de05a0ccSTrond Myklebust 		goto out_error;
635de05a0ccSTrond Myklebust 
6361da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
637eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
638f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(desc->pgio, new)) {
639f8512ad0SFred Isaman 		error = desc->pgio->pg_error;
640f8512ad0SFred Isaman 		goto out_unlock;
641f8512ad0SFred Isaman 	}
6421da177e4SLinus Torvalds 	return 0;
643de05a0ccSTrond Myklebust out_error:
644de05a0ccSTrond Myklebust 	error = PTR_ERR(new);
645de05a0ccSTrond Myklebust out_unlock:
646de05a0ccSTrond Myklebust 	unlock_page(page);
647de05a0ccSTrond Myklebust 	return error;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping,
6511da177e4SLinus Torvalds 		struct list_head *pages, unsigned nr_pages)
6521da177e4SLinus Torvalds {
6538b09bee3STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6541da177e4SLinus Torvalds 	struct nfs_readdesc desc = {
6558b09bee3STrond Myklebust 		.pgio = &pgio,
6561da177e4SLinus Torvalds 	};
6571da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
6588b09bee3STrond Myklebust 	unsigned long npages;
6595f004cf2STrond Myklebust 	int ret = -ESTALE;
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
6621da177e4SLinus Torvalds 			inode->i_sb->s_id,
6631da177e4SLinus Torvalds 			(long long)NFS_FILEID(inode),
6641da177e4SLinus Torvalds 			nr_pages);
66591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
6661da177e4SLinus Torvalds 
6675f004cf2STrond Myklebust 	if (NFS_STALE(inode))
6685f004cf2STrond Myklebust 		goto out;
6695f004cf2STrond Myklebust 
6701da177e4SLinus Torvalds 	if (filp == NULL) {
671d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
6721da177e4SLinus Torvalds 		if (desc.ctx == NULL)
6731da177e4SLinus Torvalds 			return -EBADF;
6741da177e4SLinus Torvalds 	} else
675cd3758e3STrond Myklebust 		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
6769a9fc1c0SDavid Howells 
6779a9fc1c0SDavid Howells 	/* attempt to read as many of the pages as possible from the cache
6789a9fc1c0SDavid Howells 	 * - this returns -ENOBUFS immediately if the cookie is negative
6799a9fc1c0SDavid Howells 	 */
6809a9fc1c0SDavid Howells 	ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
6819a9fc1c0SDavid Howells 					 pages, &nr_pages);
6829a9fc1c0SDavid Howells 	if (ret == 0)
6839a9fc1c0SDavid Howells 		goto read_complete; /* all pages were read */
6849a9fc1c0SDavid Howells 
6851751c363STrond Myklebust 	nfs_pageio_init_read(&pgio, inode);
6868b09bee3STrond Myklebust 
6871da177e4SLinus Torvalds 	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
6888b09bee3STrond Myklebust 
6898b09bee3STrond Myklebust 	nfs_pageio_complete(&pgio);
6908b09bee3STrond Myklebust 	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
6918b09bee3STrond Myklebust 	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
6929a9fc1c0SDavid Howells read_complete:
6931da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
6945f004cf2STrond Myklebust out:
6951da177e4SLinus Torvalds 	return ret;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
698f7b422b1SDavid Howells int __init nfs_init_readpagecache(void)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
701cd841605SFred Isaman 					     sizeof(struct nfs_read_header),
7021da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
70320c2df83SPaul Mundt 					     NULL);
7041da177e4SLinus Torvalds 	if (nfs_rdata_cachep == NULL)
7051da177e4SLinus Torvalds 		return -ENOMEM;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	return 0;
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
710266bee88SDavid Brownell void nfs_destroy_readpagecache(void)
7111da177e4SLinus Torvalds {
7121a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_rdata_cachep);
7131da177e4SLinus Torvalds }
714