blob: d6d46823d9e47b43c702c9ef6e494b9a7bd46178 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/read.c
3 *
4 * Block I/O for NFS
5 *
6 * Partial copy of Linus' read cache modifications to fs/nfs/file.c
7 * modified for async RPC by okir@monad.swb.de
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/time.h>
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/fcntl.h>
14#include <linux/stat.h>
15#include <linux/mm.h>
16#include <linux/slab.h>
17#include <linux/pagemap.h>
18#include <linux/sunrpc/clnt.h>
19#include <linux/nfs_fs.h>
20#include <linux/nfs_page.h>
Andy Adamson64419a92011-03-01 01:34:16 +000021#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Fred Isamanbae724e2011-03-01 01:34:15 +000023#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Andy Adamsonf11c88a2009-04-01 09:22:25 -040025#include "nfs4_fs.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050026#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050027#include "iostat.h"
David Howells9a9fc1c2009-04-03 16:42:44 +010028#include "fscache.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define NFSDBG_FACILITY NFSDBG_PAGECACHE
31
Trond Myklebust1751c362011-06-10 13:30:23 -040032static const struct nfs_pageio_ops nfs_pageio_read_ops;
Trond Myklebustec06c092006-03-20 13:44:27 -050033static const struct rpc_call_ops nfs_read_partial_ops;
34static const struct rpc_call_ops nfs_read_full_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Christoph Lametere18b8902006-12-06 20:33:20 -080036static struct kmem_cache *nfs_rdata_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Fred Isamancd841602012-04-20 14:47:44 -040038struct nfs_read_header *nfs_readhdr_alloc(unsigned int pagecount)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050039{
Fred Isamancd841602012-04-20 14:47:44 -040040 struct nfs_read_header *p;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050041
Trond Myklebustb6ee8cd2011-10-19 12:17:29 -070042 p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050043 if (p) {
Fred Isamancd841602012-04-20 14:47:44 -040044 struct nfs_pgio_header *hdr = &p->header;
45 struct nfs_read_data *data = &p->rpc_data;
46
47 INIT_LIST_HEAD(&hdr->pages);
48 INIT_LIST_HEAD(&data->list);
49 data->npages = pagecount;
50 data->header = hdr;
51 if (pagecount <= ARRAY_SIZE(data->page_array))
52 data->pagevec = data->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050053 else {
Fred Isamancd841602012-04-20 14:47:44 -040054 data->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
55 if (!data->pagevec) {
Trond Myklebustb6ee8cd2011-10-19 12:17:29 -070056 kmem_cache_free(nfs_rdata_cachep, p);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050057 p = NULL;
58 }
59 }
60 }
61 return p;
62}
63
Fred Isamancd841602012-04-20 14:47:44 -040064void nfs_readhdr_free(struct nfs_pgio_header *hdr)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050065{
Fred Isamancd841602012-04-20 14:47:44 -040066 struct nfs_read_header *rhdr = container_of(hdr, struct nfs_read_header, header);
67
68 kmem_cache_free(nfs_rdata_cachep, rhdr);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050069}
70
Trond Myklebust493292d2011-07-13 15:58:28 -040071void nfs_readdata_release(struct nfs_read_data *rdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Trond Myklebust383ba712008-02-19 20:04:20 -050073 put_nfs_open_context(rdata->args.context);
Fred Isamancd841602012-04-20 14:47:44 -040074 if (rdata->pagevec != rdata->page_array)
75 kfree(rdata->pagevec);
76 nfs_readhdr_free(rdata->header);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79static
Linus Torvalds1da177e2005-04-16 15:20:36 -070080int nfs_return_empty_page(struct page *page)
81{
Christoph Lametereebd2aa2008-02-04 22:28:29 -080082 zero_user(page, 0, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 SetPageUptodate(page);
84 unlock_page(page);
85 return 0;
86}
87
Trond Myklebust1de3fc12006-05-25 01:40:44 -040088static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
89{
90 unsigned int remainder = data->args.count - data->res.count;
91 unsigned int base = data->args.pgbase + data->res.count;
92 unsigned int pglen;
93 struct page **pages;
94
95 if (data->res.eof == 0 || remainder == 0)
96 return;
97 /*
98 * Note: "remainder" can never be negative, since we check for
99 * this in the XDR code.
100 */
101 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
102 base &= ~PAGE_CACHE_MASK;
103 pglen = PAGE_CACHE_SIZE - base;
Trond Myklebust79558f32006-08-22 13:44:32 -0400104 for (;;) {
105 if (remainder <= pglen) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800106 zero_user(*pages, base, remainder);
Trond Myklebust79558f32006-08-22 13:44:32 -0400107 break;
108 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800109 zero_user(*pages, base, pglen);
Trond Myklebust79558f32006-08-22 13:44:32 -0400110 pages++;
111 remainder -= pglen;
112 pglen = PAGE_CACHE_SIZE;
113 base = 0;
114 }
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400115}
116
Trond Myklebust62e4a762011-11-10 14:30:37 -0500117void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor *pgio,
Trond Myklebust1751c362011-06-10 13:30:23 -0400118 struct inode *inode)
119{
120 nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops,
121 NFS_SERVER(inode)->rsize, 0);
122}
123
Trond Myklebust493292d2011-07-13 15:58:28 -0400124void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
125{
126 pgio->pg_ops = &nfs_pageio_read_ops;
127 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
128}
Trond Myklebust1f945352011-07-13 15:59:57 -0400129EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
Trond Myklebust493292d2011-07-13 15:58:28 -0400130
Trond Myklebust1751c362011-06-10 13:30:23 -0400131static void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
132 struct inode *inode)
133{
134 if (!pnfs_pageio_init_read(pgio, inode))
135 nfs_pageio_init_read_mds(pgio, inode);
136}
137
David Howellsf42b2932009-04-03 16:42:44 +0100138int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
139 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct nfs_page *new;
142 unsigned int len;
Fred Isamanc76069b2011-03-03 15:13:48 +0000143 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Trond Myklebust49a70f22006-12-05 00:35:38 -0500145 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (len == 0)
147 return nfs_return_empty_page(page);
148 new = nfs_create_request(ctx, inode, page, 0, len);
149 if (IS_ERR(new)) {
150 unlock_page(page);
151 return PTR_ERR(new);
152 }
153 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800154 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Trond Myklebust1751c362011-06-10 13:30:23 -0400156 nfs_pageio_init_read(&pgio, inode);
Trond Myklebustd8007d42011-06-10 13:30:23 -0400157 nfs_pageio_add_request(&pgio, new);
Trond Myklebust1751c362011-06-10 13:30:23 -0400158 nfs_pageio_complete(&pgio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return 0;
160}
161
162static void nfs_readpage_release(struct nfs_page *req)
163{
Al Viro3d4ff432011-06-22 18:40:12 -0400164 struct inode *d_inode = req->wb_context->dentry->d_inode;
David Howells7f8e05f2009-04-03 16:42:45 +0100165
166 if (PageUptodate(req->wb_page))
167 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unlock_page(req->wb_page);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
Al Viro3d4ff432011-06-22 18:40:12 -0400172 req->wb_context->dentry->d_inode->i_sb->s_id,
173 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 req->wb_bytes,
175 (long long)req_offset(req));
Nick Wilson10d2c462005-09-22 21:44:28 -0700176 nfs_release_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Fred Isamanc5996c42012-04-20 14:47:41 -0400179int nfs_initiate_read(struct rpc_clnt *clnt,
180 struct nfs_read_data *data,
Andy Adamson64419a92011-03-01 01:34:16 +0000181 const struct rpc_call_ops *call_ops)
182{
Fred Isamancd841602012-04-20 14:47:44 -0400183 struct inode *inode = data->header->inode;
Andy Adamson64419a92011-03-01 01:34:16 +0000184 int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
185 struct rpc_task *task;
186 struct rpc_message msg = {
187 .rpc_argp = &data->args,
188 .rpc_resp = &data->res,
Fred Isamancd841602012-04-20 14:47:44 -0400189 .rpc_cred = data->header->cred,
Andy Adamson64419a92011-03-01 01:34:16 +0000190 };
191 struct rpc_task_setup task_setup_data = {
192 .task = &data->task,
193 .rpc_client = clnt,
194 .rpc_message = &msg,
195 .callback_ops = call_ops,
196 .callback_data = data,
197 .workqueue = nfsiod_workqueue,
198 .flags = RPC_TASK_ASYNC | swap_flags,
199 };
200
201 /* Set up the initial task struct. */
202 NFS_PROTO(inode)->read_setup(data, &msg);
203
204 dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
205 "offset %llu)\n",
206 data->task.tk_pid,
207 inode->i_sb->s_id,
208 (long long)NFS_FILEID(inode),
209 data->args.count,
210 (unsigned long long)data->args.offset);
211
212 task = rpc_run_task(&task_setup_data);
213 if (IS_ERR(task))
214 return PTR_ERR(task);
215 rpc_put_task(task);
216 return 0;
217}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000218EXPORT_SYMBOL_GPL(nfs_initiate_read);
Andy Adamson64419a92011-03-01 01:34:16 +0000219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
221 * Set up the NFS read request struct
222 */
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400223static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
224 unsigned int count, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Fred Isamancd841602012-04-20 14:47:44 -0400226 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Fred Isamancd841602012-04-20 14:47:44 -0400228 data->header->req = req;
229 data->header->inode = inode;
230 data->header->cred = req->wb_context->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 data->args.fh = NFS_FH(inode);
233 data->args.offset = req_offset(req) + offset;
234 data->args.pgbase = req->wb_pgbase + offset;
235 data->args.pages = data->pagevec;
236 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500237 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400238 data->args.lock_context = req->wb_lock_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 data->res.fattr = &data->fattr;
241 data->res.count = count;
242 data->res.eof = 0;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400243 nfs_fattr_init(&data->fattr);
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400246static int nfs_do_read(struct nfs_read_data *data,
Trond Myklebust493292d2011-07-13 15:58:28 -0400247 const struct rpc_call_ops *call_ops)
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400248{
Fred Isamancd841602012-04-20 14:47:44 -0400249 struct inode *inode = data->header->inode;
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400250
Fred Isamanc5996c42012-04-20 14:47:41 -0400251 return nfs_initiate_read(NFS_CLIENT(inode), data, call_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Trond Myklebust275acaa2011-07-12 13:42:02 -0400254static int
255nfs_do_multiple_reads(struct list_head *head,
Trond Myklebust493292d2011-07-13 15:58:28 -0400256 const struct rpc_call_ops *call_ops)
Trond Myklebust275acaa2011-07-12 13:42:02 -0400257{
258 struct nfs_read_data *data;
259 int ret = 0;
260
261 while (!list_empty(head)) {
262 int ret2;
263
264 data = list_entry(head->next, struct nfs_read_data, list);
265 list_del_init(&data->list);
266
Trond Myklebust493292d2011-07-13 15:58:28 -0400267 ret2 = nfs_do_read(data, call_ops);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400268 if (ret == 0)
269 ret = ret2;
270 }
271 return ret;
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static void
275nfs_async_read_error(struct list_head *head)
276{
277 struct nfs_page *req;
278
279 while (!list_empty(head)) {
280 req = nfs_list_entry(head->next);
281 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 nfs_readpage_release(req);
283 }
284}
285
286/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * Generate multiple requests to fill a single page.
288 *
289 * We optimize to reduce the number of read operations on the wire. If we
290 * detect that we're reading a page, or an area of a page, that is past the
291 * end of file, we do not generate NFS read operations but just clear the
292 * parts of the page that would have come back zero from the server anyway.
293 *
294 * We rely on the cached value of i_size to make this determination; another
295 * client can fill pages on the server past our cached end-of-file, but we
296 * won't see the new data until our attribute cache is updated. This is more
297 * or less conventional NFS client behavior.
298 */
Trond Myklebust275acaa2011-07-12 13:42:02 -0400299static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc, struct list_head *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Fred Isamanc76069b2011-03-03 15:13:48 +0000301 struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct page *page = req->wb_page;
Fred Isamancd841602012-04-20 14:47:44 -0400303 struct nfs_read_header *rhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct nfs_read_data *data;
Trond Myklebustd0979712011-07-12 13:42:02 -0400305 size_t rsize = desc->pg_bsize, nbytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700306 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 int requests = 0;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400308 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 nfs_list_remove_request(req);
311
Trond Myklebust275acaa2011-07-12 13:42:02 -0400312 offset = 0;
Fred Isamanc76069b2011-03-03 15:13:48 +0000313 nbytes = desc->pg_count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700314 do {
315 size_t len = min(nbytes,rsize);
316
Fred Isamancd841602012-04-20 14:47:44 -0400317 rhdr = nfs_readhdr_alloc(1);
318 if (!rhdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 goto out_bad;
Fred Isamancd841602012-04-20 14:47:44 -0400320 data = &rhdr->rpc_data;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400321 data->pagevec[0] = page;
322 nfs_read_rpcsetup(req, data, len, offset);
323 list_add(&data->list, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700325 nbytes -= len;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400326 offset += len;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700327 } while(nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 atomic_set(&req->wb_complete, requests);
Trond Myklebust50828d72011-07-12 13:42:02 -0400329 desc->pg_rpc_callops = &nfs_read_partial_ops;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400330 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331out_bad:
Trond Myklebust275acaa2011-07-12 13:42:02 -0400332 while (!list_empty(res)) {
333 data = list_entry(res->next, struct nfs_read_data, list);
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400334 list_del(&data->list);
Fred Isaman73fb7bc2012-04-20 14:47:34 -0400335 nfs_readdata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 nfs_readpage_release(req);
338 return -ENOMEM;
339}
340
Trond Myklebust275acaa2011-07-12 13:42:02 -0400341static int nfs_pagein_one(struct nfs_pageio_descriptor *desc, struct list_head *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 struct nfs_page *req;
344 struct page **pages;
Fred Isamancd841602012-04-20 14:47:44 -0400345 struct nfs_read_header *rhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct nfs_read_data *data;
Fred Isamanc76069b2011-03-03 15:13:48 +0000347 struct list_head *head = &desc->pg_list;
Peng Tao3b609182011-07-15 03:33:42 -0400348 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Fred Isamancd841602012-04-20 14:47:44 -0400350 rhdr = nfs_readhdr_alloc(nfs_page_array_len(desc->pg_base,
351 desc->pg_count));
352 if (!rhdr) {
Fred Isamanbae724e2011-03-01 01:34:15 +0000353 nfs_async_read_error(head);
Peng Tao3b609182011-07-15 03:33:42 -0400354 ret = -ENOMEM;
Fred Isamanbae724e2011-03-01 01:34:15 +0000355 goto out;
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Fred Isamancd841602012-04-20 14:47:44 -0400358 data = &rhdr->rpc_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 while (!list_empty(head)) {
361 req = nfs_list_entry(head->next);
362 nfs_list_remove_request(req);
Fred Isamancd841602012-04-20 14:47:44 -0400363 nfs_list_add_request(req, &rhdr->header.pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Fred Isamancd841602012-04-20 14:47:44 -0400366 req = nfs_list_entry(rhdr->header.pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400368 nfs_read_rpcsetup(req, data, desc->pg_count, 0);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400369 list_add(&data->list, res);
Trond Myklebust50828d72011-07-12 13:42:02 -0400370 desc->pg_rpc_callops = &nfs_read_full_ops;
Fred Isamanbae724e2011-03-01 01:34:15 +0000371out:
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Trond Myklebust493292d2011-07-13 15:58:28 -0400375int nfs_generic_pagein(struct nfs_pageio_descriptor *desc, struct list_head *head)
376{
377 if (desc->pg_bsize < PAGE_CACHE_SIZE)
378 return nfs_pagein_multi(desc, head);
379 return nfs_pagein_one(desc, head);
380}
381
382static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
Trond Myklebust1751c362011-06-10 13:30:23 -0400383{
Trond Myklebust275acaa2011-07-12 13:42:02 -0400384 LIST_HEAD(head);
385 int ret;
386
Trond Myklebust493292d2011-07-13 15:58:28 -0400387 ret = nfs_generic_pagein(desc, &head);
Trond Myklebust50828d72011-07-12 13:42:02 -0400388 if (ret == 0)
Trond Myklebust493292d2011-07-13 15:58:28 -0400389 ret = nfs_do_multiple_reads(&head, desc->pg_rpc_callops);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400390 return ret;
Trond Myklebust1751c362011-06-10 13:30:23 -0400391}
Trond Myklebust1751c362011-06-10 13:30:23 -0400392
393static const struct nfs_pageio_ops nfs_pageio_read_ops = {
394 .pg_test = nfs_generic_pg_test,
395 .pg_doio = nfs_generic_pg_readpages,
396};
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/*
Trond Myklebust0b671302006-11-14 16:12:23 -0500399 * This is the callback from RPC telling us whether a reply was
400 * received or some error occurred (timeout or socket shutdown).
401 */
402int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
403{
Fred Isamancd841602012-04-20 14:47:44 -0400404 struct inode *inode = data->header->inode;
Trond Myklebust0b671302006-11-14 16:12:23 -0500405 int status;
406
Harvey Harrison3110ff82008-05-02 13:42:44 -0700407 dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
Trond Myklebust0b671302006-11-14 16:12:23 -0500408 task->tk_status);
409
Fred Isamancd841602012-04-20 14:47:44 -0400410 status = NFS_PROTO(inode)->read_done(task, data);
Trond Myklebust0b671302006-11-14 16:12:23 -0500411 if (status != 0)
412 return status;
413
Fred Isamancd841602012-04-20 14:47:44 -0400414 nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, data->res.count);
Trond Myklebust0b671302006-11-14 16:12:23 -0500415
416 if (task->tk_status == -ESTALE) {
Fred Isamancd841602012-04-20 14:47:44 -0400417 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
418 nfs_mark_for_revalidate(inode);
Trond Myklebust0b671302006-11-14 16:12:23 -0500419 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500420 return 0;
421}
422
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400423static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
Trond Myklebust0b671302006-11-14 16:12:23 -0500424{
425 struct nfs_readargs *argp = &data->args;
426 struct nfs_readres *resp = &data->res;
427
428 if (resp->eof || resp->count == argp->count)
Trond Myklebustd61e6122009-12-05 19:32:19 -0500429 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500430
431 /* This is a short read! */
Fred Isamancd841602012-04-20 14:47:44 -0400432 nfs_inc_stats(data->header->inode, NFSIOS_SHORTREAD);
Trond Myklebust0b671302006-11-14 16:12:23 -0500433 /* Has the server at least made some progress? */
434 if (resp->count == 0)
Trond Myklebustd61e6122009-12-05 19:32:19 -0500435 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500436
437 /* Yes, so retry the read at the end of the data */
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000438 data->mds_offset += resp->count;
Trond Myklebust0b671302006-11-14 16:12:23 -0500439 argp->offset += resp->count;
440 argp->pgbase += resp->count;
441 argp->count -= resp->count;
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700442 rpc_restart_call_prepare(task);
Trond Myklebust0b671302006-11-14 16:12:23 -0500443}
444
445/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * Handle a read reply that fills part of a page.
447 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500448static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Trond Myklebustec06c092006-03-20 13:44:27 -0500450 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Trond Myklebustec06c092006-03-20 13:44:27 -0500452 if (nfs_readpage_result(task, data) != 0)
453 return;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400454 if (task->tk_status < 0)
455 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500456
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400457 nfs_readpage_truncate_uninitialised_page(data);
458 nfs_readpage_retry(task, data);
459}
460
461static void nfs_readpage_release_partial(void *calldata)
462{
463 struct nfs_read_data *data = calldata;
Fred Isamancd841602012-04-20 14:47:44 -0400464 struct nfs_page *req = data->header->req;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400465 struct page *page = req->wb_page;
466 int status = data->task.tk_status;
467
468 if (status < 0)
Trond Myklebustfba73002011-10-19 12:17:29 -0700469 set_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (atomic_dec_and_test(&req->wb_complete)) {
Trond Myklebustfba73002011-10-19 12:17:29 -0700472 if (!test_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 SetPageUptodate(page);
474 nfs_readpage_release(req);
475 }
Fred Isamancd841602012-04-20 14:47:44 -0400476 nfs_readdata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400479void nfs_read_prepare(struct rpc_task *task, void *calldata)
480{
481 struct nfs_read_data *data = calldata;
Fred Isamancd841602012-04-20 14:47:44 -0400482 NFS_PROTO(data->header->inode)->read_rpc_prepare(task, data);
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400483}
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400484
Trond Myklebustec06c092006-03-20 13:44:27 -0500485static const struct rpc_call_ops nfs_read_partial_ops = {
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400486 .rpc_call_prepare = nfs_read_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -0500487 .rpc_call_done = nfs_readpage_result_partial,
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400488 .rpc_release = nfs_readpage_release_partial,
Trond Myklebustec06c092006-03-20 13:44:27 -0500489};
490
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400491static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
492{
493 unsigned int count = data->res.count;
494 unsigned int base = data->args.pgbase;
495 struct page **pages;
496
Trond Myklebust79558f32006-08-22 13:44:32 -0400497 if (data->res.eof)
498 count = data->args.count;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400499 if (unlikely(count == 0))
500 return;
501 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
502 base &= ~PAGE_CACHE_MASK;
503 count += base;
504 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
505 SetPageUptodate(*pages);
Trond Myklebust0b671302006-11-14 16:12:23 -0500506 if (count == 0)
507 return;
508 /* Was this a short read? */
509 if (data->res.eof || data->res.count == data->args.count)
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400510 SetPageUptodate(*pages);
511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/*
514 * This is the callback from RPC telling us whether a reply was
515 * received or some error occurred (timeout or socket shutdown).
516 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500517static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Trond Myklebustec06c092006-03-20 13:44:27 -0500519 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Trond Myklebust0b671302006-11-14 16:12:23 -0500521 if (nfs_readpage_result(task, data) != 0)
522 return;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400523 if (task->tk_status < 0)
524 return;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400525 /*
Trond Myklebust0b671302006-11-14 16:12:23 -0500526 * Note: nfs_readpage_retry may change the values of
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400527 * data->args. In the multi-page case, we therefore need
Trond Myklebust0b671302006-11-14 16:12:23 -0500528 * to ensure that we call nfs_readpage_set_pages_uptodate()
529 * first.
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400530 */
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400531 nfs_readpage_truncate_uninitialised_page(data);
532 nfs_readpage_set_pages_uptodate(data);
533 nfs_readpage_retry(task, data);
534}
535
536static void nfs_readpage_release_full(void *calldata)
537{
538 struct nfs_read_data *data = calldata;
Fred Isamancd841602012-04-20 14:47:44 -0400539 struct nfs_pgio_header *hdr = data->header;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400540
Fred Isamancd841602012-04-20 14:47:44 -0400541 while (!list_empty(&hdr->pages)) {
542 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400544 nfs_list_remove_request(req);
Trond Myklebust62e4a762011-11-10 14:30:37 -0500545 nfs_readpage_release(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400547 nfs_readdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Trond Myklebustec06c092006-03-20 13:44:27 -0500550static const struct rpc_call_ops nfs_read_full_ops = {
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400551 .rpc_call_prepare = nfs_read_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -0500552 .rpc_call_done = nfs_readpage_result_full,
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400553 .rpc_release = nfs_readpage_release_full,
Trond Myklebustec06c092006-03-20 13:44:27 -0500554};
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * Read a page over NFS.
558 * We read the page synchronously in the following case:
559 * - The error flag is set for this page. This happens only when a
560 * previous async read operation failed.
561 */
562int nfs_readpage(struct file *file, struct page *page)
563{
564 struct nfs_open_context *ctx;
565 struct inode *inode = page->mapping->host;
566 int error;
567
568 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
569 page, PAGE_CACHE_SIZE, page->index);
Chuck Lever91d5b472006-03-20 13:44:14 -0500570 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
571 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /*
574 * Try to flush any pending writes to the file..
575 *
576 * NOTE! Because we own the page lock, there cannot
577 * be any new pending writes generated at this point
578 * for this page (other pages can be written to).
579 */
580 error = nfs_wb_page(inode, page);
581 if (error)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400582 goto out_unlock;
583 if (PageUptodate(page))
584 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400586 error = -ESTALE;
587 if (NFS_STALE(inode))
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400588 goto out_unlock;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (file == NULL) {
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500591 error = -EBADF;
Trond Myklebustd5308382005-11-04 15:33:38 -0500592 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (ctx == NULL)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400594 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400596 ctx = get_nfs_open_context(nfs_file_open_context(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
David Howells9a9fc1c2009-04-03 16:42:44 +0100598 if (!IS_SYNC(inode)) {
599 error = nfs_readpage_from_fscache(ctx, inode, page);
600 if (error == 0)
601 goto out;
602 }
603
Trond Myklebust8e0969f2006-12-13 15:23:44 -0500604 error = nfs_readpage_async(ctx, inode, page);
605
David Howells9a9fc1c2009-04-03 16:42:44 +0100606out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 put_nfs_open_context(ctx);
608 return error;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400609out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 unlock_page(page);
611 return error;
612}
613
614struct nfs_readdesc {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400615 struct nfs_pageio_descriptor *pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 struct nfs_open_context *ctx;
617};
618
619static int
620readpage_async_filler(void *data, struct page *page)
621{
622 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
623 struct inode *inode = page->mapping->host;
624 struct nfs_page *new;
625 unsigned int len;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400626 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Trond Myklebust49a70f22006-12-05 00:35:38 -0500628 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (len == 0)
630 return nfs_return_empty_page(page);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 new = nfs_create_request(desc->ctx, inode, page, 0, len);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400633 if (IS_ERR(new))
634 goto out_error;
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800637 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400638 if (!nfs_pageio_add_request(desc->pgio, new)) {
639 error = desc->pgio->pg_error;
640 goto out_unlock;
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 0;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400643out_error:
644 error = PTR_ERR(new);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400645out_unlock:
646 unlock_page(page);
647 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650int nfs_readpages(struct file *filp, struct address_space *mapping,
651 struct list_head *pages, unsigned nr_pages)
652{
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400653 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct nfs_readdesc desc = {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400655 .pgio = &pgio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 };
657 struct inode *inode = mapping->host;
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400658 unsigned long npages;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400659 int ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
662 inode->i_sb->s_id,
663 (long long)NFS_FILEID(inode),
664 nr_pages);
Chuck Lever91d5b472006-03-20 13:44:14 -0500665 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400667 if (NFS_STALE(inode))
668 goto out;
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (filp == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500671 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (desc.ctx == NULL)
673 return -EBADF;
674 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400675 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
David Howells9a9fc1c2009-04-03 16:42:44 +0100676
677 /* attempt to read as many of the pages as possible from the cache
678 * - this returns -ENOBUFS immediately if the cookie is negative
679 */
680 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
681 pages, &nr_pages);
682 if (ret == 0)
683 goto read_complete; /* all pages were read */
684
Trond Myklebust1751c362011-06-10 13:30:23 -0400685 nfs_pageio_init_read(&pgio, inode);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400688
689 nfs_pageio_complete(&pgio);
690 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
691 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
David Howells9a9fc1c2009-04-03 16:42:44 +0100692read_complete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 put_nfs_open_context(desc.ctx);
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400694out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return ret;
696}
697
David Howellsf7b422b2006-06-09 09:34:33 -0400698int __init nfs_init_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
Fred Isamancd841602012-04-20 14:47:44 -0400701 sizeof(struct nfs_read_header),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900703 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (nfs_rdata_cachep == NULL)
705 return -ENOMEM;
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 0;
708}
709
David Brownell266bee82006-06-27 12:59:15 -0700710void nfs_destroy_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700712 kmem_cache_destroy(nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}