blob: e18ba792872f2db9b9ece8f2d8e6cc20297b17bd [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>
21#include <linux/smp_lock.h>
22
23#include <asm/system.h>
24
Trond Myklebust49a70f22006-12-05 00:35:38 -050025#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050026#include "iostat.h"
David Howells9a9fc1c2009-04-03 16:42:44 +010027#include "fscache.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define NFSDBG_FACILITY NFSDBG_PAGECACHE
30
Trond Myklebust8d5658c2007-04-10 09:26:35 -040031static int nfs_pagein_multi(struct inode *, struct list_head *, unsigned int, size_t, int);
32static int nfs_pagein_one(struct inode *, struct list_head *, unsigned int, size_t, int);
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;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050037static mempool_t *nfs_rdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define MIN_POOL_READ (32)
40
Trond Myklebust8d5658c2007-04-10 09:26:35 -040041struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050042{
Christoph Lametere6b4f8d2006-12-06 20:33:14 -080043 struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_NOFS);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050044
45 if (p) {
46 memset(p, 0, sizeof(*p));
47 INIT_LIST_HEAD(&p->pages);
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070048 p->npages = pagecount;
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040049 if (pagecount <= ARRAY_SIZE(p->page_array))
50 p->pagevec = p->page_array;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050051 else {
Chuck Lever0d0b5cb2006-05-25 01:40:53 -040052 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
53 if (!p->pagevec) {
Trond Myklebust3feb2d42006-03-20 13:44:37 -050054 mempool_free(p, nfs_rdata_mempool);
55 p = NULL;
56 }
57 }
58 }
59 return p;
60}
61
Trond Myklebust5e4424a2008-02-25 21:53:49 -080062static void nfs_readdata_free(struct nfs_read_data *p)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050063{
64 if (p && (p->pagevec != &p->page_array[0]))
65 kfree(p->pagevec);
66 mempool_free(p, nfs_rdata_mempool);
67}
68
Trond Myklebust963d8fe2006-01-03 09:55:04 +010069void nfs_readdata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Trond Myklebust383ba712008-02-19 20:04:20 -050071 struct nfs_read_data *rdata = data;
72
73 put_nfs_open_context(rdata->args.context);
74 nfs_readdata_free(rdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static
Linus Torvalds1da177e2005-04-16 15:20:36 -070078int nfs_return_empty_page(struct page *page)
79{
Christoph Lametereebd2aa2008-02-04 22:28:29 -080080 zero_user(page, 0, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 SetPageUptodate(page);
82 unlock_page(page);
83 return 0;
84}
85
Trond Myklebust1de3fc12006-05-25 01:40:44 -040086static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
87{
88 unsigned int remainder = data->args.count - data->res.count;
89 unsigned int base = data->args.pgbase + data->res.count;
90 unsigned int pglen;
91 struct page **pages;
92
93 if (data->res.eof == 0 || remainder == 0)
94 return;
95 /*
96 * Note: "remainder" can never be negative, since we check for
97 * this in the XDR code.
98 */
99 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
100 base &= ~PAGE_CACHE_MASK;
101 pglen = PAGE_CACHE_SIZE - base;
Trond Myklebust79558f32006-08-22 13:44:32 -0400102 for (;;) {
103 if (remainder <= pglen) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800104 zero_user(*pages, base, remainder);
Trond Myklebust79558f32006-08-22 13:44:32 -0400105 break;
106 }
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800107 zero_user(*pages, base, pglen);
Trond Myklebust79558f32006-08-22 13:44:32 -0400108 pages++;
109 remainder -= pglen;
110 pglen = PAGE_CACHE_SIZE;
111 base = 0;
112 }
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400113}
114
David Howellsf42b2932009-04-03 16:42:44 +0100115int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
116 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 LIST_HEAD(one_request);
119 struct nfs_page *new;
120 unsigned int len;
121
Trond Myklebust49a70f22006-12-05 00:35:38 -0500122 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (len == 0)
124 return nfs_return_empty_page(page);
125 new = nfs_create_request(ctx, inode, page, 0, len);
126 if (IS_ERR(new)) {
127 unlock_page(page);
128 return PTR_ERR(new);
129 }
130 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800131 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 nfs_list_add_request(new, &one_request);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400134 if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400135 nfs_pagein_multi(inode, &one_request, 1, len, 0);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400136 else
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400137 nfs_pagein_one(inode, &one_request, 1, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139}
140
141static void nfs_readpage_release(struct nfs_page *req)
142{
143 unlock_page(req->wb_page);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
Trond Myklebust88be9f92007-06-05 10:42:27 -0400146 req->wb_context->path.dentry->d_inode->i_sb->s_id,
147 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 req->wb_bytes,
149 (long long)req_offset(req));
Nick Wilson10d2c462005-09-22 21:44:28 -0700150 nfs_clear_request(req);
151 nfs_release_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154/*
155 * Set up the NFS read request struct
156 */
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400157static int nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
Trond Myklebustec06c092006-03-20 13:44:27 -0500158 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 unsigned int count, unsigned int offset)
160{
Trond Myklebust84115e12007-07-14 15:39:59 -0400161 struct inode *inode = req->wb_context->path.dentry->d_inode;
162 int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
Trond Myklebust07737692007-10-25 18:42:54 -0400163 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400164 struct rpc_message msg = {
165 .rpc_argp = &data->args,
166 .rpc_resp = &data->res,
167 .rpc_cred = req->wb_context->cred,
168 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400169 struct rpc_task_setup task_setup_data = {
Trond Myklebust07737692007-10-25 18:42:54 -0400170 .task = &data->task,
Trond Myklebust84115e12007-07-14 15:39:59 -0400171 .rpc_client = NFS_CLIENT(inode),
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400172 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -0400173 .callback_ops = call_ops,
174 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -0500175 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -0400176 .flags = RPC_TASK_ASYNC | swap_flags,
177 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 data->req = req;
Trond Myklebust84115e12007-07-14 15:39:59 -0400180 data->inode = inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400181 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 data->args.fh = NFS_FH(inode);
184 data->args.offset = req_offset(req) + offset;
185 data->args.pgbase = req->wb_pgbase + offset;
186 data->args.pages = data->pagevec;
187 data->args.count = count;
Trond Myklebust383ba712008-02-19 20:04:20 -0500188 data->args.context = get_nfs_open_context(req->wb_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 data->res.fattr = &data->fattr;
191 data->res.count = count;
192 data->res.eof = 0;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400193 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Trond Myklebustec06c092006-03-20 13:44:27 -0500195 /* Set up the initial task struct. */
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400196 NFS_PROTO(inode)->read_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Chuck Levera3f565b2007-01-31 12:14:01 -0500198 dprintk("NFS: %5u initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 data->task.tk_pid,
200 inode->i_sb->s_id,
201 (long long)NFS_FILEID(inode),
202 count,
203 (unsigned long long)data->args.offset);
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400204
Trond Myklebust07737692007-10-25 18:42:54 -0400205 task = rpc_run_task(&task_setup_data);
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400206 if (IS_ERR(task))
207 return PTR_ERR(task);
208 rpc_put_task(task);
209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212static void
213nfs_async_read_error(struct list_head *head)
214{
215 struct nfs_page *req;
216
217 while (!list_empty(head)) {
218 req = nfs_list_entry(head->next);
219 nfs_list_remove_request(req);
220 SetPageError(req->wb_page);
221 nfs_readpage_release(req);
222 }
223}
224
225/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * Generate multiple requests to fill a single page.
227 *
228 * We optimize to reduce the number of read operations on the wire. If we
229 * detect that we're reading a page, or an area of a page, that is past the
230 * end of file, we do not generate NFS read operations but just clear the
231 * parts of the page that would have come back zero from the server anyway.
232 *
233 * We rely on the cached value of i_size to make this determination; another
234 * client can fill pages on the server past our cached end-of-file, but we
235 * won't see the new data until our attribute cache is updated. This is more
236 * or less conventional NFS client behavior.
237 */
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400238static int nfs_pagein_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct nfs_page *req = nfs_list_entry(head->next);
241 struct page *page = req->wb_page;
242 struct nfs_read_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700243 size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
244 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int requests = 0;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400246 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 LIST_HEAD(list);
248
249 nfs_list_remove_request(req);
250
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400251 nbytes = count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700252 do {
253 size_t len = min(nbytes,rsize);
254
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400255 data = nfs_readdata_alloc(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!data)
257 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 list_add(&data->pages, &list);
259 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700260 nbytes -= len;
261 } while(nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 atomic_set(&req->wb_complete, requests);
263
264 ClearPageError(page);
265 offset = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400266 nbytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 do {
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400268 int ret2;
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 data = list_entry(list.next, struct nfs_read_data, pages);
271 list_del_init(&data->pages);
272
273 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400275 if (nbytes < rsize)
276 rsize = nbytes;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400277 ret2 = nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400278 rsize, offset);
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400279 if (ret == 0)
280 ret = ret2;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400281 offset += rsize;
282 nbytes -= rsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 } while (nbytes != 0);
284
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400285 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287out_bad:
288 while (!list_empty(&list)) {
289 data = list_entry(list.next, struct nfs_read_data, pages);
290 list_del(&data->pages);
291 nfs_readdata_free(data);
292 }
293 SetPageError(page);
294 nfs_readpage_release(req);
295 return -ENOMEM;
296}
297
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400298static int nfs_pagein_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct nfs_page *req;
301 struct page **pages;
302 struct nfs_read_data *data;
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400303 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400305 data = nfs_readdata_alloc(npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!data)
307 goto out_bad;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 while (!list_empty(head)) {
311 req = nfs_list_entry(head->next);
312 nfs_list_remove_request(req);
313 nfs_list_add_request(req, &data->pages);
314 ClearPageError(req->wb_page);
315 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317 req = nfs_list_entry(data->pages.next);
318
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400319 return nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320out_bad:
321 nfs_async_read_error(head);
Trond Myklebustdbae4c72008-04-14 14:54:53 -0400322 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/*
Trond Myklebust0b671302006-11-14 16:12:23 -0500326 * This is the callback from RPC telling us whether a reply was
327 * received or some error occurred (timeout or socket shutdown).
328 */
329int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
330{
331 int status;
332
Harvey Harrison3110ff82008-05-02 13:42:44 -0700333 dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
Trond Myklebust0b671302006-11-14 16:12:23 -0500334 task->tk_status);
335
336 status = NFS_PROTO(data->inode)->read_done(task, data);
337 if (status != 0)
338 return status;
339
340 nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
341
342 if (task->tk_status == -ESTALE) {
Benny Halevy3a10c302008-01-23 08:58:59 +0200343 set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags);
Trond Myklebust0b671302006-11-14 16:12:23 -0500344 nfs_mark_for_revalidate(data->inode);
345 }
Trond Myklebust0b671302006-11-14 16:12:23 -0500346 return 0;
347}
348
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400349static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
Trond Myklebust0b671302006-11-14 16:12:23 -0500350{
351 struct nfs_readargs *argp = &data->args;
352 struct nfs_readres *resp = &data->res;
353
354 if (resp->eof || resp->count == argp->count)
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400355 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500356
357 /* This is a short read! */
358 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
359 /* Has the server at least made some progress? */
360 if (resp->count == 0)
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400361 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500362
363 /* Yes, so retry the read at the end of the data */
364 argp->offset += resp->count;
365 argp->pgbase += resp->count;
366 argp->count -= resp->count;
367 rpc_restart_call(task);
Trond Myklebust0b671302006-11-14 16:12:23 -0500368}
369
370/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * Handle a read reply that fills part of a page.
372 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500373static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Trond Myklebustec06c092006-03-20 13:44:27 -0500375 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Trond Myklebustec06c092006-03-20 13:44:27 -0500377 if (nfs_readpage_result(task, data) != 0)
378 return;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400379 if (task->tk_status < 0)
380 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500381
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400382 nfs_readpage_truncate_uninitialised_page(data);
383 nfs_readpage_retry(task, data);
384}
385
386static void nfs_readpage_release_partial(void *calldata)
387{
388 struct nfs_read_data *data = calldata;
389 struct nfs_page *req = data->req;
390 struct page *page = req->wb_page;
391 int status = data->task.tk_status;
392
393 if (status < 0)
Trond Myklebust0b671302006-11-14 16:12:23 -0500394 SetPageError(page);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (atomic_dec_and_test(&req->wb_complete)) {
397 if (!PageError(page))
398 SetPageUptodate(page);
399 nfs_readpage_release(req);
400 }
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400401 nfs_readdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Trond Myklebustec06c092006-03-20 13:44:27 -0500404static const struct rpc_call_ops nfs_read_partial_ops = {
405 .rpc_call_done = nfs_readpage_result_partial,
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400406 .rpc_release = nfs_readpage_release_partial,
Trond Myklebustec06c092006-03-20 13:44:27 -0500407};
408
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400409static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
410{
411 unsigned int count = data->res.count;
412 unsigned int base = data->args.pgbase;
413 struct page **pages;
414
Trond Myklebust79558f32006-08-22 13:44:32 -0400415 if (data->res.eof)
416 count = data->args.count;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400417 if (unlikely(count == 0))
418 return;
419 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
420 base &= ~PAGE_CACHE_MASK;
421 count += base;
422 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
423 SetPageUptodate(*pages);
Trond Myklebust0b671302006-11-14 16:12:23 -0500424 if (count == 0)
425 return;
426 /* Was this a short read? */
427 if (data->res.eof || data->res.count == data->args.count)
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400428 SetPageUptodate(*pages);
429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
432 * This is the callback from RPC telling us whether a reply was
433 * received or some error occurred (timeout or socket shutdown).
434 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500435static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Trond Myklebustec06c092006-03-20 13:44:27 -0500437 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Trond Myklebust0b671302006-11-14 16:12:23 -0500439 if (nfs_readpage_result(task, data) != 0)
440 return;
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400441 if (task->tk_status < 0)
442 return;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400443 /*
Trond Myklebust0b671302006-11-14 16:12:23 -0500444 * Note: nfs_readpage_retry may change the values of
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400445 * data->args. In the multi-page case, we therefore need
Trond Myklebust0b671302006-11-14 16:12:23 -0500446 * to ensure that we call nfs_readpage_set_pages_uptodate()
447 * first.
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400448 */
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400449 nfs_readpage_truncate_uninitialised_page(data);
450 nfs_readpage_set_pages_uptodate(data);
451 nfs_readpage_retry(task, data);
452}
453
454static void nfs_readpage_release_full(void *calldata)
455{
456 struct nfs_read_data *data = calldata;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 while (!list_empty(&data->pages)) {
459 struct nfs_page *req = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400461 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 nfs_readpage_release(req);
463 }
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400464 nfs_readdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Trond Myklebustec06c092006-03-20 13:44:27 -0500467static const struct rpc_call_ops nfs_read_full_ops = {
468 .rpc_call_done = nfs_readpage_result_full,
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400469 .rpc_release = nfs_readpage_release_full,
Trond Myklebustec06c092006-03-20 13:44:27 -0500470};
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * Read a page over NFS.
474 * We read the page synchronously in the following case:
475 * - The error flag is set for this page. This happens only when a
476 * previous async read operation failed.
477 */
478int nfs_readpage(struct file *file, struct page *page)
479{
480 struct nfs_open_context *ctx;
481 struct inode *inode = page->mapping->host;
482 int error;
483
484 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
485 page, PAGE_CACHE_SIZE, page->index);
Chuck Lever91d5b472006-03-20 13:44:14 -0500486 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
487 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /*
490 * Try to flush any pending writes to the file..
491 *
492 * NOTE! Because we own the page lock, there cannot
493 * be any new pending writes generated at this point
494 * for this page (other pages can be written to).
495 */
496 error = nfs_wb_page(inode, page);
497 if (error)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400498 goto out_unlock;
499 if (PageUptodate(page))
500 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400502 error = -ESTALE;
503 if (NFS_STALE(inode))
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400504 goto out_unlock;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (file == NULL) {
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500507 error = -EBADF;
Trond Myklebustd5308382005-11-04 15:33:38 -0500508 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (ctx == NULL)
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400510 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400512 ctx = get_nfs_open_context(nfs_file_open_context(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
David Howells9a9fc1c2009-04-03 16:42:44 +0100514 if (!IS_SYNC(inode)) {
515 error = nfs_readpage_from_fscache(ctx, inode, page);
516 if (error == 0)
517 goto out;
518 }
519
Trond Myklebust8e0969f2006-12-13 15:23:44 -0500520 error = nfs_readpage_async(ctx, inode, page);
521
David Howells9a9fc1c2009-04-03 16:42:44 +0100522out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 put_nfs_open_context(ctx);
524 return error;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400525out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 unlock_page(page);
527 return error;
528}
529
530struct nfs_readdesc {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400531 struct nfs_pageio_descriptor *pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 struct nfs_open_context *ctx;
533};
534
535static int
536readpage_async_filler(void *data, struct page *page)
537{
538 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
539 struct inode *inode = page->mapping->host;
540 struct nfs_page *new;
541 unsigned int len;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400542 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Trond Myklebust49a70f22006-12-05 00:35:38 -0500544 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (len == 0)
546 return nfs_return_empty_page(page);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 new = nfs_create_request(desc->ctx, inode, page, 0, len);
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400549 if (IS_ERR(new))
550 goto out_error;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (len < PAGE_CACHE_SIZE)
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800553 zero_user_segment(page, len, PAGE_CACHE_SIZE);
Fred Isamanf8512ad2008-03-19 11:24:39 -0400554 if (!nfs_pageio_add_request(desc->pgio, new)) {
555 error = desc->pgio->pg_error;
556 goto out_unlock;
557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 0;
Trond Myklebustde05a0c2007-05-20 13:05:05 -0400559out_error:
560 error = PTR_ERR(new);
561 SetPageError(page);
562out_unlock:
563 unlock_page(page);
564 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567int nfs_readpages(struct file *filp, struct address_space *mapping,
568 struct list_head *pages, unsigned nr_pages)
569{
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400570 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct nfs_readdesc desc = {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400572 .pgio = &pgio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 };
574 struct inode *inode = mapping->host;
575 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400576 size_t rsize = server->rsize;
577 unsigned long npages;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400578 int ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
581 inode->i_sb->s_id,
582 (long long)NFS_FILEID(inode),
583 nr_pages);
Chuck Lever91d5b472006-03-20 13:44:14 -0500584 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400586 if (NFS_STALE(inode))
587 goto out;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (filp == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500590 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (desc.ctx == NULL)
592 return -EBADF;
593 } else
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400594 desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
David Howells9a9fc1c2009-04-03 16:42:44 +0100595
596 /* attempt to read as many of the pages as possible from the cache
597 * - this returns -ENOBUFS immediately if the cookie is negative
598 */
599 ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
600 pages, &nr_pages);
601 if (ret == 0)
602 goto read_complete; /* all pages were read */
603
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400604 if (rsize < PAGE_CACHE_SIZE)
605 nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0);
606 else
607 nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400610
611 nfs_pageio_complete(&pgio);
612 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
613 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
David Howells9a9fc1c2009-04-03 16:42:44 +0100614read_complete:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 put_nfs_open_context(desc.ctx);
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400616out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return ret;
618}
619
David Howellsf7b422b2006-06-09 09:34:33 -0400620int __init nfs_init_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
623 sizeof(struct nfs_read_data),
624 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900625 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (nfs_rdata_cachep == NULL)
627 return -ENOMEM;
628
Matthew Dobson93d23412006-03-26 01:37:50 -0800629 nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
630 nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (nfs_rdata_mempool == NULL)
632 return -ENOMEM;
633
634 return 0;
635}
636
David Brownell266bee82006-06-27 12:59:15 -0700637void nfs_destroy_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 mempool_destroy(nfs_rdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700640 kmem_cache_destroy(nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}