blob: f0016062340dac58357d4ea15a9ca6fdbd402002 [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"
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define NFSDBG_FACILITY NFSDBG_PAGECACHE
29
Trond Myklebustbcb71bb2007-04-02 18:48:28 -040030static int nfs_pagein_multi(struct inode *, struct list_head *, size_t, int);
31static int nfs_pagein_one(struct inode *, struct list_head *, size_t, int);
Trond Myklebustec06c092006-03-20 13:44:27 -050032static const struct rpc_call_ops nfs_read_partial_ops;
33static const struct rpc_call_ops nfs_read_full_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Christoph Lametere18b8902006-12-06 20:33:20 -080035static struct kmem_cache *nfs_rdata_cachep;
Trond Myklebust3feb2d42006-03-20 13:44:37 -050036static mempool_t *nfs_rdata_mempool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define MIN_POOL_READ (32)
39
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070040struct nfs_read_data *nfs_readdata_alloc(size_t len)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050041{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -070042 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
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 Myklebust8aca67f2006-11-13 16:23:44 -050062static void nfs_readdata_rcu_free(struct rcu_head *head)
Trond Myklebust3feb2d42006-03-20 13:44:37 -050063{
Trond Myklebust8aca67f2006-11-13 16:23:44 -050064 struct nfs_read_data *p = container_of(head, struct nfs_read_data, task.u.tk_rcu);
Trond Myklebust3feb2d42006-03-20 13:44:37 -050065 if (p && (p->pagevec != &p->page_array[0]))
66 kfree(p->pagevec);
67 mempool_free(p, nfs_rdata_mempool);
68}
69
Trond Myklebust8aca67f2006-11-13 16:23:44 -050070static void nfs_readdata_free(struct nfs_read_data *rdata)
71{
72 call_rcu_bh(&rdata->task.u.tk_rcu, nfs_readdata_rcu_free);
73}
74
Trond Myklebust963d8fe2006-01-03 09:55:04 +010075void nfs_readdata_release(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 nfs_readdata_free(data);
78}
79
80static
Linus Torvalds1da177e2005-04-16 15:20:36 -070081int nfs_return_empty_page(struct page *page)
82{
83 memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
84 SetPageUptodate(page);
85 unlock_page(page);
86 return 0;
87}
88
Trond Myklebust1de3fc12006-05-25 01:40:44 -040089static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
90{
91 unsigned int remainder = data->args.count - data->res.count;
92 unsigned int base = data->args.pgbase + data->res.count;
93 unsigned int pglen;
94 struct page **pages;
95
96 if (data->res.eof == 0 || remainder == 0)
97 return;
98 /*
99 * Note: "remainder" can never be negative, since we check for
100 * this in the XDR code.
101 */
102 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
103 base &= ~PAGE_CACHE_MASK;
104 pglen = PAGE_CACHE_SIZE - base;
Trond Myklebust79558f32006-08-22 13:44:32 -0400105 for (;;) {
106 if (remainder <= pglen) {
107 memclear_highpage_flush(*pages, base, remainder);
108 break;
109 }
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400110 memclear_highpage_flush(*pages, base, pglen);
Trond Myklebust79558f32006-08-22 13:44:32 -0400111 pages++;
112 remainder -= pglen;
113 pglen = PAGE_CACHE_SIZE;
114 base = 0;
115 }
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
119 struct page *page)
120{
121 LIST_HEAD(one_request);
122 struct nfs_page *new;
123 unsigned int len;
124
Trond Myklebust49a70f22006-12-05 00:35:38 -0500125 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (len == 0)
127 return nfs_return_empty_page(page);
128 new = nfs_create_request(ctx, inode, page, 0, len);
129 if (IS_ERR(new)) {
130 unlock_page(page);
131 return PTR_ERR(new);
132 }
133 if (len < PAGE_CACHE_SIZE)
134 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 nfs_list_add_request(new, &one_request);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400137 if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
138 nfs_pagein_multi(inode, &one_request, len, 0);
139 else
140 nfs_pagein_one(inode, &one_request, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return 0;
142}
143
144static void nfs_readpage_release(struct nfs_page *req)
145{
146 unlock_page(req->wb_page);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
149 req->wb_context->dentry->d_inode->i_sb->s_id,
150 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
151 req->wb_bytes,
152 (long long)req_offset(req));
Nick Wilson10d2c462005-09-22 21:44:28 -0700153 nfs_clear_request(req);
154 nfs_release_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/*
158 * Set up the NFS read request struct
159 */
160static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
Trond Myklebustec06c092006-03-20 13:44:27 -0500161 const struct rpc_call_ops *call_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 unsigned int count, unsigned int offset)
163{
164 struct inode *inode;
Trond Myklebustec06c092006-03-20 13:44:27 -0500165 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 data->req = req;
168 data->inode = inode = req->wb_context->dentry->d_inode;
169 data->cred = req->wb_context->cred;
170
171 data->args.fh = NFS_FH(inode);
172 data->args.offset = req_offset(req) + offset;
173 data->args.pgbase = req->wb_pgbase + offset;
174 data->args.pages = data->pagevec;
175 data->args.count = count;
176 data->args.context = req->wb_context;
177
178 data->res.fattr = &data->fattr;
179 data->res.count = count;
180 data->res.eof = 0;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400181 nfs_fattr_init(&data->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Trond Myklebustec06c092006-03-20 13:44:27 -0500183 /* Set up the initial task struct. */
184 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
185 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 NFS_PROTO(inode)->read_setup(data);
187
188 data->task.tk_cookie = (unsigned long)inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Chuck Levera3f565b2007-01-31 12:14:01 -0500190 dprintk("NFS: %5u initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 data->task.tk_pid,
192 inode->i_sb->s_id,
193 (long long)NFS_FILEID(inode),
194 count,
195 (unsigned long long)data->args.offset);
196}
197
198static void
199nfs_async_read_error(struct list_head *head)
200{
201 struct nfs_page *req;
202
203 while (!list_empty(head)) {
204 req = nfs_list_entry(head->next);
205 nfs_list_remove_request(req);
206 SetPageError(req->wb_page);
207 nfs_readpage_release(req);
208 }
209}
210
211/*
212 * Start an async read operation
213 */
214static void nfs_execute_read(struct nfs_read_data *data)
215{
216 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
217 sigset_t oldset;
218
219 rpc_clnt_sigmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 rpc_execute(&data->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 rpc_clnt_sigunmask(clnt, &oldset);
222}
223
224/*
225 * Generate multiple requests to fill a single page.
226 *
227 * We optimize to reduce the number of read operations on the wire. If we
228 * detect that we're reading a page, or an area of a page, that is past the
229 * end of file, we do not generate NFS read operations but just clear the
230 * parts of the page that would have come back zero from the server anyway.
231 *
232 * We rely on the cached value of i_size to make this determination; another
233 * client can fill pages on the server past our cached end-of-file, but we
234 * won't see the new data until our attribute cache is updated. This is more
235 * or less conventional NFS client behavior.
236 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400237static int nfs_pagein_multi(struct inode *inode, struct list_head *head, size_t count, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 struct nfs_page *req = nfs_list_entry(head->next);
240 struct page *page = req->wb_page;
241 struct nfs_read_data *data;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700242 size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
243 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 int requests = 0;
245 LIST_HEAD(list);
246
247 nfs_list_remove_request(req);
248
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400249 nbytes = count;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700250 do {
251 size_t len = min(nbytes,rsize);
252
253 data = nfs_readdata_alloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!data)
255 goto out_bad;
256 INIT_LIST_HEAD(&data->pages);
257 list_add(&data->pages, &list);
258 requests++;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700259 nbytes -= len;
260 } while(nbytes != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 atomic_set(&req->wb_complete, requests);
262
263 ClearPageError(page);
264 offset = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400265 nbytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 do {
267 data = list_entry(list.next, struct nfs_read_data, pages);
268 list_del_init(&data->pages);
269
270 data->pagevec[0] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400272 if (nbytes < rsize)
273 rsize = nbytes;
274 nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
275 rsize, offset);
276 offset += rsize;
277 nbytes -= rsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 nfs_execute_read(data);
279 } while (nbytes != 0);
280
281 return 0;
282
283out_bad:
284 while (!list_empty(&list)) {
285 data = list_entry(list.next, struct nfs_read_data, pages);
286 list_del(&data->pages);
287 nfs_readdata_free(data);
288 }
289 SetPageError(page);
290 nfs_readpage_release(req);
291 return -ENOMEM;
292}
293
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400294static int nfs_pagein_one(struct inode *inode, struct list_head *head, size_t count, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 struct nfs_page *req;
297 struct page **pages;
298 struct nfs_read_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400300 data = nfs_readdata_alloc(count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (!data)
302 goto out_bad;
303
304 INIT_LIST_HEAD(&data->pages);
305 pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 while (!list_empty(head)) {
307 req = nfs_list_entry(head->next);
308 nfs_list_remove_request(req);
309 nfs_list_add_request(req, &data->pages);
310 ClearPageError(req->wb_page);
311 *pages++ = req->wb_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 req = nfs_list_entry(data->pages.next);
314
Trond Myklebustec06c092006-03-20 13:44:27 -0500315 nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 nfs_execute_read(data);
318 return 0;
319out_bad:
320 nfs_async_read_error(head);
321 return -ENOMEM;
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
Trond Myklebust0b671302006-11-14 16:12:23 -0500325 * This is the callback from RPC telling us whether a reply was
326 * received or some error occurred (timeout or socket shutdown).
327 */
328int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
329{
330 int status;
331
Chuck Levera3f565b2007-01-31 12:14:01 -0500332 dprintk("NFS: %s: %5u, (status %d)\n", __FUNCTION__, task->tk_pid,
Trond Myklebust0b671302006-11-14 16:12:23 -0500333 task->tk_status);
334
335 status = NFS_PROTO(data->inode)->read_done(task, data);
336 if (status != 0)
337 return status;
338
339 nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
340
341 if (task->tk_status == -ESTALE) {
342 set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode));
343 nfs_mark_for_revalidate(data->inode);
344 }
345 spin_lock(&data->inode->i_lock);
346 NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
347 spin_unlock(&data->inode->i_lock);
348 return 0;
349}
350
351static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
352{
353 struct nfs_readargs *argp = &data->args;
354 struct nfs_readres *resp = &data->res;
355
356 if (resp->eof || resp->count == argp->count)
357 return 0;
358
359 /* This is a short read! */
360 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
361 /* Has the server at least made some progress? */
362 if (resp->count == 0)
363 return 0;
364
365 /* Yes, so retry the read at the end of the data */
366 argp->offset += resp->count;
367 argp->pgbase += resp->count;
368 argp->count -= resp->count;
369 rpc_restart_call(task);
370 return -EAGAIN;
371}
372
373/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * Handle a read reply that fills part of a page.
375 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500376static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Trond Myklebustec06c092006-03-20 13:44:27 -0500378 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct nfs_page *req = data->req;
380 struct page *page = req->wb_page;
381
Trond Myklebustec06c092006-03-20 13:44:27 -0500382 if (nfs_readpage_result(task, data) != 0)
383 return;
Trond Myklebust0b671302006-11-14 16:12:23 -0500384
385 if (likely(task->tk_status >= 0)) {
386 nfs_readpage_truncate_uninitialised_page(data);
387 if (nfs_readpage_retry(task, data) != 0)
388 return;
389 }
390 if (unlikely(task->tk_status < 0))
391 SetPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (atomic_dec_and_test(&req->wb_complete)) {
393 if (!PageError(page))
394 SetPageUptodate(page);
395 nfs_readpage_release(req);
396 }
397}
398
Trond Myklebustec06c092006-03-20 13:44:27 -0500399static const struct rpc_call_ops nfs_read_partial_ops = {
400 .rpc_call_done = nfs_readpage_result_partial,
401 .rpc_release = nfs_readdata_release,
402};
403
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400404static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
405{
406 unsigned int count = data->res.count;
407 unsigned int base = data->args.pgbase;
408 struct page **pages;
409
Trond Myklebust79558f32006-08-22 13:44:32 -0400410 if (data->res.eof)
411 count = data->args.count;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400412 if (unlikely(count == 0))
413 return;
414 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
415 base &= ~PAGE_CACHE_MASK;
416 count += base;
417 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
418 SetPageUptodate(*pages);
Trond Myklebust0b671302006-11-14 16:12:23 -0500419 if (count == 0)
420 return;
421 /* Was this a short read? */
422 if (data->res.eof || data->res.count == data->args.count)
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400423 SetPageUptodate(*pages);
424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/*
427 * This is the callback from RPC telling us whether a reply was
428 * received or some error occurred (timeout or socket shutdown).
429 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500430static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Trond Myklebustec06c092006-03-20 13:44:27 -0500432 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Trond Myklebust0b671302006-11-14 16:12:23 -0500434 if (nfs_readpage_result(task, data) != 0)
435 return;
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400436 /*
Trond Myklebust0b671302006-11-14 16:12:23 -0500437 * Note: nfs_readpage_retry may change the values of
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400438 * data->args. In the multi-page case, we therefore need
Trond Myklebust0b671302006-11-14 16:12:23 -0500439 * to ensure that we call nfs_readpage_set_pages_uptodate()
440 * first.
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400441 */
442 if (likely(task->tk_status >= 0)) {
443 nfs_readpage_truncate_uninitialised_page(data);
444 nfs_readpage_set_pages_uptodate(data);
Trond Myklebust0b671302006-11-14 16:12:23 -0500445 if (nfs_readpage_retry(task, data) != 0)
446 return;
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 while (!list_empty(&data->pages)) {
449 struct nfs_page *req = nfs_list_entry(data->pages.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Trond Myklebust1de3fc12006-05-25 01:40:44 -0400451 nfs_list_remove_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 nfs_readpage_release(req);
453 }
454}
455
Trond Myklebustec06c092006-03-20 13:44:27 -0500456static const struct rpc_call_ops nfs_read_full_ops = {
457 .rpc_call_done = nfs_readpage_result_full,
458 .rpc_release = nfs_readdata_release,
459};
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 * Read a page over NFS.
463 * We read the page synchronously in the following case:
464 * - The error flag is set for this page. This happens only when a
465 * previous async read operation failed.
466 */
467int nfs_readpage(struct file *file, struct page *page)
468{
469 struct nfs_open_context *ctx;
470 struct inode *inode = page->mapping->host;
471 int error;
472
473 dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
474 page, PAGE_CACHE_SIZE, page->index);
Chuck Lever91d5b472006-03-20 13:44:14 -0500475 nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
476 nfs_add_stats(inode, NFSIOS_READPAGES, 1);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /*
479 * Try to flush any pending writes to the file..
480 *
481 * NOTE! Because we own the page lock, there cannot
482 * be any new pending writes generated at this point
483 * for this page (other pages can be written to).
484 */
485 error = nfs_wb_page(inode, page);
486 if (error)
487 goto out_error;
488
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400489 error = -ESTALE;
490 if (NFS_STALE(inode))
491 goto out_error;
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (file == NULL) {
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500494 error = -EBADF;
Trond Myklebustd5308382005-11-04 15:33:38 -0500495 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (ctx == NULL)
Trond Myklebustcf1308f2006-11-19 16:44:52 -0500497 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 } else
499 ctx = get_nfs_open_context((struct nfs_open_context *)
500 file->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Trond Myklebust8e0969f2006-12-13 15:23:44 -0500502 error = nfs_readpage_async(ctx, inode, page);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 put_nfs_open_context(ctx);
505 return error;
506
507out_error:
508 unlock_page(page);
509 return error;
510}
511
512struct nfs_readdesc {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400513 struct nfs_pageio_descriptor *pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct nfs_open_context *ctx;
515};
516
517static int
518readpage_async_filler(void *data, struct page *page)
519{
520 struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
521 struct inode *inode = page->mapping->host;
522 struct nfs_page *new;
523 unsigned int len;
524
525 nfs_wb_page(inode, page);
Trond Myklebust49a70f22006-12-05 00:35:38 -0500526 len = nfs_page_length(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (len == 0)
528 return nfs_return_empty_page(page);
529 new = nfs_create_request(desc->ctx, inode, page, 0, len);
530 if (IS_ERR(new)) {
531 SetPageError(page);
532 unlock_page(page);
533 return PTR_ERR(new);
534 }
535 if (len < PAGE_CACHE_SIZE)
536 memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400537 nfs_pageio_add_request(desc->pgio, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539}
540
541int nfs_readpages(struct file *filp, struct address_space *mapping,
542 struct list_head *pages, unsigned nr_pages)
543{
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400544 struct nfs_pageio_descriptor pgio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct nfs_readdesc desc = {
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400546 .pgio = &pgio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 };
548 struct inode *inode = mapping->host;
549 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400550 size_t rsize = server->rsize;
551 unsigned long npages;
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400552 int ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
555 inode->i_sb->s_id,
556 (long long)NFS_FILEID(inode),
557 nr_pages);
Chuck Lever91d5b472006-03-20 13:44:14 -0500558 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400560 if (NFS_STALE(inode))
561 goto out;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (filp == NULL) {
Trond Myklebustd5308382005-11-04 15:33:38 -0500564 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (desc.ctx == NULL)
566 return -EBADF;
567 } else
568 desc.ctx = get_nfs_open_context((struct nfs_open_context *)
569 filp->private_data);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400570 if (rsize < PAGE_CACHE_SIZE)
571 nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0);
572 else
573 nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400576
577 nfs_pageio_complete(&pgio);
578 npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
579 nfs_add_stats(inode, NFSIOS_READPAGES, npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 put_nfs_open_context(desc.ctx);
Trond Myklebust5f004cf2006-09-14 14:03:14 -0400581out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return ret;
583}
584
David Howellsf7b422b2006-06-09 09:34:33 -0400585int __init nfs_init_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
588 sizeof(struct nfs_read_data),
589 0, SLAB_HWCACHE_ALIGN,
590 NULL, NULL);
591 if (nfs_rdata_cachep == NULL)
592 return -ENOMEM;
593
Matthew Dobson93d23412006-03-26 01:37:50 -0800594 nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
595 nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (nfs_rdata_mempool == NULL)
597 return -ENOMEM;
598
599 return 0;
600}
601
David Brownell266bee82006-06-27 12:59:15 -0700602void nfs_destroy_readpagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 mempool_destroy(nfs_rdata_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700605 kmem_cache_destroy(nfs_rdata_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}