xref: /openbmc/linux/fs/nfs/read.c (revision 8b09bee3083897e375bd0bf9d60f48daedfab3e0)
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>
211da177e4SLinus Torvalds #include <linux/smp_lock.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <asm/system.h>
241da177e4SLinus Torvalds 
2549a70f27STrond Myklebust #include "internal.h"
2691d5b470SChuck Lever #include "iostat.h"
2791d5b470SChuck Lever 
281da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
291da177e4SLinus Torvalds 
30bcb71bbaSTrond Myklebust static int nfs_pagein_multi(struct inode *, struct list_head *, size_t, int);
31bcb71bbaSTrond Myklebust static int nfs_pagein_one(struct inode *, struct list_head *, size_t, int);
32ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops;
33ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops;
341da177e4SLinus Torvalds 
35e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep;
363feb2d49STrond Myklebust static mempool_t *nfs_rdata_mempool;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define MIN_POOL_READ	(32)
391da177e4SLinus Torvalds 
40e9f7bee1STrond Myklebust struct nfs_read_data *nfs_readdata_alloc(size_t len)
413feb2d49STrond Myklebust {
42e9f7bee1STrond Myklebust 	unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
43e6b4f8daSChristoph Lameter 	struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_NOFS);
443feb2d49STrond Myklebust 
453feb2d49STrond Myklebust 	if (p) {
463feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
473feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
48e9f7bee1STrond Myklebust 		p->npages = pagecount;
490d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
500d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
513feb2d49STrond Myklebust 		else {
520d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
530d0b5cb3SChuck Lever 			if (!p->pagevec) {
543feb2d49STrond Myklebust 				mempool_free(p, nfs_rdata_mempool);
553feb2d49STrond Myklebust 				p = NULL;
563feb2d49STrond Myklebust 			}
573feb2d49STrond Myklebust 		}
583feb2d49STrond Myklebust 	}
593feb2d49STrond Myklebust 	return p;
603feb2d49STrond Myklebust }
613feb2d49STrond Myklebust 
628aca67f0STrond Myklebust static void nfs_readdata_rcu_free(struct rcu_head *head)
633feb2d49STrond Myklebust {
648aca67f0STrond Myklebust 	struct nfs_read_data *p = container_of(head, struct nfs_read_data, task.u.tk_rcu);
653feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
663feb2d49STrond Myklebust 		kfree(p->pagevec);
673feb2d49STrond Myklebust 	mempool_free(p, nfs_rdata_mempool);
683feb2d49STrond Myklebust }
693feb2d49STrond Myklebust 
708aca67f0STrond Myklebust static void nfs_readdata_free(struct nfs_read_data *rdata)
718aca67f0STrond Myklebust {
728aca67f0STrond Myklebust 	call_rcu_bh(&rdata->task.u.tk_rcu, nfs_readdata_rcu_free);
738aca67f0STrond Myklebust }
748aca67f0STrond Myklebust 
75963d8fe5STrond Myklebust void nfs_readdata_release(void *data)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds         nfs_readdata_free(data);
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds static
811da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page)
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds 	memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
841da177e4SLinus Torvalds 	SetPageUptodate(page);
851da177e4SLinus Torvalds 	unlock_page(page);
861da177e4SLinus Torvalds 	return 0;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
891de3fc12STrond Myklebust static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
901de3fc12STrond Myklebust {
911de3fc12STrond Myklebust 	unsigned int remainder = data->args.count - data->res.count;
921de3fc12STrond Myklebust 	unsigned int base = data->args.pgbase + data->res.count;
931de3fc12STrond Myklebust 	unsigned int pglen;
941de3fc12STrond Myklebust 	struct page **pages;
951de3fc12STrond Myklebust 
961de3fc12STrond Myklebust 	if (data->res.eof == 0 || remainder == 0)
971de3fc12STrond Myklebust 		return;
981de3fc12STrond Myklebust 	/*
991de3fc12STrond Myklebust 	 * Note: "remainder" can never be negative, since we check for
1001de3fc12STrond Myklebust 	 * 	this in the XDR code.
1011de3fc12STrond Myklebust 	 */
1021de3fc12STrond Myklebust 	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
1031de3fc12STrond Myklebust 	base &= ~PAGE_CACHE_MASK;
1041de3fc12STrond Myklebust 	pglen = PAGE_CACHE_SIZE - base;
10579558f36STrond Myklebust 	for (;;) {
10679558f36STrond Myklebust 		if (remainder <= pglen) {
1071de3fc12STrond Myklebust 			memclear_highpage_flush(*pages, base, remainder);
10879558f36STrond Myklebust 			break;
10979558f36STrond Myklebust 		}
11079558f36STrond Myklebust 		memclear_highpage_flush(*pages, base, pglen);
11179558f36STrond Myklebust 		pages++;
11279558f36STrond Myklebust 		remainder -= pglen;
11379558f36STrond Myklebust 		pglen = PAGE_CACHE_SIZE;
11479558f36STrond Myklebust 		base = 0;
11579558f36STrond Myklebust 	}
1161de3fc12STrond Myklebust }
1171de3fc12STrond Myklebust 
1181da177e4SLinus Torvalds static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
1191da177e4SLinus Torvalds 		struct page *page)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	LIST_HEAD(one_request);
1221da177e4SLinus Torvalds 	struct nfs_page	*new;
1231da177e4SLinus Torvalds 	unsigned int len;
1241da177e4SLinus Torvalds 
12549a70f27STrond Myklebust 	len = nfs_page_length(page);
1261da177e4SLinus Torvalds 	if (len == 0)
1271da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
1281da177e4SLinus Torvalds 	new = nfs_create_request(ctx, inode, page, 0, len);
1291da177e4SLinus Torvalds 	if (IS_ERR(new)) {
1301da177e4SLinus Torvalds 		unlock_page(page);
1311da177e4SLinus Torvalds 		return PTR_ERR(new);
1321da177e4SLinus Torvalds 	}
1331da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
1341da177e4SLinus Torvalds 		memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	nfs_list_add_request(new, &one_request);
137bcb71bbaSTrond Myklebust 	if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
138bcb71bbaSTrond Myklebust 		nfs_pagein_multi(inode, &one_request, len, 0);
139bcb71bbaSTrond Myklebust 	else
140bcb71bbaSTrond Myklebust 		nfs_pagein_one(inode, &one_request, len, 0);
1411da177e4SLinus Torvalds 	return 0;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	unlock_page(req->wb_page);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
1491da177e4SLinus Torvalds 			req->wb_context->dentry->d_inode->i_sb->s_id,
1501da177e4SLinus Torvalds 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1511da177e4SLinus Torvalds 			req->wb_bytes,
1521da177e4SLinus Torvalds 			(long long)req_offset(req));
15310d2c46fSNick Wilson 	nfs_clear_request(req);
15410d2c46fSNick Wilson 	nfs_release_request(req);
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /*
1581da177e4SLinus Torvalds  * Set up the NFS read request struct
1591da177e4SLinus Torvalds  */
1601da177e4SLinus Torvalds static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
161ec06c096STrond Myklebust 		const struct rpc_call_ops *call_ops,
1621da177e4SLinus Torvalds 		unsigned int count, unsigned int offset)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	struct inode		*inode;
165ec06c096STrond Myklebust 	int flags;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	data->req	  = req;
1681da177e4SLinus Torvalds 	data->inode	  = inode = req->wb_context->dentry->d_inode;
1691da177e4SLinus Torvalds 	data->cred	  = req->wb_context->cred;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
1721da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
1731da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
1741da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
1751da177e4SLinus Torvalds 	data->args.count  = count;
1761da177e4SLinus Torvalds 	data->args.context = req->wb_context;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
1791da177e4SLinus Torvalds 	data->res.count   = count;
1801da177e4SLinus Torvalds 	data->res.eof     = 0;
1810e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
1821da177e4SLinus Torvalds 
183ec06c096STrond Myklebust 	/* Set up the initial task struct. */
184ec06c096STrond Myklebust 	flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
185ec06c096STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
1861da177e4SLinus Torvalds 	NFS_PROTO(inode)->read_setup(data);
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	data->task.tk_cookie = (unsigned long)inode;
1891da177e4SLinus Torvalds 
190a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
1911da177e4SLinus Torvalds 			data->task.tk_pid,
1921da177e4SLinus Torvalds 			inode->i_sb->s_id,
1931da177e4SLinus Torvalds 			(long long)NFS_FILEID(inode),
1941da177e4SLinus Torvalds 			count,
1951da177e4SLinus Torvalds 			(unsigned long long)data->args.offset);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds static void
1991da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	struct nfs_page	*req;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	while (!list_empty(head)) {
2041da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
2051da177e4SLinus Torvalds 		nfs_list_remove_request(req);
2061da177e4SLinus Torvalds 		SetPageError(req->wb_page);
2071da177e4SLinus Torvalds 		nfs_readpage_release(req);
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /*
2121da177e4SLinus Torvalds  * Start an async read operation
2131da177e4SLinus Torvalds  */
2141da177e4SLinus Torvalds static void nfs_execute_read(struct nfs_read_data *data)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
2171da177e4SLinus Torvalds 	sigset_t oldset;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
2201da177e4SLinus Torvalds 	rpc_execute(&data->task);
2211da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds /*
2251da177e4SLinus Torvalds  * Generate multiple requests to fill a single page.
2261da177e4SLinus Torvalds  *
2271da177e4SLinus Torvalds  * We optimize to reduce the number of read operations on the wire.  If we
2281da177e4SLinus Torvalds  * detect that we're reading a page, or an area of a page, that is past the
2291da177e4SLinus Torvalds  * end of file, we do not generate NFS read operations but just clear the
2301da177e4SLinus Torvalds  * parts of the page that would have come back zero from the server anyway.
2311da177e4SLinus Torvalds  *
2321da177e4SLinus Torvalds  * We rely on the cached value of i_size to make this determination; another
2331da177e4SLinus Torvalds  * client can fill pages on the server past our cached end-of-file, but we
2341da177e4SLinus Torvalds  * won't see the new data until our attribute cache is updated.  This is more
2351da177e4SLinus Torvalds  * or less conventional NFS client behavior.
2361da177e4SLinus Torvalds  */
237bcb71bbaSTrond Myklebust static int nfs_pagein_multi(struct inode *inode, struct list_head *head, size_t count, int flags)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
2401da177e4SLinus Torvalds 	struct page *page = req->wb_page;
2411da177e4SLinus Torvalds 	struct nfs_read_data *data;
242e9f7bee1STrond Myklebust 	size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
243e9f7bee1STrond Myklebust 	unsigned int offset;
2441da177e4SLinus Torvalds 	int requests = 0;
2451da177e4SLinus Torvalds 	LIST_HEAD(list);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	nfs_list_remove_request(req);
2481da177e4SLinus Torvalds 
249bcb71bbaSTrond Myklebust 	nbytes = count;
250e9f7bee1STrond Myklebust 	do {
251e9f7bee1STrond Myklebust 		size_t len = min(nbytes,rsize);
252e9f7bee1STrond Myklebust 
253e9f7bee1STrond Myklebust 		data = nfs_readdata_alloc(len);
2541da177e4SLinus Torvalds 		if (!data)
2551da177e4SLinus Torvalds 			goto out_bad;
2561da177e4SLinus Torvalds 		INIT_LIST_HEAD(&data->pages);
2571da177e4SLinus Torvalds 		list_add(&data->pages, &list);
2581da177e4SLinus Torvalds 		requests++;
259e9f7bee1STrond Myklebust 		nbytes -= len;
260e9f7bee1STrond Myklebust 	} while(nbytes != 0);
2611da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	ClearPageError(page);
2641da177e4SLinus Torvalds 	offset = 0;
265bcb71bbaSTrond Myklebust 	nbytes = count;
2661da177e4SLinus Torvalds 	do {
2671da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_read_data, pages);
2681da177e4SLinus Torvalds 		list_del_init(&data->pages);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 		data->pagevec[0] = page;
2711da177e4SLinus Torvalds 
272bcb71bbaSTrond Myklebust 		if (nbytes < rsize)
273bcb71bbaSTrond Myklebust 			rsize = nbytes;
274ec06c096STrond Myklebust 		nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
275ec06c096STrond Myklebust 				  rsize, offset);
2761da177e4SLinus Torvalds 		offset += rsize;
2771da177e4SLinus Torvalds 		nbytes -= rsize;
2781da177e4SLinus Torvalds 		nfs_execute_read(data);
2791da177e4SLinus Torvalds 	} while (nbytes != 0);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	return 0;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds out_bad:
2841da177e4SLinus Torvalds 	while (!list_empty(&list)) {
2851da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_read_data, pages);
2861da177e4SLinus Torvalds 		list_del(&data->pages);
2871da177e4SLinus Torvalds 		nfs_readdata_free(data);
2881da177e4SLinus Torvalds 	}
2891da177e4SLinus Torvalds 	SetPageError(page);
2901da177e4SLinus Torvalds 	nfs_readpage_release(req);
2911da177e4SLinus Torvalds 	return -ENOMEM;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
294bcb71bbaSTrond Myklebust static int nfs_pagein_one(struct inode *inode, struct list_head *head, size_t count, int flags)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	struct nfs_page		*req;
2971da177e4SLinus Torvalds 	struct page		**pages;
2981da177e4SLinus Torvalds 	struct nfs_read_data	*data;
2991da177e4SLinus Torvalds 
300bcb71bbaSTrond Myklebust 	data = nfs_readdata_alloc(count);
3011da177e4SLinus Torvalds 	if (!data)
3021da177e4SLinus Torvalds 		goto out_bad;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	INIT_LIST_HEAD(&data->pages);
3051da177e4SLinus Torvalds 	pages = data->pagevec;
3061da177e4SLinus Torvalds 	while (!list_empty(head)) {
3071da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
3081da177e4SLinus Torvalds 		nfs_list_remove_request(req);
3091da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
3101da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
3111da177e4SLinus Torvalds 		*pages++ = req->wb_page;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
3141da177e4SLinus Torvalds 
315ec06c096STrond Myklebust 	nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	nfs_execute_read(data);
3181da177e4SLinus Torvalds 	return 0;
3191da177e4SLinus Torvalds out_bad:
3201da177e4SLinus Torvalds 	nfs_async_read_error(head);
3211da177e4SLinus Torvalds 	return -ENOMEM;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds /*
3250b671301STrond Myklebust  * This is the callback from RPC telling us whether a reply was
3260b671301STrond Myklebust  * received or some error occurred (timeout or socket shutdown).
3270b671301STrond Myklebust  */
3280b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
3290b671301STrond Myklebust {
3300b671301STrond Myklebust 	int status;
3310b671301STrond Myklebust 
332a3f565b1SChuck Lever 	dprintk("NFS: %s: %5u, (status %d)\n", __FUNCTION__, task->tk_pid,
3330b671301STrond Myklebust 			task->tk_status);
3340b671301STrond Myklebust 
3350b671301STrond Myklebust 	status = NFS_PROTO(data->inode)->read_done(task, data);
3360b671301STrond Myklebust 	if (status != 0)
3370b671301STrond Myklebust 		return status;
3380b671301STrond Myklebust 
3390b671301STrond Myklebust 	nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
3400b671301STrond Myklebust 
3410b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
3420b671301STrond Myklebust 		set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode));
3430b671301STrond Myklebust 		nfs_mark_for_revalidate(data->inode);
3440b671301STrond Myklebust 	}
3450b671301STrond Myklebust 	spin_lock(&data->inode->i_lock);
3460b671301STrond Myklebust 	NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
3470b671301STrond Myklebust 	spin_unlock(&data->inode->i_lock);
3480b671301STrond Myklebust 	return 0;
3490b671301STrond Myklebust }
3500b671301STrond Myklebust 
3510b671301STrond Myklebust static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
3520b671301STrond Myklebust {
3530b671301STrond Myklebust 	struct nfs_readargs *argp = &data->args;
3540b671301STrond Myklebust 	struct nfs_readres *resp = &data->res;
3550b671301STrond Myklebust 
3560b671301STrond Myklebust 	if (resp->eof || resp->count == argp->count)
3570b671301STrond Myklebust 		return 0;
3580b671301STrond Myklebust 
3590b671301STrond Myklebust 	/* This is a short read! */
3600b671301STrond Myklebust 	nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
3610b671301STrond Myklebust 	/* Has the server at least made some progress? */
3620b671301STrond Myklebust 	if (resp->count == 0)
3630b671301STrond Myklebust 		return 0;
3640b671301STrond Myklebust 
3650b671301STrond Myklebust 	/* Yes, so retry the read at the end of the data */
3660b671301STrond Myklebust 	argp->offset += resp->count;
3670b671301STrond Myklebust 	argp->pgbase += resp->count;
3680b671301STrond Myklebust 	argp->count -= resp->count;
3690b671301STrond Myklebust 	rpc_restart_call(task);
3700b671301STrond Myklebust 	return -EAGAIN;
3710b671301STrond Myklebust }
3720b671301STrond Myklebust 
3730b671301STrond Myklebust /*
3741da177e4SLinus Torvalds  * Handle a read reply that fills part of a page.
3751da177e4SLinus Torvalds  */
376ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
3771da177e4SLinus Torvalds {
378ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
3791da177e4SLinus Torvalds 	struct nfs_page *req = data->req;
3801da177e4SLinus Torvalds 	struct page *page = req->wb_page;
3811da177e4SLinus Torvalds 
382ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
383ec06c096STrond Myklebust 		return;
3840b671301STrond Myklebust 
3850b671301STrond Myklebust 	if (likely(task->tk_status >= 0)) {
3860b671301STrond Myklebust 		nfs_readpage_truncate_uninitialised_page(data);
3870b671301STrond Myklebust 		if (nfs_readpage_retry(task, data) != 0)
3880b671301STrond Myklebust 			return;
3890b671301STrond Myklebust 	}
3900b671301STrond Myklebust 	if (unlikely(task->tk_status < 0))
3910b671301STrond Myklebust 		SetPageError(page);
3921da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete)) {
3931da177e4SLinus Torvalds 		if (!PageError(page))
3941da177e4SLinus Torvalds 			SetPageUptodate(page);
3951da177e4SLinus Torvalds 		nfs_readpage_release(req);
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
399ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = {
400ec06c096STrond Myklebust 	.rpc_call_done = nfs_readpage_result_partial,
401ec06c096STrond Myklebust 	.rpc_release = nfs_readdata_release,
402ec06c096STrond Myklebust };
403ec06c096STrond Myklebust 
4041de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
4051de3fc12STrond Myklebust {
4061de3fc12STrond Myklebust 	unsigned int count = data->res.count;
4071de3fc12STrond Myklebust 	unsigned int base = data->args.pgbase;
4081de3fc12STrond Myklebust 	struct page **pages;
4091de3fc12STrond Myklebust 
41079558f36STrond Myklebust 	if (data->res.eof)
41179558f36STrond Myklebust 		count = data->args.count;
4121de3fc12STrond Myklebust 	if (unlikely(count == 0))
4131de3fc12STrond Myklebust 		return;
4141de3fc12STrond Myklebust 	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
4151de3fc12STrond Myklebust 	base &= ~PAGE_CACHE_MASK;
4161de3fc12STrond Myklebust 	count += base;
4171de3fc12STrond Myklebust 	for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
4181de3fc12STrond Myklebust 		SetPageUptodate(*pages);
4190b671301STrond Myklebust 	if (count == 0)
4200b671301STrond Myklebust 		return;
4210b671301STrond Myklebust 	/* Was this a short read? */
4220b671301STrond Myklebust 	if (data->res.eof || data->res.count == data->args.count)
4231de3fc12STrond Myklebust 		SetPageUptodate(*pages);
4241de3fc12STrond Myklebust }
4251de3fc12STrond Myklebust 
4261da177e4SLinus Torvalds /*
4271da177e4SLinus Torvalds  * This is the callback from RPC telling us whether a reply was
4281da177e4SLinus Torvalds  * received or some error occurred (timeout or socket shutdown).
4291da177e4SLinus Torvalds  */
430ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
4311da177e4SLinus Torvalds {
432ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
4331da177e4SLinus Torvalds 
4340b671301STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
4350b671301STrond Myklebust 		return;
4361de3fc12STrond Myklebust 	/*
4370b671301STrond Myklebust 	 * Note: nfs_readpage_retry may change the values of
4381de3fc12STrond Myklebust 	 * data->args. In the multi-page case, we therefore need
4390b671301STrond Myklebust 	 * to ensure that we call nfs_readpage_set_pages_uptodate()
4400b671301STrond Myklebust 	 * first.
4411de3fc12STrond Myklebust 	 */
4421de3fc12STrond Myklebust 	if (likely(task->tk_status >= 0)) {
4431de3fc12STrond Myklebust 		nfs_readpage_truncate_uninitialised_page(data);
4441de3fc12STrond Myklebust 		nfs_readpage_set_pages_uptodate(data);
4450b671301STrond Myklebust 		if (nfs_readpage_retry(task, data) != 0)
446ec06c096STrond Myklebust 			return;
4470b671301STrond Myklebust 	}
4481da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
4491da177e4SLinus Torvalds 		struct nfs_page *req = nfs_list_entry(data->pages.next);
4501da177e4SLinus Torvalds 
4511de3fc12STrond Myklebust 		nfs_list_remove_request(req);
4521da177e4SLinus Torvalds 		nfs_readpage_release(req);
4531da177e4SLinus Torvalds 	}
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
456ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = {
457ec06c096STrond Myklebust 	.rpc_call_done = nfs_readpage_result_full,
458ec06c096STrond Myklebust 	.rpc_release = nfs_readdata_release,
459ec06c096STrond Myklebust };
460ec06c096STrond Myklebust 
4611da177e4SLinus Torvalds /*
4621da177e4SLinus Torvalds  * Read a page over NFS.
4631da177e4SLinus Torvalds  * We read the page synchronously in the following case:
4641da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
4651da177e4SLinus Torvalds  *	previous async read operation failed.
4661da177e4SLinus Torvalds  */
4671da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
4701da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
4711da177e4SLinus Torvalds 	int		error;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
4741da177e4SLinus Torvalds 		page, PAGE_CACHE_SIZE, page->index);
47591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
47691d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_READPAGES, 1);
47791d5b470SChuck Lever 
4781da177e4SLinus Torvalds 	/*
4791da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
4801da177e4SLinus Torvalds 	 *
4811da177e4SLinus Torvalds 	 * NOTE! Because we own the page lock, there cannot
4821da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
4831da177e4SLinus Torvalds 	 * for this page (other pages can be written to).
4841da177e4SLinus Torvalds 	 */
4851da177e4SLinus Torvalds 	error = nfs_wb_page(inode, page);
4861da177e4SLinus Torvalds 	if (error)
4871da177e4SLinus Torvalds 		goto out_error;
4881da177e4SLinus Torvalds 
4895f004cf2STrond Myklebust 	error = -ESTALE;
4905f004cf2STrond Myklebust 	if (NFS_STALE(inode))
4915f004cf2STrond Myklebust 		goto out_error;
4925f004cf2STrond Myklebust 
4931da177e4SLinus Torvalds 	if (file == NULL) {
494cf1308ffSTrond Myklebust 		error = -EBADF;
495d530838bSTrond Myklebust 		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
4961da177e4SLinus Torvalds 		if (ctx == NULL)
497cf1308ffSTrond Myklebust 			goto out_error;
4981da177e4SLinus Torvalds 	} else
4991da177e4SLinus Torvalds 		ctx = get_nfs_open_context((struct nfs_open_context *)
5001da177e4SLinus Torvalds 				file->private_data);
5011da177e4SLinus Torvalds 
5028e0969f0STrond Myklebust 	error = nfs_readpage_async(ctx, inode, page);
5038e0969f0STrond Myklebust 
5041da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
5051da177e4SLinus Torvalds 	return error;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds out_error:
5081da177e4SLinus Torvalds 	unlock_page(page);
5091da177e4SLinus Torvalds 	return error;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds struct nfs_readdesc {
513*8b09bee3STrond Myklebust 	struct nfs_pageio_descriptor *pgio;
5141da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
5151da177e4SLinus Torvalds };
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds static int
5181da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds 	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
5211da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
5221da177e4SLinus Torvalds 	struct nfs_page *new;
5231da177e4SLinus Torvalds 	unsigned int len;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	nfs_wb_page(inode, page);
52649a70f27STrond Myklebust 	len = nfs_page_length(page);
5271da177e4SLinus Torvalds 	if (len == 0)
5281da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
5291da177e4SLinus Torvalds 	new = nfs_create_request(desc->ctx, inode, page, 0, len);
5301da177e4SLinus Torvalds 	if (IS_ERR(new)) {
5311da177e4SLinus Torvalds 			SetPageError(page);
5321da177e4SLinus Torvalds 			unlock_page(page);
5331da177e4SLinus Torvalds 			return PTR_ERR(new);
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
5361da177e4SLinus Torvalds 		memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
537*8b09bee3STrond Myklebust 	nfs_pageio_add_request(desc->pgio, new);
5381da177e4SLinus Torvalds 	return 0;
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping,
5421da177e4SLinus Torvalds 		struct list_head *pages, unsigned nr_pages)
5431da177e4SLinus Torvalds {
544*8b09bee3STrond Myklebust 	struct nfs_pageio_descriptor pgio;
5451da177e4SLinus Torvalds 	struct nfs_readdesc desc = {
546*8b09bee3STrond Myklebust 		.pgio = &pgio,
5471da177e4SLinus Torvalds 	};
5481da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
5491da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
550*8b09bee3STrond Myklebust 	size_t rsize = server->rsize;
551*8b09bee3STrond Myklebust 	unsigned long npages;
5525f004cf2STrond Myklebust 	int ret = -ESTALE;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
5551da177e4SLinus Torvalds 			inode->i_sb->s_id,
5561da177e4SLinus Torvalds 			(long long)NFS_FILEID(inode),
5571da177e4SLinus Torvalds 			nr_pages);
55891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
5591da177e4SLinus Torvalds 
5605f004cf2STrond Myklebust 	if (NFS_STALE(inode))
5615f004cf2STrond Myklebust 		goto out;
5625f004cf2STrond Myklebust 
5631da177e4SLinus Torvalds 	if (filp == NULL) {
564d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
5651da177e4SLinus Torvalds 		if (desc.ctx == NULL)
5661da177e4SLinus Torvalds 			return -EBADF;
5671da177e4SLinus Torvalds 	} else
5681da177e4SLinus Torvalds 		desc.ctx = get_nfs_open_context((struct nfs_open_context *)
5691da177e4SLinus Torvalds 				filp->private_data);
570*8b09bee3STrond Myklebust 	if (rsize < PAGE_CACHE_SIZE)
571*8b09bee3STrond Myklebust 		nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0);
572*8b09bee3STrond Myklebust 	else
573*8b09bee3STrond Myklebust 		nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0);
574*8b09bee3STrond Myklebust 
5751da177e4SLinus Torvalds 	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
576*8b09bee3STrond Myklebust 
577*8b09bee3STrond Myklebust 	nfs_pageio_complete(&pgio);
578*8b09bee3STrond Myklebust 	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
579*8b09bee3STrond Myklebust 	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
5801da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
5815f004cf2STrond Myklebust out:
5821da177e4SLinus Torvalds 	return ret;
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
585f7b422b1SDavid Howells int __init nfs_init_readpagecache(void)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
5881da177e4SLinus Torvalds 					     sizeof(struct nfs_read_data),
5891da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
5901da177e4SLinus Torvalds 					     NULL, NULL);
5911da177e4SLinus Torvalds 	if (nfs_rdata_cachep == NULL)
5921da177e4SLinus Torvalds 		return -ENOMEM;
5931da177e4SLinus Torvalds 
59493d2341cSMatthew Dobson 	nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
5951da177e4SLinus Torvalds 						     nfs_rdata_cachep);
5961da177e4SLinus Torvalds 	if (nfs_rdata_mempool == NULL)
5971da177e4SLinus Torvalds 		return -ENOMEM;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	return 0;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
602266bee88SDavid Brownell void nfs_destroy_readpagecache(void)
6031da177e4SLinus Torvalds {
6041da177e4SLinus Torvalds 	mempool_destroy(nfs_rdata_mempool);
6051a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_rdata_cachep);
6061da177e4SLinus Torvalds }
607