xref: /openbmc/linux/fs/nfs/read.c (revision d61e612a728fb9bf848c4383f8f6645e822d5b57)
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 
221da177e4SLinus Torvalds #include <asm/system.h>
231da177e4SLinus Torvalds 
24f11c88afSAndy Adamson #include "nfs4_fs.h"
2549a70f27STrond Myklebust #include "internal.h"
2691d5b470SChuck Lever #include "iostat.h"
279a9fc1c0SDavid Howells #include "fscache.h"
2891d5b470SChuck Lever 
291da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
301da177e4SLinus Torvalds 
318d5658c9STrond Myklebust static int nfs_pagein_multi(struct inode *, struct list_head *, unsigned int, size_t, int);
328d5658c9STrond Myklebust static int nfs_pagein_one(struct inode *, struct list_head *, unsigned int, size_t, int);
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;
373feb2d49STrond Myklebust static mempool_t *nfs_rdata_mempool;
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define MIN_POOL_READ	(32)
401da177e4SLinus Torvalds 
418d5658c9STrond Myklebust struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount)
423feb2d49STrond Myklebust {
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;
495f7dbd5cSAndy Adamson 		p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
500d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
510d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
523feb2d49STrond Myklebust 		else {
530d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
540d0b5cb3SChuck Lever 			if (!p->pagevec) {
553feb2d49STrond Myklebust 				mempool_free(p, nfs_rdata_mempool);
563feb2d49STrond Myklebust 				p = NULL;
573feb2d49STrond Myklebust 			}
583feb2d49STrond Myklebust 		}
593feb2d49STrond Myklebust 	}
603feb2d49STrond Myklebust 	return p;
613feb2d49STrond Myklebust }
623feb2d49STrond Myklebust 
631ae88b2eSTrond Myklebust void nfs_readdata_free(struct nfs_read_data *p)
643feb2d49STrond Myklebust {
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 
701ae88b2eSTrond Myklebust static void nfs_readdata_release(struct nfs_read_data *rdata)
711da177e4SLinus Torvalds {
72383ba719STrond Myklebust 	put_nfs_open_context(rdata->args.context);
73383ba719STrond Myklebust 	nfs_readdata_free(rdata);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static
771da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page)
781da177e4SLinus Torvalds {
79eebd2aa3SChristoph Lameter 	zero_user(page, 0, PAGE_CACHE_SIZE);
801da177e4SLinus Torvalds 	SetPageUptodate(page);
811da177e4SLinus Torvalds 	unlock_page(page);
821da177e4SLinus Torvalds 	return 0;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
851de3fc12STrond Myklebust static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
861de3fc12STrond Myklebust {
871de3fc12STrond Myklebust 	unsigned int remainder = data->args.count - data->res.count;
881de3fc12STrond Myklebust 	unsigned int base = data->args.pgbase + data->res.count;
891de3fc12STrond Myklebust 	unsigned int pglen;
901de3fc12STrond Myklebust 	struct page **pages;
911de3fc12STrond Myklebust 
921de3fc12STrond Myklebust 	if (data->res.eof == 0 || remainder == 0)
931de3fc12STrond Myklebust 		return;
941de3fc12STrond Myklebust 	/*
951de3fc12STrond Myklebust 	 * Note: "remainder" can never be negative, since we check for
961de3fc12STrond Myklebust 	 * 	this in the XDR code.
971de3fc12STrond Myklebust 	 */
981de3fc12STrond Myklebust 	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
991de3fc12STrond Myklebust 	base &= ~PAGE_CACHE_MASK;
1001de3fc12STrond Myklebust 	pglen = PAGE_CACHE_SIZE - base;
10179558f36STrond Myklebust 	for (;;) {
10279558f36STrond Myklebust 		if (remainder <= pglen) {
103eebd2aa3SChristoph Lameter 			zero_user(*pages, base, remainder);
10479558f36STrond Myklebust 			break;
10579558f36STrond Myklebust 		}
106eebd2aa3SChristoph Lameter 		zero_user(*pages, base, pglen);
10779558f36STrond Myklebust 		pages++;
10879558f36STrond Myklebust 		remainder -= pglen;
10979558f36STrond Myklebust 		pglen = PAGE_CACHE_SIZE;
11079558f36STrond Myklebust 		base = 0;
11179558f36STrond Myklebust 	}
1121de3fc12STrond Myklebust }
1131de3fc12STrond Myklebust 
114f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
1151da177e4SLinus Torvalds 		       struct page *page)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	LIST_HEAD(one_request);
1181da177e4SLinus Torvalds 	struct nfs_page	*new;
1191da177e4SLinus Torvalds 	unsigned int len;
1201da177e4SLinus Torvalds 
12149a70f27STrond Myklebust 	len = nfs_page_length(page);
1221da177e4SLinus Torvalds 	if (len == 0)
1231da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
1241da177e4SLinus Torvalds 	new = nfs_create_request(ctx, inode, page, 0, len);
1251da177e4SLinus Torvalds 	if (IS_ERR(new)) {
1261da177e4SLinus Torvalds 		unlock_page(page);
1271da177e4SLinus Torvalds 		return PTR_ERR(new);
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
130eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	nfs_list_add_request(new, &one_request);
133bcb71bbaSTrond Myklebust 	if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
1348d5658c9STrond Myklebust 		nfs_pagein_multi(inode, &one_request, 1, len, 0);
135bcb71bbaSTrond Myklebust 	else
1368d5658c9STrond Myklebust 		nfs_pagein_one(inode, &one_request, 1, len, 0);
1371da177e4SLinus Torvalds 	return 0;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req)
1411da177e4SLinus Torvalds {
1427f8e05f6SDavid Howells 	struct inode *d_inode = req->wb_context->path.dentry->d_inode;
1437f8e05f6SDavid Howells 
1447f8e05f6SDavid Howells 	if (PageUptodate(req->wb_page))
1457f8e05f6SDavid Howells 		nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
1467f8e05f6SDavid Howells 
1471da177e4SLinus Torvalds 	unlock_page(req->wb_page);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
15088be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
15188be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1521da177e4SLinus Torvalds 			req->wb_bytes,
1531da177e4SLinus Torvalds 			(long long)req_offset(req));
15410d2c46fSNick Wilson 	nfs_clear_request(req);
15510d2c46fSNick Wilson 	nfs_release_request(req);
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds /*
1591da177e4SLinus Torvalds  * Set up the NFS read request struct
1601da177e4SLinus Torvalds  */
161dbae4c73STrond Myklebust static int nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
162ec06c096STrond Myklebust 		const struct rpc_call_ops *call_ops,
1631da177e4SLinus Torvalds 		unsigned int count, unsigned int offset)
1641da177e4SLinus Torvalds {
16584115e1cSTrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
16684115e1cSTrond Myklebust 	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
16707737691STrond Myklebust 	struct rpc_task *task;
168bdc7f021STrond Myklebust 	struct rpc_message msg = {
169bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
170bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
171bdc7f021STrond Myklebust 		.rpc_cred = req->wb_context->cred,
172bdc7f021STrond Myklebust 	};
17384115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
17407737691STrond Myklebust 		.task = &data->task,
17584115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
176bdc7f021STrond Myklebust 		.rpc_message = &msg,
17784115e1cSTrond Myklebust 		.callback_ops = call_ops,
17884115e1cSTrond Myklebust 		.callback_data = data,
179101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
18084115e1cSTrond Myklebust 		.flags = RPC_TASK_ASYNC | swap_flags,
18184115e1cSTrond Myklebust 	};
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	data->req	  = req;
18484115e1cSTrond Myklebust 	data->inode	  = inode;
185bdc7f021STrond Myklebust 	data->cred	  = msg.rpc_cred;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
1881da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
1891da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
1901da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
1911da177e4SLinus Torvalds 	data->args.count  = count;
192383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
1951da177e4SLinus Torvalds 	data->res.count   = count;
1961da177e4SLinus Torvalds 	data->res.eof     = 0;
1970e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
1981da177e4SLinus Torvalds 
199ec06c096STrond Myklebust 	/* Set up the initial task struct. */
200bdc7f021STrond Myklebust 	NFS_PROTO(inode)->read_setup(data, &msg);
2011da177e4SLinus Torvalds 
202a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
2031da177e4SLinus Torvalds 			data->task.tk_pid,
2041da177e4SLinus Torvalds 			inode->i_sb->s_id,
2051da177e4SLinus Torvalds 			(long long)NFS_FILEID(inode),
2061da177e4SLinus Torvalds 			count,
2071da177e4SLinus Torvalds 			(unsigned long long)data->args.offset);
208bdc7f021STrond Myklebust 
20907737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
210dbae4c73STrond Myklebust 	if (IS_ERR(task))
211dbae4c73STrond Myklebust 		return PTR_ERR(task);
21207737691STrond Myklebust 	rpc_put_task(task);
213dbae4c73STrond Myklebust 	return 0;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds static void
2171da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	struct nfs_page	*req;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	while (!list_empty(head)) {
2221da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
2231da177e4SLinus Torvalds 		nfs_list_remove_request(req);
2241da177e4SLinus Torvalds 		SetPageError(req->wb_page);
2251da177e4SLinus Torvalds 		nfs_readpage_release(req);
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds /*
2301da177e4SLinus Torvalds  * Generate multiple requests to fill a single page.
2311da177e4SLinus Torvalds  *
2321da177e4SLinus Torvalds  * We optimize to reduce the number of read operations on the wire.  If we
2331da177e4SLinus Torvalds  * detect that we're reading a page, or an area of a page, that is past the
2341da177e4SLinus Torvalds  * end of file, we do not generate NFS read operations but just clear the
2351da177e4SLinus Torvalds  * parts of the page that would have come back zero from the server anyway.
2361da177e4SLinus Torvalds  *
2371da177e4SLinus Torvalds  * We rely on the cached value of i_size to make this determination; another
2381da177e4SLinus Torvalds  * client can fill pages on the server past our cached end-of-file, but we
2391da177e4SLinus Torvalds  * won't see the new data until our attribute cache is updated.  This is more
2401da177e4SLinus Torvalds  * or less conventional NFS client behavior.
2411da177e4SLinus Torvalds  */
2428d5658c9STrond Myklebust static int nfs_pagein_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
2451da177e4SLinus Torvalds 	struct page *page = req->wb_page;
2461da177e4SLinus Torvalds 	struct nfs_read_data *data;
247e9f7bee1STrond Myklebust 	size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
248e9f7bee1STrond Myklebust 	unsigned int offset;
2491da177e4SLinus Torvalds 	int requests = 0;
250dbae4c73STrond Myklebust 	int ret = 0;
2511da177e4SLinus Torvalds 	LIST_HEAD(list);
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	nfs_list_remove_request(req);
2541da177e4SLinus Torvalds 
255bcb71bbaSTrond Myklebust 	nbytes = count;
256e9f7bee1STrond Myklebust 	do {
257e9f7bee1STrond Myklebust 		size_t len = min(nbytes,rsize);
258e9f7bee1STrond Myklebust 
2598d5658c9STrond Myklebust 		data = nfs_readdata_alloc(1);
2601da177e4SLinus Torvalds 		if (!data)
2611da177e4SLinus Torvalds 			goto out_bad;
2621da177e4SLinus Torvalds 		list_add(&data->pages, &list);
2631da177e4SLinus Torvalds 		requests++;
264e9f7bee1STrond Myklebust 		nbytes -= len;
265e9f7bee1STrond Myklebust 	} while(nbytes != 0);
2661da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	ClearPageError(page);
2691da177e4SLinus Torvalds 	offset = 0;
270bcb71bbaSTrond Myklebust 	nbytes = count;
2711da177e4SLinus Torvalds 	do {
272dbae4c73STrond Myklebust 		int ret2;
273dbae4c73STrond Myklebust 
2741da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_read_data, pages);
2751da177e4SLinus Torvalds 		list_del_init(&data->pages);
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 		data->pagevec[0] = page;
2781da177e4SLinus Torvalds 
279bcb71bbaSTrond Myklebust 		if (nbytes < rsize)
280bcb71bbaSTrond Myklebust 			rsize = nbytes;
281dbae4c73STrond Myklebust 		ret2 = nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
282ec06c096STrond Myklebust 				  rsize, offset);
283dbae4c73STrond Myklebust 		if (ret == 0)
284dbae4c73STrond Myklebust 			ret = ret2;
2851da177e4SLinus Torvalds 		offset += rsize;
2861da177e4SLinus Torvalds 		nbytes -= rsize;
2871da177e4SLinus Torvalds 	} while (nbytes != 0);
2881da177e4SLinus Torvalds 
289dbae4c73STrond Myklebust 	return ret;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds out_bad:
2921da177e4SLinus Torvalds 	while (!list_empty(&list)) {
2931da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_read_data, pages);
2941da177e4SLinus Torvalds 		list_del(&data->pages);
2951da177e4SLinus Torvalds 		nfs_readdata_free(data);
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 	SetPageError(page);
2981da177e4SLinus Torvalds 	nfs_readpage_release(req);
2991da177e4SLinus Torvalds 	return -ENOMEM;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3028d5658c9STrond Myklebust static int nfs_pagein_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	struct nfs_page		*req;
3051da177e4SLinus Torvalds 	struct page		**pages;
3061da177e4SLinus Torvalds 	struct nfs_read_data	*data;
307dbae4c73STrond Myklebust 	int ret = -ENOMEM;
3081da177e4SLinus Torvalds 
3098d5658c9STrond Myklebust 	data = nfs_readdata_alloc(npages);
3101da177e4SLinus Torvalds 	if (!data)
3111da177e4SLinus Torvalds 		goto out_bad;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	pages = data->pagevec;
3141da177e4SLinus Torvalds 	while (!list_empty(head)) {
3151da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
3161da177e4SLinus Torvalds 		nfs_list_remove_request(req);
3171da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
3181da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
3191da177e4SLinus Torvalds 		*pages++ = req->wb_page;
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
3221da177e4SLinus Torvalds 
323dbae4c73STrond Myklebust 	return nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
3241da177e4SLinus Torvalds out_bad:
3251da177e4SLinus Torvalds 	nfs_async_read_error(head);
326dbae4c73STrond Myklebust 	return ret;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds /*
3300b671301STrond Myklebust  * This is the callback from RPC telling us whether a reply was
3310b671301STrond Myklebust  * received or some error occurred (timeout or socket shutdown).
3320b671301STrond Myklebust  */
3330b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
3340b671301STrond Myklebust {
3350b671301STrond Myklebust 	int status;
3360b671301STrond Myklebust 
3373110ff80SHarvey Harrison 	dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
3380b671301STrond Myklebust 			task->tk_status);
3390b671301STrond Myklebust 
3400b671301STrond Myklebust 	status = NFS_PROTO(data->inode)->read_done(task, data);
3410b671301STrond Myklebust 	if (status != 0)
3420b671301STrond Myklebust 		return status;
3430b671301STrond Myklebust 
3440b671301STrond Myklebust 	nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
3450b671301STrond Myklebust 
3460b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
3473a10c30aSBenny Halevy 		set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags);
3480b671301STrond Myklebust 		nfs_mark_for_revalidate(data->inode);
3490b671301STrond Myklebust 	}
3500b671301STrond Myklebust 	return 0;
3510b671301STrond Myklebust }
3520b671301STrond Myklebust 
353fdd1e74cSTrond Myklebust static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
3540b671301STrond Myklebust {
3550b671301STrond Myklebust 	struct nfs_readargs *argp = &data->args;
3560b671301STrond Myklebust 	struct nfs_readres *resp = &data->res;
3570b671301STrond Myklebust 
3580b671301STrond Myklebust 	if (resp->eof || resp->count == argp->count)
359*d61e612aSTrond Myklebust 		return;
3600b671301STrond Myklebust 
3610b671301STrond Myklebust 	/* This is a short read! */
3620b671301STrond Myklebust 	nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
3630b671301STrond Myklebust 	/* Has the server at least made some progress? */
3640b671301STrond Myklebust 	if (resp->count == 0)
365*d61e612aSTrond Myklebust 		return;
3660b671301STrond Myklebust 
3670b671301STrond Myklebust 	/* Yes, so retry the read at the end of the data */
3680b671301STrond Myklebust 	argp->offset += resp->count;
3690b671301STrond Myklebust 	argp->pgbase += resp->count;
3700b671301STrond Myklebust 	argp->count -= resp->count;
371*d61e612aSTrond Myklebust 	nfs4_restart_rpc(task, NFS_SERVER(data->inode)->nfs_client);
3720b671301STrond Myklebust }
3730b671301STrond Myklebust 
3740b671301STrond Myklebust /*
3751da177e4SLinus Torvalds  * Handle a read reply that fills part of a page.
3761da177e4SLinus Torvalds  */
377ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
3781da177e4SLinus Torvalds {
379ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
3801da177e4SLinus Torvalds 
381ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
382ec06c096STrond Myklebust 		return;
383fdd1e74cSTrond Myklebust 	if (task->tk_status < 0)
3840b671301STrond Myklebust 		return;
385fdd1e74cSTrond Myklebust 
386fdd1e74cSTrond Myklebust 	nfs_readpage_truncate_uninitialised_page(data);
387fdd1e74cSTrond Myklebust 	nfs_readpage_retry(task, data);
3880b671301STrond Myklebust }
389fdd1e74cSTrond Myklebust 
390fdd1e74cSTrond Myklebust static void nfs_readpage_release_partial(void *calldata)
391fdd1e74cSTrond Myklebust {
392fdd1e74cSTrond Myklebust 	struct nfs_read_data *data = calldata;
393fdd1e74cSTrond Myklebust 	struct nfs_page *req = data->req;
394fdd1e74cSTrond Myklebust 	struct page *page = req->wb_page;
395fdd1e74cSTrond Myklebust 	int status = data->task.tk_status;
396fdd1e74cSTrond Myklebust 
397fdd1e74cSTrond Myklebust 	if (status < 0)
3980b671301STrond Myklebust 		SetPageError(page);
399fdd1e74cSTrond Myklebust 
4001da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete)) {
4011da177e4SLinus Torvalds 		if (!PageError(page))
4021da177e4SLinus Torvalds 			SetPageUptodate(page);
4031da177e4SLinus Torvalds 		nfs_readpage_release(req);
4041da177e4SLinus Torvalds 	}
405fdd1e74cSTrond Myklebust 	nfs_readdata_release(calldata);
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
408f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1)
409f11c88afSAndy Adamson void nfs_read_prepare(struct rpc_task *task, void *calldata)
410f11c88afSAndy Adamson {
411f11c88afSAndy Adamson 	struct nfs_read_data *data = calldata;
412f11c88afSAndy Adamson 
413f11c88afSAndy Adamson 	if (nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
414f11c88afSAndy Adamson 				&data->args.seq_args, &data->res.seq_res,
415f11c88afSAndy Adamson 				0, task))
416f11c88afSAndy Adamson 		return;
417f11c88afSAndy Adamson 	rpc_call_start(task);
418f11c88afSAndy Adamson }
419f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
420f11c88afSAndy Adamson 
421ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = {
422f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1)
423f11c88afSAndy Adamson 	.rpc_call_prepare = nfs_read_prepare,
424f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
425ec06c096STrond Myklebust 	.rpc_call_done = nfs_readpage_result_partial,
426fdd1e74cSTrond Myklebust 	.rpc_release = nfs_readpage_release_partial,
427ec06c096STrond Myklebust };
428ec06c096STrond Myklebust 
4291de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
4301de3fc12STrond Myklebust {
4311de3fc12STrond Myklebust 	unsigned int count = data->res.count;
4321de3fc12STrond Myklebust 	unsigned int base = data->args.pgbase;
4331de3fc12STrond Myklebust 	struct page **pages;
4341de3fc12STrond Myklebust 
43579558f36STrond Myklebust 	if (data->res.eof)
43679558f36STrond Myklebust 		count = data->args.count;
4371de3fc12STrond Myklebust 	if (unlikely(count == 0))
4381de3fc12STrond Myklebust 		return;
4391de3fc12STrond Myklebust 	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
4401de3fc12STrond Myklebust 	base &= ~PAGE_CACHE_MASK;
4411de3fc12STrond Myklebust 	count += base;
4421de3fc12STrond Myklebust 	for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
4431de3fc12STrond Myklebust 		SetPageUptodate(*pages);
4440b671301STrond Myklebust 	if (count == 0)
4450b671301STrond Myklebust 		return;
4460b671301STrond Myklebust 	/* Was this a short read? */
4470b671301STrond Myklebust 	if (data->res.eof || data->res.count == data->args.count)
4481de3fc12STrond Myklebust 		SetPageUptodate(*pages);
4491de3fc12STrond Myklebust }
4501de3fc12STrond Myklebust 
4511da177e4SLinus Torvalds /*
4521da177e4SLinus Torvalds  * This is the callback from RPC telling us whether a reply was
4531da177e4SLinus Torvalds  * received or some error occurred (timeout or socket shutdown).
4541da177e4SLinus Torvalds  */
455ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
4561da177e4SLinus Torvalds {
457ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
4581da177e4SLinus Torvalds 
4590b671301STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
4600b671301STrond Myklebust 		return;
461fdd1e74cSTrond Myklebust 	if (task->tk_status < 0)
462fdd1e74cSTrond Myklebust 		return;
4631de3fc12STrond Myklebust 	/*
4640b671301STrond Myklebust 	 * Note: nfs_readpage_retry may change the values of
4651de3fc12STrond Myklebust 	 * data->args. In the multi-page case, we therefore need
4660b671301STrond Myklebust 	 * to ensure that we call nfs_readpage_set_pages_uptodate()
4670b671301STrond Myklebust 	 * first.
4681de3fc12STrond Myklebust 	 */
4691de3fc12STrond Myklebust 	nfs_readpage_truncate_uninitialised_page(data);
4701de3fc12STrond Myklebust 	nfs_readpage_set_pages_uptodate(data);
471fdd1e74cSTrond Myklebust 	nfs_readpage_retry(task, data);
4720b671301STrond Myklebust }
473fdd1e74cSTrond Myklebust 
474fdd1e74cSTrond Myklebust static void nfs_readpage_release_full(void *calldata)
475fdd1e74cSTrond Myklebust {
476fdd1e74cSTrond Myklebust 	struct nfs_read_data *data = calldata;
477fdd1e74cSTrond Myklebust 
4781da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
4791da177e4SLinus Torvalds 		struct nfs_page *req = nfs_list_entry(data->pages.next);
4801da177e4SLinus Torvalds 
4811de3fc12STrond Myklebust 		nfs_list_remove_request(req);
4821da177e4SLinus Torvalds 		nfs_readpage_release(req);
4831da177e4SLinus Torvalds 	}
484fdd1e74cSTrond Myklebust 	nfs_readdata_release(calldata);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
487ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = {
488f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1)
489f11c88afSAndy Adamson 	.rpc_call_prepare = nfs_read_prepare,
490f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
491ec06c096STrond Myklebust 	.rpc_call_done = nfs_readpage_result_full,
492fdd1e74cSTrond Myklebust 	.rpc_release = nfs_readpage_release_full,
493ec06c096STrond Myklebust };
494ec06c096STrond Myklebust 
4951da177e4SLinus Torvalds /*
4961da177e4SLinus Torvalds  * Read a page over NFS.
4971da177e4SLinus Torvalds  * We read the page synchronously in the following case:
4981da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
4991da177e4SLinus Torvalds  *	previous async read operation failed.
5001da177e4SLinus Torvalds  */
5011da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page)
5021da177e4SLinus Torvalds {
5031da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
5041da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
5051da177e4SLinus Torvalds 	int		error;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
5081da177e4SLinus Torvalds 		page, PAGE_CACHE_SIZE, page->index);
50991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
51091d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_READPAGES, 1);
51191d5b470SChuck Lever 
5121da177e4SLinus Torvalds 	/*
5131da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
5141da177e4SLinus Torvalds 	 *
5151da177e4SLinus Torvalds 	 * NOTE! Because we own the page lock, there cannot
5161da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
5171da177e4SLinus Torvalds 	 * for this page (other pages can be written to).
5181da177e4SLinus Torvalds 	 */
5191da177e4SLinus Torvalds 	error = nfs_wb_page(inode, page);
5201da177e4SLinus Torvalds 	if (error)
521de05a0ccSTrond Myklebust 		goto out_unlock;
522de05a0ccSTrond Myklebust 	if (PageUptodate(page))
523de05a0ccSTrond Myklebust 		goto out_unlock;
5241da177e4SLinus Torvalds 
5255f004cf2STrond Myklebust 	error = -ESTALE;
5265f004cf2STrond Myklebust 	if (NFS_STALE(inode))
527de05a0ccSTrond Myklebust 		goto out_unlock;
5285f004cf2STrond Myklebust 
5291da177e4SLinus Torvalds 	if (file == NULL) {
530cf1308ffSTrond Myklebust 		error = -EBADF;
531d530838bSTrond Myklebust 		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
5321da177e4SLinus Torvalds 		if (ctx == NULL)
533de05a0ccSTrond Myklebust 			goto out_unlock;
5341da177e4SLinus Torvalds 	} else
535cd3758e3STrond Myklebust 		ctx = get_nfs_open_context(nfs_file_open_context(file));
5361da177e4SLinus Torvalds 
5379a9fc1c0SDavid Howells 	if (!IS_SYNC(inode)) {
5389a9fc1c0SDavid Howells 		error = nfs_readpage_from_fscache(ctx, inode, page);
5399a9fc1c0SDavid Howells 		if (error == 0)
5409a9fc1c0SDavid Howells 			goto out;
5419a9fc1c0SDavid Howells 	}
5429a9fc1c0SDavid Howells 
5438e0969f0STrond Myklebust 	error = nfs_readpage_async(ctx, inode, page);
5448e0969f0STrond Myklebust 
5459a9fc1c0SDavid Howells out:
5461da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
5471da177e4SLinus Torvalds 	return error;
548de05a0ccSTrond Myklebust out_unlock:
5491da177e4SLinus Torvalds 	unlock_page(page);
5501da177e4SLinus Torvalds 	return error;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds struct nfs_readdesc {
5548b09bee3STrond Myklebust 	struct nfs_pageio_descriptor *pgio;
5551da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
5561da177e4SLinus Torvalds };
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds static int
5591da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page)
5601da177e4SLinus Torvalds {
5611da177e4SLinus Torvalds 	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
5621da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
5631da177e4SLinus Torvalds 	struct nfs_page *new;
5641da177e4SLinus Torvalds 	unsigned int len;
565de05a0ccSTrond Myklebust 	int error;
5661da177e4SLinus Torvalds 
56749a70f27STrond Myklebust 	len = nfs_page_length(page);
5681da177e4SLinus Torvalds 	if (len == 0)
5691da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
570de05a0ccSTrond Myklebust 
5711da177e4SLinus Torvalds 	new = nfs_create_request(desc->ctx, inode, page, 0, len);
572de05a0ccSTrond Myklebust 	if (IS_ERR(new))
573de05a0ccSTrond Myklebust 		goto out_error;
574de05a0ccSTrond Myklebust 
5751da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
576eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
577f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(desc->pgio, new)) {
578f8512ad0SFred Isaman 		error = desc->pgio->pg_error;
579f8512ad0SFred Isaman 		goto out_unlock;
580f8512ad0SFred Isaman 	}
5811da177e4SLinus Torvalds 	return 0;
582de05a0ccSTrond Myklebust out_error:
583de05a0ccSTrond Myklebust 	error = PTR_ERR(new);
584de05a0ccSTrond Myklebust 	SetPageError(page);
585de05a0ccSTrond Myklebust out_unlock:
586de05a0ccSTrond Myklebust 	unlock_page(page);
587de05a0ccSTrond Myklebust 	return error;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping,
5911da177e4SLinus Torvalds 		struct list_head *pages, unsigned nr_pages)
5921da177e4SLinus Torvalds {
5938b09bee3STrond Myklebust 	struct nfs_pageio_descriptor pgio;
5941da177e4SLinus Torvalds 	struct nfs_readdesc desc = {
5958b09bee3STrond Myklebust 		.pgio = &pgio,
5961da177e4SLinus Torvalds 	};
5971da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
5981da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
5998b09bee3STrond Myklebust 	size_t rsize = server->rsize;
6008b09bee3STrond Myklebust 	unsigned long npages;
6015f004cf2STrond Myklebust 	int ret = -ESTALE;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
6041da177e4SLinus Torvalds 			inode->i_sb->s_id,
6051da177e4SLinus Torvalds 			(long long)NFS_FILEID(inode),
6061da177e4SLinus Torvalds 			nr_pages);
60791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
6081da177e4SLinus Torvalds 
6095f004cf2STrond Myklebust 	if (NFS_STALE(inode))
6105f004cf2STrond Myklebust 		goto out;
6115f004cf2STrond Myklebust 
6121da177e4SLinus Torvalds 	if (filp == NULL) {
613d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
6141da177e4SLinus Torvalds 		if (desc.ctx == NULL)
6151da177e4SLinus Torvalds 			return -EBADF;
6161da177e4SLinus Torvalds 	} else
617cd3758e3STrond Myklebust 		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
6189a9fc1c0SDavid Howells 
6199a9fc1c0SDavid Howells 	/* attempt to read as many of the pages as possible from the cache
6209a9fc1c0SDavid Howells 	 * - this returns -ENOBUFS immediately if the cookie is negative
6219a9fc1c0SDavid Howells 	 */
6229a9fc1c0SDavid Howells 	ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
6239a9fc1c0SDavid Howells 					 pages, &nr_pages);
6249a9fc1c0SDavid Howells 	if (ret == 0)
6259a9fc1c0SDavid Howells 		goto read_complete; /* all pages were read */
6269a9fc1c0SDavid Howells 
6278b09bee3STrond Myklebust 	if (rsize < PAGE_CACHE_SIZE)
6288b09bee3STrond Myklebust 		nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0);
6298b09bee3STrond Myklebust 	else
6308b09bee3STrond Myklebust 		nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0);
6318b09bee3STrond Myklebust 
6321da177e4SLinus Torvalds 	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
6338b09bee3STrond Myklebust 
6348b09bee3STrond Myklebust 	nfs_pageio_complete(&pgio);
6358b09bee3STrond Myklebust 	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
6368b09bee3STrond Myklebust 	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
6379a9fc1c0SDavid Howells read_complete:
6381da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
6395f004cf2STrond Myklebust out:
6401da177e4SLinus Torvalds 	return ret;
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds 
643f7b422b1SDavid Howells int __init nfs_init_readpagecache(void)
6441da177e4SLinus Torvalds {
6451da177e4SLinus Torvalds 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
6461da177e4SLinus Torvalds 					     sizeof(struct nfs_read_data),
6471da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
64820c2df83SPaul Mundt 					     NULL);
6491da177e4SLinus Torvalds 	if (nfs_rdata_cachep == NULL)
6501da177e4SLinus Torvalds 		return -ENOMEM;
6511da177e4SLinus Torvalds 
65293d2341cSMatthew Dobson 	nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
6531da177e4SLinus Torvalds 						     nfs_rdata_cachep);
6541da177e4SLinus Torvalds 	if (nfs_rdata_mempool == NULL)
6551da177e4SLinus Torvalds 		return -ENOMEM;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	return 0;
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
660266bee88SDavid Brownell void nfs_destroy_readpagecache(void)
6611da177e4SLinus Torvalds {
6621da177e4SLinus Torvalds 	mempool_destroy(nfs_rdata_mempool);
6631a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_rdata_cachep);
6641da177e4SLinus Torvalds }
665