xref: /openbmc/linux/fs/nfs/read.c (revision 64419a9b20938d9070fdd8c58c2fa23c911915f8)
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>
21*64419a9bSAndy Adamson #include <linux/smp_lock.h>
22*64419a9bSAndy Adamson #include <linux/module.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <asm/system.h>
25bae724efSFred Isaman #include "pnfs.h"
261da177e4SLinus Torvalds 
27f11c88afSAndy Adamson #include "nfs4_fs.h"
2849a70f27STrond Myklebust #include "internal.h"
2991d5b470SChuck Lever #include "iostat.h"
309a9fc1c0SDavid Howells #include "fscache.h"
3191d5b470SChuck Lever 
321da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
331da177e4SLinus Torvalds 
34bae724efSFred Isaman static int nfs_pagein_multi(struct inode *, struct list_head *, unsigned int, size_t, int, struct pnfs_layout_segment *);
35bae724efSFred Isaman static int nfs_pagein_one(struct inode *, struct list_head *, unsigned int, size_t, int, struct pnfs_layout_segment *);
36ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops;
37ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops;
381da177e4SLinus Torvalds 
39e18b890bSChristoph Lameter static struct kmem_cache *nfs_rdata_cachep;
403feb2d49STrond Myklebust static mempool_t *nfs_rdata_mempool;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #define MIN_POOL_READ	(32)
431da177e4SLinus Torvalds 
448d5658c9STrond Myklebust struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount)
453feb2d49STrond Myklebust {
4693870d76STrond Myklebust 	struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_KERNEL);
473feb2d49STrond Myklebust 
483feb2d49STrond Myklebust 	if (p) {
493feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
503feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
51e9f7bee1STrond Myklebust 		p->npages = pagecount;
520d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
530d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
543feb2d49STrond Myklebust 		else {
5593870d76STrond Myklebust 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
560d0b5cb3SChuck Lever 			if (!p->pagevec) {
573feb2d49STrond Myklebust 				mempool_free(p, nfs_rdata_mempool);
583feb2d49STrond Myklebust 				p = NULL;
593feb2d49STrond Myklebust 			}
603feb2d49STrond Myklebust 		}
613feb2d49STrond Myklebust 	}
623feb2d49STrond Myklebust 	return p;
633feb2d49STrond Myklebust }
643feb2d49STrond Myklebust 
651ae88b2eSTrond Myklebust void nfs_readdata_free(struct nfs_read_data *p)
663feb2d49STrond Myklebust {
673feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
683feb2d49STrond Myklebust 		kfree(p->pagevec);
693feb2d49STrond Myklebust 	mempool_free(p, nfs_rdata_mempool);
703feb2d49STrond Myklebust }
713feb2d49STrond Myklebust 
721ae88b2eSTrond Myklebust static void nfs_readdata_release(struct nfs_read_data *rdata)
731da177e4SLinus Torvalds {
74bae724efSFred Isaman 	put_lseg(rdata->lseg);
75383ba719STrond Myklebust 	put_nfs_open_context(rdata->args.context);
76383ba719STrond Myklebust 	nfs_readdata_free(rdata);
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static
801da177e4SLinus Torvalds int nfs_return_empty_page(struct page *page)
811da177e4SLinus Torvalds {
82eebd2aa3SChristoph Lameter 	zero_user(page, 0, PAGE_CACHE_SIZE);
831da177e4SLinus Torvalds 	SetPageUptodate(page);
841da177e4SLinus Torvalds 	unlock_page(page);
851da177e4SLinus Torvalds 	return 0;
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
881de3fc12STrond Myklebust static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
891de3fc12STrond Myklebust {
901de3fc12STrond Myklebust 	unsigned int remainder = data->args.count - data->res.count;
911de3fc12STrond Myklebust 	unsigned int base = data->args.pgbase + data->res.count;
921de3fc12STrond Myklebust 	unsigned int pglen;
931de3fc12STrond Myklebust 	struct page **pages;
941de3fc12STrond Myklebust 
951de3fc12STrond Myklebust 	if (data->res.eof == 0 || remainder == 0)
961de3fc12STrond Myklebust 		return;
971de3fc12STrond Myklebust 	/*
981de3fc12STrond Myklebust 	 * Note: "remainder" can never be negative, since we check for
991de3fc12STrond Myklebust 	 * 	this in the XDR code.
1001de3fc12STrond Myklebust 	 */
1011de3fc12STrond Myklebust 	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
1021de3fc12STrond Myklebust 	base &= ~PAGE_CACHE_MASK;
1031de3fc12STrond Myklebust 	pglen = PAGE_CACHE_SIZE - base;
10479558f36STrond Myklebust 	for (;;) {
10579558f36STrond Myklebust 		if (remainder <= pglen) {
106eebd2aa3SChristoph Lameter 			zero_user(*pages, base, remainder);
10779558f36STrond Myklebust 			break;
10879558f36STrond Myklebust 		}
109eebd2aa3SChristoph Lameter 		zero_user(*pages, base, pglen);
11079558f36STrond Myklebust 		pages++;
11179558f36STrond Myklebust 		remainder -= pglen;
11279558f36STrond Myklebust 		pglen = PAGE_CACHE_SIZE;
11379558f36STrond Myklebust 		base = 0;
11479558f36STrond Myklebust 	}
1151de3fc12STrond Myklebust }
1161de3fc12STrond Myklebust 
117f42b293dSDavid Howells int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
1181da177e4SLinus Torvalds 		       struct page *page)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	LIST_HEAD(one_request);
1211da177e4SLinus Torvalds 	struct nfs_page	*new;
1221da177e4SLinus Torvalds 	unsigned int len;
1231da177e4SLinus Torvalds 
12449a70f27STrond Myklebust 	len = nfs_page_length(page);
1251da177e4SLinus Torvalds 	if (len == 0)
1261da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
1271da177e4SLinus Torvalds 	new = nfs_create_request(ctx, inode, page, 0, len);
1281da177e4SLinus Torvalds 	if (IS_ERR(new)) {
1291da177e4SLinus Torvalds 		unlock_page(page);
1301da177e4SLinus Torvalds 		return PTR_ERR(new);
1311da177e4SLinus Torvalds 	}
1321da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
133eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	nfs_list_add_request(new, &one_request);
136bcb71bbaSTrond Myklebust 	if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
137bae724efSFred Isaman 		nfs_pagein_multi(inode, &one_request, 1, len, 0, NULL);
138bcb71bbaSTrond Myklebust 	else
139bae724efSFred Isaman 		nfs_pagein_one(inode, &one_request, 1, len, 0, NULL);
1401da177e4SLinus Torvalds 	return 0;
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static void nfs_readpage_release(struct nfs_page *req)
1441da177e4SLinus Torvalds {
1457f8e05f6SDavid Howells 	struct inode *d_inode = req->wb_context->path.dentry->d_inode;
1467f8e05f6SDavid Howells 
1477f8e05f6SDavid Howells 	if (PageUptodate(req->wb_page))
1487f8e05f6SDavid Howells 		nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
1497f8e05f6SDavid Howells 
1501da177e4SLinus Torvalds 	unlock_page(req->wb_page);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
15388be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
15488be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1551da177e4SLinus Torvalds 			req->wb_bytes,
1561da177e4SLinus Torvalds 			(long long)req_offset(req));
15710d2c46fSNick Wilson 	nfs_release_request(req);
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
160*64419a9bSAndy Adamson static int nfs_initiate_read(struct nfs_read_data *data, struct rpc_clnt *clnt,
161*64419a9bSAndy Adamson 		      const struct rpc_call_ops *call_ops)
162*64419a9bSAndy Adamson {
163*64419a9bSAndy Adamson 	struct inode *inode = data->inode;
164*64419a9bSAndy Adamson 	int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
165*64419a9bSAndy Adamson 	struct rpc_task *task;
166*64419a9bSAndy Adamson 	struct rpc_message msg = {
167*64419a9bSAndy Adamson 		.rpc_argp = &data->args,
168*64419a9bSAndy Adamson 		.rpc_resp = &data->res,
169*64419a9bSAndy Adamson 		.rpc_cred = data->cred,
170*64419a9bSAndy Adamson 	};
171*64419a9bSAndy Adamson 	struct rpc_task_setup task_setup_data = {
172*64419a9bSAndy Adamson 		.task = &data->task,
173*64419a9bSAndy Adamson 		.rpc_client = clnt,
174*64419a9bSAndy Adamson 		.rpc_message = &msg,
175*64419a9bSAndy Adamson 		.callback_ops = call_ops,
176*64419a9bSAndy Adamson 		.callback_data = data,
177*64419a9bSAndy Adamson 		.workqueue = nfsiod_workqueue,
178*64419a9bSAndy Adamson 		.flags = RPC_TASK_ASYNC | swap_flags,
179*64419a9bSAndy Adamson 	};
180*64419a9bSAndy Adamson 
181*64419a9bSAndy Adamson 	/* Set up the initial task struct. */
182*64419a9bSAndy Adamson 	NFS_PROTO(inode)->read_setup(data, &msg);
183*64419a9bSAndy Adamson 
184*64419a9bSAndy Adamson 	dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
185*64419a9bSAndy Adamson 			"offset %llu)\n",
186*64419a9bSAndy Adamson 			data->task.tk_pid,
187*64419a9bSAndy Adamson 			inode->i_sb->s_id,
188*64419a9bSAndy Adamson 			(long long)NFS_FILEID(inode),
189*64419a9bSAndy Adamson 			data->args.count,
190*64419a9bSAndy Adamson 			(unsigned long long)data->args.offset);
191*64419a9bSAndy Adamson 
192*64419a9bSAndy Adamson 	task = rpc_run_task(&task_setup_data);
193*64419a9bSAndy Adamson 	if (IS_ERR(task))
194*64419a9bSAndy Adamson 		return PTR_ERR(task);
195*64419a9bSAndy Adamson 	rpc_put_task(task);
196*64419a9bSAndy Adamson 	return 0;
197*64419a9bSAndy Adamson }
198*64419a9bSAndy Adamson 
1991da177e4SLinus Torvalds /*
2001da177e4SLinus Torvalds  * Set up the NFS read request struct
2011da177e4SLinus Torvalds  */
202dbae4c73STrond Myklebust static int nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
203ec06c096STrond Myklebust 		const struct rpc_call_ops *call_ops,
204bae724efSFred Isaman 		unsigned int count, unsigned int offset,
205bae724efSFred Isaman 		struct pnfs_layout_segment *lseg)
2061da177e4SLinus Torvalds {
20784115e1cSTrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	data->req	  = req;
21084115e1cSTrond Myklebust 	data->inode	  = inode;
211*64419a9bSAndy Adamson 	data->cred	  = req->wb_context->cred;
212bae724efSFred Isaman 	data->lseg	  = get_lseg(lseg);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
2151da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
2161da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
2171da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
2181da177e4SLinus Torvalds 	data->args.count  = count;
219383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
220f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
2231da177e4SLinus Torvalds 	data->res.count   = count;
2241da177e4SLinus Torvalds 	data->res.eof     = 0;
2250e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
2261da177e4SLinus Torvalds 
227*64419a9bSAndy Adamson 	if (data->lseg &&
228*64419a9bSAndy Adamson 	    (pnfs_try_to_read_data(data, call_ops) == PNFS_ATTEMPTED))
229dbae4c73STrond Myklebust 		return 0;
230*64419a9bSAndy Adamson 
231*64419a9bSAndy Adamson 	return nfs_initiate_read(data, NFS_CLIENT(inode), call_ops);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds static void
2351da177e4SLinus Torvalds nfs_async_read_error(struct list_head *head)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	struct nfs_page	*req;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	while (!list_empty(head)) {
2401da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
2411da177e4SLinus Torvalds 		nfs_list_remove_request(req);
2421da177e4SLinus Torvalds 		SetPageError(req->wb_page);
2431da177e4SLinus Torvalds 		nfs_readpage_release(req);
2441da177e4SLinus Torvalds 	}
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds /*
2481da177e4SLinus Torvalds  * Generate multiple requests to fill a single page.
2491da177e4SLinus Torvalds  *
2501da177e4SLinus Torvalds  * We optimize to reduce the number of read operations on the wire.  If we
2511da177e4SLinus Torvalds  * detect that we're reading a page, or an area of a page, that is past the
2521da177e4SLinus Torvalds  * end of file, we do not generate NFS read operations but just clear the
2531da177e4SLinus Torvalds  * parts of the page that would have come back zero from the server anyway.
2541da177e4SLinus Torvalds  *
2551da177e4SLinus Torvalds  * We rely on the cached value of i_size to make this determination; another
2561da177e4SLinus Torvalds  * client can fill pages on the server past our cached end-of-file, but we
2571da177e4SLinus Torvalds  * won't see the new data until our attribute cache is updated.  This is more
2581da177e4SLinus Torvalds  * or less conventional NFS client behavior.
2591da177e4SLinus Torvalds  */
260bae724efSFred Isaman static int nfs_pagein_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags, struct pnfs_layout_segment *lseg)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
2631da177e4SLinus Torvalds 	struct page *page = req->wb_page;
2641da177e4SLinus Torvalds 	struct nfs_read_data *data;
265e9f7bee1STrond Myklebust 	size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
266e9f7bee1STrond Myklebust 	unsigned int offset;
2671da177e4SLinus Torvalds 	int requests = 0;
268dbae4c73STrond Myklebust 	int ret = 0;
2691da177e4SLinus Torvalds 	LIST_HEAD(list);
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	nfs_list_remove_request(req);
2721da177e4SLinus Torvalds 
273bcb71bbaSTrond Myklebust 	nbytes = count;
274e9f7bee1STrond Myklebust 	do {
275e9f7bee1STrond Myklebust 		size_t len = min(nbytes,rsize);
276e9f7bee1STrond Myklebust 
2778d5658c9STrond Myklebust 		data = nfs_readdata_alloc(1);
2781da177e4SLinus Torvalds 		if (!data)
2791da177e4SLinus Torvalds 			goto out_bad;
2801da177e4SLinus Torvalds 		list_add(&data->pages, &list);
2811da177e4SLinus Torvalds 		requests++;
282e9f7bee1STrond Myklebust 		nbytes -= len;
283e9f7bee1STrond Myklebust 	} while(nbytes != 0);
2841da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
2851da177e4SLinus Torvalds 
286bae724efSFred Isaman 	/* We know lseg==NULL */
287bae724efSFred Isaman 	lseg = pnfs_update_layout(inode, req->wb_context, IOMODE_READ);
2881da177e4SLinus Torvalds 	ClearPageError(page);
2891da177e4SLinus Torvalds 	offset = 0;
290bcb71bbaSTrond Myklebust 	nbytes = count;
2911da177e4SLinus Torvalds 	do {
292dbae4c73STrond Myklebust 		int ret2;
293dbae4c73STrond Myklebust 
2941da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_read_data, pages);
2951da177e4SLinus Torvalds 		list_del_init(&data->pages);
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 		data->pagevec[0] = page;
2981da177e4SLinus Torvalds 
299bcb71bbaSTrond Myklebust 		if (nbytes < rsize)
300bcb71bbaSTrond Myklebust 			rsize = nbytes;
301dbae4c73STrond Myklebust 		ret2 = nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
302bae724efSFred Isaman 					 rsize, offset, lseg);
303dbae4c73STrond Myklebust 		if (ret == 0)
304dbae4c73STrond Myklebust 			ret = ret2;
3051da177e4SLinus Torvalds 		offset += rsize;
3061da177e4SLinus Torvalds 		nbytes -= rsize;
3071da177e4SLinus Torvalds 	} while (nbytes != 0);
308bae724efSFred Isaman 	put_lseg(lseg);
3091da177e4SLinus Torvalds 
310dbae4c73STrond Myklebust 	return ret;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds out_bad:
3131da177e4SLinus Torvalds 	while (!list_empty(&list)) {
3141da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_read_data, pages);
3151da177e4SLinus Torvalds 		list_del(&data->pages);
3161da177e4SLinus Torvalds 		nfs_readdata_free(data);
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds 	SetPageError(page);
3191da177e4SLinus Torvalds 	nfs_readpage_release(req);
3201da177e4SLinus Torvalds 	return -ENOMEM;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
323bae724efSFred Isaman static int nfs_pagein_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags, struct pnfs_layout_segment *lseg)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	struct nfs_page		*req;
3261da177e4SLinus Torvalds 	struct page		**pages;
3271da177e4SLinus Torvalds 	struct nfs_read_data	*data;
328dbae4c73STrond Myklebust 	int ret = -ENOMEM;
3291da177e4SLinus Torvalds 
3308d5658c9STrond Myklebust 	data = nfs_readdata_alloc(npages);
331bae724efSFred Isaman 	if (!data) {
332bae724efSFred Isaman 		nfs_async_read_error(head);
333bae724efSFred Isaman 		goto out;
334bae724efSFred Isaman 	}
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	pages = data->pagevec;
3371da177e4SLinus Torvalds 	while (!list_empty(head)) {
3381da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
3391da177e4SLinus Torvalds 		nfs_list_remove_request(req);
3401da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
3411da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
3421da177e4SLinus Torvalds 		*pages++ = req->wb_page;
3431da177e4SLinus Torvalds 	}
3441da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
345bae724efSFred Isaman 	if ((!lseg) && list_is_singular(&data->pages))
346bae724efSFred Isaman 		lseg = pnfs_update_layout(inode, req->wb_context, IOMODE_READ);
3471da177e4SLinus Torvalds 
348bae724efSFred Isaman 	ret = nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0, lseg);
349bae724efSFred Isaman out:
350bae724efSFred Isaman 	put_lseg(lseg);
351dbae4c73STrond Myklebust 	return ret;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds /*
3550b671301STrond Myklebust  * This is the callback from RPC telling us whether a reply was
3560b671301STrond Myklebust  * received or some error occurred (timeout or socket shutdown).
3570b671301STrond Myklebust  */
3580b671301STrond Myklebust int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
3590b671301STrond Myklebust {
3600b671301STrond Myklebust 	int status;
3610b671301STrond Myklebust 
3623110ff80SHarvey Harrison 	dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
3630b671301STrond Myklebust 			task->tk_status);
3640b671301STrond Myklebust 
3650b671301STrond Myklebust 	status = NFS_PROTO(data->inode)->read_done(task, data);
3660b671301STrond Myklebust 	if (status != 0)
3670b671301STrond Myklebust 		return status;
3680b671301STrond Myklebust 
3690b671301STrond Myklebust 	nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
3700b671301STrond Myklebust 
3710b671301STrond Myklebust 	if (task->tk_status == -ESTALE) {
3723a10c30aSBenny Halevy 		set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags);
3730b671301STrond Myklebust 		nfs_mark_for_revalidate(data->inode);
3740b671301STrond Myklebust 	}
3750b671301STrond Myklebust 	return 0;
3760b671301STrond Myklebust }
3770b671301STrond Myklebust 
378fdd1e74cSTrond Myklebust static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
3790b671301STrond Myklebust {
3800b671301STrond Myklebust 	struct nfs_readargs *argp = &data->args;
3810b671301STrond Myklebust 	struct nfs_readres *resp = &data->res;
3820b671301STrond Myklebust 
3830b671301STrond Myklebust 	if (resp->eof || resp->count == argp->count)
384d61e612aSTrond Myklebust 		return;
3850b671301STrond Myklebust 
3860b671301STrond Myklebust 	/* This is a short read! */
3870b671301STrond Myklebust 	nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
3880b671301STrond Myklebust 	/* Has the server at least made some progress? */
3890b671301STrond Myklebust 	if (resp->count == 0)
390d61e612aSTrond Myklebust 		return;
3910b671301STrond Myklebust 
3920b671301STrond Myklebust 	/* Yes, so retry the read at the end of the data */
3930b671301STrond Myklebust 	argp->offset += resp->count;
3940b671301STrond Myklebust 	argp->pgbase += resp->count;
3950b671301STrond Myklebust 	argp->count -= resp->count;
3960110ee15STrond Myklebust 	nfs_restart_rpc(task, NFS_SERVER(data->inode)->nfs_client);
3970b671301STrond Myklebust }
3980b671301STrond Myklebust 
3990b671301STrond Myklebust /*
4001da177e4SLinus Torvalds  * Handle a read reply that fills part of a page.
4011da177e4SLinus Torvalds  */
402ec06c096STrond Myklebust static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
4031da177e4SLinus Torvalds {
404ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
4051da177e4SLinus Torvalds 
406ec06c096STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
407ec06c096STrond Myklebust 		return;
408fdd1e74cSTrond Myklebust 	if (task->tk_status < 0)
4090b671301STrond Myklebust 		return;
410fdd1e74cSTrond Myklebust 
411fdd1e74cSTrond Myklebust 	nfs_readpage_truncate_uninitialised_page(data);
412fdd1e74cSTrond Myklebust 	nfs_readpage_retry(task, data);
4130b671301STrond Myklebust }
414fdd1e74cSTrond Myklebust 
415fdd1e74cSTrond Myklebust static void nfs_readpage_release_partial(void *calldata)
416fdd1e74cSTrond Myklebust {
417fdd1e74cSTrond Myklebust 	struct nfs_read_data *data = calldata;
418fdd1e74cSTrond Myklebust 	struct nfs_page *req = data->req;
419fdd1e74cSTrond Myklebust 	struct page *page = req->wb_page;
420fdd1e74cSTrond Myklebust 	int status = data->task.tk_status;
421fdd1e74cSTrond Myklebust 
422fdd1e74cSTrond Myklebust 	if (status < 0)
4230b671301STrond Myklebust 		SetPageError(page);
424fdd1e74cSTrond Myklebust 
4251da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete)) {
4261da177e4SLinus Torvalds 		if (!PageError(page))
4271da177e4SLinus Torvalds 			SetPageUptodate(page);
4281da177e4SLinus Torvalds 		nfs_readpage_release(req);
4291da177e4SLinus Torvalds 	}
430fdd1e74cSTrond Myklebust 	nfs_readdata_release(calldata);
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
433f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1)
434f11c88afSAndy Adamson void nfs_read_prepare(struct rpc_task *task, void *calldata)
435f11c88afSAndy Adamson {
436f11c88afSAndy Adamson 	struct nfs_read_data *data = calldata;
437f11c88afSAndy Adamson 
438035168abSTrond Myklebust 	if (nfs4_setup_sequence(NFS_SERVER(data->inode),
439f11c88afSAndy Adamson 				&data->args.seq_args, &data->res.seq_res,
440f11c88afSAndy Adamson 				0, task))
441f11c88afSAndy Adamson 		return;
442f11c88afSAndy Adamson 	rpc_call_start(task);
443f11c88afSAndy Adamson }
444f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
445f11c88afSAndy Adamson 
446ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_partial_ops = {
447f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1)
448f11c88afSAndy Adamson 	.rpc_call_prepare = nfs_read_prepare,
449f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
450ec06c096STrond Myklebust 	.rpc_call_done = nfs_readpage_result_partial,
451fdd1e74cSTrond Myklebust 	.rpc_release = nfs_readpage_release_partial,
452ec06c096STrond Myklebust };
453ec06c096STrond Myklebust 
4541de3fc12STrond Myklebust static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
4551de3fc12STrond Myklebust {
4561de3fc12STrond Myklebust 	unsigned int count = data->res.count;
4571de3fc12STrond Myklebust 	unsigned int base = data->args.pgbase;
4581de3fc12STrond Myklebust 	struct page **pages;
4591de3fc12STrond Myklebust 
46079558f36STrond Myklebust 	if (data->res.eof)
46179558f36STrond Myklebust 		count = data->args.count;
4621de3fc12STrond Myklebust 	if (unlikely(count == 0))
4631de3fc12STrond Myklebust 		return;
4641de3fc12STrond Myklebust 	pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
4651de3fc12STrond Myklebust 	base &= ~PAGE_CACHE_MASK;
4661de3fc12STrond Myklebust 	count += base;
4671de3fc12STrond Myklebust 	for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
4681de3fc12STrond Myklebust 		SetPageUptodate(*pages);
4690b671301STrond Myklebust 	if (count == 0)
4700b671301STrond Myklebust 		return;
4710b671301STrond Myklebust 	/* Was this a short read? */
4720b671301STrond Myklebust 	if (data->res.eof || data->res.count == data->args.count)
4731de3fc12STrond Myklebust 		SetPageUptodate(*pages);
4741de3fc12STrond Myklebust }
4751de3fc12STrond Myklebust 
4761da177e4SLinus Torvalds /*
4771da177e4SLinus Torvalds  * This is the callback from RPC telling us whether a reply was
4781da177e4SLinus Torvalds  * received or some error occurred (timeout or socket shutdown).
4791da177e4SLinus Torvalds  */
480ec06c096STrond Myklebust static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
4811da177e4SLinus Torvalds {
482ec06c096STrond Myklebust 	struct nfs_read_data *data = calldata;
4831da177e4SLinus Torvalds 
4840b671301STrond Myklebust 	if (nfs_readpage_result(task, data) != 0)
4850b671301STrond Myklebust 		return;
486fdd1e74cSTrond Myklebust 	if (task->tk_status < 0)
487fdd1e74cSTrond Myklebust 		return;
4881de3fc12STrond Myklebust 	/*
4890b671301STrond Myklebust 	 * Note: nfs_readpage_retry may change the values of
4901de3fc12STrond Myklebust 	 * data->args. In the multi-page case, we therefore need
4910b671301STrond Myklebust 	 * to ensure that we call nfs_readpage_set_pages_uptodate()
4920b671301STrond Myklebust 	 * first.
4931de3fc12STrond Myklebust 	 */
4941de3fc12STrond Myklebust 	nfs_readpage_truncate_uninitialised_page(data);
4951de3fc12STrond Myklebust 	nfs_readpage_set_pages_uptodate(data);
496fdd1e74cSTrond Myklebust 	nfs_readpage_retry(task, data);
4970b671301STrond Myklebust }
498fdd1e74cSTrond Myklebust 
499fdd1e74cSTrond Myklebust static void nfs_readpage_release_full(void *calldata)
500fdd1e74cSTrond Myklebust {
501fdd1e74cSTrond Myklebust 	struct nfs_read_data *data = calldata;
502fdd1e74cSTrond Myklebust 
5031da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
5041da177e4SLinus Torvalds 		struct nfs_page *req = nfs_list_entry(data->pages.next);
5051da177e4SLinus Torvalds 
5061de3fc12STrond Myklebust 		nfs_list_remove_request(req);
5071da177e4SLinus Torvalds 		nfs_readpage_release(req);
5081da177e4SLinus Torvalds 	}
509fdd1e74cSTrond Myklebust 	nfs_readdata_release(calldata);
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
512ec06c096STrond Myklebust static const struct rpc_call_ops nfs_read_full_ops = {
513f11c88afSAndy Adamson #if defined(CONFIG_NFS_V4_1)
514f11c88afSAndy Adamson 	.rpc_call_prepare = nfs_read_prepare,
515f11c88afSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
516ec06c096STrond Myklebust 	.rpc_call_done = nfs_readpage_result_full,
517fdd1e74cSTrond Myklebust 	.rpc_release = nfs_readpage_release_full,
518ec06c096STrond Myklebust };
519ec06c096STrond Myklebust 
5201da177e4SLinus Torvalds /*
5211da177e4SLinus Torvalds  * Read a page over NFS.
5221da177e4SLinus Torvalds  * We read the page synchronously in the following case:
5231da177e4SLinus Torvalds  *  -	The error flag is set for this page. This happens only when a
5241da177e4SLinus Torvalds  *	previous async read operation failed.
5251da177e4SLinus Torvalds  */
5261da177e4SLinus Torvalds int nfs_readpage(struct file *file, struct page *page)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
5291da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
5301da177e4SLinus Torvalds 	int		error;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
5331da177e4SLinus Torvalds 		page, PAGE_CACHE_SIZE, page->index);
53491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
53591d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_READPAGES, 1);
53691d5b470SChuck Lever 
5371da177e4SLinus Torvalds 	/*
5381da177e4SLinus Torvalds 	 * Try to flush any pending writes to the file..
5391da177e4SLinus Torvalds 	 *
5401da177e4SLinus Torvalds 	 * NOTE! Because we own the page lock, there cannot
5411da177e4SLinus Torvalds 	 * be any new pending writes generated at this point
5421da177e4SLinus Torvalds 	 * for this page (other pages can be written to).
5431da177e4SLinus Torvalds 	 */
5441da177e4SLinus Torvalds 	error = nfs_wb_page(inode, page);
5451da177e4SLinus Torvalds 	if (error)
546de05a0ccSTrond Myklebust 		goto out_unlock;
547de05a0ccSTrond Myklebust 	if (PageUptodate(page))
548de05a0ccSTrond Myklebust 		goto out_unlock;
5491da177e4SLinus Torvalds 
5505f004cf2STrond Myklebust 	error = -ESTALE;
5515f004cf2STrond Myklebust 	if (NFS_STALE(inode))
552de05a0ccSTrond Myklebust 		goto out_unlock;
5535f004cf2STrond Myklebust 
5541da177e4SLinus Torvalds 	if (file == NULL) {
555cf1308ffSTrond Myklebust 		error = -EBADF;
556d530838bSTrond Myklebust 		ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
5571da177e4SLinus Torvalds 		if (ctx == NULL)
558de05a0ccSTrond Myklebust 			goto out_unlock;
5591da177e4SLinus Torvalds 	} else
560cd3758e3STrond Myklebust 		ctx = get_nfs_open_context(nfs_file_open_context(file));
5611da177e4SLinus Torvalds 
5629a9fc1c0SDavid Howells 	if (!IS_SYNC(inode)) {
5639a9fc1c0SDavid Howells 		error = nfs_readpage_from_fscache(ctx, inode, page);
5649a9fc1c0SDavid Howells 		if (error == 0)
5659a9fc1c0SDavid Howells 			goto out;
5669a9fc1c0SDavid Howells 	}
5679a9fc1c0SDavid Howells 
5688e0969f0STrond Myklebust 	error = nfs_readpage_async(ctx, inode, page);
5698e0969f0STrond Myklebust 
5709a9fc1c0SDavid Howells out:
5711da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
5721da177e4SLinus Torvalds 	return error;
573de05a0ccSTrond Myklebust out_unlock:
5741da177e4SLinus Torvalds 	unlock_page(page);
5751da177e4SLinus Torvalds 	return error;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds struct nfs_readdesc {
5798b09bee3STrond Myklebust 	struct nfs_pageio_descriptor *pgio;
5801da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
5811da177e4SLinus Torvalds };
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds static int
5841da177e4SLinus Torvalds readpage_async_filler(void *data, struct page *page)
5851da177e4SLinus Torvalds {
5861da177e4SLinus Torvalds 	struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
5871da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
5881da177e4SLinus Torvalds 	struct nfs_page *new;
5891da177e4SLinus Torvalds 	unsigned int len;
590de05a0ccSTrond Myklebust 	int error;
5911da177e4SLinus Torvalds 
59249a70f27STrond Myklebust 	len = nfs_page_length(page);
5931da177e4SLinus Torvalds 	if (len == 0)
5941da177e4SLinus Torvalds 		return nfs_return_empty_page(page);
595de05a0ccSTrond Myklebust 
5961da177e4SLinus Torvalds 	new = nfs_create_request(desc->ctx, inode, page, 0, len);
597de05a0ccSTrond Myklebust 	if (IS_ERR(new))
598de05a0ccSTrond Myklebust 		goto out_error;
599de05a0ccSTrond Myklebust 
6001da177e4SLinus Torvalds 	if (len < PAGE_CACHE_SIZE)
601eebd2aa3SChristoph Lameter 		zero_user_segment(page, len, PAGE_CACHE_SIZE);
602f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(desc->pgio, new)) {
603f8512ad0SFred Isaman 		error = desc->pgio->pg_error;
604f8512ad0SFred Isaman 		goto out_unlock;
605f8512ad0SFred Isaman 	}
6061da177e4SLinus Torvalds 	return 0;
607de05a0ccSTrond Myklebust out_error:
608de05a0ccSTrond Myklebust 	error = PTR_ERR(new);
609de05a0ccSTrond Myklebust 	SetPageError(page);
610de05a0ccSTrond Myklebust out_unlock:
611de05a0ccSTrond Myklebust 	unlock_page(page);
612de05a0ccSTrond Myklebust 	return error;
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds int nfs_readpages(struct file *filp, struct address_space *mapping,
6161da177e4SLinus Torvalds 		struct list_head *pages, unsigned nr_pages)
6171da177e4SLinus Torvalds {
6188b09bee3STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6191da177e4SLinus Torvalds 	struct nfs_readdesc desc = {
6208b09bee3STrond Myklebust 		.pgio = &pgio,
6211da177e4SLinus Torvalds 	};
6221da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
6231da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
6248b09bee3STrond Myklebust 	size_t rsize = server->rsize;
6258b09bee3STrond Myklebust 	unsigned long npages;
6265f004cf2STrond Myklebust 	int ret = -ESTALE;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
6291da177e4SLinus Torvalds 			inode->i_sb->s_id,
6301da177e4SLinus Torvalds 			(long long)NFS_FILEID(inode),
6311da177e4SLinus Torvalds 			nr_pages);
63291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
6331da177e4SLinus Torvalds 
6345f004cf2STrond Myklebust 	if (NFS_STALE(inode))
6355f004cf2STrond Myklebust 		goto out;
6365f004cf2STrond Myklebust 
6371da177e4SLinus Torvalds 	if (filp == NULL) {
638d530838bSTrond Myklebust 		desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
6391da177e4SLinus Torvalds 		if (desc.ctx == NULL)
6401da177e4SLinus Torvalds 			return -EBADF;
6411da177e4SLinus Torvalds 	} else
642cd3758e3STrond Myklebust 		desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
6439a9fc1c0SDavid Howells 
6449a9fc1c0SDavid Howells 	/* attempt to read as many of the pages as possible from the cache
6459a9fc1c0SDavid Howells 	 * - this returns -ENOBUFS immediately if the cookie is negative
6469a9fc1c0SDavid Howells 	 */
6479a9fc1c0SDavid Howells 	ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
6489a9fc1c0SDavid Howells 					 pages, &nr_pages);
6499a9fc1c0SDavid Howells 	if (ret == 0)
6509a9fc1c0SDavid Howells 		goto read_complete; /* all pages were read */
6519a9fc1c0SDavid Howells 
65294ad1c80SFred Isaman 	pnfs_pageio_init_read(&pgio, inode);
6538b09bee3STrond Myklebust 	if (rsize < PAGE_CACHE_SIZE)
6548b09bee3STrond Myklebust 		nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0);
6558b09bee3STrond Myklebust 	else
6568b09bee3STrond Myklebust 		nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0);
6578b09bee3STrond Myklebust 
6581da177e4SLinus Torvalds 	ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
6598b09bee3STrond Myklebust 
6608b09bee3STrond Myklebust 	nfs_pageio_complete(&pgio);
6618b09bee3STrond Myklebust 	npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
6628b09bee3STrond Myklebust 	nfs_add_stats(inode, NFSIOS_READPAGES, npages);
6639a9fc1c0SDavid Howells read_complete:
6641da177e4SLinus Torvalds 	put_nfs_open_context(desc.ctx);
6655f004cf2STrond Myklebust out:
6661da177e4SLinus Torvalds 	return ret;
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
669f7b422b1SDavid Howells int __init nfs_init_readpagecache(void)
6701da177e4SLinus Torvalds {
6711da177e4SLinus Torvalds 	nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
6721da177e4SLinus Torvalds 					     sizeof(struct nfs_read_data),
6731da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
67420c2df83SPaul Mundt 					     NULL);
6751da177e4SLinus Torvalds 	if (nfs_rdata_cachep == NULL)
6761da177e4SLinus Torvalds 		return -ENOMEM;
6771da177e4SLinus Torvalds 
67893d2341cSMatthew Dobson 	nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
6791da177e4SLinus Torvalds 						     nfs_rdata_cachep);
6801da177e4SLinus Torvalds 	if (nfs_rdata_mempool == NULL)
6811da177e4SLinus Torvalds 		return -ENOMEM;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	return 0;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
686266bee88SDavid Brownell void nfs_destroy_readpagecache(void)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	mempool_destroy(nfs_rdata_mempool);
6891a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_rdata_cachep);
6901da177e4SLinus Torvalds }
691