blob: 8b48ec63f7229de8cb9435cfe82cc9d9876e6268 [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
23#include <asm/system.h>
Fred Isamanbae724e2011-03-01 01:34:15 +000024#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Andy Adamsonf11c88a2009-04-01 09:22:25 -040026#include "nfs4_fs.h"
Trond Myklebust49a70f22006-12-05 00:35:38 -050027#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050028#include "iostat.h"
David Howells9a9fc1c2009-04-03 16:42:44 +010029#include "fscache.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define NFSDBG_FACILITY NFSDBG_PAGECACHE
32
Trond Myklebust1751c362011-06-10 13:30:23 -040033static const struct nfs_pageio_ops nfs_pageio_read_ops;
Trond Myklebustec06c092006-03-20 13:44:27 -050034static const struct rpc_call_ops nfs_read_partial_ops;
35static const struct rpc_call_ops nfs_read_full_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christoph Lametere18b8902006-12-06 20:33:20 -080037static struct kmem_cache *nfs_rdata_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Trond Myklebust8d5658c2007-04-10 09:26:35 -040039struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050040{
Trond Myklebustb6ee8cd2011-10-19 12:17:29 -070041 struct nfs_read_data *p;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050042
Trond Myklebustb6ee8cd2011-10-19 12:17:29 -070043 p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050044 if (p) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050045 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070046 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040047 if (pagecount <= ARRAY_SIZE(p->page_array))
48 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050049 else {
Trond Myklebust93870d72010-05-13 12:51:03 -040050 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040051 if (!p->pagevec) {
Trond Myklebustb6ee8cd2011-10-19 12:17:29 -070052 kmem_cache_free(nfs_rdata_cachep, p);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050053 p = NULL;
54 }
55 }
56 }
57 return p;
58}
59
Trond Myklebust1ae88b22009-08-12 09:12:30 -040060void nfs_readdata_free(struct nfs_read_data *p)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050061{
62 if (p && (p->pagevec != &p->page_array[0]))
63 kfree(p->pagevec);
Trond Myklebustb6ee8cd2011-10-19 12:17:29 -070064 kmem_cache_free(nfs_rdata_cachep, p);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050065}
66
Trond Myklebust493292d2011-07-13 15:58:28 -040067void nfs_readdata_release(struct nfs_read_data *rdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Fred Isamanbae724e2011-03-01 01:34:15 +000069 put_lseg(rdata->lseg);
Trond Myklebust383ba712008-02-19 20:04:20 -050070 put_nfs_open_context(rdata->args.context);
71 nfs_readdata_free(rdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74static
Linus Torvalds1da177e2005-04-16 15:20:36 -070075int nfs_return_empty_page(struct page *page)
76{
Christoph Lametereebd2aa2008-02-04 22:28:29 -080077 zero_user(page, 0, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 SetPageUptodate(page);
79 unlock_page(page);
80 return 0;
81}
82
Trond Myklebust1de3fc12006-05-25 01:40:44 -040083static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
84{
85 unsigned int remainder = data->args.count - data->res.count;
86 unsigned int base = data->args.pgbase + data->res.count;
87 unsigned int pglen;
88 struct page **pages;
89
90 if (data->res.eof == 0 || remainder == 0)
91 return;
92 /*
93 * Note: "remainder" can never be negative, since we check for
94 * this in the XDR code.
95 */
96 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
97 base &= ~PAGE_CACHE_MASK;
98 pglen = PAGE_CACHE_SIZE - base;
Trond Myklebust79558f32006-08-22 13:44:32 -040099 for (;;) {
100 if (remainder <= pglen) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800101 zero_user(*pages, base, remainder);
Trond Myklebust79558f32006-08-22 13:44:32 -0400102 break;
103 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800104 zero_user(*pages, base, pglen);
Trond Myklebust79558f32006-08-22 13:44:32 -0400105 pages++;
106 remainder -= pglen;
107 pglen = PAGE_CACHE_SIZE;
108 base = 0;
109 }
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400110}
111
Trond Myklebust1f945352011-07-13 15:59:57 -0400112static void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor *pgio,
Trond Myklebust1751c362011-06-10 13:30:23 -0400113 struct inode *inode)
114{
115 nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops,
116 NFS_SERVER(inode)->rsize, 0);
117}
118
Trond Myklebust493292d2011-07-13 15:58:28 -0400119void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
120{
121 pgio->pg_ops = &nfs_pageio_read_ops;
122 pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
123}
Trond Myklebust1f945352011-07-13 15:59:57 -0400124EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
Trond Myklebust493292d2011-07-13 15:58:28 -0400125
Trond Myklebust1751c362011-06-10 13:30:23 -0400126static void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
127 struct inode *inode)
128{
129 if (!pnfs_pageio_init_read(pgio, inode))
130 nfs_pageio_init_read_mds(pgio, inode);
131}
132
David Howellsf42b2932009-04-03 16:42:44 +0100133int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
134 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct nfs_page *new;
137 unsigned int len;
Fred Isamanc76069b2011-03-03 15:13:48 +0000138 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Trond Myklebust49a70f22006-12-05 00:35:38 -0500140 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (len == 0)
142 return nfs_return_empty_page(page);
143 new = nfs_create_request(ctx, inode, page, 0, len);
144 if (IS_ERR(new)) {
145 unlock_page(page);
146 return PTR_ERR(new);
147 }
148 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800149 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Trond Myklebust1751c362011-06-10 13:30:23 -0400151 nfs_pageio_init_read(&pgio, inode);
Trond Myklebustd8007d42011-06-10 13:30:23 -0400152 nfs_pageio_add_request(&pgio, new);
Trond Myklebust1751c362011-06-10 13:30:23 -0400153 nfs_pageio_complete(&pgio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return 0;
155}
156
157static void nfs_readpage_release(struct nfs_page *req)
158{
Al Viro3d4ff432011-06-22 18:40:12 -0400159 struct inode *d_inode = req->wb_context->dentry->d_inode;
David Howells7f8e05f2009-04-03 16:42:45 +0100160
161 if (PageUptodate(req->wb_page))
162 nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unlock_page(req->wb_page);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
Al Viro3d4ff432011-06-22 18:40:12 -0400167 req->wb_context->dentry->d_inode->i_sb->s_id,
168 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 req->wb_bytes,
170 (long long)req_offset(req));
Nick Wilson10d2c462005-09-22 21:44:28 -0700171 nfs_release_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000174int nfs_initiate_read(struct nfs_read_data *data, struct rpc_clnt *clnt,
Andy Adamson64419a92011-03-01 01:34:16 +0000175 const struct rpc_call_ops *call_ops)
176{
177 struct inode *inode = data->inode;
178 int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
179 struct rpc_task *task;
180 struct rpc_message msg = {
181 .rpc_argp = &data->args,
182 .rpc_resp = &data->res,
183 .rpc_cred = data->cred,
184 };
185 struct rpc_task_setup task_setup_data = {
186 .task = &data->task,
187 .rpc_client = clnt,
188 .rpc_message = &msg,
189 .callback_ops = call_ops,
190 .callback_data = data,
191 .workqueue = nfsiod_workqueue,
192 .flags = RPC_TASK_ASYNC | swap_flags,
193 };
194
195 /* Set up the initial task struct. */
196 NFS_PROTO(inode)->read_setup(data, &msg);
197
198 dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
199 "offset %llu)\n",
200 data->task.tk_pid,
201 inode->i_sb->s_id,
202 (long long)NFS_FILEID(inode),
203 data->args.count,
204 (unsigned long long)data->args.offset);
205
206 task = rpc_run_task(&task_setup_data);
207 if (IS_ERR(task))
208 return PTR_ERR(task);
209 rpc_put_task(task);
210 return 0;
211}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000212EXPORT_SYMBOL_GPL(nfs_initiate_read);
Andy Adamson64419a92011-03-01 01:34:16 +0000213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
215 * Set up the NFS read request struct
216 */
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400217static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
218 unsigned int count, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Al Viro3d4ff432011-06-22 18:40:12 -0400220 struct inode *inode = req->wb_context->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 data->req = req;
Trond Myklebust84115e12007-07-14 15:39:59 -0400223 data->inode = inode;
Andy Adamson64419a92011-03-01 01:34:16 +0000224 data->cred = req->wb_context->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 data->args.fh = NFS_FH(inode);
227 data->args.offset = req_offset(req) + offset;
228 data->args.pgbase = req->wb_pgbase + offset;
229 data->args.pages = data->pagevec;
230 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500231 data->args.context = get_nfs_open_context(req->wb_context);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400232 data->args.lock_context = req->wb_lock_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 data->res.fattr = &data->fattr;
235 data->res.count = count;
236 data->res.eof = 0;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400237 nfs_fattr_init(&data->fattr);
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400238}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400240static int nfs_do_read(struct nfs_read_data *data,
Trond Myklebust493292d2011-07-13 15:58:28 -0400241 const struct rpc_call_ops *call_ops)
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400242{
Stephen Rothwell5f00bcb2011-07-25 13:59:46 -0400243 struct inode *inode = data->args.context->dentry->d_inode;
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400244
Andy Adamson64419a92011-03-01 01:34:16 +0000245 return nfs_initiate_read(data, NFS_CLIENT(inode), call_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Trond Myklebust275acaa2011-07-12 13:42:02 -0400248static int
249nfs_do_multiple_reads(struct list_head *head,
Trond Myklebust493292d2011-07-13 15:58:28 -0400250 const struct rpc_call_ops *call_ops)
Trond Myklebust275acaa2011-07-12 13:42:02 -0400251{
252 struct nfs_read_data *data;
253 int ret = 0;
254
255 while (!list_empty(head)) {
256 int ret2;
257
258 data = list_entry(head->next, struct nfs_read_data, list);
259 list_del_init(&data->list);
260
Trond Myklebust493292d2011-07-13 15:58:28 -0400261 ret2 = nfs_do_read(data, call_ops);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400262 if (ret == 0)
263 ret = ret2;
264 }
265 return ret;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static void
269nfs_async_read_error(struct list_head *head)
270{
271 struct nfs_page *req;
272
273 while (!list_empty(head)) {
274 req = nfs_list_entry(head->next);
275 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 nfs_readpage_release(req);
277 }
278}
279
280/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * Generate multiple requests to fill a single page.
282 *
283 * We optimize to reduce the number of read operations on the wire. If we
284 * detect that we're reading a page, or an area of a page, that is past the
285 * end of file, we do not generate NFS read operations but just clear the
286 * parts of the page that would have come back zero from the server anyway.
287 *
288 * We rely on the cached value of i_size to make this determination; another
289 * client can fill pages on the server past our cached end-of-file, but we
290 * won't see the new data until our attribute cache is updated. This is more
291 * or less conventional NFS client behavior.
292 */
Trond Myklebust275acaa2011-07-12 13:42:02 -0400293static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc, struct list_head *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Fred Isamanc76069b2011-03-03 15:13:48 +0000295 struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct page *page = req->wb_page;
297 struct nfs_read_data *data;
Trond Myklebustd0979712011-07-12 13:42:02 -0400298 size_t rsize = desc->pg_bsize, nbytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700299 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int requests = 0;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400301 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 nfs_list_remove_request(req);
304
Trond Myklebust275acaa2011-07-12 13:42:02 -0400305 offset = 0;
Fred Isamanc76069b2011-03-03 15:13:48 +0000306 nbytes = desc->pg_count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700307 do {
308 size_t len = min(nbytes,rsize);
309
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400310 data = nfs_readdata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!data)
312 goto out_bad;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400313 data->pagevec[0] = page;
314 nfs_read_rpcsetup(req, data, len, offset);
315 list_add(&data->list, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700317 nbytes -= len;
Trond Myklebust275acaa2011-07-12 13:42:02 -0400318 offset += len;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700319 } while(nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 atomic_set(&req->wb_complete, requests);
Trond Myklebust50828d72011-07-12 13:42:02 -0400321 desc->pg_rpc_callops = &nfs_read_partial_ops;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400322 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323out_bad:
Trond Myklebust275acaa2011-07-12 13:42:02 -0400324 while (!list_empty(res)) {
325 data = list_entry(res->next, struct nfs_read_data, list);
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400326 list_del(&data->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 nfs_readdata_free(data);
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 nfs_readpage_release(req);
330 return -ENOMEM;
331}
332
Trond Myklebust275acaa2011-07-12 13:42:02 -0400333static int nfs_pagein_one(struct nfs_pageio_descriptor *desc, struct list_head *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct nfs_page *req;
336 struct page **pages;
337 struct nfs_read_data *data;
Fred Isamanc76069b2011-03-03 15:13:48 +0000338 struct list_head *head = &desc->pg_list;
Peng Tao3b609182011-07-15 03:33:42 -0400339 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Fred Isamanc76069b2011-03-03 15:13:48 +0000341 data = nfs_readdata_alloc(nfs_page_array_len(desc->pg_base,
342 desc->pg_count));
Fred Isamanbae724e2011-03-01 01:34:15 +0000343 if (!data) {
344 nfs_async_read_error(head);
Peng Tao3b609182011-07-15 03:33:42 -0400345 ret = -ENOMEM;
Fred Isamanbae724e2011-03-01 01:34:15 +0000346 goto out;
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 while (!list_empty(head)) {
351 req = nfs_list_entry(head->next);
352 nfs_list_remove_request(req);
353 nfs_list_add_request(req, &data->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 req = nfs_list_entry(data->pages.next);
357
Trond Myklebust6e4efd52011-07-12 13:42:02 -0400358 nfs_read_rpcsetup(req, data, desc->pg_count, 0);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400359 list_add(&data->list, res);
Trond Myklebust50828d72011-07-12 13:42:02 -0400360 desc->pg_rpc_callops = &nfs_read_full_ops;
Fred Isamanbae724e2011-03-01 01:34:15 +0000361out:
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400362 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Trond Myklebust493292d2011-07-13 15:58:28 -0400365int nfs_generic_pagein(struct nfs_pageio_descriptor *desc, struct list_head *head)
366{
367 if (desc->pg_bsize < PAGE_CACHE_SIZE)
368 return nfs_pagein_multi(desc, head);
369 return nfs_pagein_one(desc, head);
370}
371
372static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
Trond Myklebust1751c362011-06-10 13:30:23 -0400373{
Trond Myklebust275acaa2011-07-12 13:42:02 -0400374 LIST_HEAD(head);
375 int ret;
376
Trond Myklebust493292d2011-07-13 15:58:28 -0400377 ret = nfs_generic_pagein(desc, &head);
Trond Myklebust50828d72011-07-12 13:42:02 -0400378 if (ret == 0)
Trond Myklebust493292d2011-07-13 15:58:28 -0400379 ret = nfs_do_multiple_reads(&head, desc->pg_rpc_callops);
Trond Myklebust275acaa2011-07-12 13:42:02 -0400380 return ret;
Trond Myklebust1751c362011-06-10 13:30:23 -0400381}
Trond Myklebust1751c362011-06-10 13:30:23 -0400382
383static const struct nfs_pageio_ops nfs_pageio_read_ops = {
384 .pg_test = nfs_generic_pg_test,
385 .pg_doio = nfs_generic_pg_readpages,
386};
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/*
Trond Myklebust0b671302006-11-14 16:12:23 -0500389 * This is the callback from RPC telling us whether a reply was
390 * received or some error occurred (timeout or socket shutdown).
391 */
392int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
393{
394 int status;
395
Harvey Harrison3110ff82008-05-02 13:42:44 -0700396 dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
Trond Myklebust0b671302006-11-14 16:12:23 -0500397 task->tk_status);
398
399 status = NFS_PROTO(data->inode)->read_done(task, data);
400 if (status != 0)
401 return status;
402
403 nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
404
405 if (task->tk_status == -ESTALE) {
Benny Halevy3a10c302008-01-23 08:58:59 +0200406 set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags);
Trond Myklebust0b671302006-11-14 16:12:23 -0500407 nfs_mark_for_revalidate(data->inode);
408 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500409 return 0;
410}
411
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400412static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
Trond Myklebust0b671302006-11-14 16:12:23 -0500413{
414 struct nfs_readargs *argp = &data->args;
415 struct nfs_readres *resp = &data->res;
416
417 if (resp->eof || resp->count == argp->count)
Trond Myklebustd61e6122009-12-05 19:32:19 -0500418 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500419
420 /* This is a short read! */
421 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
422 /* Has the server at least made some progress? */
423 if (resp->count == 0)
Trond Myklebustd61e6122009-12-05 19:32:19 -0500424 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500425
426 /* Yes, so retry the read at the end of the data */
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000427 data->mds_offset += resp->count;
Trond Myklebust0b671302006-11-14 16:12:23 -0500428 argp->offset += resp->count;
429 argp->pgbase += resp->count;
430 argp->count -= resp->count;
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700431 rpc_restart_call_prepare(task);
Trond Myklebust0b671302006-11-14 16:12:23 -0500432}
433
434/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * Handle a read reply that fills part of a page.
436 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500437static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Trond Myklebustec06c092006-03-20 13:44:27 -0500439 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Trond Myklebustec06c092006-03-20 13:44:27 -0500441 if (nfs_readpage_result(task, data) != 0)
442 return;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400443 if (task->tk_status < 0)
444 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500445
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400446 nfs_readpage_truncate_uninitialised_page(data);
447 nfs_readpage_retry(task, data);
448}
449
450static void nfs_readpage_release_partial(void *calldata)
451{
452 struct nfs_read_data *data = calldata;
453 struct nfs_page *req = data->req;
454 struct page *page = req->wb_page;
455 int status = data->task.tk_status;
456
457 if (status < 0)
Trond Myklebustfba73002011-10-19 12:17:29 -0700458 set_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (atomic_dec_and_test(&req->wb_complete)) {
Trond Myklebustfba73002011-10-19 12:17:29 -0700461 if (!test_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 SetPageUptodate(page);
463 nfs_readpage_release(req);
464 }
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400465 nfs_readdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400468#if defined(CONFIG_NFS_V4_1)
469void nfs_read_prepare(struct rpc_task *task, void *calldata)
470{
471 struct nfs_read_data *data = calldata;
472
Trond Myklebust035168a2010-06-16 09:52:26 -0400473 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400474 &data->args.seq_args, &data->res.seq_res,
475 0, task))
476 return;
477 rpc_call_start(task);
478}
479#endif /* CONFIG_NFS_V4_1 */
480
Trond Myklebustec06c092006-03-20 13:44:27 -0500481static const struct rpc_call_ops nfs_read_partial_ops = {
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400482#if defined(CONFIG_NFS_V4_1)
483 .rpc_call_prepare = nfs_read_prepare,
484#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500485 .rpc_call_done = nfs_readpage_result_partial,
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400486 .rpc_release = nfs_readpage_release_partial,
Trond Myklebustec06c092006-03-20 13:44:27 -0500487};
488
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400489static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
490{
491 unsigned int count = data->res.count;
492 unsigned int base = data->args.pgbase;
493 struct page **pages;
494
Trond Myklebust79558f32006-08-22 13:44:32 -0400495 if (data->res.eof)
496 count = data->args.count;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400497 if (unlikely(count == 0))
498 return;
499 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
500 base &= ~PAGE_CACHE_MASK;
501 count += base;
502 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
503 SetPageUptodate(*pages);
Trond Myklebust0b671302006-11-14 16:12:23 -0500504 if (count == 0)
505 return;
506 /* Was this a short read? */
507 if (data->res.eof || data->res.count == data->args.count)
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400508 SetPageUptodate(*pages);
509}
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
512 * This is the callback from RPC telling us whether a reply was
513 * received or some error occurred (timeout or socket shutdown).
514 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500515static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Trond Myklebustec06c092006-03-20 13:44:27 -0500517 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Trond Myklebust0b671302006-11-14 16:12:23 -0500519 if (nfs_readpage_result(task, data) != 0)
520 return;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400521 if (task->tk_status < 0)
522 return;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400523 /*
Trond Myklebust0b671302006-11-14 16:12:23 -0500524 * Note: nfs_readpage_retry may change the values of
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400525 * data->args. In the multi-page case, we therefore need
Trond Myklebust0b671302006-11-14 16:12:23 -0500526 * to ensure that we call nfs_readpage_set_pages_uptodate()
527 * first.
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400528 */
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400529 nfs_readpage_truncate_uninitialised_page(data);
530 nfs_readpage_set_pages_uptodate(data);
531 nfs_readpage_retry(task, data);
532}
533
534static void nfs_readpage_release_full(void *calldata)
535{
536 struct nfs_read_data *data = calldata;
Peng Tao9b7eecd2011-09-22 21:50:15 -0400537 struct nfs_pageio_descriptor pgio;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400538
Peng Tao9b7eecd2011-09-22 21:50:15 -0400539 if (data->pnfs_error) {
540 nfs_pageio_init_read_mds(&pgio, data->inode);
541 pgio.pg_recoalesce = 1;
542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 while (!list_empty(&data->pages)) {
544 struct nfs_page *req = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400546 nfs_list_remove_request(req);
Peng Tao9b7eecd2011-09-22 21:50:15 -0400547 if (!data->pnfs_error)
548 nfs_readpage_release(req);
549 else
550 nfs_pageio_add_request(&pgio, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Peng Tao9b7eecd2011-09-22 21:50:15 -0400552 if (data->pnfs_error)
553 nfs_pageio_complete(&pgio);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400554 nfs_readdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Trond Myklebustec06c092006-03-20 13:44:27 -0500557static const struct rpc_call_ops nfs_read_full_ops = {
Andy Adamsonf11c88a2009-04-01 09:22:25 -0400558#if defined(CONFIG_NFS_V4_1)
559 .rpc_call_prepare = nfs_read_prepare,
560#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500561 .rpc_call_done = nfs_readpage_result_full,
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400562 .rpc_release = nfs_readpage_release_full,
Trond Myklebustec06c092006-03-20 13:44:27 -0500563};
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * Read a page over NFS.
567 * We read the page synchronously in the following case:
568 * - The error flag is set for this page. This happens only when a
569 * previous async read operation failed.
570 */
571int nfs_readpage(struct file *file, struct page *page)
572{
573 struct nfs_open_context *ctx;
574 struct inode *inode = page->mapping->host;
575 int error;
576
577 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
578 page, PAGE_CACHE_SIZE, page->index);
Chuck Lever91d5b472006-03-20 13:44:14 -0500579 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
580 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /*
583 * Try to flush any pending writes to the file..
584 *
585 * NOTE! Because we own the page lock, there cannot
586 * be any new pending writes generated at this point
587 * for this page (other pages can be written to).
588 */
589 error = nfs_wb_page(inode, page);
590 if (error)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400591 goto out_unlock;
592 if (PageUptodate(page))
593 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400595 error = -ESTALE;
596 if (NFS_STALE(inode))
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400597 goto out_unlock;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (file == NULL) {
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500600 error = -EBADF;
Trond Myklebustd5308382005-11-04 15:33:38 -0500601 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (ctx == NULL)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400603 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400605 ctx = get_nfs_open_context(nfs_file_open_context(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
David Howells9a9fc1c2009-04-03 16:42:44 +0100607 if (!IS_SYNC(inode)) {
608 error = nfs_readpage_from_fscache(ctx, inode, page);
609 if (error == 0)
610 goto out;
611 }
612
Trond Myklebust8e0969f2006-12-13 15:23:44 -0500613 error = nfs_readpage_async(ctx, inode, page);
614
David Howells9a9fc1c2009-04-03 16:42:44 +0100615out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 put_nfs_open_context(ctx);
617 return error;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400618out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 unlock_page(page);
620 return error;
621}
622
623struct nfs_readdesc {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400624 struct nfs_pageio_descriptor *pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct nfs_open_context *ctx;
626};
627
628static int
629readpage_async_filler(void *data, struct page *page)
630{
631 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
632 struct inode *inode = page->mapping->host;
633 struct nfs_page *new;
634 unsigned int len;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400635 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Trond Myklebust49a70f22006-12-05 00:35:38 -0500637 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (len == 0)
639 return nfs_return_empty_page(page);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 new = nfs_create_request(desc->ctx, inode, page, 0, len);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400642 if (IS_ERR(new))
643 goto out_error;
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800646 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400647 if (!nfs_pageio_add_request(desc->pgio, new)) {
648 error = desc->pgio->pg_error;
649 goto out_unlock;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400652out_error:
653 error = PTR_ERR(new);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400654out_unlock:
655 unlock_page(page);
656 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659int nfs_readpages(struct file *filp, struct address_space *mapping,
660 struct list_head *pages, unsigned nr_pages)
661{
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400662 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 struct nfs_readdesc desc = {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400664 .pgio = &pgio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 };
666 struct inode *inode = mapping->host;
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400667 unsigned long npages;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400668 int ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
671 inode->i_sb->s_id,
672 (long long)NFS_FILEID(inode),
673 nr_pages);
Chuck Lever91d5b472006-03-20 13:44:14 -0500674 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400676 if (NFS_STALE(inode))
677 goto out;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (filp == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500680 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (desc.ctx == NULL)
682 return -EBADF;
683 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400684 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
David Howells9a9fc1c2009-04-03 16:42:44 +0100685
686 /* attempt to read as many of the pages as possible from the cache
687 * - this returns -ENOBUFS immediately if the cookie is negative
688 */
689 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
690 pages, &nr_pages);
691 if (ret == 0)
692 goto read_complete; /* all pages were read */
693
Trond Myklebust1751c362011-06-10 13:30:23 -0400694 nfs_pageio_init_read(&pgio, inode);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400697
698 nfs_pageio_complete(&pgio);
699 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
700 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
David Howells9a9fc1c2009-04-03 16:42:44 +0100701read_complete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 put_nfs_open_context(desc.ctx);
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400703out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return ret;
705}
706
David Howellsf7b422b2006-06-09 09:34:33 -0400707int __init nfs_init_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
710 sizeof(struct nfs_read_data),
711 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900712 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (nfs_rdata_cachep == NULL)
714 return -ENOMEM;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 0;
717}
718
David Brownell266bee82006-06-27 12:59:15 -0700719void nfs_destroy_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700721 kmem_cache_destroy(nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}