xref: /openbmc/linux/fs/nfs/read.c (revision 1f9453578f059d2651aa6c6b16756627fc9f2a74)
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 
231da177e4SLinus Torvalds #include <asm/system.h>
24bae724efSFred Isaman #include "pnfs.h"
251da177e4SLinus Torvalds 
26f11c88afSAndy Adamson #include "nfs4_fs.h"
2749a70f27STrond Myklebust #include "internal.h"
2891d5b470SChuck Lever #include "iostat.h"
299a9fc1c0SDavid Howells #include "fscache.h"
3091d5b470SChuck Lever 
311da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
321da177e4SLinus Torvalds 
331751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops;
34ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops;
35ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops;
361da177e4SLinus Torvalds 
37e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep;
383feb2d49STrond Myklebust static mempool_t *nfs_rdata_mempool;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define MIN_POOL_READ	(32)
411da177e4SLinus Torvalds 
428d5658c9STrond Myklebust struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount)
433feb2d49STrond Myklebust {
4493870d76STrond Myklebust 	struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_KERNEL);
453feb2d49STrond Myklebust 
463feb2d49STrond Myklebust 	if (p) {
473feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
483feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
49e9f7bee1STrond Myklebust 		p->npages = pagecount;
500d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
510d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
523feb2d49STrond Myklebust 		else {
5393870d76STrond Myklebust 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
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 
70493292ddSTrond Myklebust void nfs_readdata_release(struct nfs_read_data *rdata)
711da177e4SLinus Torvalds {
72bae724efSFred Isaman 	put_lseg(rdata->lseg);
73383ba719STrond Myklebust 	put_nfs_open_context(rdata->args.context);
74383ba719STrond Myklebust 	nfs_readdata_free(rdata);
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds static
781da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page)
791da177e4SLinus Torvalds {
80eebd2aa3SChristoph Lameter 	zero_user(page, 0, PAGE_CACHE_SIZE);
811da177e4SLinus Torvalds 	SetPageUptodate(page);
821da177e4SLinus Torvalds 	unlock_page(page);
831da177e4SLinus Torvalds 	return 0;
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
861de3fc12STrond Myklebust static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
871de3fc12STrond Myklebust {
881de3fc12STrond Myklebust 	unsigned int remainder = data->args.count - data->res.count;
891de3fc12STrond Myklebust 	unsigned int base = data->args.pgbase + data->res.count;
901de3fc12STrond Myklebust 	unsigned int pglen;
911de3fc12STrond Myklebust 	struct page **pages;
921de3fc12STrond Myklebust 
931de3fc12STrond Myklebust 	if (data->res.eof == 0 || remainder == 0)
941de3fc12STrond Myklebust 		return;
951de3fc12STrond Myklebust 	/*
961de3fc12STrond Myklebust 	 * Note: "remainder" can never be negative, since we check for
971de3fc12STrond Myklebust 	 * 	this in the XDR code.
981de3fc12STrond Myklebust 	 */
991de3fc12STrond Myklebust 	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
1001de3fc12STrond Myklebust 	base &= ~PAGE_CACHE_MASK;
1011de3fc12STrond Myklebust 	pglen = PAGE_CACHE_SIZE - base;
10279558f36STrond Myklebust 	for (;;) {
10379558f36STrond Myklebust 		if (remainder <= pglen) {
104eebd2aa3SChristoph Lameter 			zero_user(*pages, base, remainder);
10579558f36STrond Myklebust 			break;
10679558f36STrond Myklebust 		}
107eebd2aa3SChristoph Lameter 		zero_user(*pages, base, pglen);
10879558f36STrond Myklebust 		pages++;
10979558f36STrond Myklebust 		remainder -= pglen;
11079558f36STrond Myklebust 		pglen = PAGE_CACHE_SIZE;
11179558f36STrond Myklebust 		base = 0;
11279558f36STrond Myklebust 	}
1131de3fc12STrond Myklebust }
1141de3fc12STrond Myklebust 
115*1f945357STrond Myklebust static void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor *pgio,
1161751c363STrond Myklebust 		struct inode *inode)
1171751c363STrond Myklebust {
1181751c363STrond Myklebust 	nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops,
1191751c363STrond Myklebust 			NFS_SERVER(inode)->rsize, 0);
1201751c363STrond Myklebust }
1211751c363STrond Myklebust 
122493292ddSTrond Myklebust void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
123493292ddSTrond Myklebust {
124493292ddSTrond Myklebust 	pgio->pg_ops = &nfs_pageio_read_ops;
125493292ddSTrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
126493292ddSTrond Myklebust }
127*1f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
128493292ddSTrond Myklebust 
1291751c363STrond Myklebust static void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
1301751c363STrond Myklebust 		struct inode *inode)
1311751c363STrond Myklebust {
1321751c363STrond Myklebust 	if (!pnfs_pageio_init_read(pgio, inode))
1331751c363STrond Myklebust 		nfs_pageio_init_read_mds(pgio, inode);
1341751c363STrond Myklebust }
1351751c363STrond Myklebust 
136f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
1371da177e4SLinus Torvalds 		       struct page *page)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	struct nfs_page	*new;
1401da177e4SLinus Torvalds 	unsigned int len;
141c76069bdSFred Isaman 	struct nfs_pageio_descriptor pgio;
1421da177e4SLinus Torvalds 
14349a70f27STrond Myklebust 	len = nfs_page_length(page);
1441da177e4SLinus Torvalds 	if (len == 0)
1451da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
1461da177e4SLinus Torvalds 	new = nfs_create_request(ctx, inode, page, 0, len);
1471da177e4SLinus Torvalds 	if (IS_ERR(new)) {
1481da177e4SLinus Torvalds 		unlock_page(page);
1491da177e4SLinus Torvalds 		return PTR_ERR(new);
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
152eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
1531da177e4SLinus Torvalds 
1541751c363STrond Myklebust 	nfs_pageio_init_read(&pgio, inode);
155d8007d4dSTrond Myklebust 	nfs_pageio_add_request(&pgio, new);
1561751c363STrond Myklebust 	nfs_pageio_complete(&pgio);
1571da177e4SLinus Torvalds 	return 0;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req)
1611da177e4SLinus Torvalds {
1627f8e05f6SDavid Howells 	struct inode *d_inode = req->wb_context->path.dentry->d_inode;
1637f8e05f6SDavid Howells 
1647f8e05f6SDavid Howells 	if (PageUptodate(req->wb_page))
1657f8e05f6SDavid Howells 		nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
1667f8e05f6SDavid Howells 
1671da177e4SLinus Torvalds 	unlock_page(req->wb_page);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
17088be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
17188be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1721da177e4SLinus Torvalds 			req->wb_bytes,
1731da177e4SLinus Torvalds 			(long long)req_offset(req));
17410d2c46fSNick Wilson 	nfs_release_request(req);
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
177dc70d7b3SAndy Adamson int nfs_initiate_read(struct nfs_read_data *data, struct rpc_clnt *clnt,
17864419a9bSAndy Adamson 		      const struct rpc_call_ops *call_ops)
17964419a9bSAndy Adamson {
18064419a9bSAndy Adamson 	struct inode *inode = data->inode;
18164419a9bSAndy Adamson 	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
18264419a9bSAndy Adamson 	struct rpc_task *task;
18364419a9bSAndy Adamson 	struct rpc_message msg = {
18464419a9bSAndy Adamson 		.rpc_argp = &data->args,
18564419a9bSAndy Adamson 		.rpc_resp = &data->res,
18664419a9bSAndy Adamson 		.rpc_cred = data->cred,
18764419a9bSAndy Adamson 	};
18864419a9bSAndy Adamson 	struct rpc_task_setup task_setup_data = {
18964419a9bSAndy Adamson 		.task = &data->task,
19064419a9bSAndy Adamson 		.rpc_client = clnt,
19164419a9bSAndy Adamson 		.rpc_message = &msg,
19264419a9bSAndy Adamson 		.callback_ops = call_ops,
19364419a9bSAndy Adamson 		.callback_data = data,
19464419a9bSAndy Adamson 		.workqueue = nfsiod_workqueue,
19564419a9bSAndy Adamson 		.flags = RPC_TASK_ASYNC | swap_flags,
19664419a9bSAndy Adamson 	};
19764419a9bSAndy Adamson 
19864419a9bSAndy Adamson 	/* Set up the initial task struct. */
19964419a9bSAndy Adamson 	NFS_PROTO(inode)->read_setup(data, &msg);
20064419a9bSAndy Adamson 
20164419a9bSAndy Adamson 	dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
20264419a9bSAndy Adamson 			"offset %llu)\n",
20364419a9bSAndy Adamson 			data->task.tk_pid,
20464419a9bSAndy Adamson 			inode->i_sb->s_id,
20564419a9bSAndy Adamson 			(long long)NFS_FILEID(inode),
20664419a9bSAndy Adamson 			data->args.count,
20764419a9bSAndy Adamson 			(unsigned long long)data->args.offset);
20864419a9bSAndy Adamson 
20964419a9bSAndy Adamson 	task = rpc_run_task(&task_setup_data);
21064419a9bSAndy Adamson 	if (IS_ERR(task))
21164419a9bSAndy Adamson 		return PTR_ERR(task);
21264419a9bSAndy Adamson 	rpc_put_task(task);
21364419a9bSAndy Adamson 	return 0;
21464419a9bSAndy Adamson }
215dc70d7b3SAndy Adamson EXPORT_SYMBOL_GPL(nfs_initiate_read);
21664419a9bSAndy Adamson 
2171da177e4SLinus Torvalds /*
2181da177e4SLinus Torvalds  * Set up the NFS read request struct
2191da177e4SLinus Torvalds  */
2206e4efd56STrond Myklebust static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
2216e4efd56STrond Myklebust 		unsigned int count, unsigned int offset)
2221da177e4SLinus Torvalds {
22384115e1cSTrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	data->req	  = req;
22684115e1cSTrond Myklebust 	data->inode	  = inode;
22764419a9bSAndy Adamson 	data->cred	  = req->wb_context->cred;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
2301da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
2311da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
2321da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
2331da177e4SLinus Torvalds 	data->args.count  = count;
234383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
235f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
2381da177e4SLinus Torvalds 	data->res.count   = count;
2391da177e4SLinus Torvalds 	data->res.eof     = 0;
2400e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
2416e4efd56STrond Myklebust }
2421da177e4SLinus Torvalds 
2436e4efd56STrond Myklebust static int nfs_do_read(struct nfs_read_data *data,
244493292ddSTrond Myklebust 		const struct rpc_call_ops *call_ops)
2456e4efd56STrond Myklebust {
2466e4efd56STrond Myklebust 	struct inode *inode = data->args.context->path.dentry->d_inode;
2476e4efd56STrond Myklebust 
24864419a9bSAndy Adamson 	return nfs_initiate_read(data, NFS_CLIENT(inode), call_ops);
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
251275acaafSTrond Myklebust static int
252275acaafSTrond Myklebust nfs_do_multiple_reads(struct list_head *head,
253493292ddSTrond Myklebust 		const struct rpc_call_ops *call_ops)
254275acaafSTrond Myklebust {
255275acaafSTrond Myklebust 	struct nfs_read_data *data;
256275acaafSTrond Myklebust 	int ret = 0;
257275acaafSTrond Myklebust 
258275acaafSTrond Myklebust 	while (!list_empty(head)) {
259275acaafSTrond Myklebust 		int ret2;
260275acaafSTrond Myklebust 
261275acaafSTrond Myklebust 		data = list_entry(head->next, struct nfs_read_data, list);
262275acaafSTrond Myklebust 		list_del_init(&data->list);
263275acaafSTrond Myklebust 
264493292ddSTrond Myklebust 		ret2 = nfs_do_read(data, call_ops);
265275acaafSTrond Myklebust 		if (ret == 0)
266275acaafSTrond Myklebust 			ret = ret2;
267275acaafSTrond Myklebust 	}
268275acaafSTrond Myklebust 	return ret;
269275acaafSTrond Myklebust }
270275acaafSTrond Myklebust 
2711da177e4SLinus Torvalds static void
2721da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	struct nfs_page	*req;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	while (!list_empty(head)) {
2771da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
2781da177e4SLinus Torvalds 		nfs_list_remove_request(req);
2791da177e4SLinus Torvalds 		SetPageError(req->wb_page);
2801da177e4SLinus Torvalds 		nfs_readpage_release(req);
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /*
2851da177e4SLinus Torvalds  * Generate multiple requests to fill a single page.
2861da177e4SLinus Torvalds  *
2871da177e4SLinus Torvalds  * We optimize to reduce the number of read operations on the wire.  If we
2881da177e4SLinus Torvalds  * detect that we're reading a page, or an area of a page, that is past the
2891da177e4SLinus Torvalds  * end of file, we do not generate NFS read operations but just clear the
2901da177e4SLinus Torvalds  * parts of the page that would have come back zero from the server anyway.
2911da177e4SLinus Torvalds  *
2921da177e4SLinus Torvalds  * We rely on the cached value of i_size to make this determination; another
2931da177e4SLinus Torvalds  * client can fill pages on the server past our cached end-of-file, but we
2941da177e4SLinus Torvalds  * won't see the new data until our attribute cache is updated.  This is more
2951da177e4SLinus Torvalds  * or less conventional NFS client behavior.
2961da177e4SLinus Torvalds  */
297275acaafSTrond Myklebust static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc, struct list_head *res)
2981da177e4SLinus Torvalds {
299c76069bdSFred Isaman 	struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
3001da177e4SLinus Torvalds 	struct page *page = req->wb_page;
3011da177e4SLinus Torvalds 	struct nfs_read_data *data;
302d097971dSTrond Myklebust 	size_t rsize = desc->pg_bsize, nbytes;
303e9f7bee1STrond Myklebust 	unsigned int offset;
3041da177e4SLinus Torvalds 	int requests = 0;
305dbae4c73STrond Myklebust 	int ret = 0;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	nfs_list_remove_request(req);
3081da177e4SLinus Torvalds 
309275acaafSTrond Myklebust 	offset = 0;
310c76069bdSFred Isaman 	nbytes = desc->pg_count;
311e9f7bee1STrond Myklebust 	do {
312e9f7bee1STrond Myklebust 		size_t len = min(nbytes,rsize);
313e9f7bee1STrond Myklebust 
3148d5658c9STrond Myklebust 		data = nfs_readdata_alloc(1);
3151da177e4SLinus Torvalds 		if (!data)
3161da177e4SLinus Torvalds 			goto out_bad;
317275acaafSTrond Myklebust 		data->pagevec[0] = page;
318275acaafSTrond Myklebust 		nfs_read_rpcsetup(req, data, len, offset);
319275acaafSTrond Myklebust 		list_add(&data->list, res);
3201da177e4SLinus Torvalds 		requests++;
321e9f7bee1STrond Myklebust 		nbytes -= len;
322275acaafSTrond Myklebust 		offset += len;
323e9f7bee1STrond Myklebust 	} while(nbytes != 0);
3241da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
3251da177e4SLinus Torvalds 	ClearPageError(page);
32650828d7eSTrond Myklebust 	desc->pg_rpc_callops = &nfs_read_partial_ops;
327dbae4c73STrond Myklebust 	return ret;
3281da177e4SLinus Torvalds out_bad:
329275acaafSTrond Myklebust 	while (!list_empty(res)) {
330275acaafSTrond Myklebust 		data = list_entry(res->next, struct nfs_read_data, list);
3316e4efd56STrond Myklebust 		list_del(&data->list);
3321da177e4SLinus Torvalds 		nfs_readdata_free(data);
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 	SetPageError(page);
3351da177e4SLinus Torvalds 	nfs_readpage_release(req);
3361da177e4SLinus Torvalds 	return -ENOMEM;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
339275acaafSTrond Myklebust static int nfs_pagein_one(struct nfs_pageio_descriptor *desc, struct list_head *res)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	struct nfs_page		*req;
3421da177e4SLinus Torvalds 	struct page		**pages;
3431da177e4SLinus Torvalds 	struct nfs_read_data	*data;
344c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
3453b609184SPeng Tao 	int ret = 0;
3461da177e4SLinus Torvalds 
347c76069bdSFred Isaman 	data = nfs_readdata_alloc(nfs_page_array_len(desc->pg_base,
348c76069bdSFred Isaman 						     desc->pg_count));
349bae724efSFred Isaman 	if (!data) {
350bae724efSFred Isaman 		nfs_async_read_error(head);
3513b609184SPeng Tao 		ret = -ENOMEM;
352bae724efSFred Isaman 		goto out;
353bae724efSFred Isaman 	}
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	pages = data->pagevec;
3561da177e4SLinus Torvalds 	while (!list_empty(head)) {
3571da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
3581da177e4SLinus Torvalds 		nfs_list_remove_request(req);
3591da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
3601da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
3611da177e4SLinus Torvalds 		*pages++ = req->wb_page;
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
3641da177e4SLinus Torvalds 
3656e4efd56STrond Myklebust 	nfs_read_rpcsetup(req, data, desc->pg_count, 0);
366275acaafSTrond Myklebust 	list_add(&data->list, res);
36750828d7eSTrond Myklebust 	desc->pg_rpc_callops = &nfs_read_full_ops;
368bae724efSFred Isaman out:
369dbae4c73STrond Myklebust 	return ret;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
372493292ddSTrond Myklebust int nfs_generic_pagein(struct nfs_pageio_descriptor *desc, struct list_head *head)
373493292ddSTrond Myklebust {
374493292ddSTrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
375493292ddSTrond Myklebust 		return nfs_pagein_multi(desc, head);
376493292ddSTrond Myklebust 	return nfs_pagein_one(desc, head);
377493292ddSTrond Myklebust }
378493292ddSTrond Myklebust 
379493292ddSTrond Myklebust static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
3801751c363STrond Myklebust {
381275acaafSTrond Myklebust 	LIST_HEAD(head);
382275acaafSTrond Myklebust 	int ret;
383275acaafSTrond Myklebust 
384493292ddSTrond Myklebust 	ret = nfs_generic_pagein(desc, &head);
385275acaafSTrond Myklebust 	if (ret == 0)
386493292ddSTrond Myklebust 		ret = nfs_do_multiple_reads(&head, desc->pg_rpc_callops);
387275acaafSTrond Myklebust 	return ret;
3881751c363STrond Myklebust }
3891751c363STrond Myklebust 
3901751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_read_ops = {
3911751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
3921751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_readpages,
3931751c363STrond Myklebust };
3941751c363STrond Myklebust 
3951da177e4SLinus Torvalds /*
3960b671301STrond Myklebust  * This is the callback from RPC telling us whether a reply was
3970b671301STrond Myklebust  * received or some error occurred (timeout or socket shutdown).
3980b671301STrond Myklebust  */
3990b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
4000b671301STrond Myklebust {
4010b671301STrond Myklebust 	int status;
4020b671301STrond Myklebust 
4033110ff80SHarvey Harrison 	dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
4040b671301STrond Myklebust 			task->tk_status);
4050b671301STrond Myklebust 
4060b671301STrond Myklebust 	status = NFS_PROTO(data->inode)->read_done(task, data);
4070b671301STrond Myklebust 	if (status != 0)
4080b671301STrond Myklebust 		return status;
4090b671301STrond Myklebust 
4100b671301STrond Myklebust 	nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
4110b671301STrond Myklebust 
4120b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
4133a10c30aSBenny Halevy 		set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags);
4140b671301STrond Myklebust 		nfs_mark_for_revalidate(data->inode);
4150b671301STrond Myklebust 	}
4160b671301STrond Myklebust 	return 0;
4170b671301STrond Myklebust }
4180b671301STrond Myklebust 
419fdd1e74cSTrond Myklebust static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
4200b671301STrond Myklebust {
4210b671301STrond Myklebust 	struct nfs_readargs *argp = &data->args;
4220b671301STrond Myklebust 	struct nfs_readres *resp = &data->res;
4230b671301STrond Myklebust 
4240b671301STrond Myklebust 	if (resp->eof || resp->count == argp->count)
425d61e612aSTrond Myklebust 		return;
4260b671301STrond Myklebust 
4270b671301STrond Myklebust 	/* This is a short read! */
4280b671301STrond Myklebust 	nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
4290b671301STrond Myklebust 	/* Has the server at least made some progress? */
4300b671301STrond Myklebust 	if (resp->count == 0)
431d61e612aSTrond Myklebust 		return;
4320b671301STrond Myklebust 
4330b671301STrond Myklebust 	/* Yes, so retry the read at the end of the data */
434cbdabc7fSAndy Adamson 	data->mds_offset += resp->count;
4350b671301STrond Myklebust 	argp->offset += resp->count;
4360b671301STrond Myklebust 	argp->pgbase += resp->count;
4370b671301STrond Myklebust 	argp->count -= resp->count;
4380110ee15STrond Myklebust 	nfs_restart_rpc(task, NFS_SERVER(data->inode)->nfs_client);
4390b671301STrond Myklebust }
4400b671301STrond Myklebust 
4410b671301STrond Myklebust /*
4421da177e4SLinus Torvalds  * Handle a read reply that fills part of a page.
4431da177e4SLinus Torvalds  */
444ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
4451da177e4SLinus Torvalds {
446ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
4471da177e4SLinus Torvalds 
448ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
449ec06c096STrond Myklebust 		return;
450fdd1e74cSTrond Myklebust 	if (task->tk_status < 0)
4510b671301STrond Myklebust 		return;
452fdd1e74cSTrond Myklebust 
453fdd1e74cSTrond Myklebust 	nfs_readpage_truncate_uninitialised_page(data);
454fdd1e74cSTrond Myklebust 	nfs_readpage_retry(task, data);
4550b671301STrond Myklebust }
456fdd1e74cSTrond Myklebust 
457fdd1e74cSTrond Myklebust static void nfs_readpage_release_partial(void *calldata)
458fdd1e74cSTrond Myklebust {
459fdd1e74cSTrond Myklebust 	struct nfs_read_data *data = calldata;
460fdd1e74cSTrond Myklebust 	struct nfs_page *req = data->req;
461fdd1e74cSTrond Myklebust 	struct page *page = req->wb_page;
462fdd1e74cSTrond Myklebust 	int status = data->task.tk_status;
463fdd1e74cSTrond Myklebust 
464fdd1e74cSTrond Myklebust 	if (status < 0)
4650b671301STrond Myklebust 		SetPageError(page);
466fdd1e74cSTrond Myklebust 
4671da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete)) {
4681da177e4SLinus Torvalds 		if (!PageError(page))
4691da177e4SLinus Torvalds 			SetPageUptodate(page);
4701da177e4SLinus Torvalds 		nfs_readpage_release(req);
4711da177e4SLinus Torvalds 	}
472fdd1e74cSTrond Myklebust 	nfs_readdata_release(calldata);
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
475f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1)
476f11c88afSAndy Adamson void nfs_read_prepare(struct rpc_task *task, void *calldata)
477f11c88afSAndy Adamson {
478f11c88afSAndy Adamson 	struct nfs_read_data *data = calldata;
479f11c88afSAndy Adamson 
480035168abSTrond Myklebust 	if (nfs4_setup_sequence(NFS_SERVER(data->inode),
481f11c88afSAndy Adamson 				&data->args.seq_args, &data->res.seq_res,
482f11c88afSAndy Adamson 				0, task))
483f11c88afSAndy Adamson 		return;
484f11c88afSAndy Adamson 	rpc_call_start(task);
485f11c88afSAndy Adamson }
486f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
487f11c88afSAndy Adamson 
488ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = {
489f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1)
490f11c88afSAndy Adamson 	.rpc_call_prepare = nfs_read_prepare,
491f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
492ec06c096STrond Myklebust 	.rpc_call_done = nfs_readpage_result_partial,
493fdd1e74cSTrond Myklebust 	.rpc_release = nfs_readpage_release_partial,
494ec06c096STrond Myklebust };
495ec06c096STrond Myklebust 
4961de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
4971de3fc12STrond Myklebust {
4981de3fc12STrond Myklebust 	unsigned int count = data->res.count;
4991de3fc12STrond Myklebust 	unsigned int base = data->args.pgbase;
5001de3fc12STrond Myklebust 	struct page **pages;
5011de3fc12STrond Myklebust 
50279558f36STrond Myklebust 	if (data->res.eof)
50379558f36STrond Myklebust 		count = data->args.count;
5041de3fc12STrond Myklebust 	if (unlikely(count == 0))
5051de3fc12STrond Myklebust 		return;
5061de3fc12STrond Myklebust 	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
5071de3fc12STrond Myklebust 	base &= ~PAGE_CACHE_MASK;
5081de3fc12STrond Myklebust 	count += base;
5091de3fc12STrond Myklebust 	for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
5101de3fc12STrond Myklebust 		SetPageUptodate(*pages);
5110b671301STrond Myklebust 	if (count == 0)
5120b671301STrond Myklebust 		return;
5130b671301STrond Myklebust 	/* Was this a short read? */
5140b671301STrond Myklebust 	if (data->res.eof || data->res.count == data->args.count)
5151de3fc12STrond Myklebust 		SetPageUptodate(*pages);
5161de3fc12STrond Myklebust }
5171de3fc12STrond Myklebust 
5181da177e4SLinus Torvalds /*
5191da177e4SLinus Torvalds  * This is the callback from RPC telling us whether a reply was
5201da177e4SLinus Torvalds  * received or some error occurred (timeout or socket shutdown).
5211da177e4SLinus Torvalds  */
522ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
5231da177e4SLinus Torvalds {
524ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
5251da177e4SLinus Torvalds 
5260b671301STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
5270b671301STrond Myklebust 		return;
528fdd1e74cSTrond Myklebust 	if (task->tk_status < 0)
529fdd1e74cSTrond Myklebust 		return;
5301de3fc12STrond Myklebust 	/*
5310b671301STrond Myklebust 	 * Note: nfs_readpage_retry may change the values of
5321de3fc12STrond Myklebust 	 * data->args. In the multi-page case, we therefore need
5330b671301STrond Myklebust 	 * to ensure that we call nfs_readpage_set_pages_uptodate()
5340b671301STrond Myklebust 	 * first.
5351de3fc12STrond Myklebust 	 */
5361de3fc12STrond Myklebust 	nfs_readpage_truncate_uninitialised_page(data);
5371de3fc12STrond Myklebust 	nfs_readpage_set_pages_uptodate(data);
538fdd1e74cSTrond Myklebust 	nfs_readpage_retry(task, data);
5390b671301STrond Myklebust }
540fdd1e74cSTrond Myklebust 
541fdd1e74cSTrond Myklebust static void nfs_readpage_release_full(void *calldata)
542fdd1e74cSTrond Myklebust {
543fdd1e74cSTrond Myklebust 	struct nfs_read_data *data = calldata;
544fdd1e74cSTrond Myklebust 
5451da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
5461da177e4SLinus Torvalds 		struct nfs_page *req = nfs_list_entry(data->pages.next);
5471da177e4SLinus Torvalds 
5481de3fc12STrond Myklebust 		nfs_list_remove_request(req);
5491da177e4SLinus Torvalds 		nfs_readpage_release(req);
5501da177e4SLinus Torvalds 	}
551fdd1e74cSTrond Myklebust 	nfs_readdata_release(calldata);
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
554ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = {
555f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1)
556f11c88afSAndy Adamson 	.rpc_call_prepare = nfs_read_prepare,
557f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
558ec06c096STrond Myklebust 	.rpc_call_done = nfs_readpage_result_full,
559fdd1e74cSTrond Myklebust 	.rpc_release = nfs_readpage_release_full,
560ec06c096STrond Myklebust };
561ec06c096STrond Myklebust 
5621da177e4SLinus Torvalds /*
5631da177e4SLinus Torvalds  * Read a page over NFS.
5641da177e4SLinus Torvalds  * We read the page synchronously in the following case:
5651da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
5661da177e4SLinus Torvalds  *	previous async read operation failed.
5671da177e4SLinus Torvalds  */
5681da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page)
5691da177e4SLinus Torvalds {
5701da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
5711da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
5721da177e4SLinus Torvalds 	int		error;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
5751da177e4SLinus Torvalds 		page, PAGE_CACHE_SIZE, page->index);
57691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
57791d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_READPAGES, 1);
57891d5b470SChuck Lever 
5791da177e4SLinus Torvalds 	/*
5801da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
5811da177e4SLinus Torvalds 	 *
5821da177e4SLinus Torvalds 	 * NOTE! Because we own the page lock, there cannot
5831da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
5841da177e4SLinus Torvalds 	 * for this page (other pages can be written to).
5851da177e4SLinus Torvalds 	 */
5861da177e4SLinus Torvalds 	error = nfs_wb_page(inode, page);
5871da177e4SLinus Torvalds 	if (error)
588de05a0ccSTrond Myklebust 		goto out_unlock;
589de05a0ccSTrond Myklebust 	if (PageUptodate(page))
590de05a0ccSTrond Myklebust 		goto out_unlock;
5911da177e4SLinus Torvalds 
5925f004cf2STrond Myklebust 	error = -ESTALE;
5935f004cf2STrond Myklebust 	if (NFS_STALE(inode))
594de05a0ccSTrond Myklebust 		goto out_unlock;
5955f004cf2STrond Myklebust 
5961da177e4SLinus Torvalds 	if (file == NULL) {
597cf1308ffSTrond Myklebust 		error = -EBADF;
598d530838bSTrond Myklebust 		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
5991da177e4SLinus Torvalds 		if (ctx == NULL)
600de05a0ccSTrond Myklebust 			goto out_unlock;
6011da177e4SLinus Torvalds 	} else
602cd3758e3STrond Myklebust 		ctx = get_nfs_open_context(nfs_file_open_context(file));
6031da177e4SLinus Torvalds 
6049a9fc1c0SDavid Howells 	if (!IS_SYNC(inode)) {
6059a9fc1c0SDavid Howells 		error = nfs_readpage_from_fscache(ctx, inode, page);
6069a9fc1c0SDavid Howells 		if (error == 0)
6079a9fc1c0SDavid Howells 			goto out;
6089a9fc1c0SDavid Howells 	}
6099a9fc1c0SDavid Howells 
6108e0969f0STrond Myklebust 	error = nfs_readpage_async(ctx, inode, page);
6118e0969f0STrond Myklebust 
6129a9fc1c0SDavid Howells out:
6131da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
6141da177e4SLinus Torvalds 	return error;
615de05a0ccSTrond Myklebust out_unlock:
6161da177e4SLinus Torvalds 	unlock_page(page);
6171da177e4SLinus Torvalds 	return error;
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds struct nfs_readdesc {
6218b09bee3STrond Myklebust 	struct nfs_pageio_descriptor *pgio;
6221da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
6231da177e4SLinus Torvalds };
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds static int
6261da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page)
6271da177e4SLinus Torvalds {
6281da177e4SLinus Torvalds 	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
6291da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
6301da177e4SLinus Torvalds 	struct nfs_page *new;
6311da177e4SLinus Torvalds 	unsigned int len;
632de05a0ccSTrond Myklebust 	int error;
6331da177e4SLinus Torvalds 
63449a70f27STrond Myklebust 	len = nfs_page_length(page);
6351da177e4SLinus Torvalds 	if (len == 0)
6361da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
637de05a0ccSTrond Myklebust 
6381da177e4SLinus Torvalds 	new = nfs_create_request(desc->ctx, inode, page, 0, len);
639de05a0ccSTrond Myklebust 	if (IS_ERR(new))
640de05a0ccSTrond Myklebust 		goto out_error;
641de05a0ccSTrond Myklebust 
6421da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
643eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
644f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(desc->pgio, new)) {
645f8512ad0SFred Isaman 		error = desc->pgio->pg_error;
646f8512ad0SFred Isaman 		goto out_unlock;
647f8512ad0SFred Isaman 	}
6481da177e4SLinus Torvalds 	return 0;
649de05a0ccSTrond Myklebust out_error:
650de05a0ccSTrond Myklebust 	error = PTR_ERR(new);
651de05a0ccSTrond Myklebust 	SetPageError(page);
652de05a0ccSTrond Myklebust out_unlock:
653de05a0ccSTrond Myklebust 	unlock_page(page);
654de05a0ccSTrond Myklebust 	return error;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping,
6581da177e4SLinus Torvalds 		struct list_head *pages, unsigned nr_pages)
6591da177e4SLinus Torvalds {
6608b09bee3STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6611da177e4SLinus Torvalds 	struct nfs_readdesc desc = {
6628b09bee3STrond Myklebust 		.pgio = &pgio,
6631da177e4SLinus Torvalds 	};
6641da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
6658b09bee3STrond Myklebust 	unsigned long npages;
6665f004cf2STrond Myklebust 	int ret = -ESTALE;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
6691da177e4SLinus Torvalds 			inode->i_sb->s_id,
6701da177e4SLinus Torvalds 			(long long)NFS_FILEID(inode),
6711da177e4SLinus Torvalds 			nr_pages);
67291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
6731da177e4SLinus Torvalds 
6745f004cf2STrond Myklebust 	if (NFS_STALE(inode))
6755f004cf2STrond Myklebust 		goto out;
6765f004cf2STrond Myklebust 
6771da177e4SLinus Torvalds 	if (filp == NULL) {
678d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
6791da177e4SLinus Torvalds 		if (desc.ctx == NULL)
6801da177e4SLinus Torvalds 			return -EBADF;
6811da177e4SLinus Torvalds 	} else
682cd3758e3STrond Myklebust 		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
6839a9fc1c0SDavid Howells 
6849a9fc1c0SDavid Howells 	/* attempt to read as many of the pages as possible from the cache
6859a9fc1c0SDavid Howells 	 * - this returns -ENOBUFS immediately if the cookie is negative
6869a9fc1c0SDavid Howells 	 */
6879a9fc1c0SDavid Howells 	ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
6889a9fc1c0SDavid Howells 					 pages, &nr_pages);
6899a9fc1c0SDavid Howells 	if (ret == 0)
6909a9fc1c0SDavid Howells 		goto read_complete; /* all pages were read */
6919a9fc1c0SDavid Howells 
6921751c363STrond Myklebust 	nfs_pageio_init_read(&pgio, inode);
6938b09bee3STrond Myklebust 
6941da177e4SLinus Torvalds 	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
6958b09bee3STrond Myklebust 
6968b09bee3STrond Myklebust 	nfs_pageio_complete(&pgio);
6978b09bee3STrond Myklebust 	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
6988b09bee3STrond Myklebust 	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
6999a9fc1c0SDavid Howells read_complete:
7001da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
7015f004cf2STrond Myklebust out:
7021da177e4SLinus Torvalds 	return ret;
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
705f7b422b1SDavid Howells int __init nfs_init_readpagecache(void)
7061da177e4SLinus Torvalds {
7071da177e4SLinus Torvalds 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
7081da177e4SLinus Torvalds 					     sizeof(struct nfs_read_data),
7091da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
71020c2df83SPaul Mundt 					     NULL);
7111da177e4SLinus Torvalds 	if (nfs_rdata_cachep == NULL)
7121da177e4SLinus Torvalds 		return -ENOMEM;
7131da177e4SLinus Torvalds 
71493d2341cSMatthew Dobson 	nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
7151da177e4SLinus Torvalds 						     nfs_rdata_cachep);
7161da177e4SLinus Torvalds 	if (nfs_rdata_mempool == NULL)
7171da177e4SLinus Torvalds 		return -ENOMEM;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	return 0;
7201da177e4SLinus Torvalds }
7211da177e4SLinus Torvalds 
722266bee88SDavid Brownell void nfs_destroy_readpagecache(void)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	mempool_destroy(nfs_rdata_mempool);
7251a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_rdata_cachep);
7261da177e4SLinus Torvalds }
727